Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Client in legacy mode opens a new TCP connection for every call (mcp 2.3.0) #3953

Backlog

Description

@madosh

With mode="legacy" (protocol 2025-11-25) against a stateful Streamable HTTP server, the client opens a new TCP connection for every call_tool, even though the server answers with connection: keep-alive. In mode="2026-07-28" against a stateless server it reuses one connection, as expected.

Reproduction

# pip install mcp==2.3.0 uvicorn
import asyncio
import socket

import mcp_types as types
import uvicorn
from mcp import Client
from mcp.server.lowlevel import Server

TOOL = types.Tool(name="echo", description="Echo the text back",
                  input_schema={"type": "object", "properties": {"text": {"type": "string"}}})


async def on_list_tools(ctx, params):
    return types.ListToolsResult(tools=[TOOL])


async def on_call_tool(ctx, params):
    return types.CallToolResult(content=[types.TextContent(type="text", text=params.arguments["text"])])


def count_connections(app, seen):
    async def wrapped(scope, receive, send):
        if scope["type"] == "http":
            seen.add(scope["client"])  # (host, port): a new port means a new TCP connection
        await app(scope, receive, send)
    return wrapped


async def run(mode, stateless):
    server = Server("demo", on_list_tools=on_list_tools, on_call_tool=on_call_tool)
    seen = set()
    app = count_connections(server.streamable_http_app(stateless_http=stateless), seen)
    sock = socket.socket()
    sock.bind(("127.0.0.1", 0))
    port = sock.getsockname()[1]
    uv = uvicorn.Server(uvicorn.Config(app, log_level="warning"))
    task = asyncio.create_task(uv.serve(sockets=[sock]))
    while not uv.started:
        await asyncio.sleep(0.05)

    async with Client(f"http://127.0.0.1:{port}/mcp", mode=mode) as client:
        await client.list_tools()
        await client.call_tool("echo", {"text": "warm-up"})
        seen.clear()
        for i in range(10):
            await client.call_tool("echo", {"text": f"call {i}"})
        print(f"mode={mode!r}, stateless_http={stateless}: 10 calls used {len(seen)} TCP connection(s)")

    uv.should_exit = True
    await task


asyncio.run(run("2026-07-28", stateless=True))
asyncio.run(run("legacy", stateless=False))

Output on Windows 11 (mcp 2.3.0, httpx2 2.16.0, uvicorn 0.54.0, Python 3.13). The benchmark's own test shows the same on Ubuntu 24.04:

mode='2026-07-28', stateless_http=True: 10 calls used 1 TCP connection(s)
mode='legacy', stateless_http=False: 10 calls used 10 TCP connection(s)

Why it matters

Each new connection costs a network round trip. I measured this while benchmarking MCP against UTCP (toolbench-x). With the server behind a simulated 20 ms round trip, a call in legacy mode took 26 ms longer than a plain HTTP request, against 5 ms in 2026-07-28 mode (median, same SDK and same server code). Over HTTPS a new connection also needs a TLS handshake, so I'd expect it to be worse there, although I didn't measure legacy mode over HTTPS. On real networks, with the server 7.6 ms and 106 ms away, 2026-07-28 mode stayed at about +5 ms, so the current mode is fine.

A lot of deployed clients and servers still speak 2025-11-25, so this probably affects real traffic.

A guess at the cause

Not verified: in my tests the client is the side that closes each connection. My guess is that it closes each SSE response before reading it to the end, so httpx2 can't return the connection to its pool.

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