Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Visit each reference once when finding access-restricted properties (#5751)

Aarnav Noble11c74a712d51
diff --git a/fastmcp_slim/fastmcp/utilities/openapi/json_schema_converter.py b/fastmcp_slim/fastmcp/utilities/openapi/json_schema_converter.py
index 80348935a..5cede650c 100644
--- a/fastmcp_slim/fastmcp/utilities/openapi/json_schema_converter.py
+++ b/fastmcp_slim/fastmcp/utilities/openapi/json_schema_converter.py
@@ -309,16 +309,24 @@ def _needs_recursive_processing(
def _scope_schemas(
schema: dict[str, Any],
definitions: dict[str, Any] | None,
- seen_refs: frozenset[str] = frozenset(),
+ seen_refs: set[str] | None = None,
) -> Iterator[dict[str, Any]]:
"""Yield ``schema`` and the schemas describing the same instance through a
- local ``$ref`` or an ``allOf`` member (reference cycles are skipped)."""
+ local ``$ref`` or an ``allOf`` member.
+
+ Each reference target is visited once per walk, which skips reference
+ cycles and keeps targets shared by several members (a diamond of ``allOf``
+ references) from being walked once per path to them.
+ """
+ if seen_refs is None:
+ seen_refs = set()
yield schema
ref = schema.get("$ref")
if isinstance(ref, str) and ref.startswith("#/$defs/") and ref not in seen_refs:
+ seen_refs.add(ref)
target = (definitions or {}).get(ref[len("#/$defs/") :])
if isinstance(target, dict):
- yield from _scope_schemas(target, definitions, seen_refs | {ref})
+ yield from _scope_schemas(target, definitions, seen_refs)
all_of = schema.get("allOf")
for member in all_of if isinstance(all_of, list) else []:
if isinstance(member, dict):
diff --git a/tests/server/providers/openapi/test_write_only_responses.py b/tests/server/providers/openapi/test_write_only_responses.py
index 374e1f72a..73088daf7 100644
--- a/tests/server/providers/openapi/test_write_only_responses.py
+++ b/tests/server/providers/openapi/test_write_only_responses.py
@@ -329,6 +329,26 @@ def test_cyclic_references_do_not_hang_projection():
assert result["required"] == ["a", "b"]
+async def test_shared_all_of_references_do_not_hang_projection():
+ # Each level references the one below three times, so a walk that follows
+ # every path to the leaf visits it 3**18 times.
+ schemas: dict[str, Any] = {
+ "L0": {
+ "type": "object",
+ "required": ["id", "password"],
+ "properties": {"id": ID, "password": PASSWORD},
+ }
+ }
+ for level in range(1, 19):
+ schemas[f"L{level}"] = {
+ "allOf": [{"$ref": f"#/components/schemas/L{level - 1}"} for _ in range(3)]
+ }
+ schemas["Thing"] = {"$ref": "#/components/schemas/L18"}
+ _, output = await Harness(schemas, {"id": 1}).schemas()
+ assert "password" not in json.dumps(output)
+ assert output["$defs"]["L0"]["required"] == ["id"]
+
+
async def test_nested_object_and_array_item_scopes_stay_separate():
token = {"type": "string"}
private = {