π The fast, Pythonic way to build MCP servers and clients.
json-schema: tolerate counts larger than any real length (#5221)
nate nowackfc28a1650263
diff --git a/fastmcp_slim/fastmcp/utilities/json_schema_type.py b/fastmcp_slim/fastmcp/utilities/json_schema_type.pyindex f69b4685e..a16fa65a8 100644--- a/fastmcp_slim/fastmcp/utilities/json_schema_type.py+++ b/fastmcp_slim/fastmcp/utilities/json_schema_type.py@@ -56,6 +56,7 @@ import hashlibimport jsonimport keywordimport re+import sysimport warningsfrom collections.abc import Callable, Mappingfrom copy import deepcopy@@ -265,9 +266,19 @@ def _resolve_ref(ref: str, schemas: Mapping[str, Any]) -> Mapping[str, Any]:return current-def _count(value: Any) -> Any:- """JSON Schema counts are non-negative integers, and JSON allows writing them as `2.0`."""- return int(value) if isinstance(value, float) and value.is_integer() else value+def _count(value: Any, *, maximum: bool = False) -> Any:+ """Normalize a JSON Schema count such as minLength or maxItems for Pydantic.++ JSON allows writing a count as `2.0`. A count above `sys.maxsize` exceeds any+ real length: as a maximum it constrains nothing and is dropped, and as a+ minimum it is clamped to a value Pydantic accepts that still rejects+ everything.+ """+ if isinstance(value, float) and value.is_integer():+ value = int(value)+ if isinstance(value, int) and not isinstance(value, bool) and value > sys.maxsize:+ return None if maximum else sys.maxsize + 1+ return valuedef _create_string_type(schema: Mapping[str, Any]) -> type | Annotated[Any, ...]:@@ -282,7 +293,7 @@ def _create_string_type(schema: Mapping[str, Any]) -> type | Annotated[Any, ...]k: vfor k, v in {"min_length": _count(schema.get("minLength")),- "max_length": _count(schema.get("maxLength")),+ "max_length": _count(schema.get("maxLength"), maximum=True),"pattern": schema.get("pattern"),}.items()if v is not None@@ -399,7 +410,7 @@ def _create_array_type(k: vfor k, v in {"min_length": _count(schema.get("minItems")),- "max_length": _count(schema.get("maxItems")),+ "max_length": _count(schema.get("maxItems"), maximum=True),}.items()if v is not None}diff --git a/tests/utilities/json_schema_type/test_constraints.py b/tests/utilities/json_schema_type/test_constraints.pyindex 961eff4d8..56e20cf73 100644--- a/tests/utilities/json_schema_type/test_constraints.py+++ b/tests/utilities/json_schema_type/test_constraints.py@@ -214,3 +214,33 @@ class TestIntegralFloatCounts:json_schema_to_type({"type": "string", "format": "email", "maxLength": 30}))validator.validate_python("[email protected]")+++class TestHugeCounts:+ """Counts beyond any real length must not make a valid schema fail to convert."""++ @pytest.mark.parametrize(+ ("schema", "value"),+ [+ ({"type": "string", "maxLength": 10**20}, "abc"),+ ({"type": "string", "maxLength": 1e20}, "abc"),+ (+ {"type": "string", "format": "date-time", "maxLength": 1e20},+ "2026-09-22T00:00:00Z",+ ),+ ({"type": "array", "items": {"type": "integer"}, "maxItems": 10**20}, [1]),+ ],+ )+ def test_huge_maximum_constrains_nothing(self, schema, value):+ TypeAdapter(json_schema_to_type(schema)).validate_python(value)++ @pytest.mark.parametrize(+ ("schema", "value"),+ [+ ({"type": "string", "minLength": 10**20}, "abc"),+ ({"type": "array", "items": {"type": "integer"}, "minItems": 1e20}, [1]),+ ],+ )+ def test_huge_minimum_still_rejects(self, schema, value):+ with pytest.raises(ValidationError):+ TypeAdapter(json_schema_to_type(schema)).validate_python(value)