Add ToolInjectionMiddleware + Tools for Read/List Resource/Prompt for Client Compat (#2142)

* Add compatibility tools contrib module

Implements four standalone tools that expose resources and prompts
as callable tools for clients that only support the tools capability.

Features:
- list_resources: List all available resources
- get_resource: Read a resource by URI
- list_prompts: List all available prompts
- get_prompt: Get a prompt with optional arguments

The tools use Context to access the server instance and can be easily
added to any FastMCP server using the add_compatibility_tools helper
or by adding individual tool instances directly.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: William Easton <strawgate@users.noreply.github.com>

* Simplify compatibility tools to return raw MCP protocol objects

Return raw MCP protocol objects (ListResourcesResult, ReadResourceResult,
ListPromptsResult, GetPromptResult) instead of custom dictionaries. This
makes the tools simpler and more predictable by directly exposing what
the client methods return.

Co-authored-by: William Easton <strawgate@users.noreply.github.com>

* Add tool injection middleware

* cleanup contrib module

* More clean-up

* Clean up tool injection middleware.

* Update src/fastmcp/server/middleware/tool_injection.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Add tool injection docs

* Small cleanup of prompt middleware

* PR Feedback

* Fix tool injection tests

---------

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: William Easton <strawgate@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
William Easton 2025-10-24 17:06:58 -05:00 committed by GitHub
commit f5dbabff4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 657 additions and 0 deletions

View file

@ -449,6 +449,41 @@ 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.
### Tool Injection Middleware
Tool injection middleware is a middleware that injects tools into the server during the request lifecycle:
```python
from fastmcp.server.middleware.tool_injection import ToolInjectionMiddleware
def my_tool_fn(a: int, b: int) -> int:
return a + b
my_tool = Tool.from_function(fn=my_tool_fn, name="my_tool")
mcp.add_middleware(ToolInjectionMiddleware(tools=[my_tool]))
```
### Prompt Tool Middleware
Prompt tool middleware is a compatibility middleware for clients that are unable to list or get prompts. It provides two tools: `list_prompts` and `get_prompt` which allow clients to list and get prompts respectively using only tool calls.
```python
from fastmcp.server.middleware.tool_injection import PromptToolMiddleware
mcp.add_middleware(PromptToolMiddleware())
```
### Resource Tool Middleware
Resource tool middleware is a compatibility middleware for clients that are unable to list or read resources. It provides two tools: `list_resources` and `read_resource` which allow clients to list and read resources respectively using only tool calls.
```python
from fastmcp.server.middleware.tool_injection import ResourceToolMiddleware
mcp.add_middleware(ResourceToolMiddleware())
```
### Caching Middleware
Caching middleware is essential for improving performance and reducing server load. FastMCP provides caching middleware at `fastmcp.server.middleware.caching`.