diff --git a/src/main/reportMcpTransportError.ts b/src/main/reportMcpTransportError.ts index 9b35db78cc..ddc7870a7b 100644 --- a/src/main/reportMcpTransportError.ts +++ b/src/main/reportMcpTransportError.ts @@ -19,6 +19,16 @@ const transportSystemCode = (error: unknown): string | undefined => { return typeof error.code === "string" ? error.code : undefined; }; +/** + * System codes that mean the peer has gone. A pipe reports EPIPE, a socket + * ECONNRESET, and macOS can report ENOTCONN for a closed socket-pair stdio. + */ +const CONNECTION_LOST_CODES: ReadonlySet = new Set([ + "EPIPE", + "ECONNRESET", + "ENOTCONN", +]); + /** SDK error callbacks also report recoverable validation and protocol errors. */ export const reportMcpTransportError = ( error: unknown, @@ -29,7 +39,7 @@ export const reportMcpTransportError = ( const diagnostic = error instanceof z.ZodError ? { kind: "validation_error", message: MCP_VALIDATION_FAILED } - : code === "EPIPE" || code === "ECONNRESET" + : code !== undefined && CONNECTION_LOST_CODES.has(code) ? { kind: "connection_lost", message: MCP_CONNECTION_LOST } : { kind: "unknown", message: MCP_TRANSPORT_ERROR }; // Do not echo raw frames or exception text, which can contain caller data. diff --git a/tests/boundary/mcp/mainErrors.test.ts b/tests/boundary/mcp/mainErrors.test.ts index 52f1ac2d6c..a530f66f00 100644 --- a/tests/boundary/mcp/mainErrors.test.ts +++ b/tests/boundary/mcp/mainErrors.test.ts @@ -85,6 +85,13 @@ describe("MCP runtime errors", () => { cause: Object.assign(new Error("SECRET broken pipe"), { code: "EPIPE" }), message: MCP_CONNECTION_LOST, }, + { + name: "a disconnected stdio socket", + cause: Object.assign(new Error("SECRET not connected"), { + code: "ENOTCONN", + }), + message: MCP_CONNECTION_LOST, + }, { name: "a wrapped HTTP broken pipe", cause: wrappedListenerDisconnect("EPIPE"),