Skip to content

Commit 06280fb

Browse files
authored
fix: soft-fail MCP server connection errors instead of throwing (#485)
When both transports (streamable HTTP + SSE) fail to connect to an external MCP server, the error was thrown and double-logged via logHttpError (which emits log.exception for 500s), causing noisy alerts in Mezmo during bug duty. Now connectMCPClient returns null on all connection failures (matching the existing timeout behavior) and logs via log.softFail instead. This eliminates double-logging since all callers already handle null returns gracefully.
1 parent 136d2d8 commit 06280fb

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

src/mcp/client.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/
55
import log from '@apify/log';
66

77
import { TimeoutError } from '../errors.js';
8-
import { logHttpError } from '../utils/logging.js';
8+
import { getHttpStatusCode } from '../utils/logging.js';
99
import { ACTORIZED_MCP_CONNECTION_TIMEOUT_MSEC } from './const.js';
1010
import { getMCPServerID } from './utils.js';
1111

@@ -16,10 +16,8 @@ import { getMCPServerID } from './utils.js';
1616
export async function connectMCPClient(
1717
url: string, token: string, mcpSessionId?: string,
1818
): Promise<Client | null> {
19-
let client: Client;
2019
try {
21-
client = await createMCPStreamableClient(url, token);
22-
return client;
20+
return await createMCPStreamableClient(url, token);
2321
} catch (error) {
2422
// If streamable HTTP transport fails on not timeout error, continue with SSE transport
2523
if (error instanceof TimeoutError) {
@@ -34,15 +32,21 @@ export async function connectMCPClient(
3432
}
3533

3634
try {
37-
client = await createMCPSSEClient(url, token);
38-
return client;
35+
return await createMCPSSEClient(url, token);
3936
} catch (error) {
4037
if (error instanceof TimeoutError) {
4138
log.warning('Connection to MCP server using SSE transport timed out', { url, mcpSessionId });
4239
return null;
4340
}
44-
logHttpError(error, 'Failed to connect to MCP server using SSE transport', { url, mcpSessionId, cause: error });
45-
throw error;
41+
// External MCP server unavailability is operational, not a bug in our service
42+
const statusCode = getHttpStatusCode(error);
43+
log.softFail('Failed to connect to MCP server using SSE transport', {
44+
url,
45+
mcpSessionId,
46+
statusCode,
47+
error: error instanceof Error ? error.message : String(error),
48+
});
49+
return null;
4650
}
4751
}
4852

0 commit comments

Comments
 (0)