fastmcp 2.7.1__py3-none-any.whl → 2.8.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastmcp/__init__.py +4 -1
- fastmcp/cli/cli.py +3 -2
- fastmcp/client/auth/oauth.py +1 -1
- fastmcp/client/client.py +3 -1
- fastmcp/client/transports.py +35 -28
- fastmcp/exceptions.py +4 -0
- fastmcp/prompts/prompt.py +8 -18
- fastmcp/prompts/prompt_manager.py +7 -4
- fastmcp/resources/resource.py +21 -26
- fastmcp/resources/resource_manager.py +3 -2
- fastmcp/resources/template.py +8 -16
- fastmcp/server/auth/providers/bearer_env.py +8 -11
- fastmcp/server/openapi.py +65 -38
- fastmcp/server/proxy.py +27 -14
- fastmcp/server/server.py +320 -131
- fastmcp/settings.py +100 -37
- fastmcp/tools/__init__.py +2 -1
- fastmcp/tools/tool.py +114 -75
- fastmcp/tools/tool_manager.py +3 -2
- fastmcp/tools/tool_transform.py +665 -0
- fastmcp/utilities/components.py +55 -0
- fastmcp/utilities/exceptions.py +1 -1
- fastmcp/utilities/mcp_config.py +1 -1
- fastmcp/utilities/tests.py +3 -3
- fastmcp/utilities/types.py +0 -9
- {fastmcp-2.7.1.dist-info → fastmcp-2.8.0.dist-info}/METADATA +3 -1
- {fastmcp-2.7.1.dist-info → fastmcp-2.8.0.dist-info}/RECORD +30 -28
- {fastmcp-2.7.1.dist-info → fastmcp-2.8.0.dist-info}/WHEEL +0 -0
- {fastmcp-2.7.1.dist-info → fastmcp-2.8.0.dist-info}/entry_points.txt +0 -0
- {fastmcp-2.7.1.dist-info → fastmcp-2.8.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import inspect
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from contextvars import ContextVar
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from types import EllipsisType
|
|
8
|
+
from typing import Any, Literal
|
|
9
|
+
|
|
10
|
+
from mcp.types import EmbeddedResource, ImageContent, TextContent, ToolAnnotations
|
|
11
|
+
from pydantic import ConfigDict
|
|
12
|
+
|
|
13
|
+
from fastmcp.tools.tool import ParsedFunction, Tool
|
|
14
|
+
from fastmcp.utilities.logging import get_logger
|
|
15
|
+
from fastmcp.utilities.types import get_cached_typeadapter
|
|
16
|
+
|
|
17
|
+
logger = get_logger(__name__)
|
|
18
|
+
|
|
19
|
+
NotSet = ...
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# Context variable to store current transformed tool
|
|
23
|
+
_current_tool: ContextVar[TransformedTool | None] = ContextVar(
|
|
24
|
+
"_current_tool", default=None
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
async def forward(**kwargs) -> Any:
|
|
29
|
+
"""Forward to parent tool with argument transformation applied.
|
|
30
|
+
|
|
31
|
+
This function can only be called from within a transformed tool's custom
|
|
32
|
+
function. It applies argument transformation (renaming, validation) before
|
|
33
|
+
calling the parent tool.
|
|
34
|
+
|
|
35
|
+
For example, if the parent tool has args `x` and `y`, but the transformed
|
|
36
|
+
tool has args `a` and `b`, and an `transform_args` was provided that maps `x` to
|
|
37
|
+
`a` and `y` to `b`, then `forward(a=1, b=2)` will call the parent tool with
|
|
38
|
+
`x=1` and `y=2`.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
**kwargs: Arguments to forward to the parent tool (using transformed names).
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
The result from the parent tool execution.
|
|
45
|
+
|
|
46
|
+
Raises:
|
|
47
|
+
RuntimeError: If called outside a transformed tool context.
|
|
48
|
+
TypeError: If provided arguments don't match the transformed schema.
|
|
49
|
+
"""
|
|
50
|
+
tool = _current_tool.get()
|
|
51
|
+
if tool is None:
|
|
52
|
+
raise RuntimeError("forward() can only be called within a transformed tool")
|
|
53
|
+
|
|
54
|
+
# Use the forwarding function that handles mapping
|
|
55
|
+
return await tool.forwarding_fn(**kwargs)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
async def forward_raw(**kwargs) -> Any:
|
|
59
|
+
"""Forward directly to parent tool without transformation.
|
|
60
|
+
|
|
61
|
+
This function bypasses all argument transformation and validation, calling the parent
|
|
62
|
+
tool directly with the provided arguments. Use this when you need to call the parent
|
|
63
|
+
with its original parameter names and structure.
|
|
64
|
+
|
|
65
|
+
For example, if the parent tool has args `x` and `y`, then `forward_raw(x=1,
|
|
66
|
+
y=2)` will call the parent tool with `x=1` and `y=2`.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
**kwargs: Arguments to pass directly to the parent tool (using original names).
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
The result from the parent tool execution.
|
|
73
|
+
|
|
74
|
+
Raises:
|
|
75
|
+
RuntimeError: If called outside a transformed tool context.
|
|
76
|
+
"""
|
|
77
|
+
tool = _current_tool.get()
|
|
78
|
+
if tool is None:
|
|
79
|
+
raise RuntimeError("forward_raw() can only be called within a transformed tool")
|
|
80
|
+
|
|
81
|
+
return await tool.parent_tool.run(kwargs)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@dataclass(kw_only=True)
|
|
85
|
+
class ArgTransform:
|
|
86
|
+
"""Configuration for transforming a parent tool's argument.
|
|
87
|
+
|
|
88
|
+
This class allows fine-grained control over how individual arguments are transformed
|
|
89
|
+
when creating a new tool from an existing one. You can rename arguments, change their
|
|
90
|
+
descriptions, add default values, or hide them from clients while passing constants.
|
|
91
|
+
|
|
92
|
+
Attributes:
|
|
93
|
+
name: New name for the argument. Use None to keep original name, or ... for no change.
|
|
94
|
+
description: New description for the argument. Use None to remove description, or ... for no change.
|
|
95
|
+
default: New default value for the argument. Use ... for no change.
|
|
96
|
+
default_factory: Callable that returns a default value. Cannot be used with default.
|
|
97
|
+
type: New type for the argument. Use ... for no change.
|
|
98
|
+
hide: If True, hide this argument from clients but pass a constant value to parent.
|
|
99
|
+
required: If True, make argument required (remove default). Use ... for no change.
|
|
100
|
+
|
|
101
|
+
Examples:
|
|
102
|
+
# Rename argument 'old_name' to 'new_name'
|
|
103
|
+
ArgTransform(name="new_name")
|
|
104
|
+
|
|
105
|
+
# Change description only
|
|
106
|
+
ArgTransform(description="Updated description")
|
|
107
|
+
|
|
108
|
+
# Add a default value (makes argument optional)
|
|
109
|
+
ArgTransform(default=42)
|
|
110
|
+
|
|
111
|
+
# Add a default factory (makes argument optional)
|
|
112
|
+
ArgTransform(default_factory=lambda: time.time())
|
|
113
|
+
|
|
114
|
+
# Change the type
|
|
115
|
+
ArgTransform(type=str)
|
|
116
|
+
|
|
117
|
+
# Hide the argument entirely from clients
|
|
118
|
+
ArgTransform(hide=True)
|
|
119
|
+
|
|
120
|
+
# Hide argument but pass a constant value to parent
|
|
121
|
+
ArgTransform(hide=True, default="constant_value")
|
|
122
|
+
|
|
123
|
+
# Hide argument but pass a factory-generated value to parent
|
|
124
|
+
ArgTransform(hide=True, default_factory=lambda: uuid.uuid4().hex)
|
|
125
|
+
|
|
126
|
+
# Make an optional parameter required (removes any default)
|
|
127
|
+
ArgTransform(required=True)
|
|
128
|
+
|
|
129
|
+
# Combine multiple transformations
|
|
130
|
+
ArgTransform(name="new_name", description="New desc", default=None, type=int)
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
name: str | EllipsisType = NotSet
|
|
134
|
+
description: str | EllipsisType = NotSet
|
|
135
|
+
default: Any | EllipsisType = NotSet
|
|
136
|
+
default_factory: Callable[[], Any] | EllipsisType = NotSet
|
|
137
|
+
type: Any | EllipsisType = NotSet
|
|
138
|
+
hide: bool = False
|
|
139
|
+
required: Literal[True] | EllipsisType = NotSet
|
|
140
|
+
|
|
141
|
+
def __post_init__(self):
|
|
142
|
+
"""Validate that only one of default or default_factory is provided."""
|
|
143
|
+
has_default = self.default is not NotSet
|
|
144
|
+
has_factory = self.default_factory is not NotSet
|
|
145
|
+
|
|
146
|
+
if has_default and has_factory:
|
|
147
|
+
raise ValueError(
|
|
148
|
+
"Cannot specify both 'default' and 'default_factory' in ArgTransform. "
|
|
149
|
+
"Use either 'default' for a static value or 'default_factory' for a callable."
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
if has_factory and not self.hide:
|
|
153
|
+
raise ValueError(
|
|
154
|
+
"default_factory can only be used with hide=True. "
|
|
155
|
+
"Visible parameters must use static 'default' values since JSON schema "
|
|
156
|
+
"cannot represent dynamic factories."
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
if self.required is True and (has_default or has_factory):
|
|
160
|
+
raise ValueError(
|
|
161
|
+
"Cannot specify 'required=True' with 'default' or 'default_factory'. "
|
|
162
|
+
"Required parameters cannot have defaults."
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
if self.hide and self.required is True:
|
|
166
|
+
raise ValueError(
|
|
167
|
+
"Cannot specify both 'hide=True' and 'required=True'. "
|
|
168
|
+
"Hidden parameters cannot be required since clients cannot provide them."
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
if self.required is False:
|
|
172
|
+
raise ValueError(
|
|
173
|
+
"Cannot specify 'required=False'. Set a default value instead."
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class TransformedTool(Tool):
|
|
178
|
+
"""A tool that is transformed from another tool.
|
|
179
|
+
|
|
180
|
+
This class represents a tool that has been created by transforming another tool.
|
|
181
|
+
It supports argument renaming, schema modification, custom function injection,
|
|
182
|
+
and provides context for the forward() and forward_raw() functions.
|
|
183
|
+
|
|
184
|
+
The transformation can be purely schema-based (argument renaming, dropping, etc.)
|
|
185
|
+
or can include a custom function that uses forward() to call the parent tool
|
|
186
|
+
with transformed arguments.
|
|
187
|
+
|
|
188
|
+
Attributes:
|
|
189
|
+
parent_tool: The original tool that this tool was transformed from.
|
|
190
|
+
fn: The function to execute when this tool is called (either the forwarding
|
|
191
|
+
function for pure transformations or a custom user function).
|
|
192
|
+
forwarding_fn: Internal function that handles argument transformation and
|
|
193
|
+
validation when forward() is called from custom functions.
|
|
194
|
+
"""
|
|
195
|
+
|
|
196
|
+
model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True)
|
|
197
|
+
|
|
198
|
+
parent_tool: Tool
|
|
199
|
+
fn: Callable[..., Any]
|
|
200
|
+
forwarding_fn: Callable[..., Any] # Always present, handles arg transformation
|
|
201
|
+
transform_args: dict[str, ArgTransform]
|
|
202
|
+
|
|
203
|
+
async def run(
|
|
204
|
+
self, arguments: dict[str, Any]
|
|
205
|
+
) -> list[TextContent | ImageContent | EmbeddedResource]:
|
|
206
|
+
"""Run the tool with context set for forward() functions.
|
|
207
|
+
|
|
208
|
+
This method executes the tool's function while setting up the context
|
|
209
|
+
that allows forward() and forward_raw() to work correctly within custom
|
|
210
|
+
functions.
|
|
211
|
+
|
|
212
|
+
Args:
|
|
213
|
+
arguments: Dictionary of arguments to pass to the tool's function.
|
|
214
|
+
|
|
215
|
+
Returns:
|
|
216
|
+
List of content objects (text, image, or embedded resources) representing
|
|
217
|
+
the tool's output.
|
|
218
|
+
"""
|
|
219
|
+
from fastmcp.tools.tool import _convert_to_content
|
|
220
|
+
|
|
221
|
+
# Fill in missing arguments with schema defaults to ensure
|
|
222
|
+
# ArgTransform defaults take precedence over function defaults
|
|
223
|
+
arguments = arguments.copy()
|
|
224
|
+
properties = self.parameters.get("properties", {})
|
|
225
|
+
|
|
226
|
+
for param_name, param_schema in properties.items():
|
|
227
|
+
if param_name not in arguments and "default" in param_schema:
|
|
228
|
+
# Check if this parameter has a default_factory from transform_args
|
|
229
|
+
# We need to call the factory for each run, not use the cached schema value
|
|
230
|
+
has_factory_default = False
|
|
231
|
+
if self.transform_args:
|
|
232
|
+
# Find the original parameter name that maps to this param_name
|
|
233
|
+
for orig_name, transform in self.transform_args.items():
|
|
234
|
+
transform_name = (
|
|
235
|
+
transform.name
|
|
236
|
+
if transform.name is not NotSet
|
|
237
|
+
else orig_name
|
|
238
|
+
)
|
|
239
|
+
if (
|
|
240
|
+
transform_name == param_name
|
|
241
|
+
and transform.default_factory is not NotSet
|
|
242
|
+
):
|
|
243
|
+
# Type check to ensure default_factory is callable
|
|
244
|
+
if callable(transform.default_factory):
|
|
245
|
+
arguments[param_name] = transform.default_factory()
|
|
246
|
+
has_factory_default = True
|
|
247
|
+
break
|
|
248
|
+
|
|
249
|
+
if not has_factory_default:
|
|
250
|
+
arguments[param_name] = param_schema["default"]
|
|
251
|
+
|
|
252
|
+
token = _current_tool.set(self)
|
|
253
|
+
try:
|
|
254
|
+
result = await self.fn(**arguments)
|
|
255
|
+
return _convert_to_content(result, serializer=self.serializer)
|
|
256
|
+
finally:
|
|
257
|
+
_current_tool.reset(token)
|
|
258
|
+
|
|
259
|
+
@classmethod
|
|
260
|
+
def from_tool(
|
|
261
|
+
cls,
|
|
262
|
+
tool: Tool,
|
|
263
|
+
name: str | None = None,
|
|
264
|
+
description: str | None = None,
|
|
265
|
+
tags: set[str] | None = None,
|
|
266
|
+
transform_fn: Callable[..., Any] | None = None,
|
|
267
|
+
transform_args: dict[str, ArgTransform] | None = None,
|
|
268
|
+
annotations: ToolAnnotations | None = None,
|
|
269
|
+
serializer: Callable[[Any], str] | None = None,
|
|
270
|
+
enabled: bool | None = None,
|
|
271
|
+
) -> TransformedTool:
|
|
272
|
+
"""Create a transformed tool from a parent tool.
|
|
273
|
+
|
|
274
|
+
Args:
|
|
275
|
+
tool: The parent tool to transform.
|
|
276
|
+
transform_fn: Optional custom function. Can use forward() and forward_raw()
|
|
277
|
+
to call the parent tool. Functions with **kwargs receive transformed
|
|
278
|
+
argument names.
|
|
279
|
+
name: New name for the tool. Defaults to parent tool's name.
|
|
280
|
+
transform_args: Optional transformations for parent tool arguments.
|
|
281
|
+
Only specified arguments are transformed, others pass through unchanged:
|
|
282
|
+
- str: Simple rename
|
|
283
|
+
- ArgTransform: Complex transformation (rename/description/default/drop)
|
|
284
|
+
- None: Drop the argument
|
|
285
|
+
description: New description. Defaults to parent's description.
|
|
286
|
+
tags: New tags. Defaults to parent's tags.
|
|
287
|
+
annotations: New annotations. Defaults to parent's annotations.
|
|
288
|
+
serializer: New serializer. Defaults to parent's serializer.
|
|
289
|
+
|
|
290
|
+
Returns:
|
|
291
|
+
TransformedTool with the specified transformations.
|
|
292
|
+
|
|
293
|
+
Examples:
|
|
294
|
+
# Transform specific arguments only
|
|
295
|
+
Tool.from_tool(parent, transform_args={"old": "new"}) # Others unchanged
|
|
296
|
+
|
|
297
|
+
# Custom function with partial transforms
|
|
298
|
+
async def custom(x: int, y: int) -> str:
|
|
299
|
+
result = await forward(x=x, y=y)
|
|
300
|
+
return f"Custom: {result}"
|
|
301
|
+
|
|
302
|
+
Tool.from_tool(parent, transform_fn=custom, transform_args={"a": "x", "b": "y"})
|
|
303
|
+
|
|
304
|
+
# Using **kwargs (gets all args, transformed and untransformed)
|
|
305
|
+
async def flexible(**kwargs) -> str:
|
|
306
|
+
result = await forward(**kwargs)
|
|
307
|
+
return f"Got: {kwargs}"
|
|
308
|
+
|
|
309
|
+
Tool.from_tool(parent, transform_fn=flexible, transform_args={"a": "x"})
|
|
310
|
+
"""
|
|
311
|
+
transform_args = transform_args or {}
|
|
312
|
+
|
|
313
|
+
# Validate transform_args
|
|
314
|
+
parent_params = set(tool.parameters.get("properties", {}).keys())
|
|
315
|
+
unknown_args = set(transform_args.keys()) - parent_params
|
|
316
|
+
if unknown_args:
|
|
317
|
+
raise ValueError(
|
|
318
|
+
f"Unknown arguments in transform_args: {', '.join(sorted(unknown_args))}. "
|
|
319
|
+
f"Parent tool has: {', '.join(sorted(parent_params))}"
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
# Always create the forwarding transform
|
|
323
|
+
schema, forwarding_fn = cls._create_forwarding_transform(tool, transform_args)
|
|
324
|
+
|
|
325
|
+
if transform_fn is None:
|
|
326
|
+
# User wants pure transformation - use forwarding_fn as the main function
|
|
327
|
+
final_fn = forwarding_fn
|
|
328
|
+
final_schema = schema
|
|
329
|
+
else:
|
|
330
|
+
# User provided custom function - merge schemas
|
|
331
|
+
parsed_fn = ParsedFunction.from_function(transform_fn, validate=False)
|
|
332
|
+
final_fn = transform_fn
|
|
333
|
+
|
|
334
|
+
has_kwargs = cls._function_has_kwargs(transform_fn)
|
|
335
|
+
|
|
336
|
+
# Validate function parameters against transformed schema
|
|
337
|
+
fn_params = set(parsed_fn.parameters.get("properties", {}).keys())
|
|
338
|
+
transformed_params = set(schema.get("properties", {}).keys())
|
|
339
|
+
|
|
340
|
+
if not has_kwargs:
|
|
341
|
+
# Without **kwargs, function must declare all transformed params
|
|
342
|
+
# Check if function is missing any parameters required after transformation
|
|
343
|
+
missing_params = transformed_params - fn_params
|
|
344
|
+
if missing_params:
|
|
345
|
+
raise ValueError(
|
|
346
|
+
f"Function missing parameters required after transformation: "
|
|
347
|
+
f"{', '.join(sorted(missing_params))}. "
|
|
348
|
+
f"Function declares: {', '.join(sorted(fn_params))}"
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
# ArgTransform takes precedence over function signature
|
|
352
|
+
# Start with function schema as base, then override with transformed schema
|
|
353
|
+
final_schema = cls._merge_schema_with_precedence(
|
|
354
|
+
parsed_fn.parameters, schema
|
|
355
|
+
)
|
|
356
|
+
else:
|
|
357
|
+
# With **kwargs, function can access all transformed params
|
|
358
|
+
# ArgTransform takes precedence over function signature
|
|
359
|
+
# No validation needed - kwargs makes everything accessible
|
|
360
|
+
|
|
361
|
+
# Start with function schema as base, then override with transformed schema
|
|
362
|
+
final_schema = cls._merge_schema_with_precedence(
|
|
363
|
+
parsed_fn.parameters, schema
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
# Additional validation: check for naming conflicts after transformation
|
|
367
|
+
if transform_args:
|
|
368
|
+
new_names = []
|
|
369
|
+
for old_name, transform in transform_args.items():
|
|
370
|
+
if not transform.hide:
|
|
371
|
+
if transform.name is not NotSet:
|
|
372
|
+
new_names.append(transform.name)
|
|
373
|
+
else:
|
|
374
|
+
new_names.append(old_name)
|
|
375
|
+
|
|
376
|
+
# Check for duplicate names after transformation
|
|
377
|
+
name_counts = {}
|
|
378
|
+
for arg_name in new_names:
|
|
379
|
+
name_counts[arg_name] = name_counts.get(arg_name, 0) + 1
|
|
380
|
+
|
|
381
|
+
duplicates = [
|
|
382
|
+
arg_name for arg_name, count in name_counts.items() if count > 1
|
|
383
|
+
]
|
|
384
|
+
if duplicates:
|
|
385
|
+
raise ValueError(
|
|
386
|
+
f"Multiple arguments would be mapped to the same names: "
|
|
387
|
+
f"{', '.join(sorted(duplicates))}"
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
final_description = description if description is not None else tool.description
|
|
391
|
+
|
|
392
|
+
transformed_tool = cls(
|
|
393
|
+
fn=final_fn,
|
|
394
|
+
forwarding_fn=forwarding_fn,
|
|
395
|
+
parent_tool=tool,
|
|
396
|
+
name=name or tool.name,
|
|
397
|
+
description=final_description,
|
|
398
|
+
parameters=final_schema,
|
|
399
|
+
tags=tags or tool.tags,
|
|
400
|
+
annotations=annotations or tool.annotations,
|
|
401
|
+
serializer=serializer or tool.serializer,
|
|
402
|
+
transform_args=transform_args,
|
|
403
|
+
enabled=enabled if enabled is not None else True,
|
|
404
|
+
)
|
|
405
|
+
|
|
406
|
+
return transformed_tool
|
|
407
|
+
|
|
408
|
+
@classmethod
|
|
409
|
+
def _create_forwarding_transform(
|
|
410
|
+
cls,
|
|
411
|
+
parent_tool: Tool,
|
|
412
|
+
transform_args: dict[str, ArgTransform] | None,
|
|
413
|
+
) -> tuple[dict[str, Any], Callable[..., Any]]:
|
|
414
|
+
"""Create schema and forwarding function that encapsulates all transformation logic.
|
|
415
|
+
|
|
416
|
+
This method builds a new JSON schema for the transformed tool and creates a
|
|
417
|
+
forwarding function that validates arguments against the new schema and maps
|
|
418
|
+
them back to the parent tool's expected arguments.
|
|
419
|
+
|
|
420
|
+
Args:
|
|
421
|
+
parent_tool: The original tool to transform.
|
|
422
|
+
transform_args: Dictionary defining how to transform each argument.
|
|
423
|
+
|
|
424
|
+
Returns:
|
|
425
|
+
A tuple containing:
|
|
426
|
+
- dict: The new JSON schema for the transformed tool
|
|
427
|
+
- Callable: Async function that validates and forwards calls to the parent tool
|
|
428
|
+
"""
|
|
429
|
+
|
|
430
|
+
# Build transformed schema and mapping
|
|
431
|
+
parent_props = parent_tool.parameters.get("properties", {}).copy()
|
|
432
|
+
parent_required = set(parent_tool.parameters.get("required", []))
|
|
433
|
+
|
|
434
|
+
new_props = {}
|
|
435
|
+
new_required = set()
|
|
436
|
+
new_to_old = {}
|
|
437
|
+
hidden_defaults = {} # Track hidden parameters with constant values
|
|
438
|
+
|
|
439
|
+
for old_name, old_schema in parent_props.items():
|
|
440
|
+
# Check if parameter is in transform_args
|
|
441
|
+
if transform_args and old_name in transform_args:
|
|
442
|
+
transform = transform_args[old_name]
|
|
443
|
+
else:
|
|
444
|
+
# Default behavior - pass through (no transformation)
|
|
445
|
+
transform = ArgTransform() # Default ArgTransform with no changes
|
|
446
|
+
|
|
447
|
+
# Handle hidden parameters with defaults
|
|
448
|
+
if transform.hide:
|
|
449
|
+
# Validate that hidden parameters without user defaults have parent defaults
|
|
450
|
+
has_user_default = (
|
|
451
|
+
transform.default is not NotSet
|
|
452
|
+
or transform.default_factory is not NotSet
|
|
453
|
+
)
|
|
454
|
+
if not has_user_default and old_name in parent_required:
|
|
455
|
+
raise ValueError(
|
|
456
|
+
f"Hidden parameter '{old_name}' has no default value in parent tool "
|
|
457
|
+
f"and no default or default_factory provided in ArgTransform. Either provide a default "
|
|
458
|
+
f"or default_factory in ArgTransform or don't hide required parameters."
|
|
459
|
+
)
|
|
460
|
+
if has_user_default:
|
|
461
|
+
# Store info for later factory calling or direct value
|
|
462
|
+
hidden_defaults[old_name] = transform
|
|
463
|
+
# Skip adding to schema (not exposed to clients)
|
|
464
|
+
continue
|
|
465
|
+
|
|
466
|
+
transform_result = cls._apply_single_transform(
|
|
467
|
+
old_name,
|
|
468
|
+
old_schema,
|
|
469
|
+
transform,
|
|
470
|
+
old_name in parent_required,
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
if transform_result:
|
|
474
|
+
new_name, new_schema, is_required = transform_result
|
|
475
|
+
new_props[new_name] = new_schema
|
|
476
|
+
new_to_old[new_name] = old_name
|
|
477
|
+
if is_required:
|
|
478
|
+
new_required.add(new_name)
|
|
479
|
+
|
|
480
|
+
schema = {
|
|
481
|
+
"type": "object",
|
|
482
|
+
"properties": new_props,
|
|
483
|
+
"required": list(new_required),
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
# Create forwarding function that closes over everything it needs
|
|
487
|
+
async def _forward(**kwargs):
|
|
488
|
+
# Validate arguments
|
|
489
|
+
valid_args = set(new_props.keys())
|
|
490
|
+
provided_args = set(kwargs.keys())
|
|
491
|
+
unknown_args = provided_args - valid_args
|
|
492
|
+
|
|
493
|
+
if unknown_args:
|
|
494
|
+
raise TypeError(
|
|
495
|
+
f"Got unexpected keyword argument(s): {', '.join(sorted(unknown_args))}"
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
# Check required arguments
|
|
499
|
+
missing_args = new_required - provided_args
|
|
500
|
+
if missing_args:
|
|
501
|
+
raise TypeError(
|
|
502
|
+
f"Missing required argument(s): {', '.join(sorted(missing_args))}"
|
|
503
|
+
)
|
|
504
|
+
|
|
505
|
+
# Map arguments to parent names
|
|
506
|
+
parent_args = {}
|
|
507
|
+
for new_name, value in kwargs.items():
|
|
508
|
+
old_name = new_to_old.get(new_name, new_name)
|
|
509
|
+
parent_args[old_name] = value
|
|
510
|
+
|
|
511
|
+
# Add hidden defaults (constant values for hidden parameters)
|
|
512
|
+
for old_name, transform in hidden_defaults.items():
|
|
513
|
+
if transform.default is not NotSet:
|
|
514
|
+
parent_args[old_name] = transform.default
|
|
515
|
+
elif transform.default_factory is not NotSet:
|
|
516
|
+
# Type check to ensure default_factory is callable
|
|
517
|
+
if callable(transform.default_factory):
|
|
518
|
+
parent_args[old_name] = transform.default_factory()
|
|
519
|
+
|
|
520
|
+
return await parent_tool.run(parent_args)
|
|
521
|
+
|
|
522
|
+
return schema, _forward
|
|
523
|
+
|
|
524
|
+
@staticmethod
|
|
525
|
+
def _apply_single_transform(
|
|
526
|
+
old_name: str,
|
|
527
|
+
old_schema: dict[str, Any],
|
|
528
|
+
transform: ArgTransform,
|
|
529
|
+
is_required: bool,
|
|
530
|
+
) -> tuple[str, dict[str, Any], bool] | None:
|
|
531
|
+
"""Apply transformation to a single parameter.
|
|
532
|
+
|
|
533
|
+
This method handles the transformation of a single argument according to
|
|
534
|
+
the specified transformation rules.
|
|
535
|
+
|
|
536
|
+
Args:
|
|
537
|
+
old_name: Original name of the parameter.
|
|
538
|
+
old_schema: Original JSON schema for the parameter.
|
|
539
|
+
transform: ArgTransform object specifying how to transform the parameter.
|
|
540
|
+
is_required: Whether the original parameter was required.
|
|
541
|
+
|
|
542
|
+
Returns:
|
|
543
|
+
Tuple of (new_name, new_schema, new_is_required) if parameter should be kept,
|
|
544
|
+
None if parameter should be dropped.
|
|
545
|
+
"""
|
|
546
|
+
if transform.hide:
|
|
547
|
+
return None
|
|
548
|
+
|
|
549
|
+
# Handle name transformation - ensure we always have a string
|
|
550
|
+
if transform.name is not NotSet:
|
|
551
|
+
new_name = transform.name if transform.name is not None else old_name
|
|
552
|
+
else:
|
|
553
|
+
new_name = old_name
|
|
554
|
+
|
|
555
|
+
# Ensure new_name is always a string
|
|
556
|
+
if not isinstance(new_name, str):
|
|
557
|
+
new_name = old_name
|
|
558
|
+
|
|
559
|
+
new_schema = old_schema.copy()
|
|
560
|
+
|
|
561
|
+
# Handle description transformation
|
|
562
|
+
if transform.description is not NotSet:
|
|
563
|
+
if transform.description is None:
|
|
564
|
+
new_schema.pop("description", None) # Remove description
|
|
565
|
+
else:
|
|
566
|
+
new_schema["description"] = transform.description
|
|
567
|
+
|
|
568
|
+
# Handle required transformation first
|
|
569
|
+
if transform.required is not NotSet:
|
|
570
|
+
is_required = bool(transform.required)
|
|
571
|
+
if transform.required is True:
|
|
572
|
+
# Remove any existing default when making required
|
|
573
|
+
new_schema.pop("default", None)
|
|
574
|
+
|
|
575
|
+
# Handle default value transformation (only if not making required)
|
|
576
|
+
if transform.default is not NotSet and transform.required is not True:
|
|
577
|
+
new_schema["default"] = transform.default
|
|
578
|
+
is_required = False
|
|
579
|
+
|
|
580
|
+
# Handle type transformation
|
|
581
|
+
if transform.type is not NotSet:
|
|
582
|
+
# Use TypeAdapter to get proper JSON schema for the type
|
|
583
|
+
type_schema = get_cached_typeadapter(transform.type).json_schema()
|
|
584
|
+
# Update the schema with the type information from TypeAdapter
|
|
585
|
+
new_schema.update(type_schema)
|
|
586
|
+
|
|
587
|
+
return new_name, new_schema, is_required
|
|
588
|
+
|
|
589
|
+
@staticmethod
|
|
590
|
+
def _merge_schema_with_precedence(
|
|
591
|
+
base_schema: dict[str, Any], override_schema: dict[str, Any]
|
|
592
|
+
) -> dict[str, Any]:
|
|
593
|
+
"""Merge two schemas, with the override schema taking precedence.
|
|
594
|
+
|
|
595
|
+
Args:
|
|
596
|
+
base_schema: Base schema to start with
|
|
597
|
+
override_schema: Schema that takes precedence for overlapping properties
|
|
598
|
+
|
|
599
|
+
Returns:
|
|
600
|
+
Merged schema with override taking precedence
|
|
601
|
+
"""
|
|
602
|
+
merged_props = base_schema.get("properties", {}).copy()
|
|
603
|
+
merged_required = set(base_schema.get("required", []))
|
|
604
|
+
|
|
605
|
+
override_props = override_schema.get("properties", {})
|
|
606
|
+
override_required = set(override_schema.get("required", []))
|
|
607
|
+
|
|
608
|
+
# Override properties
|
|
609
|
+
for param_name, param_schema in override_props.items():
|
|
610
|
+
if param_name in merged_props:
|
|
611
|
+
# Merge the schemas, with override taking precedence
|
|
612
|
+
base_param = merged_props[param_name].copy()
|
|
613
|
+
base_param.update(param_schema)
|
|
614
|
+
merged_props[param_name] = base_param
|
|
615
|
+
else:
|
|
616
|
+
merged_props[param_name] = param_schema.copy()
|
|
617
|
+
|
|
618
|
+
# Handle required parameters - override takes complete precedence
|
|
619
|
+
# Start with override's required set
|
|
620
|
+
final_required = override_required.copy()
|
|
621
|
+
|
|
622
|
+
# For parameters not in override, inherit base requirement status
|
|
623
|
+
# but only if they don't have a default in the final merged properties
|
|
624
|
+
for param_name in merged_required:
|
|
625
|
+
if param_name not in override_props:
|
|
626
|
+
# Parameter not mentioned in override, keep base requirement status
|
|
627
|
+
final_required.add(param_name)
|
|
628
|
+
elif (
|
|
629
|
+
param_name in override_props
|
|
630
|
+
and "default" not in merged_props[param_name]
|
|
631
|
+
):
|
|
632
|
+
# Parameter in override but no default, keep required if it was required in base
|
|
633
|
+
if param_name not in override_required:
|
|
634
|
+
# Override doesn't specify it as required, and it has no default,
|
|
635
|
+
# so inherit from base
|
|
636
|
+
final_required.add(param_name)
|
|
637
|
+
|
|
638
|
+
# Remove any parameters that have defaults (they become optional)
|
|
639
|
+
for param_name, param_schema in merged_props.items():
|
|
640
|
+
if "default" in param_schema:
|
|
641
|
+
final_required.discard(param_name)
|
|
642
|
+
|
|
643
|
+
return {
|
|
644
|
+
"type": "object",
|
|
645
|
+
"properties": merged_props,
|
|
646
|
+
"required": list(final_required),
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
@staticmethod
|
|
650
|
+
def _function_has_kwargs(fn: Callable[..., Any]) -> bool:
|
|
651
|
+
"""Check if function accepts **kwargs.
|
|
652
|
+
|
|
653
|
+
This determines whether a custom function can accept arbitrary keyword arguments,
|
|
654
|
+
which affects how schemas are merged during tool transformation.
|
|
655
|
+
|
|
656
|
+
Args:
|
|
657
|
+
fn: Function to inspect.
|
|
658
|
+
|
|
659
|
+
Returns:
|
|
660
|
+
True if the function has a **kwargs parameter, False otherwise.
|
|
661
|
+
"""
|
|
662
|
+
sig = inspect.signature(fn)
|
|
663
|
+
return any(
|
|
664
|
+
p.kind == inspect.Parameter.VAR_KEYWORD for p in sig.parameters.values()
|
|
665
|
+
)
|