nanocoder
nanocoder copied to clipboard
[Feature] Architecture: On-demand MCP Tool Discovery to preserve local context windows
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
- Extract a lightweight "Tool Catalog" (just tool names and 1-sentence descriptions).
- Inject only the catalog into the initial system prompt.
- Provide a native
load_tool_schema(tool_name)tool. - 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)
Taking this issue here is my exact plan after going through the codebase let me know what you think!
Implementation Plan
- 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.
- Store MCP tools as lightweight catalog entries (
load_mcp_toolMeta-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.
- Introduce a built-in
- 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).
- Inject a compact
- Configuration (
source/types/config.ts,source/config/)- Add
mcp.lazyDiscovery("auto"|true|false). - Automatically activates for local models /
nano&minimaltool profiles, or when MCP schemas exceed a token threshold.
- Add
- CLI & Testing
- Update
/mcpcommand 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.
- Update