Skip to content

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

Bound schema nesting before client type conversion (#5434)

Jeremiah Lowin1075aeac4a7c
diff --git a/fastmcp_slim/fastmcp/utilities/json_schema_type.py b/fastmcp_slim/fastmcp/utilities/json_schema_type.py
index 8aaf58988..5c68ecc80 100644
--- a/fastmcp_slim/fastmcp/utilities/json_schema_type.py
+++ b/fastmcp_slim/fastmcp/utilities/json_schema_type.py
@@ -97,6 +97,24 @@ from typing_extensions import NotRequired, TypedDict
__all__ = ["JSONSchema", "json_schema_to_type"]
+def _check_nesting(schema: Any) -> None:
+ """Raise `ValueError` if dicts and lists in the schema nest too deeply.
+
+ Normalizing and hashing a schema recurse once per level of nesting. This
+ check measures the depth without recursing, so a schema that is too deep
+ fails with the same error as one that exceeds the conversion depth limit.
+ """
+ stack: list[tuple[Any, int]] = [(schema, 1)]
+ while stack:
+ node, depth = stack.pop()
+ if depth > _MAX_NESTING:
+ raise ValueError("JSON schema is too deeply nested to convert")
+ if isinstance(node, dict):
+ stack.extend((child, depth + 1) for child in node.values())
+ elif isinstance(node, list):
+ stack.extend((child, depth + 1) for child in node)
+
+
def _normalize_yaml_types(obj: Any) -> Any:
"""Convert YAML-parsed types back to JSON-native types.
@@ -198,6 +216,9 @@ _adapters: _LRUCache[TypeAdapter[Any]] = _LRUCache(
# Limits on a single conversion. Depth counts nested schemas and `$ref` hops;
# steps count every schema visited.
_MAX_CONVERSION_DEPTH = 64
+# Nesting of dicts and lists in the input. Each nested schema takes at most two
+# levels (such as `properties` and then the property's schema).
+_MAX_NESTING = 4 * _MAX_CONVERSION_DEPTH
_MAX_CONVERSION_STEPS = 50_000
@@ -315,6 +336,8 @@ def json_schema_to_type(
if schema is False:
return _UnsatisfiableType # type: ignore[return-value] # ty:ignore[invalid-return-type]
+ _check_nesting(schema)
+
# Normalise YAML-parsed types (datetime/date β†’ str, non-str keys β†’ str)
# so that downstream json.dumps/hashing and default values work correctly.
schema = _normalize_yaml_types(schema)
@@ -339,6 +362,7 @@ def json_schema_to_type_adapter(schema: Mapping[str, Any] | bool) -> TypeAdapter
Adapters are cached by schema content in a bounded cache, so the classes
they hold are released when they are evicted.
"""
+ _check_nesting(schema)
key, size = _schema_digest(schema)
adapter = _adapters.get(key)
if adapter is None:
diff --git a/tests/utilities/json_schema_type/test_generated_classes.py b/tests/utilities/json_schema_type/test_generated_classes.py
index a685a8c96..8693bc511 100644
--- a/tests/utilities/json_schema_type/test_generated_classes.py
+++ b/tests/utilities/json_schema_type/test_generated_classes.py
@@ -522,6 +522,17 @@ class TestConversionLimits:
with pytest.raises(ValueError, match="too deeply nested"):
json_schema_to_type(flat_chain_schema(400))
+ def test_very_deeply_nested_schema_stops_with_value_error(self):
+ schema: dict[str, Any] = {"type": "string"}
+ for _ in range(5000):
+ schema = {"type": "array", "items": schema}
+
+ with pytest.raises(ValueError, match="too deeply nested"):
+ json_schema_to_type(schema)
+
+ with pytest.raises(ValueError, match="too deeply nested"):
+ json_schema_to_type_adapter(schema)
+
def test_repeated_type_lists_are_rejected(self, monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(
json_schema_type, "_MAX_CONVERSION_STEPS", 200, raising=False