MCP Specification · 2026-07-28 · zh-TW
快取
Caching
以 ttlMs 與 cacheScope 提供 freshness 與 visibility hints,並與通知及分頁共同運作。
MCP 對部分 resultType: "complete" 的結果提供 caching hints,讓 client 減少不必要的 re-fetch。Caching 與 change notifications 可以同時使用。
可快取結果(Cacheable Results)
下列 operations 的 complete result,server MUST(必須)提供 caching hints:
server/discovertools/listprompts/listresources/listresources/templates/listresources/read
resultType: "input_required" 的 interim result 不可快取,也不攜帶 caching hints。
快取鍵(Cache Key)
Cache key 由 request method 與會影響結果的 parameters 組成,例如 resources/read 的 uri、list request 的 cursor。Client MUST NOT(不得)把某 request 的 cache 用到 method 或 parameters 不同的 request。
經 MRTR retry、帶有 inputResponses 或 requestState 的結果 MUST NOT(不得)快取,因為這些 inputs 不屬於一般 cache key。
存活時間(Time-to-Live, TTL)
ttlMs 是 server 給的 freshness hint,單位毫秒,概念類似 HTTP Cache-Control: max-age:
0:SHOULD(應該)立即視為 stale;client MAY(可以)在每次需要資料時重新抓取。- 正值:收到 response 後該毫秒數內 SHOULD(應該)視為 fresh。
- 缺失:client SHOULD(應該)預設為
0;通常只會發生在較舊 server。 - 負值:client SHOULD(應該)忽略並當作
0;server MUST(必須)提供>= 0的值。
fresh while: now < t_received + ttlMs
Underlying data MAY(可以)在 TTL 尚未到期前改變。TTL 只是告訴 client 多久內合理地不用重新抓取,不保證資料維持不變。
TTL 到期後,response 變成 stale;client 在下次 access 時 SHOULD(應該)重新抓取。Client 若有理由相信資料已變更,MAY(可以)在 TTL 到期前提早 re-fetch。
Client SHOULD NOT(不應)把 TTL 當 polling interval。需要資料時才檢查 freshness;若實作仍選擇主動 polling,MUST(必須)使用 jitter 與 backoff。Re-fetch 發生錯誤時 client MAY(可以)提供 stale result。
快取範圍(Cache Scope)
| 值 | 意義 |
|---|---|
public | 不含 user-specific data;client、shared gateway 或 proxy MAY(可以)跨使用者共用。 |
private | 只可在相同 authorization context 重用;cache MUST NOT(不得)跨 access token / caller 分享。 |
例如所有人相同的 tools list 可用 public;依 authenticated user 變化的 resources/read 通常應用 private。
與通知互動(Interaction with Notifications)
TTL 與 server-push notifications 互補。Server MAY(可以)只提供 TTL 而不宣告 listChanged,也 MAY(可以)同時宣告 listChanged 並提供 TTL。當相關 notification 到達時,即使 cache 仍 fresh,也會使 cached response invalidated,應立即視為 stale。
與分頁互動(Interaction with Pagination)
Paginated list 的每一頁都是獨立 cache entry:
- 每頁有自己的
ttlMs與收到時間。 - Server MAY(可以)讓不同 pages 有不同 TTL。
- 某 page stale 時,client SHOULD(應該)以該 cursor 重抓。
- 沒有 cross-page snapshot guarantee;資料變動可能造成重複或缺口。
- 需要 consistent full snapshot 的 client SHOULD(應該)從第一頁重抓。
- 原本有效 cursor 失效時,client SHOULD(應該)丟棄所有 cached pages 從頭開始。
同一 list request 的所有 pages MUST(必須)使用相同 cacheScope。
安全考量(Security Considerations)
public 代表 result 可能跨 caller 分享,即使原 request 本身經過 authentication。Server MUST(必須)理解:標記為 public 的 authenticated result 可能被 client 或 intermediary 分享到原始 authorization context 之外。
- Server implementer 應確認
cacheScope真正反映 primitive 的 intended visibility。 - Server MUST(必須)另外實作適當的 per-primitive access control。
- Server MUST NOT(不得)把
cacheScope本身當成防止未授權存取的機制。