Skip to content

fix(cli): exit the server when the MCP client closes stdin - #68

Merged
HarshCasper merged 3 commits into
mainfrom
fix/exit-on-client-disconnect
Aug 13, 2026
Merged

fix(cli): exit the server when the MCP client closes stdin#68
HarshCasper merged 3 commits into
mainfrom
fix/exit-on-client-disconnect

Conversation

@HarshCasper

@HarshCasper HarshCasper commented Aug 10, 2026

Copy link
Copy Markdown
Member

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-server containers in a day. The npx entry has the same defect and leaks host Node processes instead.

Cause

The stdio transport only subscribes to data and error on 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. --rm never fires because it requires the container to exit.

Change

  • src/cli/lifecycle.ts: exit the process when stdin emits end or close. A 1.25 s grace period lets the PostHog client flush the last telemetry batch (its flushInterval is 1 s).
  • The CLI launcher registers the watcher before loading stdio.js, so the published bin picks it up for npx users.
  • The Docker image now boots through dist/cli.js instead of dist/stdio.js, which gives containers the same behavior. docker stop keeps working through the SIGTERM handler that xmcp installs, and the image build now smoke-tests the new entrypoint.
  • New troubleshooting row in docs/DOCKER.md with a one-liner to remove stray containers from older images.

@gtsiolis gtsiolis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix looks correct, left one comment worth addressing. 🙏

Comment thread src/cli/index.ts
@HarshCasper
HarshCasper merged commit f22ef00 into main Aug 13, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants