Uh oh!
There was an error while loading. Please reload this page.
Added clean shutdown + tests - #115
Conversation
jspahrsummers
left a comment
There was a problem hiding this comment.
Thanks for working on this! Some blocking discussion, but this is definitely important
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| 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.
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.
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.
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.
davidfiala
commented
Jan 3, 2025
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 STDOUTconstonlyStderr=newConsole({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 |
jspahrsummers
commented
Jan 6, 2025
Yes, this is discussed here: https://modelcontextprotocol.io/docs/tools/debugging#server-side-logging |
Nedomas
commented
Jan 8, 2025
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: constclient=newClient({name: 'superinterface-mcp-client',version: '1.0.0',},{capabilities: {}})consttransport=ewSSEClientTransport(newURL('some-url'))awaitclient.connect(transport)awaitclient.close()awaittransport.close()But might be an unrelated bug? |
jerome3o-anthropic
commented
Jan 8, 2025
That's odd - likely a separate issue. Going to merge this now and follow up on the SSE bug later |
…issue-114 fix(server) Avoid modifying path resolution for executables on MacOS
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.