You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
444
444
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
+
445
486
### Logging Middleware
446
487
447
488
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`.
0 commit comments