Tools returning NaN or inf still fail on the client (follow-up to #3341)
Les mainteneurs répondent en général sous 1 jour
@Jeremy891102 y travaille déjà.
Depuis le 24/11/2026.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 62/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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
- Langage dominant
- Python
- Étoiles
- 25k
- Forks
- 4.2k
- Merge moyen
- 10 min
- PR mergées (30 j)
- 1
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de modelcontextprotocol/python-sdk
-
v2
Difficulté 1/5 Moins d'une heure Accessibilité débutants 86/100
modelcontextprotocol/python-sdk#3978 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Client serves cached tools after its context exits with an exceptionPeut-être pris @jonxsong l’a pris il y a 1 jour. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
modelcontextprotocol/python-sdk#3968 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
modelcontextprotocol/python-sdk#3957 ·
Les mainteneurs répondent en général sous 1 jour
-
v2
Difficulté 1/5 Moins d'une heure Accessibilité débutants 91/100
modelcontextprotocol/python-sdk#3934 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
bug v1
Difficulté 2/5 1-3 heures Accessibilité débutants 79/100
modelcontextprotocol/python-sdk#3929 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de modelcontextprotocol/python-sdk
Issues similaires
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwisePeut-être pris @abdulwaarith0 l’a pris aujourd’hui. Ouverteneeds-triage
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
MakazhanAlpamys/Soup#3690 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
bug data-types native-format
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
ClickHouse/clickhouse-connect#1320 ·
Les mainteneurs répondent en général sous 1 jour
-
Public benchmark import end-to-end test fails when the test stack's worker runs the parity jobOuverteaccepted
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
future-agi/future-agi#6392 ·
Les mainteneurs répondent en général sous 1 jour