Skip to content

Commit 81d519f

Browse files
added nonlocal model counter, and some fixes
1 parent 20c6475 commit 81d519f

File tree

1 file changed

+19
-21
lines changed
  • python/packages/core/agent_framework

1 file changed

+19
-21
lines changed

python/packages/core/agent_framework/_mcp.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from contextlib import AsyncExitStack, _AsyncGeneratorContextManager # type: ignore
1010
from datetime import timedelta
1111
from functools import partial
12-
from typing import TYPE_CHECKING, Any, Literal, cast
12+
from typing import TYPE_CHECKING, Any, Literal
1313

1414
from mcp import types
1515
from mcp.client.session import ClientSession
@@ -300,7 +300,7 @@ def _get_input_model_from_mcp_tool(tool: types.Tool) -> type[BaseModel]:
300300
return create_model(f"{tool.name}_input")
301301

302302
# Counter for generating unique model names
303-
model_counter = {"count": 0}
303+
model_counter = 0
304304

305305
def resolve_type(prop_details: dict[str, Any], parent_name: str = "") -> type:
306306
"""Resolve JSON Schema type to Python type, handling $ref, nested objects, and typed arrays.
@@ -312,6 +312,8 @@ def resolve_type(prop_details: dict[str, Any], parent_name: str = "") -> type:
312312
Returns:
313313
Python type annotation (could be int, str, list[str], or a nested Pydantic model)
314314
"""
315+
nonlocal model_counter
316+
315317
# Handle $ref by resolving the reference
316318
if "$ref" in prop_details:
317319
ref = prop_details["$ref"]
@@ -351,11 +353,10 @@ def resolve_type(prop_details: dict[str, Any], parent_name: str = "") -> type:
351353

352354
if nested_properties and isinstance(nested_properties, dict):
353355
# Generate a unique name for the nested model
354-
model_counter["count"] += 1
355-
if parent_name:
356-
nested_model_name = f"{parent_name}_nested_{model_counter['count']}"
357-
else:
358-
nested_model_name = f"NestedModel_{model_counter['count']}"
356+
model_counter += 1
357+
nested_model_name = (
358+
f"{parent_name}_nested_{model_counter}" if parent_name else f"NestedModel_{model_counter}"
359+
)
359360

360361
# Recursively build field definitions for the nested model
361362
nested_field_definitions: dict[str, Any] = {}
@@ -378,26 +379,23 @@ def resolve_type(prop_details: dict[str, Any], parent_name: str = "") -> type:
378379

379380
# Create field definition
380381
if nested_prop_name in nested_required:
381-
if nested_field_kwargs:
382-
nested_field_definitions[nested_prop_name] = (
382+
nested_field_definitions[nested_prop_name] = (
383+
(
383384
nested_python_type,
384385
Field(**nested_field_kwargs),
385386
)
386-
else:
387-
nested_field_definitions[nested_prop_name] = (nested_python_type, ...)
387+
if nested_field_kwargs
388+
else (nested_python_type, ...)
389+
)
388390
else:
389-
nested_default = nested_prop_details.get("default", None)
390-
nested_field_kwargs["default"] = nested_default
391-
if nested_field_kwargs and any(k != "default" for k in nested_field_kwargs):
392-
nested_field_definitions[nested_prop_name] = (
393-
nested_python_type,
394-
Field(**nested_field_kwargs),
395-
)
396-
else:
397-
nested_field_definitions[nested_prop_name] = (nested_python_type, nested_default)
391+
nested_field_kwargs["default"] = nested_prop_details.get("default", None)
392+
nested_field_definitions[nested_prop_name] = (
393+
nested_python_type,
394+
Field(**nested_field_kwargs),
395+
)
398396

399397
# Create and return the nested Pydantic model
400-
return cast(type[BaseModel], create_model(nested_model_name, **nested_field_definitions))
398+
return create_model(nested_model_name, **nested_field_definitions) # type: ignore
401399

402400
# If no properties defined, return bare dict
403401
return dict

0 commit comments

Comments
 (0)