π The fast, Pythonic way to build MCP servers and clients.
json-schema: accept integral float counts like minLength 2.0 (#5214)
nate nowack9657674fa872
diff --git a/fastmcp_slim/fastmcp/utilities/json_schema_type.py b/fastmcp_slim/fastmcp/utilities/json_schema_type.pyindex 779d08faa..f69b4685e 100644--- a/fastmcp_slim/fastmcp/utilities/json_schema_type.py+++ b/fastmcp_slim/fastmcp/utilities/json_schema_type.py@@ -265,6 +265,11 @@ 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 _create_string_type(schema: Mapping[str, Any]) -> type | Annotated[Any, ...]:"""Create string type with optional constraints."""if "const" in schema:@@ -276,8 +281,8 @@ def _create_string_type(schema: Mapping[str, Any]) -> type | Annotated[Any, ...]constraints = {k: vfor k, v in {- "min_length": schema.get("minLength"),- "max_length": schema.get("maxLength"),+ "min_length": _count(schema.get("minLength")),+ "max_length": _count(schema.get("maxLength")),"pattern": schema.get("pattern"),}.items()if v is not None@@ -393,8 +398,8 @@ def _create_array_type(constraints = {k: vfor k, v in {- "min_length": schema.get("minItems"),- "max_length": schema.get("maxItems"),+ "min_length": _count(schema.get("minItems")),+ "max_length": _count(schema.get("maxItems")),}.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 51cba38e6..961eff4d8 100644--- a/tests/utilities/json_schema_type/test_constraints.py+++ b/tests/utilities/json_schema_type/test_constraints.py@@ -180,3 +180,37 @@ class TestStringFormatConstraints:"""The client converts a tool's output schema on every call, so repeats must hit its adapter cache."""schema = {"type": "string", "format": "email", "maxLength": 10}assert json_schema_to_type(schema) is json_schema_to_type(dict(schema))+++class TestIntegralFloatCounts:+ """JSON allows counts like minLength to be written as 2.0, and they must still apply."""++ @pytest.mark.parametrize(+ ("schema", "valid", "invalid"),+ [+ ({"type": "string", "minLength": 2.0}, "ab", "a"),+ ({"type": "string", "maxLength": 2.0}, "ab", "abc"),+ (+ {"type": "string", "format": "date-time", "maxLength": 20.0},+ "2026-09-22T00:00:00Z",+ "2026-09-22T00:00:00.000000+00:00",+ ),+ (+ {"type": "array", "items": {"type": "string"}, "minItems": 1.0},+ ["a"],+ [],+ ),+ ],+ )+ def test_integral_float_counts_apply(self, schema, valid, invalid):+ validator = TypeAdapter(json_schema_to_type(schema))+ validator.validate_python(valid)+ with pytest.raises(ValidationError):+ validator.validate_python(invalid)++ def test_float_count_does_not_poison_a_later_int_count(self):+ json_schema_to_type({"type": "string", "format": "email", "maxLength": 30.0})+ validator = TypeAdapter(+ json_schema_to_type({"type": "string", "format": "email", "maxLength": 30})+ )+ validator.validate_python("[email protected]")