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

未关闭
#3,897 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Jeremy891102 已经在做这个了。

开始于 2026年11月24日。

  • #3903 来自 @MrSpy00 —— 已关闭,未合并
  • #3918 来自 @EnesKartal —— 已关闭,未合并
  • #3938 来自 @Echolonius —— 已关闭,未合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
62/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
api

调研方向

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 内容生成。

描述

bug v1 v2
Initial Checks
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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

modelcontextprotocol/python-sdk 的其他 Issue

查看 modelcontextprotocol/python-sdk 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。