caching
Server-side caching hints (SEP-2549, protocol revision 2026-07-28).
Results for the cacheable methods carry ttlMs/cacheScope freshness hints.
A handler sets them by returning a result with explicit ttl_ms/cache_scope
values; Server(cache_hints={method: CacheHint(...)}) fills them for handlers
that don't. Fields the handler set win, per field, so a server-wide hint never
overrides a handler's explicit choice.
CacheHint
dataclass
Freshness hint for one cacheable method's results.
ttl_ms is how long, in milliseconds, a client may consider the result
fresh (0 means immediately stale). scope is whether a cached result may
be shared across authorization contexts ("public") or only reused within
the one that produced it ("private").
Source code in src/mcp/server/caching.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
apply_cache_hint
apply_cache_hint(
result: CacheableResultT, hint: CacheHint
) -> CacheableResultT
Fill ttl_ms/cache_scope on result from hint.
Per-field: a field the handler set explicitly - even to its default value,
tracked via model_fields_set - is left alone; only unset fields take the
hint. A handler constructing results with model_construct bypasses that
tracking and is treated as having set nothing.
Source code in src/mcp/server/caching.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
validate_cache_hints
Validate a cache_hints constructor argument into a plain dict.
The Server/MCPServer signatures already close the key set and value
type for type-checked callers; this runtime gate is deliberately loose in
its parameter so it covers everyone else (e.g. a map deserialized from
config) - a bad entry fails at construction, not on the first request to
that method.
Raises:
| Type | Description |
|---|---|
ValueError
|
If a key is not a cacheable method. |
TypeError
|
If a value is not a |
Source code in src/mcp/server/caching.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |