docs: add context-aware tool factory example (#3264)

Fixes PrefectHQ/fastmcp#1841

Signed-off-by: machov <mv1742@nyu.edu>
This commit is contained in:
Manrique Vargas 2026-02-22 11:24:22 -05:00 committed by GitHub
commit c71840631e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 70 additions and 0 deletions

View file

@ -191,3 +191,41 @@ mcp.add_tool(safe_division)
The `forward()` function handles argument mapping automatically. Call it with the transformed argument names, and it maps them back to the original function's parameters.
For direct access to the original function without mapping, use `forward_raw()` with the original parameter names.
## Context-Aware Tool Factories
You can write functions that act as "factories," generating specialized versions of a tool for different contexts. For example, create a `get_my_data` tool for the current user by hiding the `user_id` parameter and providing it automatically.
```python
from fastmcp import FastMCP
from fastmcp.tools import Tool, tool
from fastmcp.tools.tool_transform import ArgTransform
# A generic tool that requires a user_id
@tool
def get_user_data(user_id: str, query: str) -> str:
"""Fetch data for a specific user."""
return f"Data for user {user_id}: {query}"
def create_user_tool(user_id: str) -> Tool:
"""Factory that creates a user-specific version of get_user_data."""
return Tool.from_tool(
get_user_data,
name="get_my_data",
description="Fetch your data. No need to specify a user ID.",
transform_args={
"user_id": ArgTransform(hide=True, default=user_id),
},
)
# Create a server with a tool customized for the current user
mcp = FastMCP("User Server")
current_user_id = "user-123" # e.g., from auth context
mcp.add_tool(create_user_tool(current_user_id))
# Clients see "get_my_data(query: str)" — user_id is injected automatically
```
This pattern is useful for multi-tenant servers where each connection gets tools pre-configured with their identity, or for wrapping generic tools with environment-specific defaults.

View file

@ -705,3 +705,35 @@ You can chain transformations by using an already transformed tool as the parent
### Context-Aware Tool Factories
You can write functions that act as "factories," generating specialized versions of a tool for different contexts. For example, you could create a `get_my_data` tool that is specific to the currently logged-in user by hiding the `user_id` parameter and providing it automatically.
```python
from fastmcp import FastMCP
from fastmcp.tools import Tool, tool
from fastmcp.tools.tool_transform import ArgTransform
# A generic tool that requires a user_id
@tool
def get_user_data(user_id: str, query: str) -> str:
"""Fetch data for a specific user."""
return f"Data for user {user_id}: {query}"
def create_user_tool(user_id: str) -> Tool:
"""Factory that creates a user-specific version of get_user_data."""
return Tool.from_tool(
get_user_data,
name="get_my_data",
description="Fetch your data. No need to specify a user ID.",
transform_args={
"user_id": ArgTransform(hide=True, default=user_id),
},
)
# Create a server with a tool customized for the current user
mcp = FastMCP("User Server")
current_user_id = "user-123" # e.g., from auth context
mcp.add_tool(create_user_tool(current_user_id))
# Clients see "get_my_data(query: str)" — user_id is injected automatically
```