Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/servers/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,47 @@ mcp.add_middleware(DetailedTimingMiddleware())

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.

### Caching Middleware

Caching middleware is essential for improving performance and reducing server load. FastMCP provides caching middleware at `fastmcp.server.middleware.caching`.

Here's how to use the full version:

```python
from fastmcp.server.middleware.caching import ResponseCachingMiddleware

mcp.add_middleware(ResponseCachingMiddleware())
```

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.

Alternatively, it can be configured to only cache specific methods, for example, only caching list tools and only caching calls to `tool1`:

```python
from fastmcp.server.middleware.caching import ResponseCachingMiddleware

mcp.add_middleware(ResponseCachingMiddleware(
method_settings=MethodSettings(
call_tool=CallToolSettings(
included_tools=["tool1"],
),
list_tools=ListToolsSettings(
ttl=30,
)
)
))
```

It can also be configured to cache to disk:

```python
from fastmcp.server.middleware.caching import ResponseCachingMiddleware, DiskCache

mcp.add_middleware(ResponseCachingMiddleware(
cache_backend=DiskCache(path="cache"),
))
```

### Logging Middleware

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`.
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ classifiers = [
[project.optional-dependencies]
websockets = ["websockets>=15.0.1"]
openai = ["openai>=1.102.0"]
caching = ["diskcache>=5.6.3", "cachetools>=6.2.0"]

[dependency-groups]
dev = [
"dirty-equals>=0.9.0",
"fastmcp[openai]",
"fastmcp[caching]",
# add optional dependencies for fastmcp dev
"fastapi>=0.115.12",
"inline-snapshot[dirty-equals]>=0.27.2",
Expand All @@ -68,6 +70,7 @@ dev = [
"pytest-xdist>=3.6.1",
"ruff",
"ty>=0.0.1a19",
"pytest-benchmark>=5.1.0",
]

[project.scripts]
Expand Down
Loading
Loading