-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Ensure stdio client closes underlying process #818
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
Changes from all commits
896e9f3
dbaf1e3
50434b7
e23c1f8
0b38067
b93d564
a8f1a33
f95739f
ba97219
539fb3d
963849b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,7 +134,6 @@ export class StdioClientTransport implements Transport { | |
| this._process.on('error', error => { | ||
| if (error.name === 'AbortError') { | ||
| // Expected when close() is called. | ||
| this.onclose?.(); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -210,8 +209,36 @@ export class StdioClientTransport implements Transport { | |
| } | ||
|
|
||
| async close(): Promise<void> { | ||
| this._abortController.abort(); | ||
| this._process = undefined; | ||
| if (this._process) { | ||
| const processToClose = this._process; | ||
| this._process = undefined; | ||
|
|
||
| const closePromise = new Promise<void>(resolve => { | ||
| processToClose.once('close', () => { | ||
| resolve(); | ||
| }); | ||
| }); | ||
|
|
||
| this._abortController.abort(); | ||
|
|
||
| // waits the underlying process to exit cleanly otherwise after 1s kills it | ||
| await Promise.race([closePromise, new Promise(resolve => setTimeout(resolve, 1_000).unref())]); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a step to send a https://modelcontextprotocol.io/specification/2025-11-25/basic/lifecycle#stdio nit: should we increase the timeout to 2s to match the Python SDK?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure abort calls SIGTERM already. Changed to 2s :) |
||
| if (processToClose.exitCode === null) { | ||
| try { | ||
| processToClose.stdin?.end(); | ||
| } catch { | ||
| // ignore errors in trying to close stdin | ||
| } | ||
|
|
||
| try { | ||
| processToClose.kill('SIGKILL'); | ||
| } catch { | ||
| // we did our best | ||
| } | ||
| } | ||
| } | ||
|
|
||
| this._readBuffer.clear(); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { setTimeout } from 'node:timers'; | ||
| import process from 'node:process'; | ||
| import { McpServer } from '../server/mcp.js'; | ||
| import { StdioServerTransport } from '../server/stdio.js'; | ||
|
|
||
| const transport = new StdioServerTransport(); | ||
|
|
||
| const server = new McpServer( | ||
| { | ||
| name: 'server-that-hangs', | ||
| title: 'Test Server that hangs', | ||
| version: '1.0.0' | ||
| }, | ||
| { | ||
| capabilities: { | ||
| logging: {} | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| await server.connect(transport); | ||
|
|
||
| const doNotExitImmediately = async (signal: NodeJS.Signals) => { | ||
| await server.sendLoggingMessage({ | ||
| level: 'debug', | ||
| data: `received signal ${signal}` | ||
| }); | ||
| setTimeout(() => process.exit(0), 30 * 1000); | ||
| }; | ||
|
|
||
| transport.onclose = () => { | ||
| server.sendLoggingMessage({ | ||
| level: 'debug', | ||
| data: 'transport: onclose called. This should never happen' | ||
| }); | ||
| }; | ||
|
|
||
| process.stdin.on('close', hadErr => { | ||
| server.sendLoggingMessage({ | ||
| level: 'debug', | ||
| data: 'stdin closed. Error: ' + hadErr | ||
| }); | ||
| }); | ||
| process.on('SIGINT', doNotExitImmediately); | ||
| process.on('SIGTERM', doNotExitImmediately); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { McpServer } from '../server/mcp.js'; | ||
| import { StdioServerTransport } from '../server/stdio.js'; | ||
|
|
||
| const transport = new StdioServerTransport(); | ||
|
|
||
| const server = new McpServer({ | ||
| name: 'test-server', | ||
| version: '1.0.0' | ||
| }); | ||
|
|
||
| await server.connect(transport); | ||
|
|
||
| const exit = async () => { | ||
| await server.close(); | ||
| process.exit(0); | ||
| }; | ||
|
|
||
| process.on('SIGINT', exit); | ||
| process.on('SIGTERM', exit); |
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.
I think according to the spec this should be after the
stdinclosure.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.
for reference, how python does it: https://github.com/modelcontextprotocol/python-sdk/blob/5983a650cc07d2dc6c6ba098e99d3545889157a9/src/mcp/client/stdio/__init__.py#L191