Skip to content

MCP Specification · 2026-07-28 · zh-TW

自動完成

Completion

Server 為 prompt arguments 與 resource-template arguments 提供情境式 autocomplete 建議。

MCP Completion 讓 server 為 Prompt arguments 與 Resource Template arguments 提供 autocomplete suggestions。當使用者輸入某個 argument value 時,server 可以依目前值與其他已完成 arguments 提供 contextual suggestions。

請求中繼資料(Request metadata)

本頁範例省略 _meta;實際每個 request 仍 MUST(必須)包含 io.modelcontextprotocol/protocolVersionio.modelcontextprotocol/clientCapabilitiesio.modelcontextprotocol/clientInfo 為選用欄位,但 client SHOULD(應該)在未特別停用時提供。

使用者互動模型(User Interaction Model)

Completion 適合類似 IDE autocomplete 的互動,例如使用者輸入時顯示 dropdown / popup suggestions。Protocol 不規定 UI。

能力宣告(Capabilities)

支援 completion 的 server MUST(必須)宣告:

{
  "capabilities": {
    "completions": {}
  }
}

請求自動完成(Requesting Completions)

Client 送 completion/complete,用 ref 指定正在完成的 Prompt 或 Resource Template,再提供目前 argument:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "completion/complete",
  "params": {
    "ref": {
      "type": "ref/prompt",
      "name": "code_review"
    },
    "argument": {
      "name": "language",
      "value": "py"
    }
  }
}
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "resultType": "complete",
    "completion": {
      "values": ["python", "pytorch", "pyside"],
      "total": 10,
      "hasMore": true
    }
  }
}

多 argument 的 Prompt / URI template 應在 context.arguments 帶入先前已解析的值,讓 server 能依上下文縮小 suggestions。

參照型別(Reference Types)

Type用途
ref/prompt以 name 指向 Prompt。
ref/resource指向 Resource URI 或 URI Template。

Completion result 最多回 100 個 values,可另提供 totalhasMore

資料型別(Data Types)

CompleteRequest

  • ref:PromptReference 或 ResourceTemplateReference。
  • argument.name:目前正在完成的 argument name。
  • argument.value:目前輸入值。
  • context.arguments:已解析 arguments 的 mapping。

CompleteResult

  • completion.values:suggestions,最多 100 個。
  • completion.total:選用的總 matches 數。
  • completion.hasMore:是否還有其他結果。

錯誤處理與安全(Errors and Security)

Server SHOULD(應該)對常見失敗回標準 JSON-RPC errors:

  • Capability 不支援:-32601 Method not found
  • 無效 prompt name / missing arguments:-32602 Invalid params
  • internal error:-32603 Internal error

Server SHOULD(應該)依 relevance 排序 suggestions、適當使用 fuzzy matching、rate limit completion requests 並驗證所有 inputs。Client SHOULD(應該)debounce 快速連續 request、適當 cache results,並能處理 missing / partial results。

安全性方面,implementation MUST(必須)

  • 驗證所有 completion inputs。
  • 實作適當的 rate limiting。
  • 控制 sensitive suggestions 的 access。
  • 避免 completion 成為 information-disclosure channel。