Skip to content

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

perf(schema): skip unnecessary reference sibling restoration (#6161)

Andrei Vince1cce9a74394b
diff --git a/fastmcp_slim/fastmcp/utilities/json_schema.py b/fastmcp_slim/fastmcp/utilities/json_schema.py
index 3fdafcbd9..ba9efa645 100644
--- a/fastmcp_slim/fastmcp/utilities/json_schema.py
+++ b/fastmcp_slim/fastmcp/utilities/json_schema.py
@@ -209,7 +209,7 @@ def _strip_remote_refs(obj: Any) -> Any:
return obj
-def _strip_discriminator(obj: Any) -> Any:
+def _strip_discriminator(obj: Any, *, copy_data: bool = False) -> Any:
"""Recursively remove OpenAPI ``discriminator`` keys from a schema.
Pydantic emits ``discriminator.mapping`` with values like
@@ -235,12 +235,17 @@ def _strip_discriminator(obj: Any) -> Any:
if k in _SUBSCHEMA_MAP_KEYS and isinstance(v, dict):
# The keys here are user property/definition names, so one
# named like a keyword above still holds a sub-schema.
- result[k] = {name: _strip_discriminator(sub) for name, sub in v.items()}
+ result[k] = {
+ name: _strip_discriminator(sub, copy_data=copy_data)
+ for name, sub in v.items()
+ }
+ elif k in _DATA_KEYS:
+ result[k] = _copy_schema({"value": v})["value"] if copy_data else v
else:
- result[k] = v if k in _DATA_KEYS else _strip_discriminator(v)
+ result[k] = _strip_discriminator(v, copy_data=copy_data)
return result
if isinstance(obj, list):
- return [_strip_discriminator(item) for item in obj]
+ return [_strip_discriminator(item, copy_data=copy_data) for item in obj]
return obj
@@ -285,6 +290,30 @@ def require_discriminator_property(schema: dict[str, Any]) -> dict[str, Any]:
return result
+def _needs_ref_merge(schema: dict[str, Any]) -> bool:
+ """Keep sibling restoration and the existing handling of other pointers."""
+ stack: list[Any] = [schema]
+ while stack:
+ node = stack.pop()
+ if isinstance(node, dict):
+ if "$ref" in node:
+ ref = node["$ref"]
+ # The merge only traverses referenced definitions under $defs.
+ # Other pointers retain their previous shared literal values.
+ # jsonref also normalizes these characters before URI lookup.
+ if (
+ not isinstance(ref, str)
+ or not ref.startswith("#/$defs/")
+ or any(character in ref for character in "\t\r\n")
+ or any(key not in ("$ref", "$defs") for key in node)
+ ):
+ return True
+ stack.extend(node.values())
+ elif isinstance(node, list):
+ stack.extend(node)
+ return False
+
+
def dereference_refs(schema: dict[str, Any]) -> dict[str, Any]:
"""Resolve all $ref references in a JSON schema by inlining definitions.
@@ -341,10 +370,11 @@ def dereference_refs(schema: dict[str, Any]) -> dict[str, Any]:
# Merge sibling keywords that were lost during dereferencing
# Pydantic puts description, default, examples as siblings to $ref
- merged = _merge_ref_siblings(schema, dereferenced, schema)
+ needs_merge = _needs_ref_merge(schema)
+ if needs_merge:
+ dereferenced = _merge_ref_siblings(schema, dereferenced, schema)
# Type assertion: top-level schema is always a dict
- assert isinstance(merged, dict)
- dereferenced = merged
+ assert isinstance(dereferenced, dict)
# Remove $defs since all references have been resolved
if "$defs" in dereferenced:
@@ -361,7 +391,10 @@ def dereference_refs(schema: dict[str, Any]) -> dict[str, Any]:
# is an OpenAPI extension; after inlining, the `anyOf` variants
# already carry `const` on the discriminant field, making the
# mapping redundant.
- dereferenced = _strip_discriminator(dereferenced)
+ # Without the sibling pass, jsonref still shares referenced containers.
+ # This traversal copies schemas; also copy literal instance data to keep
+ # each reference's defaults, consts, examples and enums independent.
+ dereferenced = _strip_discriminator(dereferenced, copy_data=not needs_merge)
if not _text_within_limit(dereferenced):
return resolve_root_ref(schema)
diff --git a/tests/utilities/test_json_schema_expansion.py b/tests/utilities/test_json_schema_expansion.py
index ce23c1f7a..657adb541 100644
--- a/tests/utilities/test_json_schema_expansion.py
+++ b/tests/utilities/test_json_schema_expansion.py
@@ -1,6 +1,7 @@
"""`dereference_refs` inlines references unless the result would be very large."""
from collections.abc import Callable
+from copy import deepcopy
from typing import Any
import pytest
@@ -81,6 +82,41 @@ def test_schema_is_inlined(defs_key: str, pointer: str) -> None:
assert value == {"type": "string"}
[email protected]("keyword", ["default", "const", "examples", "enum"])
+def test_inlined_reference_instance_data_is_independent(keyword: str) -> None:
+ value: Any = {
+ "items": [1],
+ "a": {"$ref": "#/$defs/Item"},
+ "b": {"$ref": "#/$defs/Item"},
+ }
+ if keyword in {"examples", "enum"}:
+ value = [value]
+ definition = {"type": "object", keyword: value}
+ schema = {
+ "type": "object",
+ "properties": {
+ "left": {"$ref": "#/$defs/Record"},
+ "right": {"$ref": "#/$defs/Record"},
+ },
+ "$defs": {"Record": definition, "Item": {"type": "object"}},
+ }
+ original = deepcopy(schema)
+ result = dereference_refs(schema)
+ left = result["properties"]["left"][keyword]
+ right = result["properties"]["right"][keyword]
+ if isinstance(left, list):
+ left = left[0]
+ right = right[0]
+ left["items"].append(2)
+ left["a"]["changed"] = True
+
+ assert right["items"] == [1]
+ assert "changed" not in left["b"]
+ assert "changed" not in right["a"]
+ assert "changed" not in right["b"]
+ assert schema == original
+
+
@pytest.mark.parametrize("build", LIST_SCHEMAS)
def test_large_list_schema_keeps_defs(
build: Callable[[int], dict[str, Any]],