Spaces:
Running
Running
Fix: prune hidden parameter defs (#1257)
Browse files
src/fastmcp/tools/tool_transform.py
CHANGED
|
@@ -13,6 +13,7 @@ from pydantic.functional_validators import BeforeValidator
|
|
| 13 |
|
| 14 |
from fastmcp.tools.tool import ParsedFunction, Tool, ToolResult, _convert_to_content
|
| 15 |
from fastmcp.utilities.components import _convert_set_default_none
|
|
|
|
| 16 |
from fastmcp.utilities.logging import get_logger
|
| 17 |
from fastmcp.utilities.types import (
|
| 18 |
FastMCPBaseModel,
|
|
@@ -645,6 +646,7 @@ class TransformedTool(Tool):
|
|
| 645 |
|
| 646 |
if parent_defs:
|
| 647 |
schema["$defs"] = parent_defs
|
|
|
|
| 648 |
|
| 649 |
# Create forwarding function that closes over everything it needs
|
| 650 |
async def _forward(**kwargs):
|
|
|
|
| 13 |
|
| 14 |
from fastmcp.tools.tool import ParsedFunction, Tool, ToolResult, _convert_to_content
|
| 15 |
from fastmcp.utilities.components import _convert_set_default_none
|
| 16 |
+
from fastmcp.utilities.json_schema import compress_schema
|
| 17 |
from fastmcp.utilities.logging import get_logger
|
| 18 |
from fastmcp.utilities.types import (
|
| 19 |
FastMCPBaseModel,
|
|
|
|
| 646 |
|
| 647 |
if parent_defs:
|
| 648 |
schema["$defs"] = parent_defs
|
| 649 |
+
schema = compress_schema(schema, prune_defs=True)
|
| 650 |
|
| 651 |
# Create forwarding function that closes over everything it needs
|
| 652 |
async def _forward(**kwargs):
|
tests/tools/test_tool_transform.py
CHANGED
|
@@ -192,6 +192,31 @@ async def test_hide_required_param_with_user_default_works():
|
|
| 192 |
assert result.structured_content == {"result": 25}
|
| 193 |
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
async def test_forward_with_argument_mapping(add_tool):
|
| 196 |
"""Test that forward() applies argument mapping correctly."""
|
| 197 |
|
|
|
|
| 192 |
assert result.structured_content == {"result": 25}
|
| 193 |
|
| 194 |
|
| 195 |
+
async def test_hidden_param_prunes_defs():
|
| 196 |
+
class VisibleType(BaseModel):
|
| 197 |
+
x: int
|
| 198 |
+
|
| 199 |
+
class HiddenType(BaseModel):
|
| 200 |
+
y: int
|
| 201 |
+
|
| 202 |
+
@Tool.from_function
|
| 203 |
+
def tool_with_refs(a: VisibleType, b: HiddenType | None = None) -> int:
|
| 204 |
+
return a.x + (b.y if b else 0)
|
| 205 |
+
|
| 206 |
+
# Hide parameter 'b'
|
| 207 |
+
new_tool = Tool.from_tool(
|
| 208 |
+
tool_with_refs, transform_args={"b": ArgTransform(hide=True)}
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
schema = new_tool.parameters
|
| 212 |
+
# Only 'a' should be visible
|
| 213 |
+
assert list(schema["properties"].keys()) == ["a"]
|
| 214 |
+
# $defs should only contain VisibleType, not HiddenType
|
| 215 |
+
defs = schema.get("$defs", {})
|
| 216 |
+
assert "VisibleType" in defs
|
| 217 |
+
assert "HiddenType" not in defs
|
| 218 |
+
|
| 219 |
+
|
| 220 |
async def test_forward_with_argument_mapping(add_tool):
|
| 221 |
"""Test that forward() applies argument mapping correctly."""
|
| 222 |
|