1. 什么是采样? #
采样(Sampling)是 MCP 服务器通过客户端向语言模型发起「生成/补全」请求的标准化方式。服务器无需自己的 API 密钥,由客户端控制模型访问和权限。
协议版本:2025-11-25
| 通俗理解 | 说明 |
|---|---|
| 服务器用 AI | 服务器想「让 AI 写一段话」时,通过客户端转发给 LLM |
| 客户端控制 | 客户端持有 API 密钥,可审核、拒绝或修改请求 |
| 代理式行为 | 服务器可在采样中附带工具,让 LLM 调用工具并继续对话 |
2. 本章你将学到 #
- 采样的能力声明与协议消息
- 基本采样与带工具的采样
- 多轮工具循环与消息约束
- 模型偏好、错误处理与安全要点
3. 典型流程 #
sequenceDiagram
participant S as 服务器
participant C as 客户端
participant U as 用户
participant L as 语言模型
S->>C: sampling/createMessage(消息 + 可选工具)
C->>U: 展示请求,待审核
C->>L: 转发已批准的请求
L-->>C: 生成响应
C->>U: 展示响应,待审核
C->>S: 返回已批准的响应
信任与安全:应有用户参与,具备拒绝能力;应用应提供审核 UI,允许查看/编辑提示和响应。
4. 能力 #
客户端必须在初始化时声明 sampling 能力:
| 能力 | 说明 |
|---|---|
sampling |
基本采样 |
sampling.tools |
支持在采样中使用工具(服务器可传 tools 数组) |
sampling.context |
软弃用,建议避免使用 |
5. 协议消息 #
5.1 基本采样(sampling/createMessage) #
请求:
{
"jsonrpc": "2.0",
"id": 1,
"method": "sampling/createMessage",
"params": {
"messages": [
{
"role": "user",
"content": { "type": "text", "text": "What is the capital of France?" }
}
],
"modelPreferences": {
"hints": [{ "name": "claude-3-sonnet" }],
"intelligencePriority": 0.8,
"speedPriority": 0.5
},
"systemPrompt": "You are a helpful assistant.",
"maxTokens": 100
}
}响应:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"role": "assistant",
"content": { "type": "text", "text": "The capital of France is Paris." },
"model": "claude-3-sonnet-20240307",
"stopReason": "endTurn"
}
}5.2 带工具的采样 #
服务器在请求中加入 tools 和可选的 toolChoice:LLM 可请求调用工具,服务器执行后把结果作为 tool_result 发回,继续下一轮采样。
5.3 工具选择模式(toolChoice) #
| 模式 | 说明 |
|---|---|
{ "mode": "auto" } |
模型自行决定是否使用工具(默认) |
{ "mode": "required" } |
模型必须至少使用一个工具 |
{ "mode": "none" } |
模型不得使用任何工具(可用于强制结束工具循环) |
6. 多轮工具循环 #
- 服务器发送带
tools的采样请求 - LLM 返回
stopReason: "toolUse",内容含tool_use块 - 服务器执行工具,将结果作为
tool_result加入消息 - 服务器发送带工具结果的新采样请求
- 重复 2–4 直至 LLM 返回
stopReason: "endTurn"
7. 消息内容约束 #
7.1 工具结果消息 #
包含工具结果(type: "tool_result")的用户消息必须仅含工具结果,不能与文本、图像等混合。
7.2 工具使用与结果平衡 #
每个 tool_use 块必须有对应的 tool_result 匹配(toolUseId 与 id 一致),且位于下一轮助手消息之前。所有工具使用都解析后,对话才能继续。
8. 流程示意 #
sequenceDiagram
participant S as 服务器
participant C as 客户端
participant U as 用户
participant L as 大语言模型
S->>C: sampling/createMessage
C->>U: 提示用户审核
U-->>C: 批准
C->>L: 转发请求
L-->>C: 生成内容
C->>U: 提示用户审核
U-->>C: 批准
C-->>S: 返回响应
9. 模型偏好 #
9.1 能力优先级(0–1) #
| 字段 | 说明 |
|---|---|
costPriority |
成本优先,越高越偏好便宜模型 |
speedPriority |
速度优先,越高越偏好快速模型 |
intelligencePriority |
能力优先,越高越偏好强模型 |
9.2 模型提示(hints) #
服务端可建议模型名称或系列:
{
"hints": [
{ "name": "claude-3-sonnet" },
{ "name": "claude" }
],
"costPriority": 0.3,
"speedPriority": 0.8,
"intelligencePriority": 0.5
}客户端做最终选择,可将提示映射到不同提供商的等效模型。
10. 消息内容类型 #
| 类型 | 说明 |
|---|---|
text |
文本内容 |
image |
图像(base64 + mimeType) |
audio |
音频(base64 + mimeType) |
tool_use |
工具调用请求 |
tool_result |
工具执行结果 |
11. 错误处理 #
| 情况 | 错误码 |
|---|---|
| 用户拒绝采样请求 | -1 |
| 缺少工具结果 | -32602 |
| 工具结果与其他内容混合 | -32602 |
示例: 用户拒绝
{
"jsonrpc": "2.0",
"id": 3,
"error": {
"code": -1,
"message": "User rejected sampling request"
}
}12. 安全要点 #
| 要点 | 说明 |
|---|---|
| 用户审批 | 客户端应当实施用户审批控制 |
| 内容验证 | 双方应当验证消息内容 |
| 工具结果 | 每个 tool_use 必须有对应 tool_result,用户消息仅含工具结果 |
| 迭代限制 | 双方应当对工具循环实施迭代限制 |
| 敏感数据 | 双方必须妥善处理敏感数据 |