Fix MCP server exiting before in-flight tool calls finish responding - #1
Merged
Merged
Conversation
StdioServer.start() resolved as soon as stdin closed, without waiting for _handleLine promises still in flight (they were fired via .catch() and never tracked). serve.js exits the process right after start() resolves, so any tools/call still awaiting real work (indexing/DB queries) at that moment lost its response — the client saw the process exit with no reply. Track pending line-handling promises and wait for them before resolving.
There was a problem hiding this comment.
Pull request overview
This PR prevents the MCP stdio server from exiting as soon as stdin closes by tracking per-line request handlers and waiting for all in-flight request processing to finish before StdioServer.start() resolves. This aligns the server lifecycle with cgraph serve, which exits the process immediately after start() completes, ensuring clients don’t lose tool responses mid-flight.
Changes:
- Track
_handleLine(...)promises in apendingset. - Remove completed promises from tracking when they settle.
- Delay
start()resolution until readline closes and all pending handlers have finished.
| }); | ||
|
|
||
| rl.on('close', resolve); | ||
| rl.on('close', () => { Promise.all(pending).then(resolve); }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StdioServer.start() resolved as soon as stdin closed, without waiting for _handleLine promises still in flight (they were fired via .catch() and never tracked). serve.js exits the process right after start() resolves, so any tools/call still awaiting real work (indexing/DB queries) at that moment lost its response — the client saw the process exit with no reply. Track pending line-handling promises and wait for them before resolving.