Skip to content

Building AI agents, atomically

fix(mcp): decode escaped definition names in schema references

pei711729f823fac12
diff --git a/atomic-agents/atomic_agents/connectors/mcp/schema_transformer.py b/atomic-agents/atomic_agents/connectors/mcp/schema_transformer.py
index cc52c0f..ad8cad8 100644
--- a/atomic-agents/atomic_agents/connectors/mcp/schema_transformer.py
+++ b/atomic-agents/atomic_agents/connectors/mcp/schema_transformer.py
@@ -29,7 +29,8 @@ class SchemaTransformer:
def _resolve_ref(ref_path: str, root_schema: Dict[str, Any], model_cache: Dict[str, Type]) -> Type:
"""Resolve a $ref to a Pydantic model."""
# Extract ref name from path like "#/$defs/MyObject" or "#/definitions/ANode"
- ref_name = ref_path.split("/")[-1]
+ # JSON Pointer decodes ~1 before ~0 so a literal ~1 is not decoded twice.
+ ref_name = ref_path.split("/")[-1].replace("~1", "/").replace("~0", "~")
if ref_name in model_cache:
return model_cache[ref_name]
diff --git a/atomic-agents/tests/connectors/mcp/test_schema_transformer.py b/atomic-agents/tests/connectors/mcp/test_schema_transformer.py
index 0e2a425..781beef 100644
--- a/atomic-agents/tests/connectors/mcp/test_schema_transformer.py
+++ b/atomic-agents/tests/connectors/mcp/test_schema_transformer.py
@@ -71,6 +71,30 @@ class TestSchemaTransformer:
class TestCreateModelFromSchema:
+ @pytest.mark.parametrize("definitions_key", ["$defs", "definitions"])
+ @pytest.mark.parametrize(
+ "name, reference_token",
+ [("Item", "Item"), ("ns/Item", "ns~1Item"), ("ns~Item", "ns~0Item"), ("ns~1Item", "ns~01Item")],
+ )
+ def test_ref_names_with_json_pointer_escapes(self, definitions_key, name, reference_token):
+ """Escaped definition names retain nested types and validation for MCP tools."""
+ reference = {"$ref": f"#/{definitions_key}/{reference_token}"}
+ schema = {
+ "type": "object",
+ "properties": {"item": reference, "items": {"type": "array", "items": reference}},
+ "required": ["item", "items"],
+ definitions_key: {name: {"type": "object", "properties": {"count": {"type": "integer"}}, "required": ["count"]}},
+ }
+ model = SchemaTransformer.create_model_from_schema(schema, "ItemsInput", "load_items")
+
+ instance = model(tool_name="load_items", item={"count": 42}, items=[{"count": 7}])
+ assert instance.item.count == 42
+ assert instance.items[0].count == 7
+ with pytest.raises(ValueError):
+ model(tool_name="load_items", item={"count": "invalid"}, items=[])
+ with pytest.raises(ValueError):
+ model(tool_name="load_items", item={"count": 42}, items=[{}])
+
@pytest.mark.parametrize(
"prop_schema, valid_value",
[