MCP Specification · 2026-07-28 · zh-TW
資源
Resources
Server 以 URI 暴露檔案、資料庫 schema 與應用程式資料,供 client 作為模型上下文。
MCP 讓 server 以標準方式向 client 暴露 Resources。每個 resource 由 URI 唯一識別,可代表檔案、database schema 或應用程式特有資料,供 host application 納入模型 context。
本頁範例為簡潔省略 _meta;實際每個 request 仍 MUST(必須)包含 io.modelcontextprotocol/protocolVersion 與 io.modelcontextprotocol/clientCapabilities。io.modelcontextprotocol/clientInfo 為選用欄位,但 client SHOULD(應該)在未特別停用時提供。
使用者互動模型(User Interaction Model)
Resources 是 application-driven primitive。Host application 可以提供 resource picker、搜尋/過濾介面,或依 heuristics / model selection 自動納入 context;protocol 本身不強制 UI。
能力宣告(Capabilities)
支援 resources 的 server MUST(必須)宣告:
{
"capabilities": {
"resources": {
"listChanged": true,
"subscribe": true
}
}
}
listChanged:清單變更時是否發 notification。subscribe:是否支援透過subscriptions/listen的resourceSubscriptions訂閱指定 resource update。
兩者可獨立支援,也可都不支援。宣告 resources capability 的 server MUST(必須)回應 resources/list;集合 MAY(可以)為空,也 MAY(可以)隨時間改變,但 MUST NOT(不得)因 connection state 或同一 connection 上其他 request 的副作用而變化。集合 MAY(可以)依 request authorization 不同。
列出資源(Listing Resources)
resources/list 支援 pagination 與 caching。
{
"jsonrpc": "2.0",
"id": 1,
"method": "resources/list",
"params": {"cursor": "optional-cursor-value"}
}
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"resultType": "complete",
"resources": [{
"uri": "file:///project/src/main.rs",
"name": "main.rs",
"title": "Rust Software Application Main File",
"mimeType": "text/x-rust"
}],
"nextCursor": "next-page-cursor",
"ttlMs": 300000,
"cacheScope": "public"
}
}
讀取資源(Reading Resources)
Client 用 resources/read 依 URI 取得內容。此 operation 支援 caching;server MAY(可以)對單一 read 回多個 resource contents,例如讀目錄時同時回多個檔案。
{
"jsonrpc": "2.0",
"id": 2,
"method": "resources/read",
"params": {"uri": "file:///project/src/main.rs"}
}
Server MAY(可以)回 InputRequiredResult 要求 MRTR 額外輸入;client 重試時依 MRTR 帶入 inputResponses,並在 server 有提供時原樣帶回 requestState。若 URI scheme 是 https://,client MAY(可以)直接從 web 取得內容,前提是該 resource 的設計確實允許 client 自行 fetch。
資源範本(Resource Templates)
Resource templates 使用 RFC 6570 URI Template 暴露 parameterized resources,arguments 可以透過 Completion API 自動完成。resources/templates/list 同時支援 pagination 與 caching。
{
"jsonrpc": "2.0",
"id": 3,
"method": "resources/templates/list",
"params": {"cursor": "optional-cursor-value"}
}
典型 template 可用 file:///{path} 表示 project files。
訂閱與通知(Subscriptions)
Server 宣告 listChanged 時,在 resources 清單改變後 SHOULD(應該)發:
{"jsonrpc":"2.0","method":"notifications/resources/list_changed"}
Client 若要追蹤指定 resource,透過 subscriptions/listen 的 resourceSubscriptions filter 訂閱;resource 變更時 server 送:
{
"jsonrpc": "2.0",
"method": "notifications/resources/updated",
"params": {
"_meta": {"io.modelcontextprotocol/subscriptionId": 4},
"uri": "file:///project/src/main.rs"
}
}
註解(Annotations)
Resources、resource templates 與 content blocks 可攜帶 annotations:
audience:user/assistant的目標 audience。priority:0.0–1.0 的重要度 hint。lastModified:ISO 8601 修改時間。
Client 可用它們做 filter、排序或決定 context 優先度。
常見 URI 配置(Common URI Schemes)
https://:Server SHOULD(應該)只在 client 能自行直接從 web fetch / load resource、不需要再透過 MCP server 讀取時使用;其他情況 server SHOULD(應該)優先使用其他 scheme 或自訂 scheme,即使實際內容仍由 server 自網路下載。file://:表示 filesystem-like resource,不一定對應實體 filesystem;MCP server MAY(可以)搭配 XDG MIME type(例如inode/directory)表示 directory 等非一般檔案。git://:Git version-control integration。- Custom scheme:MUST(必須)符合 RFC 3986。
錯誤處理與安全(Errors and Security)
不存在的 resource MUST(必須)回 JSON-RPC -32602 Invalid params;server 對 internal error SHOULD(應該)回 -32603 Internal error。Client 為相容舊版也 SHOULD(應該)接受舊的 -32002 作為 resource-not-found error。Server MUST NOT(不得)用空 contents array 表示不存在,因為會和「存在但內容為空」混淆。
- Server MUST(必須)驗證所有 resource URI。
- 敏感 resources SHOULD(應該)實作 access control。
- Binary data MUST(必須)正確編碼。
- 操作前 SHOULD(應該)檢查 resource permission。
- 處理
file://時 server MUST(必須)sanitize path,避免 directory traversal。