MCP Specification · 2026-07-28 · zh-TW
取樣
Sampling · Deprecated
Server 經 client 要求 LLM generation 的既有功能;2026-07-28 已棄用,保留供相容性參考。
Sampling 自 MCP 2026-07-28 起已棄用。新實作 SHOULD NOT(不應)採用;既有實作 SHOULD(應該)遷移為直接整合 LLM provider API。本頁保留既有協定語意以支援相容性。
Sampling(取樣)讓 MCP server 能透過用戶端要求 LLM completion / generation,而不必由 server 自己持有模型 API key。Client 保留模型存取、選擇、權限與人機互動控制權。
使用者互動模型(User Interaction Model)
Sampling 可以巢狀在其他 server feature 的處理流程中。基於 trust & safety,SHOULD(應該)始終保留能拒絕 sampling request 的 human-in-the-loop;應用程式 SHOULD(應該)讓使用者檢查 sampling request、檢視/修改 prompt,並在結果回給 server 前審閱 generated response。
能力宣告(Capabilities)
支援 sampling 的 client MUST(必須)在每個 request 的 _meta.io.modelcontextprotocol/clientCapabilities 宣告 sampling:
{
"_meta": {
"io.modelcontextprotocol/clientCapabilities": {
"sampling": {}
}
}
}
若 client 支援 sampling request 內的 tool use,MUST(必須)透過 sampling.tools 宣告:
{
"_meta": {
"io.modelcontextprotocol/clientCapabilities": {
"sampling": { "tools": {} }
}
}
}
Server MUST NOT(不得)把 tool-enabled sampling request 送給沒有宣告 sampling.tools 的 client。
sampling.context 與 includeContext: "thisServer" / "allServers" 也已棄用;server SHOULD(應該)避免使用這些值,並且除非 client 宣告 sampling.context,否則 SHOULD NOT(不應)使用它們。省略 includeContext 時等同 "none"。
建立訊息(Creating Messages)
Server 若在處理原始 request 時需要 LLM generation,會回 InputRequiredResult,並在 inputRequests 中放入 sampling/createMessage:
{
"method": "sampling/createMessage",
"params": {
"messages": [
{
"role": "user",
"content": { "type": "text", "text": "法國首都是哪裡?" }
}
],
"modelPreferences": {
"costPriority": 0.3,
"intelligencePriority": 0.8,
"speedPriority": 0.5
},
"temperature": 0.1,
"maxTokens": 100
}
}
Client 執行 sampling 後,把結果放進 MRTR 重試 request 的 inputResponses:
{
"role": "assistant",
"content": { "type": "text", "text": "法國首都是巴黎。" },
"model": "example-model",
"stopReason": "endTurn"
}
Sampling 中的工具(Tools in Sampling)
Server 可在 sampling request 中提供 tools 與選用的 toolChoice。這些 tool definitions 只屬於該 sampling request,不必對應 server 原本註冊的 MCP tools。
toolChoice.mode:
auto:模型自行決定是否使用工具(預設)。required:模型在完成前 MUST(必須)至少使用一個工具。none:模型 MUST NOT(不得)使用任何工具。
MCP 允許模型平行提出多個 tool use;若 provider 支援關閉 parallel tool use,implementation MAY(可以)把這項控制做成 extension,但它不屬於 core MCP。
多回合工具迴圈(Multi-turn Tool Loop)
當模型回 stopReason: "toolUse" 時,server 通常:
- 執行所有 tool uses。
- 把 tool results 加入 message history,再送一個新的
sampling/createMessage。 - 取得下一個模型 response;若仍有 tool use 就重複。
- 實作 SHOULD(應該)設定 iteration limit,避免無限 tool loop。
訊息內容限制(Message Content Constraints)
當 user message 包含 tool_result 時,該 message MUST(必須)只包含 tool results,不得混入 text、image、audio 等其他 content type。
每個 assistant message 中的 ToolUseContent 都 MUST(必須)在下一個 user message 由相同 toolUseId 的 ToolResultContent 完整對應;該 user message 必須完全由 tool results 組成,而且所有 tool use 都解決以前不得繼續其他訊息。
// 有效概念順序
user: text
assistant: tool_use(call_a), tool_use(call_b)
user: tool_result(call_a), tool_result(call_b)
assistant: text
跨 API 相容性(Cross-API Compatibility)
Sampling 的 message model 刻意抽象化,以支援不同 LLM provider。MCP 使用 user 與 assistant 兩種 role;tool use 由 assistant 產生,tool result 則由 user role 回傳。規範允許平行 tool use。
資料型別(Data Types)
訊息(Messages)
Sampling message MUST(必須)包含 role(user 或 assistant)與 content。Content 可為 text、image、audio,以及 tool use / tool result blocks。
不同 sampling request 間的 message list SHOULD NOT(不應)被保留為隱含 session state。
模型偏好(Model Preferences)
Server 不能假設 client 一定能使用某個特定模型,因此 modelPreferences 以 0–1 的抽象優先序與 optional hints 表示需求:
costPriority:越高越偏好低成本。speedPriority:越高越偏好低延遲。intelligencePriority:越高越偏好高能力模型。hints:依偏好順序提供可彈性比對 model name 的建議;client MAY(可以)映射到其他 provider 的等效模型,最後選擇權仍在 client。
Client SHOULD(應該)尊重 model preference hints,但 hints 是 advisory,不能凌駕 client 自己的可用性、權限或模型選擇政策。
系統提示詞(System Prompt)
Server 可提供 systemPrompt,但 client MAY(可以)修改或忽略,且不必告知 server。
上下文納入(Context Inclusion)
includeContext 的 thisServer 與 allServers 已棄用;none 表示不額外加入 context。Client 基於資料分享風險 MAY(可以)修改或忽略此欄位。
Sampling 參數(Sampling Parameters)
maxTokens 是必填,client MUST(必須)遵守。Client MAY(可以)修改或忽略 temperature、stopSequences 與 provider-specific metadata。
結果欄位(Result Fields)
結果包含 role、content、model 與選用 stopReason。常見 stop reason 包括 endTurn、stopSequence、maxTokens、toolUse;implementation MAY(可以)定義其他值。
錯誤處理(Error Handling)
若 sampling 發生錯誤或使用者拒絕,client 不必特別用 error replay 原始 request;在 InputRequiredResult 模式下,server 並沒有同步等待另一個 response。
安全考量(Security Considerations)
- Client SHOULD(應該)實作使用者審核與同意控制。
- 雙方 SHOULD(應該)驗證 message content。
- Client SHOULD(應該)尊重 model preference hints。
- Client SHOULD(應該)實作 rate limiting。
- 雙方 MUST(必須)適當處理敏感資料。
- 使用工具時,server MUST(必須)確保每個 tool use 都有對應 tool result,且 tool-result user message 不混入其他 content。
- 雙方 SHOULD(應該)限制 tool-loop iteration 次數。