Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(server): exit when MCP client closes stdin pipe#2003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:main
Are you sure you want to change the base?
Changes from all commits
9463ca1e7d3907554a427a3060ebFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@modelcontextprotocol/server": patch | ||
| --- | ||
| Exit server process when MCP client closes stdin pipe. Previously, `StdioServerTransport` failed to detect when a client closed its stdin pipe, causing server processes to accumulate as zombies. This adds `close` and `end` event listeners on stdin that trigger proper transport cleanup. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -44,6 +44,16 @@ export class StdioServerTransport implements Transport { | ||
| // Ignore errors during close — we're already in an error path | ||
| }); | ||
| }; | ||
| _onstdinclose = () => { | ||
| this.close().catch(() => { | ||
| // Ignore errors during close — stdin pipe ended | ||
| }); | ||
| }; | ||
| _onstdinend = () => { | ||
| this.close().catch(() => { | ||
| // Ignore errors during close — stdin pipe ended | ||
| }); | ||
| }; | ||
| /** | ||
| * Starts listening for messages on `stdin`. | ||
| @@ -58,6 +68,8 @@ export class StdioServerTransport implements Transport { | ||
| this._started = true; | ||
| this._stdin.on('data', this._ondata); | ||
| this._stdin.on('error', this._onerror); | ||
| this._stdin.on('close', this._onstdinclose); | ||
| this._stdin.on('end', this._onstdinend); | ||
| this._stdout.on('error', this._onstdouterror); | ||
| } | ||
| @@ -85,6 +97,8 @@ export class StdioServerTransport implements Transport { | ||
| // Remove our event listeners first | ||
| this._stdin.off('data', this._ondata); | ||
| this._stdin.off('error', this._onerror); | ||
| this._stdin.off('close', this._onstdinclose); | ||
| this._stdin.off('end', this._onstdinend); | ||
| this._stdout.off('error', this._onstdouterror); | ||
Comment on lines
97
to
103
CopilotAI | ||
| // Check if we were the only data listener | ||
Uh oh!
There was an error while loading. Please reload this page.
CopilotAIMay 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
start()only listens forstdin'scloseevent, but a disconnected pipe commonly emitsend(andcloseis not guaranteed for all Readable streams). To reliably shut down on client disconnect, also listen forstdin'sendevent and remove that listener inclose()alongside the existingclosecleanup.