Skip to content

Commit 2718ad2

Browse files
Fix microsoft#3600: Pass JSON schemas through without Pydantic conversion
This change optimizes FunctionTool and MCP flows by passing JSON schemas directly to providers without converting them to Pydantic models first. Key changes: - Store JSON schema as-is when supplied to FunctionTool - Skip Pydantic model_validate for schema-supplied tools in invoke() - Return MCP tool schemas directly without conversion - Add comprehensive tests for schema passthrough behavior Performance benefits: - Eliminates expensive Pydantic model creation for supplied schemas - Preserves exact schema structure (additionalProperties, custom fields, etc.) - Reduces memory overhead and initialization time Maintains backward compatibility: - Function signature inference still uses Pydantic models - Explicit Pydantic models passed as input_model work as before - All existing tests pass
1 parent 77e90e6 commit 2718ad2

4 files changed

Lines changed: 442 additions & 64 deletions

File tree

‎python/packages/core/agent_framework/_mcp.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
from ._tools import (
3030
FunctionTool,
31-
_build_pydantic_model_from_json_schema,
3231
)
3332
from ._types import (
3433
Content,
@@ -355,8 +354,12 @@ def _prepare_message_for_mcp(
355354
return messages
356355

357356

358-
def _get_input_model_from_mcp_prompt(prompt: types.Prompt) -> type[BaseModel]:
359-
"""Creates a Pydantic model from a prompt's parameters."""
357+
def _get_input_model_from_mcp_prompt(prompt: types.Prompt) -> dict[str, Any] | type[BaseModel]:
358+
"""Get the input model from an MCP prompt.
359+
360+
For prompts with arguments, returns the schema as a dict.
361+
For prompts without arguments, returns an empty Pydantic model.
362+
"""
360363
# Check if 'arguments' is missing or empty
361364
if not prompt.arguments:
362365
return create_model(f"{prompt.name}_input")
@@ -374,13 +377,16 @@ def _get_input_model_from_mcp_prompt(prompt: types.Prompt) -> type[BaseModel]:
374377
if prompt_argument.required:
375378
required.append(prompt_argument.name)
376379

377-
schema = {"properties": properties, "required": required}
378-
return _build_pydantic_model_from_json_schema(prompt.name, schema)
380+
schema = {"type": "object", "properties": properties, "required": required}
381+
return schema
379382

380383

381-
def _get_input_model_from_mcp_tool(tool: types.Tool) -> type[BaseModel]:
382-
"""Creates a Pydantic model from a tools parameters."""
383-
return _build_pydantic_model_from_json_schema(tool.name, tool.inputSchema)
384+
def _get_input_model_from_mcp_tool(tool: types.Tool) -> dict[str, Any]:
385+
"""Get the input schema from an MCP tool.
386+
387+
Returns the schema as-is without conversion to Pydantic model.
388+
"""
389+
return tool.inputSchema
384390

385391

386392
def _normalize_mcp_name(name: str) -> str:

‎python/packages/core/agent_framework/_tools.py‎

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ class WeatherArgs(BaseModel):
240240
"input_model",
241241
"_invocation_duration_histogram",
242242
"_cached_parameters",
243+
"_input_schema",
244+
"_schema_supplied",
243245
}
244246

245247
def __init__(
@@ -299,6 +301,16 @@ def __init__(
299301
# FunctionTool-specific attributes
300302
self.func = func
301303
self._instance = None # Store the instance for bound methods
304+
305+
# Track if schema was supplied as JSON dict (for optimization)
306+
self._schema_supplied = isinstance(input_model, Mapping) and not (
307+
inspect.isclass(input_model) and issubclass(input_model, BaseModel)
308+
)
309+
310+
# Store the original JSON schema if provided
311+
self._input_schema: dict[str, Any] | None = dict(input_model) if self._schema_supplied else None
312+
313+
# Only create Pydantic model if schema wasn't supplied as dict
302314
self.input_model = self._resolve_input_model(input_model)
303315
self._cached_parameters: dict[str, Any] | None = None
304316
self.approval_mode = approval_mode or "never_require"
@@ -367,15 +379,21 @@ def __get__(self, obj: Any, objtype: type | None = None) -> FunctionTool[ArgsT]:
367379
return self
368380

369381
def _resolve_input_model(self, input_model: type[ArgsT] | Mapping[str, Any] | None) -> type[ArgsT]:
370-
"""Resolve the input model for the function."""
382+
"""Resolve the input model for the function.
383+
384+
When a JSON schema is provided as a Mapping, we no longer convert it to a Pydantic model.
385+
Instead, we store the schema as-is and use EmptyInputModel as a placeholder.
386+
"""
371387
if input_model is None:
372388
if self.func is None:
373389
return cast(type[ArgsT], EmptyInputModel)
374390
return cast(type[ArgsT], _create_input_model_from_func(func=self.func, name=self.name))
375391
if inspect.isclass(input_model) and issubclass(input_model, BaseModel):
376392
return input_model
377393
if isinstance(input_model, Mapping):
378-
return cast(type[ArgsT], _create_model_from_json_schema(self.name, input_model))
394+
# Don't convert to Pydantic model - store schema as-is
395+
# Use EmptyInputModel as placeholder since we won't use it for validation
396+
return cast(type[ArgsT], EmptyInputModel)
379397
raise TypeError("input_model must be a Pydantic BaseModel subclass or a JSON schema dict.")
380398

381399
def __call__(self, *args: Any, **kwargs: Any) -> Any:
@@ -436,9 +454,22 @@ async def invoke(
436454
original_kwargs = dict(kwargs)
437455
tool_call_id = original_kwargs.pop("tool_call_id", None)
438456
if arguments is not None:
439-
if not isinstance(arguments, self.input_model):
440-
raise TypeError(f"Expected {self.input_model.__name__}, got {type(arguments).__name__}")
441-
kwargs = arguments.model_dump(exclude_none=True)
457+
# For schema-supplied tools, skip Pydantic validation and extract kwargs directly
458+
if self._schema_supplied:
459+
# Extract kwargs from arguments (could be dict or BaseModel)
460+
if isinstance(arguments, dict):
461+
kwargs = arguments
462+
elif hasattr(arguments, "model_dump"):
463+
kwargs = arguments.model_dump(exclude_none=True)
464+
elif hasattr(arguments, "__dict__"):
465+
kwargs = {k: v for k, v in arguments.__dict__.items() if not k.startswith("_")}
466+
else:
467+
kwargs = dict(arguments)
468+
else:
469+
# For Pydantic models, do the normal validation
470+
if not isinstance(arguments, self.input_model):
471+
raise TypeError(f"Expected {self.input_model.__name__}, got {type(arguments).__name__}")
472+
kwargs = arguments.model_dump(exclude_none=True)
442473
if getattr(self, "_forward_runtime_kwargs", False) and original_kwargs:
443474
kwargs.update(original_kwargs)
444475
else:
@@ -475,11 +506,15 @@ async def invoke(
475506
}
476507
}
477508
attributes.update({
478-
OtelAttr.TOOL_ARGUMENTS: arguments.model_dump_json(ensure_ascii=False)
479-
if arguments
480-
else json.dumps(serializable_kwargs, default=str, ensure_ascii=False)
481-
if serializable_kwargs
482-
else "None"
509+
OtelAttr.TOOL_ARGUMENTS: (
510+
arguments.model_dump_json(ensure_ascii=False)
511+
if arguments and hasattr(arguments, "model_dump_json")
512+
else json.dumps(arguments, default=str, ensure_ascii=False)
513+
if arguments and not hasattr(arguments, "model_dump_json")
514+
else json.dumps(serializable_kwargs, default=str, ensure_ascii=False)
515+
if serializable_kwargs
516+
else "None"
517+
)
483518
})
484519
with get_function_span(attributes=attributes) as span:
485520
attributes[OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME] = self.name
@@ -523,7 +558,12 @@ def parameters(self) -> dict[str, Any]:
523558
The result is cached after the first call for performance.
524559
"""
525560
if self._cached_parameters is None:
526-
self._cached_parameters = self.input_model.model_json_schema()
561+
# If schema was supplied as JSON, return it directly
562+
if self._schema_supplied and self._input_schema is not None:
563+
self._cached_parameters = self._input_schema
564+
else:
565+
# Otherwise generate from Pydantic model
566+
self._cached_parameters = self.input_model.model_json_schema()
527567
return self._cached_parameters
528568

529569
@staticmethod

‎python/packages/core/tests/core/test_mcp.py‎

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -587,52 +587,30 @@ def test_prepare_message_for_mcp():
587587
def test_get_input_model_from_mcp_tool_parametrized(
588588
test_id, input_schema, valid_data, expected_values, invalid_data, validation_check
589589
):
590-
"""Parametrized test for JSON schema to Pydantic model conversion.
590+
"""Parametrized test for MCP tool input schema passthrough.
591591
592-
This test covers various edge cases including:
593-
- Basic types with required/optional fields
594-
- Nested objects
595-
- $ref resolution
596-
- Typed arrays (strings, integers, objects)
597-
- Deeply nested structures
598-
- Complex $ref with nested structures
599-
- Mixed types
592+
This test verifies that MCP tool schemas are passed through as-is
593+
without Pydantic conversion, which improves performance and preserves
594+
the original schema structure.
600595
601596
To add a new test case, add a tuple to the parametrize decorator with:
602597
- test_id: A descriptive name for the test case
603598
- input_schema: The JSON schema (inputSchema dict)
604-
- valid_data: Valid data to instantiate the model
605-
- expected_values: Dict of expected values (supports dot notation for nested access)
606-
- invalid_data: Invalid data to test validation errors (None to skip)
607-
- validation_check: Optional callable to perform additional validation checks
599+
- valid_data: Valid data (used to verify FunctionTool works with the schema)
600+
- expected_values: Not used in this test (kept for compatibility)
601+
- invalid_data: Not used in this test (kept for compatibility)
602+
- validation_check: Not used in this test (kept for compatibility)
608603
"""
609604
tool = types.Tool(name="test_tool", description="A test tool", inputSchema=input_schema)
610-
model = _get_input_model_from_mcp_tool(tool)
605+
schema = _get_input_model_from_mcp_tool(tool)
611606

612-
# Test valid data
613-
instance = model(**valid_data)
614-
615-
# Check expected values
616-
for field_path, expected_value in expected_values.items():
617-
# Support dot notation and array indexing for nested access
618-
current = instance
619-
parts = field_path.replace("]", "").replace("[", ".").split(".")
620-
for part in parts:
621-
current = current[int(part)] if part.isdigit() else getattr(current, part)
622-
assert current == expected_value, f"Field {field_path} = {current}, expected {expected_value}"
623-
624-
# Run additional validation checks if provided
625-
if validation_check:
626-
assert validation_check(instance), f"Validation check failed for {test_id}"
627-
628-
# Test invalid data if provided
629-
if invalid_data is not None:
630-
with pytest.raises(ValidationError):
631-
model(**invalid_data)
607+
# Verify schema is returned as-is (dict)
608+
assert isinstance(schema, dict), f"Expected dict, got {type(schema)}"
609+
assert schema == input_schema, "Schema should be passed through unchanged"
632610

633611

634612
def test_get_input_model_from_mcp_prompt():
635-
"""Test creation of input model from MCP prompt."""
613+
"""Test creation of input schema from MCP prompt."""
636614
prompt = types.Prompt(
637615
name="test_prompt",
638616
description="A test prompt",
@@ -641,16 +619,15 @@ def test_get_input_model_from_mcp_prompt():
641619
types.PromptArgument(name="arg2", description="Second argument", required=False),
642620
],
643621
)
644-
model = _get_input_model_from_mcp_prompt(prompt)
645-
646-
# Create an instance to verify the model works
647-
instance = model(arg1="test", arg2="optional")
648-
assert instance.arg1 == "test"
649-
assert instance.arg2 == "optional"
650-
651-
# Test validation
652-
with pytest.raises(ValidationError): # Missing required arg1
653-
model(arg2="optional")
622+
result = _get_input_model_from_mcp_prompt(prompt)
623+
624+
# Should return a dict (schema)
625+
assert isinstance(result, dict), f"Expected dict, got {type(result)}"
626+
assert result["type"] == "object"
627+
assert "arg1" in result["properties"]
628+
assert "arg2" in result["properties"]
629+
assert "arg1" in result["required"]
630+
assert "arg2" not in result["required"]
654631

655632

656633
# MCPTool tests

0 commit comments

Comments
 (0)