fix(mcp): don't leave a server connected when tool discovery fails - #801
Open
serhiizghama wants to merge 2 commits into
Open
fix(mcp): don't leave a server connected when tool discovery fails#801serhiizghama wants to merge 2 commits into
serhiizghama wants to merge 2 commits into
Conversation
Register the client, transport, and config only after listTools() succeeds, and close the partially-established client on failure so its transport and child process don't leak.
serhiizghama
requested review from
Avtrkrb,
akramcodez and
will-lamerton
as code owners
August 6, 2026 01:48
akramcodez
approved these changes
Aug 6, 2026
akramcodez
left a comment
Collaborator
There was a problem hiding this comment.
LGTM thanks @serhiizghama
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.
Closes #787.
connectToServer()registered the client, transport, and config in its internal maps right after the handshake but before callinglistTools(). So when a server connects fine but its initialtools/listfails, the rejection propagates as expected — but the server is left registered:isServerConnected()returns true,getServerInfo()reportsconnected: truewith zero tools, and the stdio child stays alive for the rest of the session.Moved the map registration to after
listTools()succeeds, so a server only becomes visible once both connection and tool discovery are done. The catch now also closes the partially-established client best-effort, so its transport / child process doesn't leak when discovery fails.Added two AVA tests around the connect lifecycle — one for the failed
listTools()path (rejects, nothing registered, client closed) and one confirming the success path still registers. They use a smallcreateClient()seam so the test can inject a client without standing up a real transport.