导航菜单

  • 1.什么是MCP
  • 2.架构概览
  • 3.理解MCP服务器
  • 4.理解MCP客户端
  • 5.版本控制
  • 6.连接本地MCP服务器
  • 7.连接远程MCP服务器
  • 8.构建MCP服务器
  • 9.构建MCP客户端
  • 10.SDKs
  • 11.理解MCP中的授权
  • 12.安全最佳实践
  • 13.MCP Inspector
  • 14.规范
  • 15.关键变更
  • 16.架构
  • 17.基础协议概述
  • 18.生命周期
  • 19.传输
  • 20.授权
  • 21.取消
  • 22.Ping
  • 23.进度
  • 24.任务
  • 25.根
  • 26.采样
  • 27.引导
  • 29.提示
  • 30.资源
  • 31.工具
  • 32.补全
  • 33.日志
  • 34.分页
  • 35.模式参考
  • Keycloak
  • 28.服务器功能
  • 1. 什么是提示?
  • 2. 本章你将学到
  • 3. 典型场景
  • 4. 能力
  • 5. 协议消息
    • 5.1 列出提示(prompts/list)
    • 5.2 获取提示(prompts/get)
    • 5.3 列表变更通知
  • 6. 流程示意
  • 7. 数据结构
    • 7.1 提示定义
    • 7.2 消息内容类型
  • 8. 错误处理

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
← 上一节 28.服务器功能 下一节 30.资源 →

访问验证

请输入访问令牌

Token不正确,请重新输入