Skip to content

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

Closed

Description

@akramcodez

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

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

Activity

  1. added
    enhancement
    performance
    architecture
    area:mcp
    on Oct 5, 2026
  2. added
    area:providers
    proposal
    on Oct 7, 2026
  3. rishu685 commented on Oct 8, 2026

    @rishu685
    Contributor

    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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    architecturearea:mcparea:providersenhancementperformanceproposal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions