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

# 完成

<Info>**协议修订**: 2024-11-05</Info>

Model Context Protocol (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. 客户端**应该**：
   * 对快速完成请求进行去抖
   * 在适当的地方缓存完成结果
   * 优雅地处理缺失或部分结果

## 安全

实现**必须**：

* 验证所有完成输入
* 实现适当的速率限制
* 控制对敏感建议的访问
* 防止基于完成的情報披露
