New in version: 2.8.0
Tool transformation allows you to create new, enhanced tools from existing ones. This powerful feature enables you to adapt tools for different contexts, simplify complex interfaces, or add custom logic without duplicating code.
Why Transform Tools?
Often, an existing tool is almost perfect for your use case, but it might have:- A confusing description (or no description at all).
- Argument names or descriptions that are not intuitive for an LLM (e.g.,
q
instead ofquery
). - Unnecessary parameters that you want to hide from the LLM.
- A need for input validation before the original tool is called.
- A need to modify or format the tool’s output.
Basic Transformation
The primary way to create a transformed tool is with theTool.from_tool()
class method. At its simplest, you can use it to change a tool’s top-level metadata like its name
, description
, or tags
.
In the following simple example, we take a generic search
tool and adjust its name and description to help an LLM client better understand its purpose.
When you transform a tool, the original tool remains registered on the server. To avoid confusing an LLM with two similar tools, you can disable the original one:
find_products
with a clear, domain-specific purpose and relevant tags, even though it still uses the original generic search
function’s logic.
Parameters
TheTool.from_tool()
class method is the primary way to create a transformed tool. It takes the following parameters:
tool
: The tool to transform. This is the only required argument.name
: An optional name for the new tool.description
: An optional description for the new tool.transform_args
: A dictionary ofArgTransform
objects, one for each argument you want to modify.transform_fn
: An optional function that will be called instead of the parent tool’s logic.output_schema
: Control output schema and structured outputs (see Output Schema Control).tags
: An optional set of tags for the new tool.annotations
: An optional set ofToolAnnotations
for the new tool.serializer
: An optional function that will be called to serialize the result of the new tool.meta
: Control meta information for the tool. UseNone
to remove meta, any dict to set meta, or leave unset to inherit from parent.
TransformedTool
object that wraps the parent tool and applies the transformations you specify. You can add this tool to your MCP server using its add_tool()
method.
Modifying Arguments
To modify a tool’s parameters, provide a dictionary ofArgTransform
objects to the transform_args
parameter of Tool.from_tool()
. Each key is the name of the original argument you want to modify.
You only need to provide a
transform_args
entry for arguments you want to modify. All other arguments will be passed through unchanged.The ArgTransform Class
To modify an argument, you need to create anArgTransform
object. This object has the following parameters:
name
: The new name for the argument.description
: The new description for the argument.default
: The new default value for the argument.default_factory
: A function that will be called to generate a default value for the argument. This is useful for arguments that need to be generated for each tool call, such as timestamps or unique IDs.hide
: Whether to hide the argument from the LLM.required
: Whether the argument is required, usually used to make an optional argument be required instead.type
: The new type for the argument.
Certain combinations of parameters are not allowed. For example, you can only use
default_factory
with hide=True
, because dynamic defaults cannot be represented in a JSON schema for the client. You can only set required=True for arguments that do not declare a default value.Descriptions
By far the most common reason to transform a tool, after its own description, is to improve its argument descriptions. A good description is crucial for helping an LLM understand how to use a parameter correctly. This is especially important when wrapping tools from external APIs, whose argument descriptions may be missing or written for developers, not LLMs. In this example, we add a helpful description to theuser_id
argument:
Names
At times, you may want to rename an argument to make it more intuitive for an LLM. For example, in the following example, we take a genericq
argument and expand it to search_query
:
Default Values
You can update the default value for any argument using thedefault
parameter. Here, we change the default value of the y
argument to 10:
Hiding Arguments
Sometimes a tool requires arguments that shouldn’t be exposed to the LLM, such as API keys, configuration flags, or internal IDs. You can hide these parameters usinghide=True
. Note that you can only hide arguments that have a default value (or for which you provide a new default), because the LLM can’t provide a value at call time.
To pass a constant value to the parent tool, combine
hide=True
with default=<value>
.to
, subject
, and body
parameters. The api_key
is supplied automatically from an environment variable.
For values that must be generated for each tool call (like timestamps or unique IDs), use default_factory
, which is called with no arguments every time the tool is called. For example,
default_factory
can only be used with hide=True
. This is because visible parameters need static defaults that can be represented in a JSON schema for the client.Meta Information
New in version: 2.11.0
You can control meta information on transformed tools using the meta
parameter. Meta information is additional data about the tool that doesn’t affect its functionality but can be used by clients for categorization, routing, or other purposes.
meta
parameter, the transformed tool inherits the parent tool’s meta information.
Required Values
In rare cases where you want to make an optional argument required, you can setrequired=True
. This has no effect if the argument was already required.
Modifying Tool Behavior
With great power comes great responsibility. Modifying tool behavior is a very advanced feature.
from_tool()
method takes a transform_fn
parameter, which is an async function that replaces the parent tool’s logic and gives you complete control over the tool’s execution.
The Transform Function
Thetransform_fn
is an async function that completely replaces the parent tool’s logic.
Critically, the transform function’s arguments are used to determine the new tool’s final schema. Any arguments that are not already present in the parent tool schema OR the transform_args
will be added to the new tool’s schema. Note that when transform_args
and your function have the same argument name, the transform_args
metadata will take precedence, if provided.
The name / docstring of the
transform_fn
are ignored. Only its arguments are used to determine the final schema.Calling the Parent Tool
Most of the time, you don’t want to completely replace the parent tool’s behavior. Instead, you want to add validation, modify inputs, or post-process outputs while still leveraging the parent tool’s core functionality. For this, FastMCP provides the specialforward()
and forward_raw()
functions.
Both forward()
and forward_raw()
are async functions that let you call the parent tool from within your transform_fn
:
forward()
(recommended): Automatically handles argument mapping based on yourArgTransform
configurations. Call it with the transformed argument names.forward_raw()
: Bypasses all transformation and calls the parent tool directly with its original argument names. This is rarely needed unless you’re doing complex argument manipulation, perhaps withoutarg_transforms
.
x
and y
are positive before calling the parent tool:
In the simplest case, your parent tool and your transform function have the same arguments. You can call
forward()
with the same argument names as the parent tool:Passing Arguments with **kwargs
If yourtransform_fn
includes **kwargs
in its signature, it will receive all arguments from the parent tool after ArgTransform
configurations have been applied. This is powerful for creating flexible validation functions that don’t require you to add every argument to the function signature.
In the following example, we wrap a parent tool that accepts two arguments x
and y
. These are renamed to a
and b
in the transformed tool, and the transform only validates a
, passing the other argument through as **kwargs
.
In the above example,
**kwargs
receives the renamed argument b
, not the original argument y
. It is therefore recommended to use with forward()
, not forward_raw()
.Modifying MCP Tools with MCPConfig
When running MCP Servers under FastMCP withMCPConfig
, you can also apply a subset of tool transformations
directly in the MCPConfig json file.
tools
section is a dictionary of tool names to tool configurations. Each tool configuration is a
dictionary of tool properties.
See the MCPConfigTransport documentation for more details.
Output Schema Control
New in version: 2.10.0
Transformed tools inherit output schemas from their parent by default, but you can control this behavior:
Inherit from Parent (Default)
ToolResult
for complete control over both content blocks and structured outputs.
Common Patterns
Tool transformation is a flexible feature that supports many powerful patterns. Here are a few common use cases to give you ideas.Adapting Remote or Generated Tools
This is one of the most common reasons to use tool transformation. Tools from remote servers (via a proxy) or generated from an OpenAPI spec 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.Chaining Transformations
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.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 aget_my_data
tool that is specific to the currently logged-in user by hiding the user_id
parameter and providing it automatically.