Skip to content

fix(mcp): keep on-demand discovery working after a server reconnects - #2052

Merged
will-lamerton merged 1 commit into
mainfrom
fix/mcp-discovery-reconnect
Nov 11, 2026
Merged

will-lamerton merged 1 commit into
mainfrom
fix/mcp-discovery-reconnect

Conversation

@will-lamerton

Copy link
Copy Markdown
Member

Description

On-demand MCP discovery (load_tool_schema and the catalog, which turns on at 8 or more MCP tools or with mcpDiscovery: "always") breaks after a server outage. It also sends the model a misleading error.

1. Reconnect loads every schema. When a server went unhealthy, ToolManager removed its tools from both the registry and the catalog. When it recovered to connected, the listener called registerMcpTools(getToolEntries(server)) whatever the discovery mode, and didn't put the tools back in the catalog. Example: a 40-tool GitHub server in a discovery session blips once. After that every request carries all 40 schemas, which is exactly what discovery exists to prevent, and the catalog section in the prompt is empty.

Fix: initializeMCP records whether it chose discovery. When a server goes unhealthy, the manager remembers which of that server's tools were registered. On recovery, restoreServerTools():

  • puts the server's tools back in the catalog
  • with discovery on, registers only the tools the model had loaded before the outage, so the rest stay on-demand
  • with discovery off, registers every tool, as before, and now also refills the catalog

2. Calling a catalog tool before loading it gave the wrong error. The catalog shows each tool's name, so models often call one directly. It isn't registered until its schema is loaded, so partitionToolCalls rejected it as does not exist … Available tools are: … (interactive loop) or Unknown tool: (plain/ACP). The mode check had the same gap ("not available in the current mode"). Nothing told the model it only had to load the schema. Now a call to an unloaded catalog tool is refused with: … is an on-demand MCP tool whose schema is not loaded yet. Call load_tool_schema with tool_name "X" first …. This only applies when load_tool_schema is offered. Otherwise the existing message stays.

The related isReadOnly issue (it trusts an unloaded tool's readOnlyHint) is a separate PR.

Overlap: #1863 also edits the unhealthy/recovery listener in initializeMCP, so whichever merges second will need a small rebase. The two changes don't conflict in intent: #1863 limits which names MCP can add or remove, and this PR decides which of those names are registered and which stay in the catalog.

Type of Change

  • Bug fix

Changeset

  • Added a changeset (patch)

Testing

New tests in source/tools/tool-manager.spec.ts. They use a fake client through the existing createMCPClient() seam and drive its health listener:

  • discovery mode: load one tool, the server goes unhealthy and then recovers. Only that tool is registered again, the catalog lists all 10 again with 9 unloaded, and the rest still load on demand.
  • discovery off: recovery registers all 10 tools and refills the catalog.
  • an unloaded catalog tool is refused with the load_tool_schema hint by both filterValidToolCalls and partitionUnknownToolCalls, and the same call is valid once loaded.

With tool-manager.ts and tool-filters.ts reverted to main, all three fail. tsc --noEmit and biome are clean. AVA passes for source/tools/**, source/hooks/chat-handler/**, source/acp, source/plain, source/mcp, the custom-tools end-to-end spec and prompt-builder (1267 tests).

🤖 Generated with Claude Code

When a server recovered, the health listener registered every tool it
has, whatever the discovery mode, and never put them back in the catalog.
A discovery-mode session then sent every schema in every request.
Recovery now refills the catalog and registers only the tools that were
loaded before the outage (or every tool when discovery is off).

A call to a catalog tool whose schema isn't loaded was rejected as
"does not exist" / "Unknown tool"; it now tells the model to call
load_tool_schema first.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions github-actions Bot added area:tools Tool implementations and tool-calling area:tui Terminal UI labels Nov 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

nc-review could not produce a verdict this run (agent exit 1). This is a problem with the review agent, not with your pull request. A maintainer can retry with /re-review.

@will-lamerton
will-lamerton merged commit 6c1fb70 into main Nov 11, 2026
19 checks passed
@will-lamerton
will-lamerton deleted the fix/mcp-discovery-reconnect branch November 11, 2026 14:54
jay23606 added a commit to jay23606/nanocoder that referenced this pull request Nov 12, 2026
…ano-Collective#2052)

When a server recovered, the health listener registered every tool it
has, whatever the discovery mode, and never put them back in the catalog.
A discovery-mode session then sent every schema in every request.
Recovery now refills the catalog and registers only the tools that were
loaded before the outage (or every tool when discovery is off).

A call to a catalog tool whose schema isn't loaded was rejected as
"does not exist" / "Unknown tool"; it now tells the model to call
load_tool_schema first.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
diyaavirmani added a commit to diyaavirmani/nanocoder that referenced this pull request Nov 16, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:tools Tool implementations and tool-calling area:tui Terminal UI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant