-
Notifications
You must be signed in to change notification settings - Fork 85
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
Added clean shutdown + tests #115
Conversation
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.
Thanks for working on this! Some blocking discussion, but this is definitely important
if (remainingDataListeners === 0) { | ||
// Only pause stdin if we were the only listener | ||
// This prevents interfering with other parts of the application that might be using stdin | ||
this._stdin.pause(); |
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'm a bit surprised that this has any effect on termination, but then reading about Node.js readable streams, I'm like 😵
It feels a bit janky to do things this way, but I don't really have a better idea…
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.
Per top-level PR comment, I don't see how both MCP and anything else can co-exist on the same stdin stream.
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.
Certainly not while messages are streaming in. The only question is whether there'd ever be a reason for a server implementation to want to read from stdin after terminating the normal MCP stuff. Probably not? But it's always hard to anticipate how people will use a library.
I believe that since you are communicating a specific protocol over stdin/stdout, then it does assume you have permanently taken over both STDIO FDs. This has interesting implications that I've had to work around. Specifically, wouldn't a Here was an approximate workaround that I put at the top of servers to route the most common occurences to stderr instead: // make console.log and console.info go to STDERR because MCP is going to take over STDOUT
const onlyStderr = new Console({ stdout: process.stderr, stderr: process.stderr });
console.info = onlyStderr.info;
console.log = onlyStderr.log; In this case, I'd argue that yes it's fine/expected for you to end the stdin/stdout streams. It should be clearly documented that this behavior occurs IMHO. Likewise, perhaps you should hotpatch the console logs in your |
Yes, this is discussed here: https://modelcontextprotocol.io/docs/tools/debugging#server-side-logging |
Even though the fix here is for stdio specifically, I’m wondering isn’t the same leak happening in the SSE transport too? I can’t seem to be getting rid of the active connection even with doing this: const client = new Client({
name: 'superinterface-mcp-client',
version: '1.0.0',
}, {
capabilities: {}
})
const transport = ew SSEClientTransport(
new URL('some-url')
)
await client.connect(transport)
await client.close()
await transport.close() But might be an unrelated bug? |
That's odd - likely a separate issue. Going to merge this now and follow up on the SSE bug later |
Motivation and Context
#113
When closing a StdioServerTransport, the process would hang indefinitely due to unclosed stdin/stdout streams and lingering event listeners. This was particularly noticeable in server implementations that needed to shut down cleanly. The fix ensures proper cleanup of all resources, preventing process hangs.
How Has This Been Tested?
Breaking Changes
None. This is a bug fix that maintains the existing API contract.
Types of changes
Checklist
Additional context
The fix involves three key changes in StdioServerTransport.close():
The integration test was added to prevent regression, as unit tests alone didn't catch this issue due to the asynchronous nature of Node.js stream cleanup.