Skip to content

Commit 834f96d

Browse files
authored
fix(schema): preserve root metadata on fallback (#4178)
1 parent 5c2627c commit 834f96d

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

fastmcp_slim/fastmcp/utilities/json_schema.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ def resolve_root_ref(schema: dict[str, Any]) -> dict[str, Any]:
281281
if def_name in defs:
282282
# Create a new schema by copying the referenced definition
283283
resolved = dict(defs[def_name])
284+
285+
# Preserve root-level sibling metadata from the original schema.
286+
# Pydantic may put user-facing fields such as title, description,
287+
# default, or examples next to the root $ref. Those fields still
288+
# describe the root schema even when we can only resolve that
289+
# root reference for circular schemas.
290+
for key, value in schema.items():
291+
if key not in {"$ref", "$defs"}:
292+
resolved[key] = value
293+
284294
# Preserve $defs for nested references (other fields may still use them)
285295
resolved["$defs"] = defs
286296
return resolved

tests/utilities/test_json_schema.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,19 @@ def test_falls_back_for_circular_refs(self):
120120
}
121121
},
122122
"$ref": "#/$defs/Node",
123+
"title": "Tree node",
124+
"description": "A recursive tree node.",
125+
"examples": [{"children": []}],
123126
}
124127
result = dereference_refs(schema)
125128

126129
# Should fall back to resolve_root_ref behavior
127130
# Root should be resolved but nested refs preserved
128131
assert result.get("type") == "object"
129132
assert "$defs" in result # $defs preserved for circular refs
133+
assert result["title"] == "Tree node"
134+
assert result["description"] == "A recursive tree node."
135+
assert result["examples"] == [{"children": []}]
130136

131137
def test_falls_back_for_circular_json_pointer_refs(self):
132138
"""Test that circular JSON Pointer $ref (non-$defs) does not crash.

0 commit comments

Comments
 (0)