Skip to content

Modern Streamable HTTP passes NaN/Infinity from request JSON into tool handlers #3961

Backlog

Description

@beartester34

What happened
A modern Streamable HTTP tools/call whose arguments contain the non-standard literals NaN, Infinity or -Infinity reaches the tool handler as non-finite Python floats and gets HTTP 200. These literals are outside RFC 8259's number grammar. The modern path parses with json.loads(body) (src/mcp/server/_streamable_http_modern.py:402), whose default accepts them.

I hit this while hardening my own MCP server built on this SDK: a NaN argument went straight into my handler.

Expected
Reject before dispatch as a parse error (-32700), like other malformed bodies.

Reproduce (main at 77001c9, in-process, no network, via Server.streamable_http_app()):

import json

import anyio
import httpx2

from mcp.server import Server
from mcp.server.transport_security import TransportSecuritySettings
from mcp.types import CallToolResult
from mcp_types import CLIENT_CAPABILITIES_META_KEY, PROTOCOL_VERSION_META_KEY
from mcp_types.version import LATEST_MODERN_VERSION

HEADERS = {
    "mcp-protocol-version": LATEST_MODERN_VERSION,
    "mcp-method": "tools/call",
    "mcp-name": "probe",
    "accept": "application/json",
    "content-type": "application/json",
}


def call_body(arguments):
    # json.dumps writes NaN/Infinity literally (allow_nan=True)
    return json.dumps({
        "jsonrpc": "2.0", "id": 1, "method": "tools/call",
        "params": {
            "_meta": {PROTOCOL_VERSION_META_KEY: LATEST_MODERN_VERSION, CLIENT_CAPABILITIES_META_KEY: {}},
            "name": "probe", "arguments": arguments,
        },
    }).encode()


async def main():
    seen = []

    async def tool(ctx, params):
        seen.append(params.arguments)
        return CallToolResult(content=[])

    app = Server("probe", on_call_tool=tool).streamable_http_app(
        json_response=True, stateless_http=True,
        transport_security=TransportSecuritySettings(allowed_hosts=["test"]),
    )
    async with app.router.lifespan_context(app), httpx2.AsyncClient(
        transport=httpx2.ASGITransport(app=app), base_url="http://test"
    ) as c:
        codes = [
            (await c.post("/mcp", content=call_body({"x": v}), headers=HEADERS)).status_code
            for v in (float("nan"), float("inf"), float("-inf"))
        ]
    print("HTTP statuses:", codes)
    print("handler arguments:", seen)


anyio.run(main)

Output:

HTTP statuses: [200, 200, 200]
handler arguments: [{'x': nan}, {'x': inf}, {'x': -inf}]

Related: #3341 and #3897 cover non-finite floats in tool output (server → client). This is the inbound direction.

Possible fix: make the modern parser reject non-finite constants before dispatch (e.g. a parse_constant that raises), with a test. The legacy path (pydantic_core.from_json, src/mcp/server/streamable_http.py:581) also accepts these by default, so it may need a separate look. If you'd take an outside PR, I'd like to fix this myself.

AI disclosure: I used AI tools in my own detection-and-fix workflow; I ran the reproduction myself.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions