Jeremiah Lowin commited on
Commit
dd55e0e
·
unverified ·
2 Parent(s): 99ea2b907ad6e0

Merge pull request #806 from strawgate/add-examples

Browse files
src/fastmcp/tools/tool_transform.py CHANGED
@@ -97,6 +97,7 @@ class ArgTransform:
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'
@@ -137,6 +138,7 @@ class ArgTransform:
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."""
@@ -584,6 +586,10 @@ class TransformedTool(Tool):
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
 
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'
 
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."""
 
586
  # Update the schema with the type information from TypeAdapter
587
  new_schema.update(type_schema)
588
 
589
+ # Handle examples transformation
590
+ if transform.examples is not NotSet:
591
+ new_schema["examples"] = transform.examples
592
+
593
  return new_name, new_schema, is_required
594
 
595
  @staticmethod
tests/tools/test_tool_transform.py CHANGED
@@ -987,3 +987,35 @@ class TestEnableDisable:
987
 
988
  with pytest.raises(ToolError):
989
  await client.call_tool("new_add", {"x": 1, "y": 2})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
987
 
988
  with pytest.raises(ToolError):
989
  await client.call_tool("new_add", {"x": 1, "y": 2})
990
+
991
+
992
+ def test_arg_transform_examples_in_schema(add_tool):
993
+ # Simple example
994
+ new_tool = Tool.from_tool(
995
+ add_tool,
996
+ transform_args={
997
+ "old_x": ArgTransform(examples=[1, 2, 3]),
998
+ },
999
+ )
1000
+ prop = get_property(new_tool, "old_x")
1001
+ assert prop["examples"] == [1, 2, 3]
1002
+
1003
+ # Nested example (e.g., for array type)
1004
+ new_tool2 = Tool.from_tool(
1005
+ add_tool,
1006
+ transform_args={
1007
+ "old_x": ArgTransform(examples=[["a", "b"], ["c", "d"]]),
1008
+ },
1009
+ )
1010
+ prop2 = get_property(new_tool2, "old_x")
1011
+ assert prop2["examples"] == [["a", "b"], ["c", "d"]]
1012
+
1013
+ # If not set, should not be present
1014
+ new_tool3 = Tool.from_tool(
1015
+ add_tool,
1016
+ transform_args={
1017
+ "old_x": ArgTransform(),
1018
+ },
1019
+ )
1020
+ prop3 = get_property(new_tool3, "old_x")
1021
+ assert "examples" not in prop3