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

# 补全

模型上下文协议 (MCP) 为服务器提供了一种标准化方式，以便为提示词和资源 URI 提供参数自动补全建议。这使得用户能够在输入参数值时接收上下文建议，从而实现丰富的、类似 IDE 的体验。

## 用户交互模型

MCP 中的补全功能旨在支持类似于 IDE 代码补全的交互式用户体验。

例如，应用程序可以在用户输入时在下拉菜单或弹出菜单中显示补全建议，并能够过滤和选择可用选项。

然而，实现方可以自由通过任何适合其需求的界面模式来暴露补全功能——协议本身并不强制要求任何特定的用户交互模型。

## 协议消息

### 请求补全

为了获取补全建议，客户端发送 `completion/complete` 请求，通过引用类型指定正在补全的内容：

**请求：**

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "completion/complete",
  "params": {
    "ref": {
      "type": "ref/prompt",
      "name": "code_review"
    },
    "argument": {
      "name": "language",
      "value": "py"
    }
  }
}
```

**响应：**

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "completion": {
      "values": ["python", "pytorch", "pyside"],
      "total": 10,
      "hasMore": true
    }
  }
}
```

### 引用类型

协议支持两种类型的补全引用：

| 类型             | 描述       | 示例                                                  |
| -------------- | -------- | --------------------------------------------------- |
| `ref/prompt`   | 按名称引用提示词 | `{"type": "ref/prompt", "name": "code_review"}`     |
| `ref/resource` | 引用资源 URI | `{"type": "ref/resource", "uri": "file:///{path}"}` |

### 补全结果

服务器返回按相关性排序的补全值数组，包括：

* 每个响应最多 100 项
* 可选的可用匹配总数
* 布尔值，指示是否存在额外结果

## 消息流

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

    Note over Client: User types argument
    Client->>Server: completion/complete
    Server-->>Client: Completion suggestions

    Note over Client: User continues typing
    Client->>Server: completion/complete
    Server-->>Client: Refined suggestions
```

## 数据类型

### CompleteRequest

* `ref`: 一个 `PromptReference` 或 `ResourceReference`
* `argument`: 包含以下的对象：
  * `name`: 参数名称
  * `value`: 当前值

### CompleteResult

* `completion`: 包含以下的对象：
  * `values`: 建议数组（最多 100 个）
  * `total`: 可选的匹配总数
  * `hasMore`: 额外结果标志

## 实现注意事项

1. 服务器 **应该**：
   * 返回按相关性排序的建议
   * 在适当的情况下实现模糊匹配
   * 对补全请求进行速率限制
   * 验证所有输入

2. 客户端 **应该**：
   * 对快速的补全请求进行防抖处理
   * 在适当的情况下缓存补全结果
   * 优雅地处理缺失或部分结果

## 安全性

实现方 **必须**：

* 验证所有补全输入
* 实施适当的速率限制
* 控制对敏感建议的访问
* 防止基于补全的信息泄露
