fix(cli): exit the server when the MCP client closes stdin - #68
Merged
Conversation
gtsiolis
approved these changes
Aug 10, 2026
gtsiolis
left a comment
Member
There was a problem hiding this comment.
Fix looks correct, left one comment worth addressing. 🙏
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In Docker mode, MCP server containers accumulate on the host. Every client session starts one container, and none of them exit when the session ends. A user reported collecting eight running
localstack/localstack-mcp-servercontainers in a day. The npx entry has the same defect and leaks host Node processes instead.Cause
The stdio transport only subscribes to
dataanderroron stdin. Closing stdin is the shutdown signal defined by the MCP spec for stdio servers, and Docker delivers that EOF reliably: when the client detaches, the daemon closes the container's stdin (StdinOnce). The server ignores it. Once any live handle exists in the process (telemetry flush timers, keep-alive sockets, log streams), the event loop never drains and the process runs forever.--rmnever fires because it requires the container to exit.Change
src/cli/lifecycle.ts: exit the process when stdin emitsendorclose. A 1.25 s grace period lets the PostHog client flush the last telemetry batch (itsflushIntervalis 1 s).stdio.js, so the published bin picks it up for npx users.dist/cli.jsinstead ofdist/stdio.js, which gives containers the same behavior.docker stopkeeps working through the SIGTERM handler that xmcp installs, and the image build now smoke-tests the new entrypoint.docs/DOCKER.mdwith a one-liner to remove stray containers from older images.