Skip to main content

fastmcp.server.middleware.caching

A middleware for response caching.

Classes

CachableResourceContent

A wrapper for ResourceContent that can be cached.

CachableResourceResult

A wrapper for ResourceResult that can be cached. Methods:

get_size

wrap

unwrap

CachableToolResult

Methods:

wrap

unwrap

CachableMessage

A wrapper for Message that can be cached.

CachablePromptResult

A wrapper for PromptResult that can be cached. Methods:

get_size

wrap

unwrap

SharedMethodSettings

Shared config for a cache method.

ListToolsSettings

Configuration options for Tool-related caching.

ListResourcesSettings

Configuration options for Resource-related caching.

ListPromptsSettings

Configuration options for Prompt-related caching.

CallToolSettings

Configuration options for Tool-related caching.

ReadResourceSettings

Configuration options for Resource-related caching.

GetPromptSettings

Configuration options for Prompt-related caching.

ResponseCachingStatistics

ResponseCachingMiddleware

The response caching middleware offers a simple way to cache responses to mcp methods. The Middleware supports cache invalidation via notifications from the server. The Middleware implements TTL-based caching but cache implementations may offer additional features like LRU eviction, size limits, and more. When items are retrieved from the cache they will no longer be the original objects, but rather no-op objects this means that response caching may not be compatible with other middleware that expects original subclasses. Notes:
  • Caches tools/call, resources/read, prompts/get, tools/list, resources/list, and prompts/list requests.
  • Cache keys are derived from method name and arguments.
Methods:

on_list_tools

List tools from the cache, if caching is enabled, and the result is in the cache. Otherwise, otherwise call the next middleware and store the result in the cache if caching is enabled.

on_list_resources

List resources from the cache, if caching is enabled, and the result is in the cache. Otherwise, otherwise call the next middleware and store the result in the cache if caching is enabled.

on_list_prompts

List prompts from the cache, if caching is enabled, and the result is in the cache. Otherwise, otherwise call the next middleware and store the result in the cache if caching is enabled.

on_call_tool

Call a tool from the cache, if caching is enabled, and the result is in the cache. Otherwise, otherwise call the next middleware and store the result in the cache if caching is enabled.

on_read_resource

Read a resource from the cache, if caching is enabled, and the result is in the cache. Otherwise, otherwise call the next middleware and store the result in the cache if caching is enabled.

on_get_prompt

Get a prompt from the cache, if caching is enabled, and the result is in the cache. Otherwise, otherwise call the next middleware and store the result in the cache if caching is enabled.

statistics

Get the statistics for the cache.