💠
💠 2026-11-23 10:14:31
MCP(Model Context Protocol)
MCP 是 Anthropic 提出的 Agent ↔ 工具/数据 协议。用 JSON-RPC 2.0 统一 Host、Client、Server 之间的能力发现、调用与结果返回;协议不绑定模型,也不绑定传输层。
1. 核心模型
1.1 角色
| 角色 |
职责 |
| Host |
LLM 应用(IDE、桌面客户端、Agent 运行时),负责用户确认、权限与上下文编排 |
| Client |
Host 内部与单个 Server 的 1:1 连接,维护协议状态 |
| Server |
暴露 Tools / Resources / Prompts 的进程或服务 |
1.2 三个正交概念
| 概念 |
控制方 |
作用 |
典型场景 |
| Tools |
模型 |
执行动作,可能产生副作用 |
查库、发邮件、调外部 API |
| Resources |
应用/用户 |
只读上下文,URI 标识 |
文件、数据库记录、Git 对象 |
| Prompts |
用户 |
可复用的指令模板 |
code review、周报模板 |
- Tools:JSON Schema 描述输入/输出;
annotations 标注只读、破坏性等行为;2025-06-18 起支持 structuredContent 结构化输出。
- Resources:URI 即身份,例如
file:///project/src/main.java、db://postgres/customers/123、git://repo/HEAD:README.md。服务端承诺只读,客户端可缓存;资源变化通过订阅推送,不需要轮询。
- Prompts:服务端声明、可版本化的参数化模板,避免把领域指令硬编码进 system prompt 或伪装成 Tool。
1.3 Client 侧能力
| 能力 |
方向 |
作用 |
现状 |
| Sampling |
Server → Client |
借用客户端背后的 LLM 完成推理 |
2026-07-28 起弃用 |
| Roots |
Server → Client |
获取文件系统边界 |
2026-07-28 起弃用 |
| Elicitation |
Server → Client |
向用户补充询问信息 |
仍在使用 |
| Logging |
Server → Client |
结构化日志推送 |
2026-07-28 起弃用 |
1.4 与 Function Call 的区别
Function Call 只解决“模型输出一个调用意图”;MCP 额外解决:
- 能力发现:Server 自描述 Tools / Resources / Prompts,客户端无需预置清单。
- 传输与生命周期:stdio / HTTP、协议版本、会话或每请求元数据。
- 权限与确认:Host 统一拦截工具调用,用户可审阅。
- 跨应用复用:同一个 Server 可被多个 Host 复用。
1.5 三者协作示例
用户:帮我审查这个 PR。
- Agent 发现 Server 声明了 Prompt
code_review,读取参数定义。
- 读取 Resource
git://repo/pulls/42.diff 填充模板。
- 审查中读取 Resource
api://service/v1/openapi.json 了解接口规范。
- 发现 SQL 注入风险,输出建议。
- 用户说“发邮件给作者”,Agent 调用 Tool
send_email,Host 弹出确认。
2. 版本演进
2.1 总览
1
2
3
4
5
|
flowchart LR
V1["2024-11-05<br/>初版<br/>HTTP+SSE 双端点<br/>连接即会话"] --> V2["2025-03-26<br/>Streamable HTTP 单端点<br/>Mcp-Session-Id"]
V2 --> V3["2025-06-18<br/>MCP-Protocol-Version<br/>Elicitation / 结构化输出"]
V3 --> V4["2025-11-25<br/>最后一个 initialize 握手版<br/>Tasks / Sampling 工具调用"]
V4 --> V5["2026-07-28<br/>无状态核心<br/>server/discover + MRTR"]
|
| 版本 |
传输与生命周期 |
关键变化 |
| 2024-11-05 |
stdio + HTTP+SSE 双端点;initialize 握手,连接即会话 |
初版:Tools / Resources / Prompts、Sampling、Roots |
| 2025-03-26 |
Streamable HTTP 单端点;initialize + Mcp-Session-Id |
OAuth 2.1、JSON-RPC 批处理、Tool annotations、SSE 断线续传 |
| 2025-06-18 |
同上;后续请求带 MCP-Protocol-Version |
移除批处理、结构化输出、Elicitation、OAuth Resource Server / RFC 8707 |
| 2025-11-25 |
同上;最后一个依赖 initialize 握手的版本 |
OIDC Discovery、URL 模式 Elicitation、Sampling 工具调用、实验性 Tasks、图标元数据 |
| 2026-07-28 |
Streamable HTTP;无握手、无 Session,每请求自描述 |
当前版本:server/discover、MRTR、subscriptions/listen、Tasks 扩展化、Roots/Sampling/Logging 弃用 |
2.2 2024-11-05:初版,双端点 HTTP+SSE
- JSON-RPC 2.0 承载请求、响应、通知。
initialize 协商协议版本与双方能力,notifications/initialized 后进入操作阶段。
- HTTP 传输需要两个端点:
GET /sse 建立长连接,服务端先下发 endpoint 事件;客户端后续所有上行消息 POST 到该 endpoint,服务端响应和通知走 SSE。
- 状态绑定在 SSE 连接上,集群必须做 sticky session 或共享会话存储;网关超时、断线重连都会影响会话。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
sequenceDiagram
autonumber
participant C as Client
participant S as Server
C->>S: GET /sse
S-->>C: event: endpoint<br/>data: /messages?sessionId=...
Note over C,S: 之后上行消息一律 POST 到 endpoint
C->>S: POST /messages initialize
S-->>C: SSE: initialize result
C->>S: POST /messages notifications/initialized
loop 会话期间
C->>S: POST /messages tools/call
S-->>C: SSE: result / 通知 / 反向请求
end
|
2.3 2025-03-26:Streamable HTTP
- 收拢为单端点
/mcp,同时支持 POST 和 GET。
- 每个 JSON-RPC 消息一个
POST;响应可以直接返回 application/json,也可以升级为 text/event-stream 流。
- 初始化响应可下发
Mcp-Session-Id,后续请求必须携带;服务端可用 DELETE 结束会话。
GET 可选,用于服务端主动推送请求或通知。
- SSE 事件可带
id,客户端用 Last-Event-ID 断线续传。
- 新增 OAuth 2.1 授权框架、JSON-RPC 批处理、Tool annotations、音频内容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
sequenceDiagram
autonumber
participant C as Client
participant S as Server
C->>S: POST /mcp initialize
S-->>C: InitializeResult<br/>Mcp-Session-Id: 1868a90c...
C->>S: POST /mcp notifications/initialized<br/>Mcp-Session-Id
S-->>C: 202 Accepted
C->>S: POST /mcp tools/call<br/>Mcp-Session-Id
alt 普通响应
S-->>C: application/json result
else 流式响应
S-->>C: SSE: notifications/progress
S-->>C: SSE: result
end
opt 需要服务端主动消息
C->>S: GET /mcp<br/>Mcp-Session-Id
S-->>C: SSE: 服务端请求 / 通知
end
C->>S: DELETE /mcp<br/>Mcp-Session-Id
|
2.4 2025-06-18:能力与安全补齐
- 移除 JSON-RPC 批处理。
- Tool 结果支持
structuredContent,并可在结果中返回 Resource links。
- 新增 Elicitation:服务端在一次工具调用中反向向用户询问补充信息。
- MCP Server 被归类为 OAuth Resource Server,配合 RFC 8707 Resource Indicators,防止恶意 Server 获取访问令牌。
- HTTP 后续请求必须携带
MCP-Protocol-Version,值为初始化协商出的版本。
- 新增
title 字段,区分程序标识 name 与人类可读名称。
1
2
3
4
5
6
7
8
9
10
11
12
|
sequenceDiagram
autonumber
participant U as User
participant C as Client
participant S as Server
C->>S: POST tools/call<br/>MCP-Protocol-Version: 2025-06-18
S-->>C: SSE: elicitation/create
C->>U: 展示表单 / 确认
U-->>C: 提交
C->>S: POST elicitation result
S-->>C: SSE: tools/call result<br/>structuredContent
|
这一代仍是“有状态 + 长连接反向请求”模型;反向请求依赖具体连接,跨节点路由依然困难。
2.5 2025-11-25:最后的有状态版本
initialize / initialized 握手仍在,这是最后一个依赖握手的版本。
- 新增实验性 Tasks:长任务返回
CreateTaskResult 和 taskId;客户端用 tasks/get 轮询状态,完成后用 tasks/result 取最终结果。
- Elicitation 支持 URL 模式,适合 OAuth 授权等站外交互。
- Sampling 支持
tools / toolChoice,服务端可借客户端 LLM 执行工具调用。
- 新增 OIDC Discovery、增量 scope 同意、Client ID Metadata Documents、Tool/Resource/Prompt 图标元数据。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
sequenceDiagram
autonumber
participant C as Client
participant S as Server
C->>S: POST tools/call<br/>params.task.ttl
S-->>C: CreateTaskResult<br/>taskId / status: working
loop 按 pollInterval 轮询
C->>S: POST tasks/get
S-->>C: status: working / completed
end
C->>S: POST tasks/result
S-->>C: 最终 CallToolResult
|
2.6 2026-07-28:无状态核心(Modern)
这是 MCP 最大的一次重构:协议不再有会话,每个请求自带全部上下文。
- 删除
initialize / notifications/initialized 握手与 Mcp-Session-Id。
- 每个请求在
params._meta 中携带:
io.modelcontextprotocol/protocolVersion(必填)
io.modelcontextprotocol/clientCapabilities(必填)
io.modelcontextprotocol/clientInfo(建议)
- HTTP 侧新增/保留
MCP-Protocol-Version、Mcp-Method、Mcp-Name 请求头,网关和 WAF 不解析 body 也能路由、审计。
server/discover 替代握手期能力发现:Server 必须实现;Client 可选调用,也可直接发请求并根据 UnsupportedProtocolVersionError 重试。
- MRTR(Multi Round-Trip Requests) 替代服务端反向请求:
roots/list、sampling/createMessage、elicitation/create 不再作为独立请求推送,而是放进 InputRequiredResult 的 inputRequests 返回;客户端收集输入后带 inputResponses 和 requestState 重试原请求。
subscriptions/listen 替代 GET 流和 resources/subscribe:一条长 POST 响应流承载客户端显式订阅的变更通知。
- 移除
ping、logging/setLevel、notifications/roots/list_changed;移除 SSE 断线续传。
- Tasks 移出核心协议,变为官方扩展
io.modelcontextprotocol/tasks;双方通过 capabilities.extensions 协商扩展。
- 列表与读取结果要求
ttlMs / cacheScope,明确缓存新鲜度与共享范围。
_meta 支持 W3C traceparent / tracestate / baggage,可把 OpenTelemetry 链路透传到 Server。
- 所有结果必须带
resultType:complete 或 input_required。
- Roots、Sampling、Logging 进入 12 个月弃用期;HTTP+SSE 传输也正式标记为弃用。
现代请求示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "query_db",
"arguments": { "sql": "select 1" },
"_meta": {
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
"io.modelcontextprotocol/clientInfo": {
"name": "my-agent",
"version": "1.0.0"
},
"io.modelcontextprotocol/clientCapabilities": {
"elicitation": {}
}
}
}
}
|
普通请求与 MRTR:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
sequenceDiagram
autonumber
participant C as Client
participant S as Server
opt 可选
C->>S: POST server/discover
S-->>C: 支持的版本 / 能力 / 身份
end
C->>S: POST tools/call<br/>MCP-Protocol-Version + _meta
alt 信息足够
S-->>C: resultType: complete
else 需要用户补充信息
S-->>C: resultType: input_required<br/>inputRequests + requestState
Note over C: 本地收集输入,不依赖服务端会话
C->>S: POST tools/call(新 id)<br/>inputResponses + requestState
S-->>C: resultType: complete
end
|
变更订阅:
1
2
3
4
5
6
7
8
9
10
11
|
sequenceDiagram
autonumber
participant C as Client
participant S as Server
C->>S: POST subscriptions/listen<br/>notifications: toolsListChanged, resourceSubscriptions
S-->>C: SSE: notifications/subscriptions/acknowledged
Note over C,S: 响应流保持打开
S-->>C: SSE: notifications/tools/list_changed
S-->>C: SSE: notifications/resources/updated
Note over C,S: 客户端关闭流,或服务端优雅结束
|
2.7 状态模型对比
| 维度 |
2024-11-05 |
2025-03-26 ~ 2025-11-25 |
2026-07-28 |
| 协议版本协商 |
initialize 握手 |
initialize 握手 |
每请求 _meta + 错误重试 |
| 会话 |
SSE 连接即会话 |
Mcp-Session-Id |
无会话 |
| 服务端 → 客户端请求 |
SSE 推送 |
SSE 推送 |
MRTR InputRequiredResult |
| 变更通知 |
resources/subscribe + SSE |
resources/subscribe + GET SSE |
subscriptions/listen |
| 水平扩展 |
需要 sticky session |
需要会话存储或粘滞 |
任意节点、无粘滞 |
| 长任务 |
无 |
实验性 Tasks |
Tasks 扩展 |
3. 传输层
3.1 stdio
客户端把 Server 作为子进程启动,通过 stdin / stdout 交换换行分隔的 JSON-RPC 消息,stderr 用于日志。
| 特性 |
stdio |
说明 |
| 全双工 |
✅ |
两条管道天然双向 |
| 延迟 |
✅ 无网络开销 |
本地进程间通信 |
| 连接管理 |
✅ 简单 |
一个进程一条管道,无集群、负载均衡、会话保持 |
| 生命周期 |
进程级 |
启动即连,退出即断 |
3.2 HTTP+SSE vs Streamable HTTP
| 能力 |
旧版 HTTP+SSE(2024-11-05) |
Streamable HTTP(2025-03-26+) |
| 端点 |
两个:/sse + /messages |
一个:/mcp |
| 上行 |
POST 到 endpoint 事件给出的地址 |
每条消息一个 POST |
| 下行 |
必须长期保持 SSE |
响应可 JSON,也可按需升级 SSE |
| 会话 |
连接即状态 |
Mcp-Session-Id(2026 起移除) |
| 集群 |
需要 sticky session |
2026 起完全无状态,适配 CDN / LB / API Gateway |
| 断线续传 |
不支持 |
2025-03-26 ~ 2025-11-25 支持 Last-Event-ID;2026 移除 |
注意这里的 Streamable HTTP 默认是承载于HTTP1.1 其实就是一个长连接,服务端依据调用的情况是响应json还是返回sse。
3.3 HTTP/1.1 与 HTTP/2
MCP 不强制 HTTP/2:
- HTTP/1.1 可以通过
chunked 传输和 SSE 工作,但同一连接上难以同时承载多个独立的流式请求。
- HTTP/2 的多路复用更适合“多个并发 POST + 长 SSE 订阅”并存的场景,减少队头阻塞。
- 二进制内容(图片、PDF)在 JSON-RPC 中需要 Base64,仍有编码成本;大文件更适合放 Resource URI 或对象存储,由客户端按需拉取。
4. 授权演进
| 版本 |
授权相关变化 |
| 2024-11-05 |
无标准授权框架 |
| 2025-03-26 |
引入基于 OAuth 2.1 的授权框架 |
| 2025-06-18 |
Server 归类为 OAuth Resource Server;RFC 8707 Resource Indicators;保护资源元数据 |
| 2025-11-25 |
OIDC Discovery;增量 scope 同意;Client ID Metadata Documents |
| 2026-07-28 |
校验 RFC 9207 iss;DCR 要求 application_type;凭据按 issuer 绑定;DCR 弃用,推荐 Client ID Metadata Documents |
5. 生态工具
6. 版本链接