Skip to content

Commit 03da107

Browse files
committed
Add docs
1 parent c600a16 commit 03da107

2 files changed

Lines changed: 66 additions & 5 deletions

File tree

docs/servers/middleware.mdx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,47 @@ mcp.add_middleware(DetailedTimingMiddleware())
442442

443443
The built-in versions include custom logger support, proper formatting, and **DetailedTimingMiddleware** provides operation-specific hooks like `on_call_tool` and `on_read_resource` for granular timing.
444444

445+
### Caching Middleware
446+
447+
Caching middleware is essential for improving performance and reducing server load. FastMCP provides caching middleware at `fastmcp.server.middleware.caching`.
448+
449+
Here's how to use the full version:
450+
451+
```python
452+
from fastmcp.server.middleware.caching import ResponseCachingMiddleware
453+
454+
mcp.add_middleware(ResponseCachingMiddleware())
455+
```
456+
457+
Out of the box, it caches call/list tool, resources, and prompts. Sending a notification of a tool/resource/prompt change will invalidate the cache for the affected method.
458+
459+
Alternatively, it can be configured to only cache specific methods, for example, only caching list tools and only caching calls to `tool1`:
460+
461+
```python
462+
from fastmcp.server.middleware.caching import ResponseCachingMiddleware
463+
464+
mcp.add_middleware(ResponseCachingMiddleware(
465+
method_settings=MethodSettings(
466+
call_tool=CallToolSettings(
467+
included_tools=["tool1"],
468+
),
469+
list_tools=ListToolsSettings(
470+
ttl=30,
471+
)
472+
)
473+
))
474+
```
475+
476+
It can also be configured to cache to disk:
477+
478+
```python
479+
from fastmcp.server.middleware.caching import ResponseCachingMiddleware, DiskCache
480+
481+
mcp.add_middleware(ResponseCachingMiddleware(
482+
cache_backend=DiskCache(path="cache"),
483+
))
484+
```
485+
445486
### Logging Middleware
446487

447488
Request and response logging is crucial for debugging, monitoring, and understanding usage patterns in your MCP server. FastMCP provides comprehensive logging middleware at `fastmcp.server.middleware.logging`.

src/fastmcp/server/middleware/caching.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,24 +314,44 @@ class SharedMethodSettings(TypedDict):
314314
ttl: NotRequired[int]
315315

316316

317+
class ListToolsSettings(SharedMethodSettings):
318+
pass
319+
320+
321+
class ListResourcesSettings(SharedMethodSettings):
322+
pass
323+
324+
325+
class ListPromptsSettings(SharedMethodSettings):
326+
pass
327+
328+
317329
class CallToolSettings(SharedMethodSettings):
318330
"""Extra configuration options for Tool-related caching."""
319331

320332
included_tools: NotRequired[list[str]]
321333
excluded_tools: NotRequired[list[str]]
322334

323335

336+
class ReadResourceSettings(SharedMethodSettings):
337+
pass
338+
339+
340+
class GetPromptSettings(SharedMethodSettings):
341+
pass
342+
343+
324344
class MethodSettings(TypedDict):
325345
"""Config for the response caching middleware methods."""
326346

327-
list_tools: NotRequired[SharedMethodSettings]
347+
list_tools: NotRequired[ListToolsSettings]
328348
call_tool: NotRequired[CallToolSettings]
329349

330-
list_resources: NotRequired[SharedMethodSettings]
331-
read_resource: NotRequired[SharedMethodSettings]
350+
list_resources: NotRequired[ListResourcesSettings]
351+
read_resource: NotRequired[ReadResourceSettings]
332352

333-
list_prompts: NotRequired[SharedMethodSettings]
334-
get_prompt: NotRequired[SharedMethodSettings]
353+
list_prompts: NotRequired[ListPromptsSettings]
354+
get_prompt: NotRequired[GetPromptSettings]
335355

336356

337357
MethodSettingsType = TypeVar("MethodSettingsType", bound=SharedMethodSettings)

0 commit comments

Comments
 (0)