Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/mcp/server/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ def _extract_meta(params: Mapping[str, Any] | None) -> RequestParamsMeta | None:
return None


def _check_finite_floats(obj: Any) -> None:
import math

if isinstance(obj, float) and (math.isnan(obj) or math.isinf(obj)):
raise ValueError(f"Tool result contains non-finite float ({obj!r}), which is not valid JSON.")
if isinstance(obj, dict):
for v in obj.values():
_check_finite_floats(v)
elif isinstance(obj, (list, tuple)):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This walk skips sets and frozensets, so a BaseModel field such as set[float] containing NaN still reaches JSON-mode serialization and becomes null. Recurse through set-like values too.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/mcp/server/runner.py, line 118:

<comment>This walk skips sets and frozensets, so a `BaseModel` field such as `set[float]` containing `NaN` still reaches JSON-mode serialization and becomes `null`. Recurse through set-like values too.</comment>

<file context>
@@ -107,6 +107,22 @@ def _extract_meta(params: Mapping[str, Any] | None) -> RequestParamsMeta | None:
+    if isinstance(obj, dict):
+        for v in obj.values():
+            _check_finite_floats(v)
+    elif isinstance(obj, (list, tuple)):
+        for v in obj:
+            _check_finite_floats(v)
</file context>
Suggested change
elif isinstance(obj, (list, tuple)):
elif isinstance(obj, (list, tuple, set, frozenset)):

for v in obj:
_check_finite_floats(v)
elif isinstance(obj, BaseModel):
for name in obj.__class__.model_fields:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This checks only stored model fields, not computed fields or serializer-produced values. A computed field returning NaN passes this scan but is included by model_dump(mode="json") and becomes null; validate those emitted values before JSON-mode conversion.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/mcp/server/runner.py, line 122:

<comment>This checks only stored model fields, not computed fields or serializer-produced values. A computed field returning `NaN` passes this scan but is included by `model_dump(mode="json")` and becomes `null`; validate those emitted values before JSON-mode conversion.</comment>

<file context>
@@ -107,6 +107,22 @@ def _extract_meta(params: Mapping[str, Any] | None) -> RequestParamsMeta | None:
+        for v in obj:
+            _check_finite_floats(v)
+    elif isinstance(obj, BaseModel):
+        for name in obj.__class__.model_fields:
+            _check_finite_floats(getattr(obj, name))
+
</file context>

_check_finite_floats(getattr(obj, name))
Comment on lines +122 to +123

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This loop skips extra="allow" values stored in __pydantic_extra__, even though model_dump includes them; an extra NaN therefore still becomes null on the wire. Check the model’s extra values as well as its declared fields.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/mcp/server/runner.py, line 122:

<comment>This loop skips `extra="allow"` values stored in `__pydantic_extra__`, even though `model_dump` includes them; an extra `NaN` therefore still becomes `null` on the wire. Check the model’s extra values as well as its declared fields.</comment>

<file context>
@@ -107,6 +107,22 @@ def _extract_meta(params: Mapping[str, Any] | None) -> RequestParamsMeta | None:
+        for v in obj:
+            _check_finite_floats(v)
+    elif isinstance(obj, BaseModel):
+        for name in obj.__class__.model_fields:
+            _check_finite_floats(getattr(obj, name))
+
</file context>
Suggested change
for name in obj.__class__.model_fields:
_check_finite_floats(getattr(obj, name))
for name in obj.__class__.model_fields:
_check_finite_floats(getattr(obj, name))
if obj.__pydantic_extra__:
_check_finite_floats(obj.__pydantic_extra__)



def _dump_result(result: Any) -> dict[str, Any]:
if result is None:
return {}
Expand All @@ -115,8 +131,10 @@ def _dump_result(result: Any) -> dict[str, Any]:
# already raise in `_inner`; this catches middleware returning one.
raise MCPError.from_error_data(result)
if isinstance(result, BaseModel):
_check_finite_floats(result)
return result.model_dump(by_alias=True, mode="json", exclude_none=True)
if isinstance(result, dict):
_check_finite_floats(result)
# Copied so callers own the returned dict: handlers and middleware may
# retain the object they returned, and the outbound pipeline shapes the
# wire form without reaching into anything the handler still holds.
Expand Down
Loading