mastra-ai/mastra · critical · Error
Could not connect to server with any available HTTP transpor
Error message
Could not connect to server with any available HTTP transport
What it means
When connecting to a remote MCP server over HTTP, connectHttp first tries the Streamable HTTP transport; if that fails it falls back to the legacy SSE transport. If the SSE fallback also fails, it logs the SSE error and throws this aggregate error, since no HTTP transport could establish a session with the server.
Source
Thrown at packages/mcp/src/client/client.ts:676
});
try {
await this.client.connect(sseTransport, { timeout: this.serverConfig.timeout ?? this.timeout });
this.transport = sseTransport;
this.log('debug', 'Successfully connected using deprecated HTTP+SSE transport.');
} catch (sseError) {
if (authProvider && sseError instanceof UnauthorizedError) {
this.markNeedsAuth(sseTransport);
throw sseError;
}
// Surface policy violations directly instead of the generic connect error.
if (isUrlPolicyError(sseError)) {
throw sseError;
}
this.log(
'error',
`Failed to connect with SSE transport after failing to connect to Streamable HTTP transport first. SSE error: ${sseError}`,
);
throw new Error('Could not connect to server with any available HTTP transport');
}
}
// Reaching here means a transport connected; any earlier authorization requirement is satisfied.
// Close, don't just drop, any transport left pending from an earlier 401 so its event stream
// and session resources are released rather than abandoned.
this.closePendingAuthTransport();
if (authProvider) {
this._authState = 'authorized';
}
}
/**
* Detaches whatever transport is still attached to the underlying SDK Client.
*
* The SDK assigns its internal `_transport` before `transport.start()` and never
* clears it when `start()` throws, and its cleanup after a failed initialize is a
* fire-and-forget `void this.close()`. Either way a stale transport can remainView on GitHub (pinned to 75dd419e61)
Solutions
- Verify the url points to the MCP server's HTTP endpoint (correct path and port)
- Test the endpoint manually (curl the MCP endpoint) to see the HTTP status returned
- Check auth: complete any OAuth flow (finishAuth) if the server returned 401
- Confirm which transports the server supports and configure the client accordingly
- Check server logs and any intermediate proxy/CORS configuration
Example fix
// before
const client = new InternalMastraMCPClient({ name: 'x', url: 'https://api.example.com' });
// after
const client = new InternalMastraMCPClient({ name: 'x', url: 'https://api.example.com/mcp' });
await client.connect(); // ensure /mcp is the real MCP endpoint Defensive patterns
Strategy: try-catch
Validate before calling
const res = await fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', method: 'initialize', id: 1, params: {} }) });
if (!res.ok) throw new Error(`MCP endpoint ${url} returned HTTP ${res.status}`); Try / catch
try {
await client.connect();
} catch (e) {
if (e instanceof Error && e.message.includes('any available HTTP transport')) {
// endpoint unreachable/unsupported: check url, auth, server uptime
}
throw e;
} Prevention
- Confirm the url is the actual MCP HTTP endpoint with curl before configuring
- Complete OAuth (finishAuth) when the server returns 401
- Verify server uptime and which transports it supports (streamable HTTP vs SSE)
- Check proxy/CORS settings that block EventSource or POST requests
When it happens
Trigger: Calling connect()/connectHttp(url) where the streamable-HTTP handshake fails (4xx/5xx, wrong endpoint, non-MCP HTTP server) AND the SSE fallback also fails (endpoint rejects EventSource, 404, CORS, auth rejection).
Common situations: Pointing the client at a URL that is not an MCP endpoint; server only supports one transport and it is misconfigured; reverse proxy stripping EventSource headers; expired OAuth tokens rejected by both transports; server down entirely.
Understand the failure class
Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.
Related errors
- SSE connection not established
- Failed to fetch servers from ${registry.servers_url}: ${resp
- Slack OAuth HTTP error: ${tokenResponse.status} ${tokenRespo
- Failed to stream background tasks: ${response.statusText}
- Failed to stream agent builder action: ${response.statusText
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/f5afe4f61cf40a6b.
Report an issue: GitHub.