caching
Client-side response caching primitives (SEP-2549, protocol revision 2026-07-28).
CacheMode
module-attribute
CacheMode = Literal['use', 'refresh', 'bypass']
Per-call cache behavior: "use" serves and stores, "refresh" stores
without serving, "bypass" skips the cache entirely.
MAX_TTL_MS
module-attribute
Cap on any entry's time-to-live (24 hours, in milliseconds); larger ttlMs values are clamped down.
CacheKey
dataclass
Identity of one cached response; compare as the field tuple, never a flattened string (collision hazard).
Source code in src/mcp/client/caching.py
44 45 46 47 48 49 50 51 52 53 54 | |
CacheEntry
dataclass
One cached response with its freshness and sharing metadata.
Source code in src/mcp/client/caching.py
57 58 59 60 61 62 63 64 65 66 67 68 | |
value
instance-attribute
value: Any
The cached result; the SDK deep-copies on write and on serve, so a store may hold it as-is.
scope
instance-attribute
scope: Literal['public', 'private']
Server-asserted cacheScope: only "public" entries may be shared across authorization contexts.
expires_at
instance-attribute
expires_at: float | None
Epoch seconds after which the entry is stale; None is never fresh.
ResponseCacheStore
Bases: Protocol
Storage contract for the client response cache.
Each Client calls its store from a single event loop; per-operation
atomicity is the implementation's responsibility. Operations may raise -
the SDK degrades to a miss rather than failing the call. A serializing
store must round-trip value back to the result model object (a
wrong-shape entry is a miss, never an error). A lookup may issue two
sequential get calls (private arm, then public).
Source code in src/mcp/client/caching.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
CacheConfig
dataclass
Configuration for a Client's response cache.
Raises:
| Type | Description |
|---|---|
ValueError
|
On a custom |
Source code in src/mcp/client/caching.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
store
class-attribute
instance-attribute
store: ResponseCacheStore | None = None
Backing store; None means a per-client InMemoryResponseCacheStore.
A custom store requires an explicit partition.
partition
class-attribute
instance-attribute
partition: str = ''
Authorization-context identifier isolating "private"-scoped entries
within a shared store. Derive it from a verified credential - never from
request-supplied data or the server URL. Fixed for the Client's
lifetime: construct a new Client when the principal changes.
target_id
class-attribute
instance-attribute
target_id: str | None = None
Server-identity override for custom transports and proxies where the SDK cannot derive one from a URL; must be non-empty when provided.
default_ttl_ms
class-attribute
instance-attribute
default_ttl_ms: int = 0
TTL in milliseconds for results carrying no ttlMs hint; the default 0 leaves them uncached.
clock
class-attribute
instance-attribute
Wall-clock source returning epoch seconds; injectable for expiry tests.
share_public
class-attribute
instance-attribute
share_public: bool = False
Serve server-marked "public" entries across every partition in the store.
WARNING: this trusts the server's "public" classification for every
principal sharing the store - a mislabeled response leaks across tenants.
Constructor-level only: the per-call cache_mode can never widen sharing.
InMemoryResponseCacheStore
Default in-process ResponseCacheStore.
Method bodies are synchronous, so concurrent tasks never observe a torn
write. max_entries caps the whole store, evicting least-recently-used
at the cap (0 disables it); get and set both refresh recency, so a
hot entry survives churn from other keys.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/mcp/client/caching.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
ClientResponseCache
Coordinates the Client caching verbs with a ResponseCacheStore: keys, era gate, TTL/scope, eviction.
Source code in src/mcp/client/caching.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | |
read
async
read(
method: str, params_key: str
) -> CacheableResult | None
Serve a fresh entry for the key, or None; the served result is a deep copy.
Source code in src/mcp/client/caching.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
capture
Register the key for eviction-race detection before the fetch; write takes the returned generation.
Source code in src/mcp/client/caching.py
248 249 250 251 252 253 254 255 256 | |
write
async
write(
method: str,
params_key: str,
result: CacheableResult,
gen_at_capture: int,
mode: Literal["use", "refresh"],
) -> None
Store a fetched result under the arm its resolved scope selects.
Source code in src/mcp/client/caching.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
evict_method
async
evict_method(method: str) -> None
Evict the method's cursor-less entry.
Source code in src/mcp/client/caching.py
297 298 299 | |
evict_key
async
Evict one key from both arms.
Only the current era's arms are touched; other-era entries in a persistent store age out by TTL.
Source code in src/mcp/client/caching.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
evict_for_notification
async
evict_for_notification(
notification: ServerNotification,
) -> None
Map a server notification to the entries it makes stale.
Eviction is eventual (spawned-task dispatch): the generation bump closes the write-back race; a racing read may briefly serve the old entry.
Source code in src/mcp/client/caching.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |