From 11a7ac93cc492c3df5c1ad80b4b45f2f04bc9f4c Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:24:39 +0800 Subject: [PATCH] fix(mcp): report ENOTCONN on stdio as a lost connection (#11490) The CLI already treats ENOTCONN on stdout as a closed reader (#11275). The MCP transport classifier still reported it as an unknown transport error. It now shares one set of peer-gone codes: EPIPE, ECONNRESET and ENOTCONN. Fixes #11362 --- src/main/reportMcpTransportError.ts | 12 +++++++++++- tests/boundary/mcp/mainErrors.test.ts | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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"),