Repository navigation
fix(tools): keep custom tools registered when MCP disconnects - #1056
will-lamerton merged 4 commits into
Conversation
disconnectMCP unregistered the MCP tools by name, then rebuilt the registry from the built-in exports and cleared the custom tool map. Any workspace custom tool disappeared with it, and only a restart brought it back. The targeted unregisterMany call above already removes exactly the MCP tools, so the rebuild was both redundant and destructive. Drop it and let the registry keep everything else. Closes Nano-Collective#1034
will-lamerton
left a comment
There was a problem hiding this comment.
Thanks for this - the fix itself is correct. getToolEntries() derives its keys from getNativeToolsRegistry(), so unregisterMany(mcpToolNames) already removes exactly the MCP set and the rebuild was pure redundancy. I applied the diff locally: the spec file passes 7/7, tool-manager + tool-registry + mcp-client pass 168/168, tsc --noEmit clean.
Three things before merge.
1. The test can't catch the regression it's for. The stub returns getNativeToolsRegistry: () => ({}), so unregisterMany gets an empty array and is a no-op. A change that deleted the unregisterMany call entirely would still pass this test, leaving MCP tools registered against a dead client. Please register a fake MCP entry, have the stub return that name, and assert the MCP tool is gone and the custom tool remains.
2. The metadata claim isn't asserted. The PR body and changeset both lean on approval/readOnly surviving for plan and headless filtering, but the asserts only cover hasTool / isCustomTool / getCustomToolNames / getAllTools. A getAvailableToolNames(undefined, 'plan') and 'headless' check after the disconnect is one line each and locks in the actual behaviour.
3. The changeset needs adjusting in both directions.
It overstates the symptom: disconnectMCP() has exactly one production call site, the shutdown handler registered in initializeMCP (tool-manager.ts:112-118). reinitializeMCPServers doesn't go through it, it just calls initializeMCP again. So "custom tools disappeared until Nanocoder was restarted" isn't reachable today - at shutdown, clearing state is harmless. Still worth fixing since the method is public and the daemon is a plausible future caller, but let's describe the defect in the method rather than a user-facing symptom.
It also undersells the blast radius. The rebuild destroyed two more things worth naming:
- Skill-registered tools.
registerSkillTool()writes into the same registry, so bundle and skill tools vanished on disconnect too. - The
web_searchgate. The constructor unregistersweb_searchwhen there's no Brave key, butallToolExportsincludes it, so the rebuild silently resurrected an unusable tool after every disconnect.
Both are cheap to cover with a test each if you're willing.
Unrelated and out of scope, but I noticed it while tracing the call sites: reinitializeMCPServers overwrites this.mcpClient without disconnecting the old one, which orphans child processes and re-registers the mcp-client shutdown handler each time. Worth a separate issue.
The stub reported an empty native tool registry, so unregisterMany() ran over an empty array and deleting that call entirely would still have passed. The test now registers a tool, has the stub report its name, and asserts it is gone while the custom tool stays. Assert the mode filtering too. The approval and read_only metadata lives in the map that was cleared, so plan and headless filtering is what actually proves it survived, rather than the tool merely being present. Cover the two other things the rebuild destroyed: skill tools registered through registerSkillTool(), and the constructor's removal of web_search when no Brave Search key is set, which the rebuild resurrected as an unusable tool. Correct the changeset in both directions. It claimed custom tools vanished until a restart, which is not reachable: the only caller is the shutdown handler, where discarding state is harmless. It also named only one of the four things the rebuild threw away. Reported by @will-lamerton in review of Nano-Collective#1056.
|
All three taken. Pushed in d1c24da. 1. The test could not fail for the right reasonCorrect, and this is the second time in these PRs that I have written a test that passes against the bug. The stub reported an empty registry, so It now registers a tool, has the stub report that name, and asserts the MCP tool is gone while the custom tool stays. Two checks that it earns its place: The first of those is the case you named, and it passed before this change. 2. Metadata now assertedAdded 3. Changeset corrected in both directionsI verified both halves rather than taking them on faith. Overstated. Confirmed: Undersold. Confirmed both, and both now have a test:
The The PR description had the same overstatement and has been rewritten too, rather than leaving a claim I now know is wrong sitting at the top of the PR. On
|
|
Hey @rascal-sl - all looking good. Please could you resolve the conflict and then good to go :) |
…wipes-custom-tools # Conflicts: # source/tools/tool-manager.spec.ts
|
Conflict resolved in e07fb4d, The conflict was additive: we both appended a test to the end of
Both still catch it. The PR diff is still the same four files, so the merge did not drag anything else in. One note in case it matters for CI: my clone was shallow and |
Description
disconnectMCP()removed the MCP tools correctly, then threw away everything else:unregisterMany(mcpToolNames)on the line above already removes exactly the MCP set, so the rebuild was pure redundancy. It is gone. Nothing else changes.What the rebuild actually destroyed
My first description named only custom tools and claimed they stayed gone until a restart. @will-lamerton corrected both halves in review, and he is right on each.
It was not reachable.
disconnectMCP()has exactly one production call site: the shutdown handler registered ininitializeMCP(tool-manager.ts:98-104).reinitializeMCPServerslives inuseAppInitialization.tsxand callsinitializeMCPagain without going through it. At shutdown, discarding state is harmless, so no user has hit this. The method is public, so a future caller would.It threw away more than I said. Four things, not one:
approvalandread_onlymetadata that plan and headless filtering readregisterSkillTool()writes into the same registryweb_searchgateallToolExportsincludes it, so the rebuild resurrected an unusable toolunregisterManywere ever dropped, which is what the test now pinsType of Change
Tests
The first version of the test could not fail for the right reason, which @will-lamerton caught: the stub reported an empty native registry, so
unregisterManyran over an empty array.It now registers a tool, has the stub report that name, and asserts the MCP tool is gone while the custom tool stays. Two checks that it earns its place:
The mode filtering is asserted too, since the
approvalandread_onlymetadata lives in the map that was cleared, sogetAvailableToolNames(undefined, 'plan')and'headless'are what actually prove it survived rather than the tool merely being present.The
web_searchassertion compares against the pre-disconnect state rather than a fixed value, so it holds whether or not a Brave key is configured locally.Commands run
Not run: Semgrep, not installed here.
pnpm run buildis needed beforetest:allon a fresh clone, or thecli-integration.spec.tscases fail onMODULE_NOT_FOUND.Platform: macOS, Node 24, pnpm 11.
Note
Branches from
main, independent of #1043 and #1045.