主题
进度
Model Context Protocol (MCP) 通过通知消息为长时间运行的操作提供可选的进度跟踪。服务端 MAY(可以)发送进度通知,以报告客户端已发出的请求的状态。
进度流程
当客户端希望接收某个请求的进度更新时,它会在请求元数据中包含一个 progressToken。
- 进度令牌 MUST(必须)是字符串或整数值
- 进度令牌可由客户端以任意方式选择,但 MUST(必须)在所有活动请求中唯一
json
{
"jsonrpc": "2.0",
"id": 1,
"method": "some_method",
"params": {
"_meta": {
"progressToken": "abc123"
}
}
}随后服务端 MAY(可以)发送进度通知,其中包含:
- 原始的进度令牌
- 当前的进度值
- 可选的 "total" 值
- 可选的 "message" 值
json
{
"jsonrpc": "2.0",
"method": "notifications/progress",
"params": {
"progressToken": "abc123",
"progress": 50,
"total": 100,
"message": "Reticulating splines..."
}
}- 即使总数未知,
progress值 MUST(必须)随每条通知递增。 progress和total值 MAY(可以)为浮点数。message字段 SHOULD(应该)提供相关的人类可读的进度信息。
行为要求
进度通知 MUST(必须)仅引用满足以下条件的令牌:
- 在活动请求中提供
- 与进行中的操作相关联
收到带有进度令牌的请求的服务端 MAY(可以):
- 选择不发送任何进度通知
- 以其认为合适的任意频率发送通知
- 在总数未知时省略 total 值
sequenceDiagram
participant Client
participant Server
Note over Client,Server: Request with progress token
Client->>Server: Method request with progressToken
Note over Client,Server: Progress updates
Server-->>Client: Progress notification (0.2/1.0)
Server-->>Client: Progress notification (0.6/1.0)
Server-->>Client: Progress notification (1.0/1.0)
Note over Client,Server: Operation complete
Server->>Client: Method response
实现说明
- 客户端和服务端 SHOULD(应该)跟踪活动的进度令牌
- 双方 SHOULD(应该)实现速率限制以防止洪泛
- 进度通知 MUST(必须)在完成后停止