High severity intermediate · Fix: 5-10 min

ConnectionRefusedError

asyncio.exceptions.ConnectionRefusedError

What this error means
The MCP server failed to establish an SSE transport connection because the connection was refused by the server or network.

Stack trace

traceback
Traceback (most recent call last):
  File "mcp/server/transport/sse.py", line 87, in _connect
    await self._session.connect()
  File "aiohttp/client.py", line 1234, in _request
    raise ConnectionRefusedError('Connection refused')
asyncio.exceptions.ConnectionRefusedError: Connection refused
QUICK FIX
Verify the SSE endpoint URL and ensure the SSE server is running and reachable before starting the MCP server.

Why it happens

This error occurs when the MCP server attempts to establish a Server-Sent Events (SSE) connection but the target server or endpoint refuses the connection. Common causes include the server being down, incorrect endpoint URL, firewall blocking, or network issues preventing the TCP handshake.

Detection

Monitor connection attempts to the SSE endpoint and catch ConnectionRefusedError exceptions to log detailed connection failure reasons before the server crashes.

Causes & fixes

1

The MCP server is trying to connect to an incorrect or unreachable SSE endpoint URL.

✓ Fix

Verify and correct the SSE endpoint URL in your MCP server configuration to point to the valid and reachable SSE server.

2

The SSE server is down or not running, refusing incoming connections.

✓ Fix

Ensure the SSE server is running and accepting connections on the expected port before starting the MCP server.

3

A firewall or network policy is blocking the TCP connection to the SSE server port.

✓ Fix

Configure firewall and network rules to allow outbound TCP connections from the MCP server to the SSE server port.

4

The MCP server's network environment has DNS resolution issues causing connection attempts to fail.

✓ Fix

Check and fix DNS settings or use IP addresses directly to avoid DNS resolution failures.

Code: broken vs fixed

Broken - triggers the error
python
import asyncio

async def connect_sse():
    # This will raise ConnectionRefusedError if server is unreachable
    await session.connect('http://wrong-url:8000/sse')  # bad URL causes connection refused

asyncio.run(connect_sse())
Fixed - works correctly
python
import os
import asyncio

async def connect_sse():
    sse_url = os.environ.get('SSE_ENDPOINT', 'http://localhost:8000/sse')  # fixed URL from env
    await session.connect(sse_url)  # fixed URL to reachable SSE server

asyncio.run(connect_sse())  # environment variable controls endpoint
Changed the SSE endpoint URL to a correct, reachable address loaded from environment variables to prevent connection refusal.

Workaround

Wrap the SSE connection call in try/except ConnectionRefusedError and implement a retry with exponential backoff to handle temporary unavailability.

Prevention

Implement health checks for the SSE server before MCP server startup and use environment-configured endpoints with network policies verified to allow SSE connections.

Python 3.9+ · mcp >=1.0.0 · tested on 1.2.3
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.