Skip to content

Commit 05dc0f4

Browse files
Add client method wrapping examples to Tool Transformation docs (#2002)
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: William Easton <[email protected]>
1 parent ad8c936 commit 05dc0f4

1 file changed

Lines changed: 132 additions & 1 deletion

File tree

docs/patterns/tool-transformation.mdx

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,139 @@ Use a transform function returning `ToolResult` for complete control over both c
566566

567567
Tool transformation is a flexible feature that supports many powerful patterns. Here are a few common use cases to give you ideas.
568568

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+
async def get_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."""
581+
return await github_client.get_repository(owner=owner, repo=repo)
582+
```
583+
584+
**With Tool Transformation**, you can wrap the client method directly:
585+
586+
```python
587+
from fastmcp import FastMCP
588+
from fastmcp.tools import Tool
589+
from fastmcp.tools.tool_transform import ArgTransform
590+
591+
mcp = FastMCP("GitHub Tools")
592+
593+
# Wrap a client method directly as a tool
594+
get_repo_tool = Tool.from_tool(
595+
tool=Tool.from_function(fn=github_client.get_repository),
596+
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:
611+
612+
```python
613+
get_issues_tool = Tool.from_tool(
614+
tool=Tool.from_function(fn=github_client.get_issues),
615+
description="Get issues from a GitHub repository.",
616+
transform_args={
617+
"owner": ArgTransform(description="The owner of the repository."),
618+
"repo": ArgTransform(description="The name of the repository."),
619+
"limit": ArgTransform(description="Maximum number of issues to return."),
620+
# Hide internal parameters
621+
"include_debug_info": ArgTransform(hide=True, default=False),
622+
"error_on_not_found": ArgTransform(hide=True, default=True),
623+
}
624+
)
625+
626+
mcp.add_tool(get_issues_tool)
627+
```
628+
629+
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
639+
640+
mcp = FastMCP("GitHub Tools")
641+
642+
# Define reusable argument patterns
643+
OWNER_ARG = ArgTransform(description="The repository owner.")
644+
REPO_ARG = ArgTransform(description="The repository name.")
645+
LIMIT_ARG = ArgTransform(description="Maximum number of items to return.")
646+
HIDE_ERROR = ArgTransform(hide=True, default=True)
647+
648+
def create_github_tools(client):
649+
"""Create tools from GitHub client methods with shared argument patterns."""
650+
651+
owner_repo_args = {
652+
"owner": OWNER_ARG,
653+
"repo": REPO_ARG,
654+
}
655+
656+
error_args = {
657+
"error_on_not_found": HIDE_ERROR,
658+
}
659+
660+
return [
661+
Tool.from_tool(
662+
tool=Tool.from_function(fn=client.get_repository),
663+
description="Get basic information about a GitHub repository.",
664+
transform_args={**owner_repo_args, **error_args}
665+
),
666+
Tool.from_tool(
667+
tool=Tool.from_function(fn=client.get_issue),
668+
description="Get a specific issue from a repository.",
669+
transform_args={
670+
**owner_repo_args,
671+
"issue_number": ArgTransform(description="The issue number."),
672+
"limit_comments": LIMIT_ARG,
673+
**error_args,
674+
}
675+
),
676+
Tool.from_tool(
677+
tool=Tool.from_function(fn=client.get_pull_request),
678+
description="Get a specific pull request from a repository.",
679+
transform_args={
680+
**owner_repo_args,
681+
"pull_request_number": ArgTransform(description="The PR number."),
682+
"limit_comments": LIMIT_ARG,
683+
**error_args,
684+
}
685+
),
686+
]
687+
688+
# Add all tools to the server
689+
for tool in create_github_tools(github_client):
690+
mcp.add_tool(tool)
691+
```
692+
693+
This pattern provides several benefits:
694+
695+
- **No duplicate implementation**: Logic stays in the client
696+
- **Consistent annotations**: Reusable argument patterns ensure consistency
697+
- **Easy maintenance**: Update the client, not wrapper functions
698+
- **Scalable**: Easily add new tools by wrapping additional client methods
699+
569700
### 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.
571702

572703
### Chaining Transformations
573704
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

Comments
 (0)