> ## Documentation Index
> Fetch the complete documentation index at: https://mcp.developerdoc.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 提示

<div id="enable-section-numbers" />

Model Context Protocol (MCP) 为服务器向客户端公开提示模板提供了标准化方式。
提示允许服务器提供用于与语言模型交互的结构化消息和指令。
客户端可以发现可用提示、检索其内容，并提供参数来对其进行定制。

## 用户交互模型

提示被设计为**用户控制**，也就是说，服务器向客户端公开提示，是为了让用户能够明确选择并使用它们。

通常，提示会通过用户界面中由用户发起的命令触发，使用户能够自然地发现并调用可用提示。

例如，可以作为斜杠命令：

<img src="https://mintcdn.com/mcp-af858c62/jSqzMzM_desZfWyn/specification/2025-11-25/server/slash-command.png?fit=max&auto=format&n=jSqzMzM_desZfWyn&q=85&s=51e36ee801045320c32096ff50825afa" alt="作为斜杠命令公开的提示示例" width="293" height="106" data-path="specification/2025-11-25/server/slash-command.png" />

不过，实现者可以自由地通过任何适合自身需求的界面模式公开提示；
协议本身并不强制规定任何特定的用户交互模型。

## 能力

支持提示的服务器在[初始化](/specification/2025-11-25/basic/lifecycle#initialization)期间
**MUST** 声明 `prompts` 能力：

```json theme={null}
{
  "capabilities": {
    "prompts": {
      "listChanged": true
    }
  }
}
```

`listChanged` 表示服务器是否会在可用提示列表变化时发出通知。

## 协议消息

### 列出提示

为了检索可用提示，客户端会发送 `prompts/list` 请求。此操作支持[分页](/specification/2025-11-25/server/utilities/pagination)。

**请求：**

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "prompts/list",
  "params": {
    "cursor": "optional-cursor-value"
  }
}
```

**响应：**

```json theme={null}
{
  "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
          }
        ],
        "icons": [
          {
            "src": "https://example.com/review-icon.svg",
            "mimeType": "image/svg+xml",
            "sizes": ["any"]
          }
        ]
      }
    ],
    "nextCursor": "next-page-cursor"
  }
}
```

### 获取提示

为了检索特定提示，客户端会发送 `prompts/get` 请求。参数可以通过[补全 API](/specification/2025-11-25/server/utilities/completion)自动补全。

**请求：**

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "prompts/get",
  "params": {
    "name": "code_review",
    "arguments": {
      "code": "def hello():\n    print('world')"
    }
  }
}
```

**响应：**

```json theme={null}
{
  "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` 能力的服务器 **SHOULD** 发送通知：

```json theme={null}
{
  "jsonrpc": "2.0",
  "method": "notifications/prompts/list_changed"
}
```

## 消息流

```mermaid theme={null}
sequenceDiagram
    participant Client
    participant Server

    Note over Client,Server: Discovery
    Client->>Server: prompts/list
    Server-->>Client: List of prompts

    Note over Client,Server: Usage
    Client->>Server: prompts/get
    Server-->>Client: Prompt content

    opt listChanged
      Note over Client,Server: Changes
      Server--)Client: prompts/list_changed
      Client->>Server: prompts/list
      Server-->>Client: Updated prompts
    end
```

## 数据类型

### Prompt

提示定义包括：

* `name`：提示的唯一标识符
* `title`：用于显示的可选人类可读提示名称。
* `description`：可选的人类可读描述
* `icons`：用于在用户界面中显示的可选图标数组
* `arguments`：用于定制的可选参数列表

### PromptMessage

提示中的消息可以包含：

* `role`："user" 或 "assistant"，用于表示说话者
* `content`：以下内容类型之一：

<Note>
  提示消息中的所有内容类型都支持可选的
  [annotations](./resources#annotations)，用于提供受众、优先级和修改时间等元数据。
</Note>

#### 文本内容

文本内容表示纯文本消息：

```json theme={null}
{
  "type": "text",
  "text": "The text content of the message"
}
```

这是自然语言交互中最常用的内容类型。

#### 图像内容

图像内容允许在消息中包含视觉信息：

```json theme={null}
{
  "type": "image",
  "data": "base64-encoded-image-data",
  "mimeType": "image/png"
}
```

图像数据 **MUST** 经过 base64 编码，并包含有效的 MIME 类型。
这使得在视觉上下文很重要的场景中可以进行多模态交互。

#### 音频内容

音频内容允许在消息中包含音频信息：

```json theme={null}
{
  "type": "audio",
  "data": "base64-encoded-audio-data",
  "mimeType": "audio/wav"
}
```

音频数据 MUST 经过 base64 编码，并包含有效的 MIME 类型。
这使得在音频上下文很重要的场景中可以进行多模态交互。

#### 嵌入式资源

嵌入式资源允许在消息中直接引用服务器端资源：

```json theme={null}
{
  "type": "resource",
  "resource": {
    "uri": "resource://example",
    "mimeType": "text/plain",
    "text": "Resource content"
  }
}
```

资源可以包含文本或二进制（blob）数据，并且 **MUST** 包含：

* 有效的资源 URI
* 适当的 MIME 类型
* 文本内容或经过 base64 编码的 blob 数据

嵌入式资源使提示能够将文档、代码示例或其他参考材料等由服务器管理的内容无缝纳入对话流程。

## 错误处理

服务器 **SHOULD** 针对常见失败情况返回标准 JSON-RPC 错误：

* 无效提示名称：`-32602`（Invalid params）
* 缺少必需参数：`-32602`（Invalid params）
* 内部错误：`-32603`（Internal error）

## 实现注意事项

1. 服务器 **SHOULD** 在处理前验证提示参数
2. 客户端 **SHOULD** 为大型提示列表处理分页
3. 双方 **SHOULD** 遵守能力协商

## 安全

实现 **MUST** 仔细验证所有提示输入和输出，防止注入攻击或未经授权访问资源。
