mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-22 21:44:18 +02:00
add examples to param transformation
This commit is contained in:
parent
5f25729274
commit
1a6bb55b27
2 changed files with 38 additions and 0 deletions
|
|
@ -97,6 +97,7 @@ class ArgTransform:
|
|||
type: New type for the argument. Use ... for no change.
|
||||
hide: If True, hide this argument from clients but pass a constant value to parent.
|
||||
required: If True, make argument required (remove default). Use ... for no change.
|
||||
examples: Examples for the argument. Use ... for no change.
|
||||
|
||||
Examples:
|
||||
# Rename argument 'old_name' to 'new_name'
|
||||
|
|
@ -137,6 +138,7 @@ class ArgTransform:
|
|||
type: Any | EllipsisType = NotSet
|
||||
hide: bool = False
|
||||
required: Literal[True] | EllipsisType = NotSet
|
||||
examples: Any | EllipsisType = NotSet
|
||||
|
||||
def __post_init__(self):
|
||||
"""Validate that only one of default or default_factory is provided."""
|
||||
|
|
@ -584,6 +586,10 @@ class TransformedTool(Tool):
|
|||
# Update the schema with the type information from TypeAdapter
|
||||
new_schema.update(type_schema)
|
||||
|
||||
# Handle examples transformation
|
||||
if transform.examples is not NotSet:
|
||||
new_schema["examples"] = transform.examples
|
||||
|
||||
return new_name, new_schema, is_required
|
||||
|
||||
@staticmethod
|
||||
|
|
|
|||
|
|
@ -987,3 +987,35 @@ class TestEnableDisable:
|
|||
|
||||
with pytest.raises(ToolError):
|
||||
await client.call_tool("new_add", {"x": 1, "y": 2})
|
||||
|
||||
|
||||
def test_arg_transform_examples_in_schema(add_tool):
|
||||
# Simple example
|
||||
new_tool = Tool.from_tool(
|
||||
add_tool,
|
||||
transform_args={
|
||||
"old_x": ArgTransform(examples=[1, 2, 3]),
|
||||
},
|
||||
)
|
||||
prop = get_property(new_tool, "old_x")
|
||||
assert prop["examples"] == [1, 2, 3]
|
||||
|
||||
# Nested example (e.g., for array type)
|
||||
new_tool2 = Tool.from_tool(
|
||||
add_tool,
|
||||
transform_args={
|
||||
"old_x": ArgTransform(examples=[["a", "b"], ["c", "d"]]),
|
||||
},
|
||||
)
|
||||
prop2 = get_property(new_tool2, "old_x")
|
||||
assert prop2["examples"] == [["a", "b"], ["c", "d"]]
|
||||
|
||||
# If not set, should not be present
|
||||
new_tool3 = Tool.from_tool(
|
||||
add_tool,
|
||||
transform_args={
|
||||
"old_x": ArgTransform(),
|
||||
},
|
||||
)
|
||||
prop3 = get_property(new_tool3, "old_x")
|
||||
assert "examples" not in prop3
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue