Python: fix(claude): preserve $defs in JSON schema for nested Pydantic models (#3655)

* fix(claude): preserve $defs in JSON schema for nested Pydantic models

- Preserve $defs section from Pydantic JSON schema when converting FunctionTool to SDK MCP tool
- This fixes tools with nested Pydantic models that use $ref references
- Add test for nested type schema preservation

Fixes #3654

* Adjust shared state import

* Fix MCP tool kwargs serialization bug

---------

Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
This commit is contained in:
Dineshsuriya D
2026-02-04 12:04:26 +05:30
committed by GitHub
Unverified
parent 06d43ee130
commit 5c6cf4fc92
6 changed files with 50 additions and 5 deletions
@@ -511,6 +511,9 @@ class ClaudeAgent(BaseAgent, Generic[TOptions]):
"properties": schema.get("properties", {}),
"required": schema.get("required", []),
}
# Preserve $defs for nested type references (Pydantic uses $defs for nested models)
if "$defs" in schema:
input_schema["$defs"] = schema["$defs"]
return SdkMcpTool(
name=func_tool.name,
@@ -499,6 +499,33 @@ class TestClaudeAgentToolConversion:
assert sdk_tool.input_schema is not None
assert "properties" in sdk_tool.input_schema # type: ignore[operator]
def test_function_tool_to_sdk_mcp_tool_preserves_defs_for_nested_types(self) -> None:
"""Test that $defs is preserved for tools with nested Pydantic models."""
from pydantic import BaseModel
class Address(BaseModel):
street: str
city: str
class Person(BaseModel):
name: str
address: Address
@tool
def create_person(person: Person) -> str:
"""Create a person with address."""
return f"{person.name} lives at {person.address.street}, {person.address.city}"
agent = ClaudeAgent()
sdk_tool = agent._function_tool_to_sdk_mcp_tool(create_person) # type: ignore[reportPrivateUsage]
# Verify $defs is preserved in the schema
assert sdk_tool.input_schema is not None
assert "$defs" in sdk_tool.input_schema # type: ignore[operator]
assert "Address" in sdk_tool.input_schema["$defs"] # type: ignore[index]
# Verify the nested reference exists in properties
assert "person" in sdk_tool.input_schema["properties"] # type: ignore[index]
async def test_tool_handler_success(self) -> None:
"""Test tool handler executes successfully."""