Configure persistent and distributed storage for caching and OAuth state management
New in version 2.13.0FastMCP uses pluggable storage backends for caching responses and managing OAuth state. By default, all storage is in-memory, which is perfect for development but doesn’t persist across restarts. FastMCP includes support for multiple storage backends, and you can easily extend it with custom implementations.
The storage layer is powered by py-key-value-aio, an async key-value library maintained by a core FastMCP maintainer. This library provides a unified interface for multiple backends, making it easy to swap implementations based on your deployment needs.
Best for: Development, testing, single-process deploymentsIn-memory storage is the default for all FastMCP storage needs. It’s fast, requires no setup, and is perfect for getting started.
Copy
from key_value.aio.stores.memory import MemoryStore# Used by default - no configuration needed# But you can also be explicit:cache_store = MemoryStore()
Best for: Single-server production deployments, persistent cachingDisk storage persists data to the filesystem, allowing it to survive server restarts.
Before using these backends in production, review the py-key-value documentation to understand the maturity level and limitations of your chosen backend. Some backends may be in preview or have specific constraints that make them unsuitable for production use.
The OAuth Proxy and OAuth auth providers use storage for persisting OAuth client registrations and upstream tokens. By default, storage is automatically encrypted using FernetEncryptionWrapper. When providing custom storage, wrap it in FernetEncryptionWrapper to encrypt sensitive OAuth tokens at rest.Development (default behavior):By default, FastMCP automatically manages keys and storage based on your platform:
Mac/Windows: Keys are auto-managed via system keyring, storage defaults to disk. Suitable only for development and local testing.
Linux: Keys are ephemeral, storage defaults to memory.
No configuration needed:
Copy
from fastmcp.server.auth.providers.github import GitHubProviderauth = GitHubProvider( client_id="your-id", client_secret="your-secret", base_url="https://your-server.com")
Production:For production deployments, configure explicit keys and persistent network-accessible storage with encryption:
Both parameters are required for production. Wrap your storage in FernetEncryptionWrapper to encrypt sensitive OAuth tokens at rest - without it, tokens are stored in plaintext. See OAuth Token Security and Key and Storage Management for complete setup details.
The Response Caching Middleware caches tool calls, resource reads, and prompt requests. Storage configuration is passed via the cache_storage parameter:
Copy
from fastmcp import FastMCPfrom fastmcp.server.middleware.caching import ResponseCachingMiddlewarefrom key_value.aio.stores.disk import DiskStoremcp = FastMCP("My Server")# Cache to disk instead of memorymcp.add_middleware(ResponseCachingMiddleware( cache_storage=DiskStore(directory="cache")))
For multi-server deployments sharing a Redis instance:
The FastMCP Client uses storage for persisting OAuth tokens locally. By default, tokens are stored in memory:
Copy
from fastmcp.client.auth import OAuthClientProviderfrom key_value.aio.stores.disk import DiskStore# Store tokens on disk for persistence across restartstoken_storage = DiskStore(directory="~/.local/share/fastmcp/tokens")oauth_provider = OAuthClientProvider( mcp_url="https://your-mcp-server.com/mcp/sse", token_storage=token_storage)
This allows clients to reconnect without re-authenticating after restarts.