Repository navigation
fix(mcp): don't let an MCP tool replace or remove a built-in (#1853) - #1863
Conversation
The tool registry is a plain name-keyed map, and MCP entries were registered after the built-ins, so an MCP tool named like a built-in (the filesystem server's write_file and list_directory, the git server's git_commit) silently replaced it: handler, schema and approval policy. git_commit stopped prompting in auto-accept. The unhealthy and disconnect paths then removed MCP tools by name, which deleted the built-in for the rest of the session. ToolManager now tracks the names the MCP client registered. An MCP tool whose name is already taken by a built-in, custom or skill tool is skipped, the same way a colliding custom tool is, and is reported on the server's init result. The TUI, --plain and the daemon print a startup warning for it. The unhealthy path, the recovery to connected and disconnectMCP only add or remove names MCP holds. The MCP lookups behind /tools, the confirmation prompt and plan mode's read-only filter also only apply to those names, so a skipped tool's annotation can't hide or relabel the built-in. Two existing specs planted "MCP" tools with registerSkillTool and set the private mcpClient field; they now connect a fake client through initializeMCP instead. Closes Nano-Collective#1853.
nc-review: comments — 1 nit@diyaavirmani — a few things worth a look, none blocking. Closes #1853 cleanly. ToolManager now tracks names registered by MCP and treats any name collision with a built-in/custom/skill tool as an MCP skip (parallel to the existing custom-tool collision handling). Unhealthy/recovery/disconnect paths now mutate only MCP-owned names; the MCP lookups ( ⚪ nit · The recovery test covers 🔴 blocking · 🟠 a reviewer would ask for a change · ⚪ optional Automated code review — correctness, security, design, tests, plus duplicates and scope. A human still decides; this is not a substitute for review and is not exhaustive. The required status checks separately cover lint, formatting, types, unused dependencies, the test suite and the build. This bot never merges. Maintainers can rerun with |
|
Hi @diyaavirmani, thanks for this PR! It looks like a maintainer has left feedback Whenever you get a chance, could you take a look at the open comments? |
Resolves the conflicts with Nano-Collective#1914, which gives each ACP session a ToolManager layered over the shared one and skips a session MCP tool whose name the parent already offers. - registerMCPEntries replaces registerMcpTools. The layered registry's hasTool already sees the parent's tools, so a name the parent offers is skipped by the same check as a built-in, and is also reported in skippedTools. Upstream's warning log is kept for every skip. - mcpReadOnly reads only names this manager's MCP client registered, then falls back to the parent. Upstream's version read the client's mapping for any name, which let a skipped tool's annotation decide whether plan mode offers the built-in ("plan mode keeps a built-in that an unannotated MCP tool is named like" fails without this). - getMCPToolInfo keeps the registered-name lookup and upstream's parent fallback. - New spec: a session's skipped MCP tool is reported and does not mark the existing tool as an MCP tool.
|
Merged main to clear the conflicts with #1914 (68628ea). #1914's per-session
Testing on Node 22: @addyCooks could you take another look when you get a chance? |
addyCooks
left a comment
There was a problem hiding this comment.
Thanks, this looks good. The tests cover the cases we discussed in #1853 and CI is green. One thing before merge:
The branch now conflicts with main in tool-manager.ts (initializeMCP). Main added the warning for two MCP servers exposing the same tool name (getToolNameConflicts). Please rebase and keep both warnings. Note the early return results when nothing is skipped would skip that conflicts warning, so move it, and add a test that both warnings still fire. Then I'll merge.
Resolve the initializeMCP conflict with main's duplicate MCP tool name warning (getToolNameConflicts) and keep both warnings: - The duplicate-name warning now runs before the early return taken when no tool was skipped, so it fires in the common case of two servers sharing a name that no built-in uses. - A skipped name is left out of the duplicate-name warning: when two servers offer a built-in's name, the built-in handles the calls, so naming a server they "go to" would be wrong. - Skipped names are reported for every server that offers them, not only for the server the tool mapping routes the name to. Specs cover a shared name with nothing skipped, a skip alongside a shared name (both warnings), and a built-in name two servers share. The fakes the PR added for initializeMCP now provide getToolNameConflicts.
|
Thanks @addyCooks, done. I merged main in (6829401) rather than rebasing, and kept both warnings. The duplicate-name warning now runs before the early return, so it fires even when nothing is skipped. A name that a built-in already uses is left out of it and reported as skipped for each server that offers it. |
Conflicts with on-demand MCP discovery (Nano-Collective#1651, Nano-Collective#2052, Nano-Collective#2057): - tool-manager.ts: kept discovery, the catalog and outage recovery, with this branch's registration rule on every path. A name a built-in, custom or skill tool has is skipped from the catalog too, so load_tool_schema can't offer the MCP schema for it, and loadMcpTool won't claim it. Tools loaded on demand are tracked, so an outage or a disconnect removes them and never the built-in. - daemon/entry.ts, plain/initialize.ts: pass upstream's tune config and keep the skipped-tools warning. - Specs: kept both sides; setGlobalMessageQueue moved to @/components/message-queue (Nano-Collective#2155), and the custom-tools stand-in client now maps its tool, which initializeMCP reads.
|
@addyCooks
Three new tests cover discovery mode. The shadowed name stays out of the catalog. A tool loaded on demand is removed on disconnect while the built-in stays. An outage and recovery restore the loaded tool without touching the built-in. All three fail without these changes. Format, types, lint and knip pass, and the tools, MCP, ACP, daemon, plain-mode, hooks and conversation specs pass locally. CI is green (18/18). Could you take another look? |
addyCooks
left a comment
There was a problem hiding this comment.
Nice work as always @diyaavirmani !
Approving 🎉
Description
Closes #1853.
The tool registry is a plain name-keyed map, and MCP tools were registered after the built-ins. So an MCP tool with a built-in's name silently replaced it: handler, schema and approval policy. The filesystem server's
write_fileandlist_directory, and the git server'sgit_commit, are common examples.git_committhen ran without a prompt in auto-accept. When the server later went unhealthy or disconnected, its tools were removed by name, which deleted the built-in for the rest of the session.As agreed in the issue, built-ins always win.
Change (
source/tools/tool-manager.ts):ToolManagertracks the names the MCP client registered.MCPInitResult.skippedTools).connected, anddisconnectMCPonly add or remove names that MCP itself registered. A built-in can no longer be replaced on recovery or removed on disconnect.getMCPToolInfo, used by/toolsand the confirmation prompt, and plan mode's read-only filter. So a skipped tool can't relabel the built-in as an MCP tool, and its missingreadOnlyHintcan't hide the built-in in plan mode. I found the plan-mode case while working on this: an unannotated MCPlist_directorywas hiding the built-inlist_directoryin plan mode.Warning:
describeSkippedMCPTools()builds one message, and the TUI (aWarningMessage),--plain(writeStatus) and the daemon (console.error) all print it at startup:Docs: a short "Tool Name Collisions" section in
docs/configuration/mcp-configuration.md.Type of Change
Changeset
pnpm changeset) describing this change for the changelogTesting
Automated Tests
.spec.ts/tsxfilespnpm test:allcompletes successfully): see below; the only failures also happen without this changeNew tests in
source/tools/tool-manager.spec.ts. They use a fake client through the existingcreateMCPClient()seam:skippedToolslists it, and the server's other tool still registersskippedToolsis unset when nothing collidesconnecteddoesn't replace the built-in, and its own tool comes back (the maintainer's extra case)disconnectMCPremoves only MCP tools (the maintainer's extra case)git_commitstill needs approval in auto-accept with a server offeringgit_commitdescribeSkippedMCPToolsformattingWith only
tool-manager.tsreverted (plus a stub for the new formatter), 7 of these fail. The other two, the "nothing collides" case and the formatter, are expected to pass there.Two existing tests created their "MCP" tools with
registerSkillTooland set the privatemcpClientfield, so as far asToolManageris concerned those tools were never registered by MCP. They now connect a fake client throughinitializeMCP, with the same assertions:tool-manager.spec.ts)custom-tools/end-to-end.spec.ts)Format, lint, types and knip pass. On macOS the full AVA suite passed 9,711 tests (20 skipped). The only 2 failures are the
gh-pr-worktreetests that fail the same way on upstream main (/varvs/private/vartmpdir paths on macOS).Manual Testing
End to end through the real CLI, on macOS with Node 22.23.3:
--plain run --json, auto-accept,@modelcontextprotocol/sdkthat logs each call and never writes to disk.These are the cases from the issue. The source of truth is
notes.txton disk, the MCP server's own call log, and the CLI's JSON report.9a244b89257b95e2)write_file(control)write_filenotes.txt, MCP never calledecho_tool+crash(control)list_directoryworkslist_directory+crashNot connectedUnknown tool: list_directory, no longer offeredlist_directoryworks and stays offeredgit_commit(control)git_commitIn the interactive TUI, I started in a project whose server offers
write_fileandecho_tool, accepted the trust prompt, and ran/tools:/toolslistswrite_file (fixture)./toolslistsecho_tool (fixture), andwrite_fileis listed with the built-ins.The fix cherry-picks cleanly onto the v1.32.0 release PR, and the branch merges cleanly with current main.
Checklist
WarningMessage,writeStatusandconsole.error, the same as MCP connection failures)