A tool returning a non-finite float puts invalid JSON in the text content block
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 68/100
Rechercherichtung
Beginne bei func_metadata.py:669, wo pydantic_core.to_json nicht annotierte Tool-Ergebnisse rendert, und vergleiche denselben Aufruf in resources/types.py:103 und prompts/base.py:206. Überprüfe das in #3341 beschriebene Verhalten strukturierter Ausgaben. Als erledigt gilt dies, wenn nicht endliche Werte keine ungültigen JSON-Daten mehr erzeugen oder der Server einen Serialisierungsfehler meldet und sich die betroffenen Pfade konsistent verhalten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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), and it reproduces on the latest 1.x too.
Description
This is a different code path from #3341, which I filed for the structured-output case. Same input, different failure, and the two behave inconsistently with each other, so I am reporting it separately. Happy to fold it into #3341 if you would rather track them together.
When a tool has no return annotation, no output schema is generated and the result is rendered to a text block by pydantic_core.to_json(result, fallback=str, indent=2) (func_metadata.py:669 on 2.1.0). to_json writes non-finite floats as the bare tokens NaN and Infinity. RFC 8259 does not allow either, so the text block is not valid JSON. is_error is False, so the call reports success.
The text block sent to the client:
{
"mean": 1.5,
"stddev": NaN,
"max": Infinity
}
Python's json.loads accepts this, because it is lenient about those tokens by default. A spec-compliant parser does not. Node:
> JSON.parse('{"mean":1.5,"stddev":NaN,"max":Infinity}')
SyntaxError: Unexpected token 'N', ..."stddev":NaN,"max":"... is not valid JSON
So the same tool result is readable from a Python client and breaks a TypeScript one.
What I expected: a text content block holding JSON should be parseable by any JSON parser, or the SDK should fail at the server with an error that names the serialisation problem.
Worth noting alongside #3341: for the same input, the structured path silently converts the value to null and the text path emits invalid JSON. Whatever the fix is, it would be good if those two agreed.
The same to_json(..., fallback=str, indent=2) call is used in resources/types.py:103, and prompts/base.py:206 follows the same pattern, so the fix probably wants to cover those too.
Example Code
import json
import math
import anyio
from mcp.client.client import Client
from mcp.server.mcpserver import MCPServer
server = MCPServer("stats")
@server.tool()
def summarise():
"""No return annotation, so the result goes out as a text block."""
return {"mean": 1.5, "stddev": math.nan, "max": math.inf}
async def main() -> None:
async with Client(server) as client:
result = await client.call_tool("summarise")
text = result.content[0].text
print("is_error:", result.is_error)
print("text block:", text)
# Reject NaN/Infinity the way a spec-compliant parser does.
json.loads(text, parse_constant=lambda c: (_ for _ in ()).throw(ValueError(f"invalid JSON token: {c}")))
anyio.run(main)
Output:
is_error: False
text block: {
"mean": 1.5,
"stddev": NaN,
"max": Infinity
}
ValueError: invalid JSON token: NaN
Python & MCP Python SDK
Python 3.13.11, Linux.
mcp 2.1.0 (latest 2.x): func_metadata.py:669
mcp 1.29.1 (latest 1.x): fastmcp/utilities/func_metadata.py:558
Also reproduces on main at d8b63830.
Reported with AI assistance; I ran the reproduction above and read the output.
- Vorherrschende Sprache
- Python
- Sterne
- 24.3k
- Forks
- 4k
- Ø Merge
- 1 T. 19 Min.
- Gemergte PRs (30 T.)
- 29
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus modelcontextprotocol/python-sdk
-
v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
modelcontextprotocol/python-sdk#3546 · 5 Kommentare ·
-
v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
modelcontextprotocol/python-sdk#3545 · 1 Kommentar ·
-
v1 v2
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 91/100
modelcontextprotocol/python-sdk#3508 · 2 Kommentare ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
modelcontextprotocol/python-sdk#3504 ·
-
v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
modelcontextprotocol/python-sdk#3492 · 1 Kommentar ·
Alle Issues in modelcontextprotocol/python-sdk
Ähnliche Issues
-
triage/confirmed
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 92/100
NousResearch/hermes-agent#118866 ·
-
bug
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
apache/cloudstack#14222 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100