1. 什么是提示? #
提示(Prompts)是服务器提供的预定义模板,用户选择后,客户端将模板内容(可带参数)发给语言模型,引导 AI 完成特定任务。
协议修订版:2025-11-25
| 通俗理解 | 说明 |
|---|---|
| 快捷指令 | 像斜杠命令「/总结」「/代码审查」,用户选一个,自动生成发给 AI 的完整消息 |
| 用户控制 | 由用户显式选择触发,而非模型自动调用 |
| 可参数化 | 模板可含占位符,用户填写后替换(如「要审查的代码」) |
2. 本章你将学到 #
- 提示的能力声明与协议消息
- 如何列出、获取提示
- 提示的数据结构(参数、消息内容类型)
- 错误处理与实现建议
3. 典型场景 #
用户输入 /code_review,客户端向服务器请求 code_review 提示,传入当前选中的代码。服务器返回填充好的消息列表,客户端将其发给 LLM,LLM 据此进行代码审查。
4. 能力 #
服务器必须在初始化时声明 prompts 能力:
{
"capabilities": {
"prompts": {
"listChanged": true
}
}
}| 字段 | 说明 |
|---|---|
listChanged |
为 true 时,提示列表变化时应当发送 notifications/prompts/list_changed |
5. 协议消息 #
5.1 列出提示(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",
"title": "Request 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"
}
}5.2 获取提示(prompts/get) #
请求:
{
"jsonrpc": "2.0",
"id": 2,
"method": "prompts/get",
"params": {
"name": "code_review",
"arguments": {
"code": "def hello():\n print('world')"
}
}
}参数可通过补全 API 自动补全。
响应:
{
"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')"
}
}
]
}
}5.3 列表变更通知 #
当提示列表变化时,服务器应当发送:
{
"jsonrpc": "2.0",
"method": "notifications/prompts/list_changed"
}6. 流程示意 #
sequenceDiagram
participant C as 客户端
participant S as 服务器
C->>S: prompts/list
S-->>C: 提示列表
C->>S: prompts/get (name, arguments)
S-->>C: 填充后的消息
opt 列表变更
S-->>C: notifications/prompts/list_changed
C->>S: prompts/list
end
7. 数据结构 #
7.1 提示定义 #
| 字段 | 说明 |
|---|---|
name |
唯一标识符 |
title |
可选,显示名称 |
description |
可选,描述 |
arguments |
可选,参数列表(name、description、required) |
icons |
可选,图标数组 |
7.2 消息内容类型 #
| 类型 | 说明 |
|---|---|
text |
纯文本 |
image |
图像(base64 + mimeType) |
audio |
音频(base64 + mimeType) |
resource |
嵌入式资源引用(uri、mimeType、text 或 blob) |
8. 错误处理 #
| 情况 | 错误码 |
|---|---|
| 无效提示名称 | -32602 |
| 缺少必需参数 | -32602 |
| 内部错误 | -32603 |