> ## Documentation Index
> Fetch the complete documentation index at: https://mcp.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 采样

<div id="enable-section-numbers" />

模型上下文协议 (MCP) 提供了一种标准化方式，允许服务器通过客户端请求语言模型的采样（“补全”或“生成”）。此流程允许客户端保持对模型访问、选择和权限的控制，同时使服务器能够利用 AI 能力——无需服务器 API 密钥。服务器可以请求文本、音频或基于图像的交互，并可选择在其提示中包含来自 MCP 服务器的上下文。

## 用户交互模型

MCP 中的采样允许服务器实现代理行为，通过使 LLM 调用能够 *嵌套* 发生在其他 MCP 服务器功能内部。

实现可以自由地通过任何适合其需求的接口模式来暴露采样——协议本身不强制任何特定的用户交互模型。

<Warning>
  为了信任、安全和安保，**应该**始终有人类在环中，并拥有拒绝采样请求的能力。

  应用程序 **应该**：

  * 提供易于直观审查采样请求的 UI
  * 允许用户在发送前查看和编辑提示
  * 在交付前展示生成的响应以供审查
</Warning>

## 能力

支持采样的客户端 **必须** 在 [初始化](/specification/2025-06-18/basic/lifecycle#initialization) 期间声明 `sampling` 能力：

```json theme={null}
{
  "capabilities": {
    "sampling": {}
  }
}
```

## 协议消息

### 创建消息

要请求语言模型生成，服务器发送一个 `sampling/createMessage` 请求：

**请求：**

```json theme={null}
{
  "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
  }
}
```

**响应：**

```json theme={null}
{
  "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"
  }
}
```

## 消息流程

```mermaid theme={null}
sequenceDiagram
    participant 服务器
    participant 客户端
    participant 用户
    participant LLM

    Note over 服务器，客户端：服务器发起采样
    服务器->>客户端：sampling/createMessage

    Note over 客户端，用户：人类在环审查
    客户端->>用户：展示请求以供审批
    用户-->>客户端：审查并批准/修改

    Note over 客户端，LLM：模型交互
    客户端->>LLM：转发已批准的请求
    LLM-->>客户端：返回生成内容

    Note over 客户端，用户：响应审查
    客户端->>用户：展示响应以供审批
    用户-->>客户端：审查并批准/修改

    Note over 服务器，客户端：完成请求
    客户端-->>服务器：返回已批准的响应
```

## 数据类型

### 消息

采样消息可以包含：

#### 文本内容

```json theme={null}
{
  "type": "text",
  "text": "The message content"
}
```

#### 图像内容

```json theme={null}
{
  "type": "image",
  "data": "base64-encoded-image-data",
  "mimeType": "image/jpeg"
}
```

#### 音频内容

```json theme={null}
{
  "type": "audio",
  "data": "base64-encoded-audio-data",
  "mimeType": "audio/wav"
}
```

### 模型偏好

MCP 中的模型选择需要仔细抽象，因为服务器和客户端可能使用具有不同模型产品的不同 AI 提供商。服务器不能简单地按名称请求特定模型，因为客户端可能无法访问该确切模型，或者可能更喜欢使用不同提供商的等效模型。

为了解决这个问题，MCP 实现了一个偏好系统，结合了抽象能力优先级和可选模型提示：

#### 能力优先级

服务器通过三个归一化的优先级值 (0-1) 表达其需求：

* `costPriority`：最小化成本有多重要？较高的值偏好更便宜的模型。
* `speedPriority`：低延迟有多重要？较高的值偏好更快的模型。
* `intelligencePriority`：高级能力有多重要？较高的值偏好能力更强的模型。

#### 模型提示

虽然优先级有助于根据特征选择模型，但 `hints` 允许服务器建议特定模型或模型系列：

* 提示被视为子字符串，可以灵活匹配模型名称
* 多个提示按偏好顺序评估
* 客户端 **可以** 将提示映射到不同提供商的等效模型
* 提示是建议性的——客户端进行最终模型选择

例如：

```json theme={null}
{
  "hints": [
    { "name": "claude-3-sonnet" }, // 偏好 Sonnet 类别模型
    { "name": "claude" } // 回退到任何 Claude 模型
  ],
  "costPriority": 0.3, // 成本不太重要
  "speedPriority": 0.8, // 速度非常重要
  "intelligencePriority": 0.5 // 中等能力需求
}
```

客户端处理这些偏好以从其可用选项中选择合适的模型。例如，如果客户端无法访问 Claude 模型但有 Gemini，它可能会基于相似的能力将 sonnet 提示映射到 `gemini-1.5-pro`。

## 错误处理

客户端 **应该** 为常见失败情况返回错误：

错误示例：

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 1,
  "error": {
    "code": -1,
    "message": "User rejected sampling request"
  }
}
```

## 安全考量

1. 客户端 **应该** 实现用户审批控制
2. 双方 **应该** 验证消息内容
3. 客户端 **应该** 尊重模型偏好提示
4. 客户端 **应该** 实现速率限制
5. 双方 **必须** 妥善处理敏感数据
