Skip to content

Context7 Platform -- Up-to-date docs for AI agents

fix(mcp): exit stdio server when parent closes stdio (#2577)

Fahreddin Özcan78b98266954d
diff --git a/.changeset/mcp-stdio-orphan-exit.md b/.changeset/mcp-stdio-orphan-exit.md
new file mode 100644
index 00000000..5a5281bb
--- /dev/null
+++ b/.changeset/mcp-stdio-orphan-exit.md
@@ -0,0 +1,5 @@
+---
+"@upstash/context7-mcp": patch
+---
+
+Exit the stdio MCP server when the parent process closes its stdio. Previously, if the parent (e.g. Claude Code) was force-killed shortly after a tool call, an idle undici keep-alive socket to the Context7 API would keep libuv's event loop alive past stdin EOF, leaving an orphaned `node` process that consumed memory until the kernel tore the socket down (which on Cloudflare-fronted endpoints can take hours). The server now listens for `end`/`close` on stdin and `SIGHUP` and exits cleanly. Fixes #2542.
diff --git a/packages/mcp/src/index.ts b/packages/mcp/src/index.ts
index 6894a045..cdf8dbb8 100644
--- a/packages/mcp/src/index.ts
+++ b/packages/mcp/src/index.ts
@@ -524,6 +524,11 @@ async function main() {
startServer(initialPort);
} else {
stdioApiKey = cliOptions.apiKey || process.env.CONTEXT7_API_KEY;
+
+ process.stdin.on("end", () => process.exit(0));
+ process.stdin.on("close", () => process.exit(0));
+ process.on("SIGHUP", () => process.exit(0));
+
const transport = new StdioServerTransport();
const server = createMcpServer();