Fix: prune hidden parameter defs (#1257)

This commit is contained in:
Muhammad Khalid 2025-07-28 13:00:56 -05:00 committed by GitHub
commit a63d44576b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 0 deletions

View file

@ -13,6 +13,7 @@ from pydantic.functional_validators import BeforeValidator
from fastmcp.tools.tool import ParsedFunction, Tool, ToolResult, _convert_to_content
from fastmcp.utilities.components import _convert_set_default_none
from fastmcp.utilities.json_schema import compress_schema
from fastmcp.utilities.logging import get_logger
from fastmcp.utilities.types import (
FastMCPBaseModel,
@ -645,6 +646,7 @@ class TransformedTool(Tool):
if parent_defs:
schema["$defs"] = parent_defs
schema = compress_schema(schema, prune_defs=True)
# Create forwarding function that closes over everything it needs
async def _forward(**kwargs):

View file

@ -192,6 +192,31 @@ async def test_hide_required_param_with_user_default_works():
assert result.structured_content == {"result": 25}
async def test_hidden_param_prunes_defs():
class VisibleType(BaseModel):
x: int
class HiddenType(BaseModel):
y: int
@Tool.from_function
def tool_with_refs(a: VisibleType, b: HiddenType | None = None) -> int:
return a.x + (b.y if b else 0)
# Hide parameter 'b'
new_tool = Tool.from_tool(
tool_with_refs, transform_args={"b": ArgTransform(hide=True)}
)
schema = new_tool.parameters
# Only 'a' should be visible
assert list(schema["properties"].keys()) == ["a"]
# $defs should only contain VisibleType, not HiddenType
defs = schema.get("$defs", {})
assert "VisibleType" in defs
assert "HiddenType" not in defs
async def test_forward_with_argument_mapping(add_tool):
"""Test that forward() applies argument mapping correctly."""