Repository navigation
fix(server): reject non-finite floats (NaN/inf) in _dump_result #3980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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)): | ||||||||||||||
| 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. Choose a reason for hiding this commentThe 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 Prompt for AI agents |
||||||||||||||
| _check_finite_floats(getattr(obj, name)) | ||||||||||||||
|
Comment on lines
+122
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This loop skips Prompt for AI agents
Suggested change
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _dump_result(result: Any) -> dict[str, Any]: | ||||||||||||||
| if result is None: | ||||||||||||||
| return {} | ||||||||||||||
|
|
@@ -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. | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
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
BaseModelfield such asset[float]containingNaNstill reaches JSON-mode serialization and becomesnull. Recurse through set-like values too.Prompt for AI agents