协议修订: 2024-11-05
用户交互模型
提示被设计为用户控制,这意味着它们从服务器暴露到客户端,目的是让用户能够明确选择它们用于使用。 通常,提示会通过用户界面中的用户发起的命令触发,这允许用户自然地发现和调用可用的提示。 例如,作为斜杠命令:
功能
支持提示的服务器必须在初始化期间声明prompts 功能:
listChanged 指示服务器是否会在可用提示列表更改时发出通知。
协议消息
列出提示
要检索可用的提示,客户端发送prompts/list 请求。此操作支持分页。
请求:
获取提示
要检索特定的提示,客户端发送prompts/get 请求。参数可以通过完成API自动完成。
请求:
列表更改通知
当可用提示列表更改时,声明了listChanged 功能的服务器应该发送通知:
消息流程
数据类型
提示
提示定义包括:name:提示的唯一标识符description:可选的人类可读描述arguments:用于自定义的可选参数列表
PromptMessage
提示中的消息可以包含:role: “user” 或 “assistant” 以指示说话者content:以下内容类型之一:
文本内容
文本内容表示纯文本消息:图像内容
图像内容允许在消息中包含视觉信息:嵌入资源
嵌入资源允许在消息中直接引用服务器端资源:- 有效的资源URI
- 适当的MIME类型
- 文本内容或base64编码的blob数据
错误处理
服务器应该为常见失败情况返回标准JSON-RPC错误:- 无效提示名称:
-32602(无效参数) - 缺少必需参数:
-32602(无效参数) - 内部错误:
-32603(内部错误)
实现考虑
- 服务器应该在处理前验证提示参数
- 客户端应该处理大型提示列表的分页
- 双方应该尊重功能协商
安全
实现必须仔细验证所有提示输入和输出以防止注入攻击或对资源的未经授权访问。 The Model Context Protocol (MCP) provides a standardized way for servers to expose prompt templates to clients. Prompts allow servers to provide structured messages and instructions for interacting with language models. Clients can discover available prompts, retrieve their contents, and provide arguments to customize them.User Interaction Model
Prompts are designed to be user-controlled, meaning they are exposed from servers to clients with the intention of the user being able to explicitly select them for use. Typically, prompts would be triggered through user-initiated commands in the user interface, which allows users to naturally discover and invoke available prompts. For example, as slash commands:
Capabilities
Servers that support prompts MUST declare theprompts capability during
initialization:
listChanged indicates whether the server will emit notifications when the list of
available prompts changes.
Protocol Messages
Listing Prompts
To retrieve available prompts, clients send aprompts/list request. This operation
supports
pagination.
Request:
Getting a Prompt
To retrieve a specific prompt, clients send aprompts/get request. Arguments may be
auto-completed through the completion API.
Request:
List Changed Notification
When the list of available prompts changes, servers that declared thelistChanged
capability SHOULD send a notification:
Message Flow
Data Types
Prompt
A prompt definition includes:name: Unique identifier for the promptdescription: Optional human-readable descriptionarguments: Optional list of arguments for customization
PromptMessage
Messages in a prompt can contain:role: Either “user” or “assistant” to indicate the speakercontent: One of the following content types:
Text Content
Text content represents plain text messages:Image Content
Image content allows including visual information in messages:Embedded Resources
Embedded resources allow referencing server-side resources directly in messages:- A valid resource URI
- The appropriate MIME type
- Either text content or base64-encoded blob data
Error Handling
Servers SHOULD return standard JSON-RPC errors for common failure cases:- Invalid prompt name:
-32602(Invalid params) - Missing required arguments:
-32602(Invalid params) - Internal errors:
-32603(Internal error)
Implementation Considerations
- Servers SHOULD validate prompt arguments before processing
- Clients SHOULD handle pagination for large prompt lists
- Both parties SHOULD respect capability negotiation