Skip to content

fix(server): reject non-finite floats (NaN/inf) in _dump_result - #3980

Open
AadityaBhuree wants to merge 1 commit into
modelcontextprotocol:mainfrom
AadityaBhuree:fix/reject-non-finite-floats-in-dump-result
Open

AadityaBhuree wants to merge 1 commit into
modelcontextprotocol:mainfrom
AadityaBhuree:fix/reject-non-finite-floats-in-dump-result

Conversation

@AadityaBhuree

Copy link
Copy Markdown

Closes #3897 - Validate that BaseModel/dict tool results in _dump_result() (src/mcp/server/runner.py) do not contain non-finite floats (NaN, inf, -inf) before Pydantic's mode='json' silently coerces them to null and breaks client-side schema validation.

Copilot AI adaptive review requested due to automatic review settings December 5, 2026 14:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

3 issues found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. 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. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/mcp/server/runner.py">

<violation number="1" location="src/mcp/server/runner.py:118">
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.</violation>

<violation number="2" location="src/mcp/server/runner.py:122">
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.</violation>

<violation number="3" location="src/mcp/server/runner.py:122">
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.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

Re-trigger cubic

Comment thread src/mcp/server/runner.py
Comment on lines +122 to +123
for name in obj.__class__.model_fields:
_check_finite_floats(getattr(obj, name))

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__)

Comment thread src/mcp/server/runner.py
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>

Comment thread src/mcp/server/runner.py
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)):

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tools returning NaN or inf still fail on the client (follow-up to #3341)

2 participants