File size: 4,322 Bytes
1814d17
 
 
 
 
 
 
 
 
7f20f81
1814d17
 
 
 
53ba205
 
 
1814d17
53ba205
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
bb7d65e
 
53ba205
bb7d65e
1814d17
53ba205
bb7d65e
1814d17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb7d65e
1814d17
 
 
 
 
 
 
 
 
 
 
 
 
 
e526bb1
bb7d65e
 
 
1814d17
 
 
 
 
 
 
 
bb7d65e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1814d17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
title: tool_transform
sidebarTitle: tool_transform
---

# `fastmcp.tools.tool_transform`

## Classes

### `ArgTransform` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/tools/tool_transform.py#L85"><Icon icon="github" size="14" /></a></sup>


Configuration for transforming a parent tool's argument.

This class allows fine-grained control over how individual arguments are transformed
when creating a new tool from an existing one. You can rename arguments, change their
descriptions, add default values, or hide them from clients while passing constants.

**Examples:**

Rename argument 'old_name' to 'new_name'
```python
ArgTransform(name="new_name")
```

Change description only
```python
ArgTransform(description="Updated description")
```

Add a default value (makes argument optional)
```python
ArgTransform(default=42)
```

Add a default factory (makes argument optional)
```python
ArgTransform(default_factory=lambda: time.time())
```

Change the type
```python
ArgTransform(type=str)
```

Hide the argument entirely from clients
```python
ArgTransform(hide=True)
```

Hide argument but pass a constant value to parent
```python
ArgTransform(hide=True, default="constant_value")
```

Hide argument but pass a factory-generated value to parent
```python
ArgTransform(hide=True, default_factory=lambda: uuid.uuid4().hex)
```

Make an optional parameter required (removes any default)
```python
ArgTransform(required=True)
```

Combine multiple transformations
```python
ArgTransform(name="new_name", description="New desc", default=None, type=int)
```


### `TransformedTool` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/tools/tool_transform.py#L199"><Icon icon="github" size="14" /></a></sup>


A tool that is transformed from another tool.

This class represents a tool that has been created by transforming another tool.
It supports argument renaming, schema modification, custom function injection,
and provides context for the forward() and forward_raw() functions.

The transformation can be purely schema-based (argument renaming, dropping, etc.)
or can include a custom function that uses forward() to call the parent tool
with transformed arguments.


**Methods:**

#### `from_tool` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/tools/tool_transform.py#L280"><Icon icon="github" size="14" /></a></sup>

```python
from_tool(cls, tool: Tool, name: str | None = None, description: str | None = None, tags: set[str] | None = None, transform_fn: Callable[..., Any] | None = None, transform_args: dict[str, ArgTransform] | None = None, annotations: ToolAnnotations | None = None, serializer: Callable[[Any], str] | None = None, enabled: bool | None = None) -> TransformedTool
```

Create a transformed tool from a parent tool.

**Args:**
- `tool`: The parent tool to transform.
- `transform_fn`: Optional custom function. Can use forward() and forward_raw()
to call the parent tool. Functions with **kwargs receive transformed
argument names.
- `name`: New name for the tool. Defaults to parent tool's name.
- `transform_args`: Optional transformations for parent tool arguments.
Only specified arguments are transformed, others pass through unchanged\:
- Simple rename (str)
- Complex transformation (rename/description/default/drop) (ArgTransform)
- Drop the argument (None)
- `description`: New description. Defaults to parent's description.
- `tags`: New tags. Defaults to parent's tags.
- `annotations`: New annotations. Defaults to parent's annotations.
- `serializer`: New serializer. Defaults to parent's serializer.

**Returns:**
- TransformedTool with the specified transformations.

**Examples:**

# Transform specific arguments only
```python
Tool.from_tool(parent, transform_args={"old": "new"})  # Others unchanged
```

# Custom function with partial transforms
```python
async def custom(x: int, y: int) -> str:
    result = await forward(x=x, y=y)
    return f"Custom: {result}"

Tool.from_tool(parent, transform_fn=custom, transform_args={"a": "x", "b": "y"})
```

# Using **kwargs (gets all args, transformed and untransformed)
```python
async def flexible(**kwargs) -> str:
    result = await forward(**kwargs)
    return f"Got: {kwargs}"

Tool.from_tool(parent, transform_fn=flexible, transform_args={"a": "x"})
```