Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103---title: cachingsidebarTitle: caching---
# `fastmcp.client.caching`
A client response cache store backed by AsyncKeyValue.
The MCP SDK's client response cache (SEP-2549) reads and writes through apluggable `ResponseCacheStore` protocol; the default is a per-client in-memoryLRU. This module adapts that protocol onto the `AsyncKeyValue` key-valueabstraction FastMCP already uses for its other state-management surfaces (theevent store, the OAuth proxy, the response-caching middleware), so a fleet ofFastMCP clients — for example a set of proxy replicas — can share oneRedis-backed response cache.
Because a shared store mingles cached responses across principals, the SDKrequires an explicit `partition` on any custom store (and FastMCP additionallyrequires a `target_id`). The partition is folded into every stored key soentries can never collide or leak across authorization contexts, and theadapter round-trips each result through a small type-tagged envelope validatedagainst an allowlist of cacheable result models — a stored value that names anunknown type is treated as a miss, never imported by name.
Example: ```python from fastmcp import Client from fastmcp.client.caching import KeyValueResponseCacheStore from mcp.client.caching import CacheConfig from key_value.aio.stores.redis import RedisStore
store = KeyValueResponseCacheStore(storage=RedisStore(url="redis://localhost")) config = CacheConfig(store=store, partition="tenant-a", target_id="weather-api") client = Client("https://example.com/mcp", mode="auto", cache=config) ```
## Classes
### `KeyValueResponseCacheStore` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/caching.py#L95" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
A `ResponseCacheStore` backed by any `AsyncKeyValue` store.
Implements the SDK client response cache contract (`get`/`set`/`delete`/`clear`) over the key-value abstraction FastMCP already uses elsewhere, so adistributed deployment can point every client at one shared backend (memory,Redis, etc.). Pass an instance as `CacheConfig(store=...)`; the SDK requiresan explicit `partition` on any custom store, and FastMCP additionallyrequires a `target_id`.
Each adapter instance owns one collection (`collection`), so `clear()` onlyaffects its own namespace and never another tenant's data. `clear()` needsthe backend to support collection destruction or key enumeration; against abackend that supports neither it is a no-op and entries age out by TTL (awarning is logged once).
The SDK wraps every store call defensively — a raised operation degrades toa cache miss rather than failing the request — so this adapter does notre-wrap its own operations.
**Args:**- `storage`: The `AsyncKeyValue` backend. Defaults to an in-process `MemoryStore`.- `collection`: Collection namespace for this adapter's entries.
**Methods:**
#### `get` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/caching.py#L152" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonget(self, key: CacheKey) -> CacheEntry | None```
#### `set` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/caching.py#L166" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonset(self, key: CacheKey, entry: CacheEntry) -> None```
#### `delete` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/caching.py#L182" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythondelete(self, key: CacheKey) -> None```
#### `clear` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/caching.py#L185" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonclear(self) -> None```
Clear this adapter's collection only.
Prefers deleting each enumerated key (which leaves the collectionusable), and falls back to whole-collection destruction. Against abackend that supports neither, this is a no-op (entries age out by TTL)and a warning is logged once. Either path is scoped to this adapter'sown collection, so a shared store's other tenants are never touched.