You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tools returning NaN or inf still fail on the client (follow-up to #3341) #3897
A tool that returns a non-finite float (nan, inf, -inf) makes call_tool raise on the client, so the whole result is lost. This was reported in #3341 and labeled bug, but #3341 was later closed as not planned by its author, and the related fix for text content (#3429, for #3385) was closed by the issue-link bot. It still reproduces on 2.3.0 and on main (77001c9).
This comes up a lot with ML tools: an evaluation tool that returns a metrics model loses every metric when one of them is undefined, for example AUC on a subgroup that contains only one class.
What happens: the server keeps nan in structured_content, the outbound dump turns it into null (the analysis in #3341 points at _dump_result in runner.py), and the client then validates null against the tool's own output schema ("type": "number") and raises RuntimeError: Invalid structured content returned by tool ..., which surfaces wrapped in an ExceptionGroup.
Expected: the call returns a result the client can use, or a tool error result (isError: true) that names the non-finite value, as suggested in #3341, instead of an exception on the client.
If a fix along those lines is welcome, I'd like to work on it.
I used an AI assistant to find and reproduce this and to draft this report.
Example Code
importmathimportanyiofrompydanticimportBaseModelfrommcpimportClientfrommcp.serverimportMCPServermcp=MCPServer("nan-demo")
classMetrics(BaseModel):
accuracy: floatauc: float@mcp.tool()defevaluate() ->Metrics:
# AUC is undefined when the evaluation set contains a single classreturnMetrics(accuracy=0.9, auc=math.nan)
asyncdefmain():
asyncwithClient(mcp) asclient:
awaitclient.call_tool("evaluate", {})
anyio.run(main)
# ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)# RuntimeError: Invalid structured content returned by tool evaluate: None is not of type 'number'
Python & MCP Python SDK
Python 3.13.18
mcp 2.3.0 (also reproduces on main at 77001c9)
pydantic 2.13.5
Activity