nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] Architecture: On-demand MCP Tool Discovery to preserve local context windows

Open akramcodez opened this issue 1 month ago • 1 comments

Description

Implement an on-demand, lazy-loading discovery mechanism for MCP (Model Context Protocol) tool schemas, rather than hydrating all available tools into the system prompt at the start of a session.

Use Case

As users add more MCP servers, the number of available tools grows rapidly. Passing 50+ massive JSON tool schemas into the system prompt destroys the limited context window of local models (e.g., Llama 3) and heavily degrades their reasoning ability and time-to-first-token (TTFT).

Proposed Solution

  1. Extract a lightweight "Tool Catalog" (just tool names and 1-sentence descriptions).
  2. Inject only the catalog into the initial system prompt.
  3. Provide a native load_tool_schema(tool_name) tool.
  4. When the agent realizes it needs a specific tool from the catalog, it calls load_tool_schema, and the harness dynamically injects the full schema into the context for subsequent turns.

Alternatives Considered

  • Hardcoding a limit on allowed tools (reduces functionality).
  • Relying on the user to manually toggle tools on/off via CLI flags (bad UX).

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] This feature aligns with the project's goals (local-first AI assistance)

akramcodez avatar Oct 05 '26 08:10 akramcodez

Taking this issue here is my exact plan after going through the codebase let me know what you think!

Implementation Plan

  1. Two-Tier MCP Registry (source/mcp/)
    • Store MCP tools as lightweight catalog entries (name, description, serverName) in memory.
    • Hold the full JSON parameter schemas separately so they aren't hydrated into the system prompt upfront.
  2. load_mcp_tool Meta-Tool (source/tools/mcp/)
    • Introduce a built-in load_mcp_tool({ tool_name: string }) tool.
    • When called, it dynamically activates and registers the full parameter schema into the session's active tool list (ToolManager) for subsequent turns.
  3. System Prompt Catalog (source/app/prompts/)
    • Inject a compact <mcp_tools_catalog> prompt section listing available tool names with 1-sentence descriptions when lazy mode is active (~15 tokens per tool instead of hundreds).
  4. Configuration (source/types/config.ts, source/config/)
    • Add mcp.lazyDiscovery ("auto" | true | false).
    • Automatically activates for local models / nano & minimal tool profiles, or when MCP schemas exceed a token threshold.
  5. CLI & Testing
    • Update /mcp command to display which tools are in catalog vs active/hydrated.
    • Add unit and integration tests verifying on-demand schema loading, turn-by-turn prompt updates, and token savings.

rishu685 avatar Oct 08 '26 07:10 rishu685