Skip to content

Commit 957d1cb

Browse files
hyperpolymathclaude
andcommitted
fix(mcp): add crash resilience to claude-ai-mcp server
Unhandled exceptions and EPIPE from closed pipes were crashing the MCP server process, killing all connected Claude Code sessions. Add global error handlers so a single bad request no longer takes down the server. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9ed7688 commit 957d1cb

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

cartridges/claude-ai-mcp/src/server.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,40 @@ function handleToolCall(msg) {
354354
}
355355
})
356356
.catch((err) => {
357-
sendError(msg.id, -32603, err.message);
357+
const errMsg = err instanceof Error ? err.message : String(err);
358+
sendError(msg.id, -32603, errMsg);
358359
});
359360
}
360361

362+
// ---------------------------------------------------------------------------
363+
// Crash resilience — keep the server alive when a single request fails
364+
// ---------------------------------------------------------------------------
365+
366+
process.on("uncaughtException", (err) => {
367+
process.stderr.write(`[${SERVER_NAME}] uncaughtException: ${err.message}\n`);
368+
process.stderr.write(`${err.stack}\n`);
369+
});
370+
371+
process.on("unhandledRejection", (reason) => {
372+
const msg = reason instanceof Error ? reason.message : String(reason);
373+
process.stderr.write(`[${SERVER_NAME}] unhandledRejection: ${msg}\n`);
374+
});
375+
376+
process.stdin.on("end", () => {
377+
process.stderr.write(`[${SERVER_NAME}] stdin closed — exiting cleanly\n`);
378+
process.exit(0);
379+
});
380+
381+
process.stdin.on("error", (err) => {
382+
process.stderr.write(`[${SERVER_NAME}] stdin error: ${err.message}\n`);
383+
});
384+
385+
process.stdout.on("error", (err) => {
386+
// EPIPE — Claude Code closed its end; exit cleanly instead of crashing
387+
if (err.code === "EPIPE") {
388+
process.exit(0);
389+
}
390+
process.stderr.write(`[${SERVER_NAME}] stdout error: ${err.message}\n`);
391+
});
392+
361393
process.stderr.write(`[${SERVER_NAME}] MCP server running on stdio\n`);

0 commit comments

Comments
 (0)