π The fast, Pythonic way to build MCP servers and clients.
fix(tools): reject non-finite floats at the JSON serialization boundary (#5665)
akushonkamen74e8220a9139
diff --git a/fastmcp_slim/fastmcp/tools/base.py b/fastmcp_slim/fastmcp/tools/base.pyindex d75cc60ae..f1b96f966 100644--- a/fastmcp_slim/fastmcp/tools/base.py+++ b/fastmcp_slim/fastmcp/tools/base.py@@ -1,7 +1,10 @@from __future__ import annotationsimport inspect+import json+import mathfrom collections.abc import Callable+from functools import lru_cachefrom typing import (TYPE_CHECKING,Annotated,@@ -74,8 +77,52 @@ def default_serializer(data: Any) -> str:return _JSONABLE_ADAPTER.dump_json(data, fallback=str).decode()+@lru_cache(maxsize=5000)+def _get_json_serializer(annotation: Any) -> pydantic_core.SchemaSerializer:+ """Build a serializer that follows the real JSON serialization contract.++ `ser_json_inf_nan="constants"` keeps non-finite floats as NaN/Infinity JSON+ tokens instead of silently rewriting them to null, so the tokens that reach+ the wire can be rejected at the JSON boundary (see `_reject_nonfinite`).+ The schema is the annotation's own, so custom serializers and explicit+ per-model policies keep control of their output.+ """+ return pydantic_core.SchemaSerializer(+ get_cached_typeadapter(annotation).core_schema,+ config={"ser_json_inf_nan": "constants"},+ )+++def _reject_nonfinite(value: Any, path: str = "$") -> None:+ """Raise ToolError if a non-finite float survived JSON serialization.++ JSON cannot represent NaN or Infinity. A non-finite float in the parsed+ output means the value would reach the client as an invalid JSON literal+ (or be silently rewritten to null by stricter serializers). Values the+ serialization contract legitimately converted β custom serializers or+ explicit per-model `ser_json_inf_nan` policies β never appear here.+ """+ if isinstance(value, float) and not math.isfinite(value):+ raise ToolError(+ f"Tool result contains a non-finite float ({value!r}) at {path}; "+ "JSON cannot represent NaN or Infinity"+ )+ if isinstance(value, dict):+ for key, item in value.items():+ _reject_nonfinite(item, f"{path}.{key}")+ elif isinstance(value, list | tuple):+ for index, item in enumerate(value):+ _reject_nonfinite(item, f"{path}[{index}]")++def _serialize_to_jsonable(data: Any, annotation: Any = Any) -> Any:- """Serialize through Pydantic, falling back for unsupported annotations."""+ """Serialize through the real JSON serialization contract.++ The value is serialized to JSON and parsed back, so the output is exactly+ what would go on the wire. JSON cannot represent NaN or Infinity: rather+ than letting them be silently rewritten to null, any non-finite float that+ survives serialization is rejected. Falls back for unsupported annotations.+ """if (annotation is inspect.Signature.emptyor annotation is None@@ -83,14 +130,20 @@ def _serialize_to_jsonable(data: Any, annotation: Any = Any) -> Any:or annotation is ...or isinstance(annotation, str)):- adapter = _JSONABLE_ADAPTER+ serializer = _get_json_serializer(Any)else:try:- return get_cached_typeadapter(annotation).dump_python(data, mode="json")+ serializer = _get_json_serializer(annotation)except PydanticSchemaGenerationError:- adapter = _JSONABLE_ADAPTER-- return adapter.dump_python(data, mode="json")+ serializer = _get_json_serializer(Any)++ payload = serializer.to_json(data)+ value = json.loads(payload)+ # Only walk the structure when a NaN/Infinity token was emitted; the+ # substring check keeps clean results on the cheap path.+ if b"NaN" in payload or b"Infinity" in payload:+ _reject_nonfinite(value)+ return valueclass ToolResult(BaseModel):diff --git a/tests/tools/tool/test_nonfinite_serialization.py b/tests/tools/tool/test_nonfinite_serialization.pynew file mode 100644index 000000000..c2f6b0c6d--- /dev/null+++ b/tests/tools/tool/test_nonfinite_serialization.py@@ -0,0 +1,235 @@+"""Non-finite float results must be rejected, not silently rewritten to null.++JSON cannot represent NaN or Infinity. Serializing them through the default+JSON contract turns them into ``null``, silently corrupting structured content+(#5150). The fix serializes through the real JSON layer and rejects any+non-finite float that survives it, while custom serializers and explicit+per-model policies keep control of their own output.+"""++import math+from typing import Any++import pytest+from pydantic import BaseModel, ConfigDict, field_serializer++from fastmcp import Client, FastMCP+from fastmcp.exceptions import ToolError+from fastmcp.tools.base import ToolResult, _serialize_to_jsonable+++class TestNonFiniteStructuredContentRejected:+ async def test_nan_scalar_raises(self):+ mcp = FastMCP()++ @mcp.tool+ def scalar() -> float:+ return float("nan")++ async with Client(mcp) as client:+ with pytest.raises(ToolError, match="non-finite"):+ await client.call_tool("scalar", {})++ async def test_infinity_scalar_raises(self):+ mcp = FastMCP()++ @mcp.tool+ def scalar() -> float:+ return float("inf")++ async with Client(mcp) as client:+ with pytest.raises(ToolError, match="non-finite"):+ await client.call_tool("scalar", {})++ async def test_negative_infinity_scalar_raises(self):+ mcp = FastMCP()++ @mcp.tool+ def scalar() -> float:+ return float("-inf")++ async with Client(mcp) as client:+ with pytest.raises(ToolError, match=r"non-finite float \(-inf\)"):+ await client.call_tool("scalar", {})++ async def test_nan_in_dict_raises_with_value_and_path(self):+ mcp = FastMCP()++ @mcp.tool+ def stats() -> dict[str, float]:+ return {"mean": float("nan"), "count": 2.0}++ async with Client(mcp) as client:+ with pytest.raises(ToolError, match=r"nan.*\$\.mean"):+ await client.call_tool("stats", {})++ async def test_nan_in_list_raises(self):+ mcp = FastMCP()++ @mcp.tool+ def values() -> list[float]:+ return [1.0, float("nan")]++ async with Client(mcp) as client:+ with pytest.raises(ToolError, match="non-finite"):+ await client.call_tool("values", {})++ async def test_nan_in_nested_containers_raises(self):+ mcp = FastMCP()++ @mcp.tool+ def nested() -> dict[str, list[dict[str, float]]]:+ return {"rows": [{"v": 1.0}, {"v": float("inf")}]}++ async with Client(mcp) as client:+ with pytest.raises(ToolError, match=r"non-finite.*\$\.rows\[1\]\.v"):+ await client.call_tool("nested", {})+++class TestFiniteValuesUnaffected:+ async def test_finite_structured_content_round_trips(self):+ mcp = FastMCP()++ @mcp.tool+ def stats() -> dict[str, float]:+ return {"mean": 2.5, "count": 2.0}++ async with Client(mcp) as client:+ result = await client.call_tool("stats", {})++ assert result.structured_content == {"mean": 2.5, "count": 2.0}++ async def test_nan_converted_by_custom_field_serializer_is_preserved(self):+ class Report(BaseModel):+ mean: float++ @field_serializer("mean")+ def _mean(self, value: float) -> str:+ return f"{value:.4f}"++ mcp = FastMCP()++ @mcp.tool+ def report() -> Report:+ return Report(mean=float("nan"))++ async with Client(mcp) as client:+ result = await client.call_tool("report", {})++ # The custom serializer deliberately converted the value to valid+ # JSON, so it must pass through untouched.+ assert result.structured_content == {"mean": "nan"}+++class TestSerializeToJsonableUnit:+ def test_any_fallback_rejects_non_finite(self):+ with pytest.raises(ToolError, match="non-finite"):+ _serialize_to_jsonable({"v": float("nan")})++ def test_any_fallback_rejects_non_finite_nested(self):+ with pytest.raises(ToolError, match=r"non-finite.*\$\.a\[1\]"):+ _serialize_to_jsonable({"a": [1.0, math.inf]})++ def test_typed_annotation_rejects_non_finite(self):+ with pytest.raises(ToolError, match="non-finite"):+ _serialize_to_jsonable({"v": float("nan")}, dict[str, float])++ def test_finite_values_pass_through(self):+ value = {"a": [1.0, 2.5], "b": None, "c": "x"}+ assert _serialize_to_jsonable(value, dict[str, Any]) == value++ def test_string_mentioning_nan_is_not_rejected(self):+ value = {"note": "the value was NaN at index 0", "data": [1.0]}+ assert _serialize_to_jsonable(value) == value++ def test_explicit_model_null_policy_is_preserved(self):+ class Lenient(BaseModel):+ model_config = ConfigDict(ser_json_inf_nan="null")+ mean: float++ # An explicit per-model policy deliberately emits null, which is valid+ # JSON β the boundary must not second-guess it.+ assert _serialize_to_jsonable(Lenient(mean=float("nan")), Lenient) == {+ "mean": None+ }++ def test_tool_result_structured_content_rejects_non_finite(self):+ with pytest.raises(ToolError, match="non-finite"):+ ToolResult(structured_content={"v": float("nan")})+++class TestDocumentedResidualBehavior:+ """Documented trade-offs that remain by design (see follow-up issues).++ These tests pin the *current* behavior so any future policy change is a+ conscious, reviewed decision rather than a silent drift.+ """++ async def test_default_base_model_policy_still_nulls_silently(self):+ """A plain BaseModel's embedded config wins over the outer constants policy.++ pydantic's per-model default (`ser_json_inf_nan="null"`) rewrites the+ non-finite float to valid-JSON ``null``, so the substring guard never+ fires and the caller gets ``{"mean": null}`` β not a ToolError. This is+ the documented judgment call: overriding a model's embedded config from+ outside is at odds with preserving model control.+ """++ class Stats(BaseModel):+ mean: float++ mcp = FastMCP()++ @mcp.tool+ def stats() -> Stats:+ return Stats(mean=float("nan"))++ async with Client(mcp) as client:+ # Not rejected at the serialization boundary β the model's own+ # default policy governed β so the null reaches the client and+ # fails *its* output-schema validation with an error that never+ # mentions nan.+ with pytest.raises(RuntimeError, match="None is not of type 'number'"):+ await client.call_tool("stats", {})++ # Pin the boundary behavior: no ToolError, just the model's null.+ assert _serialize_to_jsonable(Stats(mean=float("nan")), Stats) == {"mean": None}++ def test_default_serializer_rewrites_non_finite_to_null(self):+ """`default_serializer` (the text-content path) still nulls non-finites.++ It goes through pydantic's default JSON policy, so it can disagree with+ the structured path this PR fixed. Tracked as a follow-up.+ """+ from fastmcp.tools.base import default_serializer++ assert default_serializer({"mean": float("nan")}) == '{"mean":null}'++ async def test_unserializable_result_falls_back_to_text_content_softly(self):+ """A value the structured serializer cannot handle becomes content-only.++ `convert_result` catches the serialization error and returns text+ content with `is_error` false β a soft success. This is the leftover+ text-path risk: a non-finite value that fails structured serialization+ surfaces as text, never as a tool error (follow-up: fail the text path+ too, e.g. an `inf_nan_mode="error"`-style check).+ """++ class Unserializable:+ def __str__(self):+ return "nan"++ mcp = FastMCP()++ @mcp.tool+ def weird():+ return Unserializable()++ async with Client(mcp) as client:+ result = await client.call_tool("weird", {})++ assert result.is_error is False+ assert result.structured_content is None+ # The text content went through the default JSON serializer, which+ # stringifies the value ("nan" as a JSON string), with no error raised.+ assert result.content[0].text == '"nan"'