Add cache_hints constructor map for SEP-2549 caching hints#3015
Draft
maxisbey wants to merge 1 commit into
Draft
Add cache_hints constructor map for SEP-2549 caching hints#3015maxisbey wants to merge 1 commit into
maxisbey wants to merge 1 commit into
Conversation
Server and MCPServer take cache_hints={method: CacheHint(...)} to set
ttlMs/cacheScope on the six cacheable results server-wide. The runner
fills the typed result after the handler returns, so fields a handler
sets explicitly win, per field (via model_fields_set), and the existing
serialize sieve keeps pre-2026 wires clean. Keys are typed as the
CacheableMethod Literal so editors autocomplete them and flag typos;
runtime validation still rejects bad keys and values at construction
for untyped callers.
Part of #2899.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
cache_hintsconstructor option toServerandMCPServerso server operators can set the SEP-2549 caching hints (ttlMs/cacheScope) emitted on the six cacheable results.Motivation and Context
The 2026-07-28 schema requires
ttlMs/cacheScopeon the results oftools/list,prompts/list,resources/list,resources/templates/list,resources/read, andserver/discover. The models default them to0/"private"(immediately stale, never shared) — wire-valid and safe, but until now there was no way for anMCPServeroperator to declare anything else: every high-level server told every client "don't cache this", regardless of intent. Lowlevel handlers could set the fields on the results they return, but only per handler.This adds the server-wide knob:
Design notes:
Server(next toinstructions=);MCPServerforwards it. One mechanism covers all six methods, includingserver/discover, whose default handler is lowlevel-owned.ServerRunnerfills the typed result after the handler returns and before serialization, so the existing version sieve still strips the fields for pre-2026 peers, andresultType: "input_required"results are never stamped (they aren'tCacheableResultmodels).model_fields_set: a field the handler set explicitly — even to the default value — always wins over the configured hint. A configured hint can never clobber a handler's explicitcache_scope.CacheableMethodLiteral(the analogue of the TypeScript SDK'sPartial<Record<CacheableResultMethod, CacheHint>>), so editors autocomplete them and type checkers flag typos. Runtime validation still rejects unknown keys (ValueError) and non-CacheHintvalues (TypeError) at construction for untyped callers.cacheScopereaches every page of a paginated list by construction: the map is keyed by method, not cursor.How Has This Been Tested?
tests/server/test_caching.py: hint validation, per-field precedence (including explicit-default-wins), wire round-trips on both server tiers, discover, and per-page scope consistency.tests/server/test_runner.pyproving configured hints never reach a 2025-11-25 peer.docs/advanced/caching.mdwith runnabledocs_src/caching/examples pinned bytests/docs_src/test_caching.py.cachingscenario (CI) covers the emitted-hints requirements.Breaking Changes
None — new optional keyword argument; defaults unchanged.
Types of changes
Checklist
Additional context
Part of #2899. Client-side cache honoring (a freshness cache that uses these hints) and a per-registration
@mcp.resource(..., cache_hint=...)override are intentionally out of scope for this PR.AI Disclaimer