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
Copy file name to clipboardExpand all lines: docs/patterns/tool-transformation.mdx
+132-1Lines changed: 132 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -566,8 +566,139 @@ Use a transform function returning `ToolResult` for complete control over both c
566
566
567
567
Tool transformation is a flexible feature that supports many powerful patterns. Here are a few common use cases to give you ideas.
568
568
569
+
### Exposing Client Methods as Tools
570
+
571
+
A powerful use case for tool transformation is exposing methods from existing Python clients (GitHub clients, API clients, database clients, etc.) directly as MCP tools. This pattern eliminates boilerplate wrapper functions and treats tools as annotations around client methods.
572
+
573
+
**Without Tool Transformation**, you typically create wrapper functions that duplicate annotations:
574
+
575
+
```python
576
+
asyncdefget_repository(
577
+
owner: Annotated[str, "The owner of the repository."],
578
+
repo: Annotated[str, "The name of the repository."],
579
+
) -> Repository:
580
+
"""Get basic information about a GitHub repository."""
description="Get basic information about a GitHub repository.",
597
+
transform_args={
598
+
"owner": ArgTransform(description="The owner of the repository."),
599
+
"repo": ArgTransform(description="The name of the repository."),
600
+
}
601
+
)
602
+
603
+
mcp.add_tool(get_repo_tool)
604
+
```
605
+
606
+
This pattern keeps the implementation in your client and treats the tool as an annotation layer, avoiding duplicate code.
607
+
608
+
#### Hiding Client-Specific Arguments
609
+
610
+
Client methods often have internal parameters (debug flags, auth tokens, rate limit settings) that shouldn't be exposed to LLMs. Use `hide=True` with a default value to handle these automatically:
The LLM only sees `owner`, `repo`, and `limit`. Internal parameters are supplied automatically.
630
+
631
+
#### Reusable Argument Patterns
632
+
633
+
When wrapping multiple client methods, you can define reusable argument transformations. This scales well for larger tool sets and keeps annotations consistent:
634
+
635
+
```python
636
+
from fastmcp import FastMCP
637
+
from fastmcp.tools import Tool
638
+
from fastmcp.tools.tool_transform import ArgTransform
-**Easy maintenance**: Update the client, not wrapper functions
698
+
-**Scalable**: Easily add new tools by wrapping additional client methods
699
+
569
700
### Adapting Remote or Generated Tools
570
-
This is one of the most common reasons to use tool transformation. Tools from remote servers (via a [proxy](/servers/proxy)) or generated from an [OpenAPI spec](/integrations/openapi) are often too generic for direct use by an LLM. You can use transformation to create a simpler, more intuitive version for your specific needs.
701
+
This is one of the most common reasons to use tool transformation. Tools from remote MCP servers (via a [proxy](/servers/proxy)) or generated from an [OpenAPI spec](/integrations/openapi) are often too generic for direct use by an LLM. You can use transformation to create a simpler, more intuitive version for your specific needs.
571
702
572
703
### Chaining Transformations
573
704
You can chain transformations by using an already transformed tool as the parent for a new transformation. This lets you build up complex behaviors in layers, for example, first renaming arguments, and then adding validation logic to the renamed tool.
0 commit comments