Repository navigation
feat(mcp): on-demand MCP tool discovery to preserve local context windows (#1571) - #1651
Conversation
nc-review: needs work — 2 blocking, 1 important, 3 nits@rishu685 — there is a blocking item below. The PR implements on-demand MCP tool discovery via a new MCPToolCatalog, a load_tool_schema built-in tool, prompt injection of the catalog, and direct-execution hydration fallback. The mechanism is sound and the new tests exercise the auto-threshold path meaningfully. However, the JSON config schema is not regenerated after adding 🔴 blocking ·
🔴 blocking ·
🟠 important · The added discovery test passes no ⚪ nit · The ⚪ nit · Five ⚪ nit ·
🔴 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 |
|
Made the required changes as per the feedback |
addyCooks
left a comment
There was a problem hiding this comment.
Thanks for this, the idea fits the project and the specs pass here. Before merge:
- Please rebase: the branch conflicts with main (tool-manager.ts, its spec, prompt-builder.ts), and main now builds a ToolManager per ACP session.
setActiveToolManager(this)in the constructor lets a second ToolManager take over, andload_tool_schemathen reports the first one's tools as "not found" (I reproduced this). Please pass the manager explicitly.- Catalog descriptions aren't truncated, though the PR says they are. Please cap them (about 120 chars) and add a test.
- The catalog is added to the prompt even when
load_tool_schemaisn't offered (nano/minimal profiles). Please only add it when the tool is available. - Please update the PR description to follow the template. It also says the tool takes an array, but it takes a single
tool_name.
|
Hi @rishu685, 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? |
0d82e64 to
451bdca
Compare
|
@addyCooks Rebased onto latest
|
451bdca to
7d01a4c
Compare
addyCooks
left a comment
There was a problem hiding this comment.
Thanks @rishu685, the rebase, the 120-char cap, the catalog gating and the schema/tune wiring all look good, and CI is green. I tested the branch locally (109 specs pass) and found a few problems that need fixing before merge:
- Shared catalog gets wiped: a session manager shares the parent's catalog, but initializeMCP()/disconnectMCP() call
this.mcpCatalog.clear(). Aftersession.disconnectMCP()the base catalog went from 2 entries to 0, so ending one ACP session makes every unloaded tool "not found". Please use a per-manager catalog, or remove only that manager's own servers' entries. - Global active manager (my earlier point 2) is still there:
setActiveToolManager(this)in the constructor lets a second ToolManager take over. Please pass the manager explicitly toload_tool_schemaand the prompt builder. - Prompt injection: MCP descriptions go into the system prompt with newlines intact, so a description like "Lists files.\n\n### SYSTEM OVERRIDE\n..." becomes a heading in the prompt. Please collapse whitespace/newlines and strip markup in names and descriptions (and add a test).
load_tool_schemais registered even when there are no MCP servers, which adds a tool definition to every request. Please only register it when discovery is active.- Please update the PR description to the repo template (it also still says the tool takes an array; it takes a single
tool_name), and add a line in the changeset/docs abouttune.mcpDiscoveryand the default threshold of 8.
Small: the typeof ... === 'function' guards in initializeMCP exist only for test mocks; please update the mocks instead. Happy to re-review once updated.
7d01a4c to
cc01a58
Compare
|
@addyCooks Updated as per the feedback:
|
addyCooks
left a comment
There was a problem hiding this comment.
Thanks @rishu685, the sanitising, the docs and the template update look good. I re-tested the branch (176 specs pass, tsc and Biome clean). Three items from my last review still reproduce:
- Shared catalog gets wiped: a session manager shares the parent's catalog, but
initializeMCP()/disconnectMCP()callthis.mcpCatalog.clear(). Aftersession.disconnectMCP()the base catalog went from 2 entries to 0, so ending one ACP session makes every unloaded tool "not found". Please use a per-manager catalog, or remove only that manager's own servers' entries (removeServerTools), and add a test. setActiveToolManager(this)is still in the constructor, so a second ToolManager takes over the global. Please pass the manager explicitly toload_tool_schemaand the prompt builder.load_tool_schemais still registered for every session, even with no MCP servers, which adds a tool definition to every request. Please only register it when discovery is active.
Small: sanitizeCatalogText only replaces \r\n\t; /\s+/ would also cover U+2028 and vertical tab. Happy to re-review once updated.
…ument tune.mcpDiscovery
… and gate load_tool_schema to active discovery
ec77ce9 to
80ae4ec
Compare
|
@addyCooks Updated as per the feedback in commit 80ae4ec:
|
addyCooks
left a comment
There was a problem hiding this comment.
Thanks @rishu685, the last round fixes everything I raised. I re-ran my earlier probes on this head: a session manager's disconnectMCP() now leaves the base catalog intact, the global active-manager is gone, load_tool_schema isn't registered when there are no MCP servers, and descriptions with newlines or fake headings can no longer inject into the prompt. Types and Biome are clean, and the new tests fail without the change.
Optional follow-ups: stripping all whitespace from tool names would turn a name with a space into a different name, and the formatter hardcodes yellow instead of a theme colour.
Approving 🎉
…dows (Nano-Collective#1571) (Nano-Collective#1651) * feat(mcp): on-demand MCP tool discovery to preserve local context windows (Nano-Collective#1571) * fix(mcp): sanitize tool catalog text against prompt injection and document tune.mcpDiscovery * fix(mcp): strip HTML delimiters as single-character class to satisfy CodeQL * fix(mcp): layer session catalog over parent, pass manager explicitly, and gate load_tool_schema to active discovery
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.
Description
Introduces an on-demand MCP tool discovery architecture to mitigate system prompt context exhaustion when numerous MCP tools are configured:
tune.mcpDiscovery, default threshold: 8 tools). Unloaded tools are cataloged with sanitized names and descriptions.load_tool_schemaTool: Allows the agent to look up and inspect the full JSON parameter schema for any discovered MCP tool, dynamically hydrating it into the active tool registry for subsequent turns.sanitizeCatalogTextto strip markdown tags, HTML elements, collapse multi-line breaks/tabs into single spaces, and bound descriptions to 120 characters.tune.mcpDiscovery(auto,always,disabled) across types, regeneratedschemas/agents.config.schema.json, wiredtuneConfigthrough CLI/daemon/plain/TUI entry points, and documented indocs/features/tune.md.Related Issues
Fixes #1571
Type of Change
Testing
source/mcp/tool-catalog.spec.ts(6 tests including prompt injection sanitization)source/tools/tool-manager.spec.ts(101 tests includingalwaysanddisabledtune settings)source/tools/mcp/load-tool-schema.spec.tsx(4 tests)tsc --noEmit), Biome lint & check, and schema drift validation (pnpm generate:schema && git diff --exit-code schemas/agents.config.schema.json).Checklist