Repository navigation
fix(server): reject non-finite floats (NaN/inf) in _dump_result - #3980
AadityaBhuree wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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
| for name in obj.__class__.model_fields: | ||
| _check_finite_floats(getattr(obj, name)) |
There was a problem hiding this comment.
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>
| 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__) |
| for v in obj: | ||
| _check_finite_floats(v) | ||
| elif isinstance(obj, BaseModel): | ||
| for name in obj.__class__.model_fields: |
There was a problem hiding this comment.
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>
| if isinstance(obj, dict): | ||
| for v in obj.values(): | ||
| _check_finite_floats(v) | ||
| elif isinstance(obj, (list, tuple)): |
There was a problem hiding this comment.
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>
| elif isinstance(obj, (list, tuple)): | |
| elif isinstance(obj, (list, tuple, set, frozenset)): |
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.