协议版本: 2024-11-05
Model Context Protocol (MCP) 提供了一种标准化方式,使服务器能够通过客户端向语言模型请求LLM采样("补全" 或 "生成")。此流程允许客户端保持对模型访问、选择与权限的控制,同时让服务器能够利用AI能力——无需服务器API密钥。服务器可以请求基于文本或图像的交互,并可选择在其提示中包含来自MCP服务器的上下文。

用户交互模型

Sampling in MCP allows servers to implement agentic behaviors, by enabling LLM calls to occur 嵌套的 inside other MCP server features. 实现方式可自由选择任何适合其需求的接口模式来公开采样——协议本身并不强制规定任何特定的用户交互模型。
For trust & safety and security, there 应当 always be a human in the loop with the ability to deny sampling requests.Applications 应当:
  • 提供直观易用的用户界面,方便审阅采样请求
  • 允许用户在发送前查看和编辑提示
  • 在投送之前展示生成的响应以供审阅

能力

Clients that support sampling 必须 declare the sampling capability during initialization:
{
  "capabilities": {
    "sampling": {}
  }
}

协议消息

创建信息

To request a language model generation, servers send a sampling/createMessage request: 请求:
{
  "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"
  }
}

消息流

数据类型

消息

采样消息可以包含:

文本内容

{
  "type": "text",
  "text": "The message content"
}

图像内容

{
  "type": "image",
  "data": "base64-encoded-image-data",
  "mimeType": "image/jpeg"
}

模型偏好设置

MCP中的模型选择需要谨慎抽象,因为服务器和客户端可能使用不同的AI供应商,它们提供的模型也各不相同。服务器无法简单地通过名称请求特定模型,因为客户端可能无法访问该确切模型,或者可能倾向于使用不同供应商的等效模型。 为解决这个问题,MCP实现了一个偏好系统,将抽象能力优先级与可选的模型提示相结合:

能力优先级

服务端通过三个标准化优先级值(0-1)来表达其需求:
  • costPriority: 最小化成本的重要性如何?较高的值更青睐更便宜的模型。
  • speedPriority: 低延迟的重要性如何?较高值更倾向于选择速度更快的模型。
  • intelligencePriority: 高级能力有多重要?越高的值越偏好性能更强的模型。

模型提示

While priorities help select models based on characteristics, hints allow servers to suggest specific models or model families:
  • 提示被视为可以灵活匹配模型名称的子字符串
  • 多个提示根据偏好顺序进行评估。
  • 客户端 可以将提示映射到来自不同提供者的等效模型
  • 提示仅供参考——客户端拥有最终模型选择权
例如:
{
  "hints": [
    { "name": "claude-3-sonnet" }, // Prefer Sonnet-class models
    { "name": "claude" } // Fall back to any Claude model
  ],
  "costPriority": 0.3, // Cost is less important
  "speedPriority": 0.8, // Speed is very important
  "intelligencePriority": 0.5 // Moderate capability needs
}
The client processes these preferences to select an appropriate model from its available options. For instance, if the client doesn’t have access to Claude models but has Gemini, it might map the sonnet hint to gemini-1.5-pro based on similar capabilities.

错误处理

Clients 应当 return errors for common failure cases: 示例错误:
{
  "jsonrpc": "2.0",
  "id": 1,
  "error": {
    "code": -1,
    "message": "User rejected sampling request"
  }
}

安全注意事项

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