概述

社区译文;来源: MCP 80041ab0

本页定义核心协议的消息模式,即客户端和服务器如何组合 JSON-RPC 的请求、响应和通知来形成交互。每一种传输(英文)都承载这些模式;不同传输只在消息的分帧和交付方式上有所区别。

每次交互都由客户端开始:

  • 客户端发送 JSON-RPC 请求_和_通知。
  • 服务器以 JSON-RPC 响应(结果或错误)回答每个请求;在响应前,可以选择发送限定于该请求的_通知_。

服务器MUST NOT发起 JSON-RPC 请求,客户端也不发送 JSON-RPC 响应。

请求与响应

客户端发送请求;服务器以结果或错误作答。请求处理期间,服务器MAY发送限定于该请求的通知,例如 notifications/progress 和 notifications/message(英文)。

  sequenceDiagram
    participant Client
    participant Server

    Client->>Server: 请求
    Server-->>Client: notifications/progress(可选)
    Server-->>Client: 响应

多轮往返请求

当服务器需要客户端输入(采样(Sampling)、信息征询(Elicitation)或根目录(Roots))才能完成请求时,它会返回 InputRequiredResult,随后客户端携带匹配的 inputResponses 重试该请求。参见多轮往返请求。

  sequenceDiagram
    participant Client
    participant Server

    Client->>Server: 请求(id: 1)
    Server-->>Client: InputRequiredResult(inputRequests)
    Client->>Server: 请求(id: 2,原始参数 + inputResponses)
    Server-->>Client: 响应

订阅与通知

为了接收变更通知(列表变更、资源更新),客户端发送 subscriptions/listen 请求;该请求的响应是一条长期存续、用于传送所请求通知类型的流。流状态限定于该请求:如果底层通道丢失,客户端需要重新发出请求。

  sequenceDiagram
    participant Client
    participant Server

    Client->>Server: subscriptions/listen
    Server-->>Client: notifications/subscriptions/acknowledged
    note over Client,Server: 流保持打开
    Server-->>Client: notifications/*(带 subscriptionId)

增加模式

核心协议的所有功能都由这些模式构成。增加新模式的协议修订版会在本页定义该模式。由于模式完全通过请求、响应和通知表达,传输无需修改即可承载新模式。