Skip to content

fix(mcp): don't leave a server connected when tool discovery fails - #801

Open
serhiizghama wants to merge 2 commits into
Nano-Collective:mainfrom
serhiizghama:fix/mcp-failed-tool-discovery-cleanup
Open

fix(mcp): don't leave a server connected when tool discovery fails#801
serhiizghama wants to merge 2 commits into
Nano-Collective:mainfrom
serhiizghama:fix/mcp-failed-tool-discovery-cleanup

Conversation

@serhiizghama

Copy link
Copy Markdown

Closes #787.

connectToServer() registered the client, transport, and config in its internal maps right after the handshake but before calling listTools(). So when a server connects fine but its initial tools/list fails, the rejection propagates as expected — but the server is left registered: isServerConnected() returns true, getServerInfo() reports connected: true with 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 small createClient() seam so the test can inject a client without standing up a real transport.

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.

@akramcodez akramcodez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks @serhiizghama

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP server remains connected when tool discovery fails

2 participants