fastmcp.utilities.token_cache
In-memory cache for token verification results.
Provides a generic TTL-based cache for AccessToken objects, designed to
reduce repeated network calls during opaque-token verification. Only
successful verifications should be cached; errors and failures must be
retried on every request.
Example:
Classes
TokenCache
TTL-based in-memory cache for AccessToken objects.
Features:
- SHA-256 hashed cache keys (fixed size, regardless of token length).
- Per-entry TTL that respects both the configured
ttl_secondsand the token’s ownexpires_atclaim (whichever is sooner). - Bounded size with FIFO eviction when the cache is full.
- Periodic cleanup of expired entries to prevent unbounded growth.
- Defensive deep copies on both store and retrieve to prevent callers from mutating cached values.
ttl_seconds is None or 0, or
when max_size is 0. Negative values raise ValueError.
Methods:
enabled
get
(True, AccessToken)on a cache hit,(False, None)on a miss- or when caching is disabled. The returned
AccessTokenis a deep - copy that is safe to mutate.

