Tools returning NaN or inf still fail on the client (follow-up to #3341)
维护者通常 1 天内回复
@Jeremy891102 已经在做这个了。
开始于 2026年11月24日。
评估
调研方向
Start from _dump_result in runner.py, which turns nan/inf into null during the outbound dump; follow the value to the client-side validation of structured_content against the tool output schema, where the RuntimeError in the reproduction is raised. Run the example script against a local checkout to confirm the failure on main. Done means the call no longer raises for non-finite floats — either a usable result or an isError: true tool error naming the value — with a regression test alongside the existing call_tool tests; the approach still needs a maintainer decision, since the thread has no agreed direction yet.
由索引模型根据 Issue 内容生成。
描述
Initial Checks
- I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Release line
2.x (current stable)
Description
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
import math
import anyio
from pydantic import BaseModel
from mcp import Client
from mcp.server import MCPServer
mcp = MCPServer("nan-demo")
class Metrics(BaseModel):
accuracy: float
auc: float
@mcp.tool()
def evaluate() -> Metrics:
# AUC is undefined when the evaluation set contains a single class
return Metrics(accuracy=0.9, auc=math.nan)
async def main():
async with Client(mcp) as client:
await client.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
- 主要语言
- Python
- 星标
- 25k
- 派生
- 4.2k
- 平均合并
- 10 分钟
- 30 天内合并 PR
- 1
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/python-sdk 的其他 Issue
-
v2
难度 1/5 1 小时以内 新手友好度 86/100
modelcontextprotocol/python-sdk#3978 · 2 条评论 ·
维护者通常 1 天内回复
-
Client serves cached tools after its context exits with an exception可能已有人在做 @jonxsong 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
modelcontextprotocol/python-sdk#3968 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
modelcontextprotocol/python-sdk#3957 ·
维护者通常 1 天内回复
-
v2
难度 1/5 1 小时以内 新手友好度 91/100
modelcontextprotocol/python-sdk#3934 · 2 条评论 ·
维护者通常 1 天内回复
-
bug v1
难度 2/5 1-3 小时 新手友好度 79/100
modelcontextprotocol/python-sdk#3929 · 2 条评论 ·
维护者通常 1 天内回复
查看 modelcontextprotocol/python-sdk 的全部 Issue
相似的 Issue
-
auto-reported bug
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
-
backend bug
难度 2/5 1-3 小时 新手友好度 86/100
nolte/kamerplanter#3193 ·
维护者通常 1 天内回复
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwise可能已有人在做 @abdulwaarith0 今天认领。 未关闭needs-triage
难度 2/5 1-3 小时 新手友好度 84/100
MakazhanAlpamys/Soup#3690 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复