Fix: MCP Server Connection Closed
Updated 2026-12-01
MCP server connection closed means the transport shut down while the client still expected JSON-RPC: the stdio child exited, HTTP/SSE returned an error, or your Python client closed the session early. Treat it differently from a server that never appears in the UI—here the handshake started, then the pipe broke. For blank tool lists and config typos, start with Fix: MCP server not connecting. Last tested 2026-12-01.
Prerequisites
- The MCP server
commandandargsfrom Claude Desktop, Cursor.cursor/mcp.json, or your Python MCP client - Terminal access to rerun the same command outside the GUI
Docs used:
Steps
1. Reproduce outside the client
Copy command and args into a shell. A stdio server should stay running and wait silently:
npx -y @modelcontextprotocol/server-filesystem /tmp
If the process exits immediately, read stderr—the client only reports Connection closed.
2. Run the same path through MCP Inspector
npx @modelcontextprotocol/inspector@latest npx -y @modelcontextprotocol/server-filesystem /tmp
Inspector shows the crash before Claude or Cursor wraps it as Connection closed.
3. Python client: keep the session context open
McpError: Connection closed on the first list_tools() usually means the server subprocess died during initialize(). A common mistake is exiting async with too early:
# Wrong: server process stops before call_tool
params = StdioServerParameters(command="uv", args=["run", "server.py"])
# Right: one async with for the whole agent turn
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools = (await session.list_tools()).tools
# ... LLM loop and session.call_tool stay inside this block
Match the pattern in Connect an AI agent to an MCP server.
4. Check for stdout pollution after connect
If the server prints banners or debug logs to stdout after startup, the next read may fail and the client reports Connection closed or invalid JSON. Log to stderr only (stdio MCP rules).
5. HTTP/SSE: idle proxies and auth expiry
Remote servers (type: http or sse) can close idle connections at load balancers. Refresh tokens before expiry (Fix: MCP authorization Bearer error). For Streamable HTTP on Vercel, confirm the route stays warm or retry with backoff (Deploy MCP server to Vercel).
6. Claude Agent SDK: subprocess MCP timeout
The Agent SDK waits up to MCP_TIMEOUT (default 30 seconds) for stdio servers on the first turn. Slow npx cold starts can hit the deadline and surface as a failed connection (connection timing). Warm the server once in a terminal or pin a local install instead of npx -y.
7. Cursor-specific logs
In Cursor, open Output → MCP Logs after a disconnect. Pair with step 1—the log often shows the exit code from the child process.
Common errors
McpError: Connection closed right after initialize() — server traceback on startup (step 1–2). Missing venv, wrong cwd, or import errors are frequent culprits.
Closed mid-conversation after several tool calls — server uncaught exception inside a tool, or OOM kill. Re-run with logging on stderr; cap agent steps with Fix: AI agent stuck in a loop.
HTTP 401 then connection closed — stale Bearer token. See Fix: MCP authorization Bearer error.
Works in Inspector, closed in Desktop — PATH mismatch (spawn ENOENT disguised as close). Use absolute command paths from Fix: MCP server not connecting.
FAQ
What does MCP connection closed mean?
The client lost the MCP transport while a request was in flight. For stdio, the child process usually exited.
How is this different from MCP server not connecting?
Not connecting covers config, PATH, and JSON mistakes before a healthy session. Connection closed means a session started (or was attempted) and the transport dropped.
Why does my Python agent get Connection closed on the first tool call?
The stdio server crashed, stdout was polluted, or your code closed ClientSession before call_tool. Keep one session open for the whole turn.
Should I retry automatically?
Retry once after fixing the root cause (credentials, PATH, crash log). Blind retries on approval or mutation tools can duplicate side effects—pair with LangGraph: human in the loop when tools are destructive.