Skip to main content

fastmcp.server.middleware.caching

A middleware for response caching.

Classes

CachableReadResourceContents

A wrapper for ReadResourceContents that can be cached. Methods:

get_size

get_size(self) -> int

get_sizes

get_sizes(cls, values: Sequence[Self]) -> int

wrap

wrap(cls, values: Sequence[ReadResourceContents]) -> list[Self]

unwrap

unwrap(cls, values: Sequence[Self]) -> list[ReadResourceContents]

CachableToolResult

Methods:

wrap

wrap(cls, value: ToolResult) -> Self

unwrap

unwrap(self) -> ToolResult

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

on_list_tools(self, context: MiddlewareContext[mcp.types.ListToolsRequest], call_next: CallNext[mcp.types.ListToolsRequest, Sequence[Tool]]) -> Sequence[Tool]
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

on_list_resources(self, context: MiddlewareContext[mcp.types.ListResourcesRequest], call_next: CallNext[mcp.types.ListResourcesRequest, Sequence[Resource]]) -> Sequence[Resource]
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

on_list_prompts(self, context: MiddlewareContext[mcp.types.ListPromptsRequest], call_next: CallNext[mcp.types.ListPromptsRequest, Sequence[Prompt]]) -> Sequence[Prompt]
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

on_call_tool(self, context: MiddlewareContext[mcp.types.CallToolRequestParams], call_next: CallNext[mcp.types.CallToolRequestParams, ToolResult]) -> ToolResult
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

on_read_resource(self, context: MiddlewareContext[mcp.types.ReadResourceRequestParams], call_next: CallNext[mcp.types.ReadResourceRequestParams, Sequence[ReadResourceContents]]) -> Sequence[ReadResourceContents]
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

on_get_prompt(self, context: MiddlewareContext[mcp.types.GetPromptRequestParams], call_next: CallNext[mcp.types.GetPromptRequestParams, mcp.types.GetPromptResult]) -> mcp.types.GetPromptResult
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

statistics(self) -> ResponseCachingStatistics
Get the statistics for the cache.
I