协议版本: 2024-11-05
Model Context Protocol (MCP) 为服务器向客户端公开提示模板提供了一种标准化方式。提示允许服务器为与语言模型交互提供结构化消息和指令。客户端可以发现可用提示、检索其内容,并提供参数进行自定义。

用户交互模型

Prompts are designed to be 用户控制的, meaning they are exposed from servers to clients with the intention of the user being able to explicitly select them for use. 通常,提示会通过用户在界面中发起的命令触发,这使得用户能够自然地发现并调用可用的提示。 例如,以斜线命令形式: Example of prompt exposed as slash command 然而,开发者可以自由地通过任何适合他们需求的界面模式来公开提示 - 协议本身并不强制要求任何特定的用户交互模型。

能力

Servers that support prompts 必须 declare the prompts capability during initialization:
{
  "capabilities": {
    "prompts": {
      "listChanged": true
    }
  }
}
listChanged indicates whether the server will emit notifications when the list of available prompts changes.

协议消息

展示提示项列表

要获取可用提示,客户端发送一个prompts/list请求。此操作支持分页 请求:
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "prompts/list",
  "params": {
    "cursor": "optional-cursor-value"
  }
}
响应: 响应:
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "prompts": [
      {
        "name": "code_review",
        "description": "Asks the LLM to analyze code quality and suggest improvements",
        "arguments": [
          {
            "name": "code",
            "description": "The code to review",
            "required": true
          }
        ]
      }
    ],
    "nextCursor": "next-page-cursor"
  }
}

获取提示

To retrieve a specific prompt, clients send a prompts/get request. Arguments may be auto-completed through the completion API. 请求:
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "prompts/get",
  "params": {
    "name": "code_review",
    "arguments": {
      "code": "def hello():\n    print('world')"
    }
  }
}
响应: 响应:
{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "description": "Code review prompt",
    "messages": [
      {
        "role": "user",
        "content": {
          "type": "text",
          "text": "Please review this Python code:\ndef hello():\n    print('world')"
        }
      }
    ]
  }
}

列表变更通知

当可用提示列表发生变化时,声明了listChanged 功能的服务器应当发送通知:
{
  "jsonrpc": "2.0",
  "method": "notifications/prompts/list_changed"
}

消息流

数据类型

提示

一个提示定义包含:
  • name: 提示的唯一标识符
  • description: 可选的人类可读描述
  • arguments: 可选的参数列表,用于自定义

提示消息

提示消息可以包含:
  • role: 可以是“用户”或“助手”用以标识发言者
  • content: 以下内容类型之一:

文本内容

文本内容代表纯文本消息:
{
  "type": "text",
  "text": "The text content of the message"
}
这是自然语言交互中最常用的内容类型。

图像内容

图像内容允许在消息中包含视觉信息:
{
  "type": "image",
  "data": "base64-encoded-image-data",
  "mimeType": "image/png"
}
The image data 必须 be base64-encoded and include a valid MIME type. This enables multi-modal interactions where visual context is important.

嵌入资源

嵌入式资源允许直接在消息中引用服务器端资源:
{
  "type": "resource",
  "resource": {
    "uri": "resource://example",
    "mimeType": "text/plain",
    "text": "Resource content"
  }
}
Resources can contain either text or binary (blob) data and 必须 include:
  • 有效的资源URI
  • 适当的MIME类型
  • 文本内容或 base64 编码的二进制数据
内嵌资源使提示能够无缝整合服务器管理的内容,如文档、代码示例或其他参考资料,直接融入对话流程。

错误处理

Servers 应当 return standard JSON-RPC errors for common failure cases:
  • 无效的提示名称: -32602 (无效参数)
  • 缺少必需的参数:-32602(无效参数)
  • 内部错误:-32603 (内部错误)

实施注意事项

  1. 在处理之前,服务器 必须 验证 prompt 参数
  2. 客户端应当处理大型提示列表的分页
  3. 双方尊重能力协商

安全性

实现方案必须仔细验证所有提示输入和输出,以防止 注入攻击或对资源的未授权访问。