fastmcp 2.7.1__py3-none-any.whl → 2.8.1__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.
@@ -0,0 +1,669 @@
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 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 MCPContent, 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
+ examples: Examples for the argument. Use ... for no change.
101
+
102
+ Examples:
103
+ # Rename argument 'old_name' to 'new_name'
104
+ ArgTransform(name="new_name")
105
+
106
+ # Change description only
107
+ ArgTransform(description="Updated description")
108
+
109
+ # Add a default value (makes argument optional)
110
+ ArgTransform(default=42)
111
+
112
+ # Add a default factory (makes argument optional)
113
+ ArgTransform(default_factory=lambda: time.time())
114
+
115
+ # Change the type
116
+ ArgTransform(type=str)
117
+
118
+ # Hide the argument entirely from clients
119
+ ArgTransform(hide=True)
120
+
121
+ # Hide argument but pass a constant value to parent
122
+ ArgTransform(hide=True, default="constant_value")
123
+
124
+ # Hide argument but pass a factory-generated value to parent
125
+ ArgTransform(hide=True, default_factory=lambda: uuid.uuid4().hex)
126
+
127
+ # Make an optional parameter required (removes any default)
128
+ ArgTransform(required=True)
129
+
130
+ # Combine multiple transformations
131
+ ArgTransform(name="new_name", description="New desc", default=None, type=int)
132
+ """
133
+
134
+ name: str | EllipsisType = NotSet
135
+ description: str | EllipsisType = NotSet
136
+ default: Any | EllipsisType = NotSet
137
+ default_factory: Callable[[], Any] | EllipsisType = NotSet
138
+ type: Any | EllipsisType = NotSet
139
+ hide: bool = False
140
+ required: Literal[True] | EllipsisType = NotSet
141
+ examples: Any | EllipsisType = NotSet
142
+
143
+ def __post_init__(self):
144
+ """Validate that only one of default or default_factory is provided."""
145
+ has_default = self.default is not NotSet
146
+ has_factory = self.default_factory is not NotSet
147
+
148
+ if has_default and has_factory:
149
+ raise ValueError(
150
+ "Cannot specify both 'default' and 'default_factory' in ArgTransform. "
151
+ "Use either 'default' for a static value or 'default_factory' for a callable."
152
+ )
153
+
154
+ if has_factory and not self.hide:
155
+ raise ValueError(
156
+ "default_factory can only be used with hide=True. "
157
+ "Visible parameters must use static 'default' values since JSON schema "
158
+ "cannot represent dynamic factories."
159
+ )
160
+
161
+ if self.required is True and (has_default or has_factory):
162
+ raise ValueError(
163
+ "Cannot specify 'required=True' with 'default' or 'default_factory'. "
164
+ "Required parameters cannot have defaults."
165
+ )
166
+
167
+ if self.hide and self.required is True:
168
+ raise ValueError(
169
+ "Cannot specify both 'hide=True' and 'required=True'. "
170
+ "Hidden parameters cannot be required since clients cannot provide them."
171
+ )
172
+
173
+ if self.required is False:
174
+ raise ValueError(
175
+ "Cannot specify 'required=False'. Set a default value instead."
176
+ )
177
+
178
+
179
+ class TransformedTool(Tool):
180
+ """A tool that is transformed from another tool.
181
+
182
+ This class represents a tool that has been created by transforming another tool.
183
+ It supports argument renaming, schema modification, custom function injection,
184
+ and provides context for the forward() and forward_raw() functions.
185
+
186
+ The transformation can be purely schema-based (argument renaming, dropping, etc.)
187
+ or can include a custom function that uses forward() to call the parent tool
188
+ with transformed arguments.
189
+
190
+ Attributes:
191
+ parent_tool: The original tool that this tool was transformed from.
192
+ fn: The function to execute when this tool is called (either the forwarding
193
+ function for pure transformations or a custom user function).
194
+ forwarding_fn: Internal function that handles argument transformation and
195
+ validation when forward() is called from custom functions.
196
+ """
197
+
198
+ model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True)
199
+
200
+ parent_tool: Tool
201
+ fn: Callable[..., Any]
202
+ forwarding_fn: Callable[..., Any] # Always present, handles arg transformation
203
+ transform_args: dict[str, ArgTransform]
204
+
205
+ async def run(self, arguments: dict[str, Any]) -> list[MCPContent]:
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
+ # Handle examples transformation
588
+ if transform.examples is not NotSet:
589
+ new_schema["examples"] = transform.examples
590
+
591
+ return new_name, new_schema, is_required
592
+
593
+ @staticmethod
594
+ def _merge_schema_with_precedence(
595
+ base_schema: dict[str, Any], override_schema: dict[str, Any]
596
+ ) -> dict[str, Any]:
597
+ """Merge two schemas, with the override schema taking precedence.
598
+
599
+ Args:
600
+ base_schema: Base schema to start with
601
+ override_schema: Schema that takes precedence for overlapping properties
602
+
603
+ Returns:
604
+ Merged schema with override taking precedence
605
+ """
606
+ merged_props = base_schema.get("properties", {}).copy()
607
+ merged_required = set(base_schema.get("required", []))
608
+
609
+ override_props = override_schema.get("properties", {})
610
+ override_required = set(override_schema.get("required", []))
611
+
612
+ # Override properties
613
+ for param_name, param_schema in override_props.items():
614
+ if param_name in merged_props:
615
+ # Merge the schemas, with override taking precedence
616
+ base_param = merged_props[param_name].copy()
617
+ base_param.update(param_schema)
618
+ merged_props[param_name] = base_param
619
+ else:
620
+ merged_props[param_name] = param_schema.copy()
621
+
622
+ # Handle required parameters - override takes complete precedence
623
+ # Start with override's required set
624
+ final_required = override_required.copy()
625
+
626
+ # For parameters not in override, inherit base requirement status
627
+ # but only if they don't have a default in the final merged properties
628
+ for param_name in merged_required:
629
+ if param_name not in override_props:
630
+ # Parameter not mentioned in override, keep base requirement status
631
+ final_required.add(param_name)
632
+ elif (
633
+ param_name in override_props
634
+ and "default" not in merged_props[param_name]
635
+ ):
636
+ # Parameter in override but no default, keep required if it was required in base
637
+ if param_name not in override_required:
638
+ # Override doesn't specify it as required, and it has no default,
639
+ # so inherit from base
640
+ final_required.add(param_name)
641
+
642
+ # Remove any parameters that have defaults (they become optional)
643
+ for param_name, param_schema in merged_props.items():
644
+ if "default" in param_schema:
645
+ final_required.discard(param_name)
646
+
647
+ return {
648
+ "type": "object",
649
+ "properties": merged_props,
650
+ "required": list(final_required),
651
+ }
652
+
653
+ @staticmethod
654
+ def _function_has_kwargs(fn: Callable[..., Any]) -> bool:
655
+ """Check if function accepts **kwargs.
656
+
657
+ This determines whether a custom function can accept arbitrary keyword arguments,
658
+ which affects how schemas are merged during tool transformation.
659
+
660
+ Args:
661
+ fn: Function to inspect.
662
+
663
+ Returns:
664
+ True if the function has a **kwargs parameter, False otherwise.
665
+ """
666
+ sig = inspect.signature(fn)
667
+ return any(
668
+ p.kind == inspect.Parameter.VAR_KEYWORD for p in sig.parameters.values()
669
+ )