claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

Feature Request: Lazy-Loading / On-Demand MCP Tool Registration

Open dev-punia-altimate opened this issue 10 months ago • 1 comments

Summary

Add support for lazy-loading MCP servers so tools are only registered when needed, rather than loading all tools at conversation start. This would dramatically reduce token consumption for users with multiple MCP servers configured.

Problem

Currently, all configured MCP servers load their tools at conversation start, consuming significant context tokens:

Server Tools Tokens
notion 14 ~30k
github 37 ~27k
atlassian 22 ~20k
sentry 18 ~21k
playwright 22 ~14.5k
serena 29 ~20k
Total 142 ~150k

Real-world impact: In a typical debugging session, I only used 6 tools (~6.6k tokens) but paid for 150k tokens of tool definitions. That's 95% waste.

Proposed Solution

Option A: Keyword-Triggered Loading (Recommended)

{
  "mcpServers": {
    "sentry": {
      "command": "npx",
      "args": ["-y", "@sentry/mcp-server"],
      "lazyLoad": {
        "enabled": true,
        "triggerKeywords": ["sentry", "error", "debug", "issue", "stacktrace"],
        "description": "Error tracking and debugging tools"
      }
    },
    "notion": {
      "command": "npx", 
      "args": ["@notionhq/mcp-server"],
      "lazyLoad": {
        "enabled": true,
        "triggerKeywords": ["notion", "document", "wiki", "page"],
        "description": "Notion workspace tools"
      }
    }
  }
}

Behavior:

  1. At conversation start, Claude sees only descriptions of lazy servers (minimal tokens)
  2. When user mentions "check sentry for errors", Claude recognizes keyword match
  3. Claude prompts: "I need Sentry tools for this. Enable? [Y/n]"
  4. On approval, tools are loaded and available for rest of conversation

Option B: Explicit Request Tool

Add a built-in tool:

enable_mcp_server(server: string): void
// Claude can call this when it determines tools are needed

Option C: Category-Based Loading

{
  "mcpServerGroups": {
    "debugging": ["sentry", "github"],
    "documentation": ["notion", "atlassian", "confluence"],
    "testing": ["playwright"],
    "coding": ["serena", "morph-mcp"]
  },
  "defaultGroup": "coding"
}

Benefits

  1. Token Efficiency: 90%+ reduction in base token usage
  2. Faster Startup: Less tool parsing at conversation start
  3. Better UX: Users don't need to manually manage MCP configs
  4. Scalability: Users can install many MCPs without penalty

Alternatives Considered

Approach Limitation
Multiple config profiles Manual switching, no mid-conversation loading
Meta-MCP proxy server Requires custom development per user
Remove unused MCPs Tedious, loses functionality when needed

Additional Context

  • MCP protocol may need extension for dynamic tool registration
  • Could be implemented client-side in Claude Code without protocol changes
  • Similar to VS Code's extension activation events

Willingness to Contribute

I'd be happy to help with design discussions or implementation if this moves forward.


Environment:

  • Claude Code version: Latest
  • OS: macOS
  • MCP Servers: 8 configured, typically use 2-3 per session

dev-punia-altimate avatar Jan 08 '26 09:01 dev-punia-altimate

Wow this feature is requested a lot!

clarknoah avatar Jan 08 '26 10:01 clarknoah

ENABLE_TOOL_SEARCH works pretty darn well for a beta - try it!

ariccio avatar Jan 08 '26 16:01 ariccio

I've had this same frustration and had some shower thoughts:

Instead of explicit keywords, we could leverage a) Skills; b) plugins; c) the fact there's already a LLM available.

So a MCP does not be loaded in full into the context; it seems like it should be possible to instead load a MCP as a plugin that exposes skills/slash commands (?). When a MCP is first initialized, Claude could feed its individual tool definitions into Haiku and have it generate a skill definition with a summarized front matter. (And maybe regenerate automatically when the checksum of the long tool description changes.)

Assuming that long form skill definitions aren't preserved across compactions (since they can always be reloaded), this should alleviate the immediate pain point in a fairly simple way that fits the CC of doing things. Then down the line someone smarter than me can look into say whether there's a point in making "MCP skills" use forked contexts or not.

millimoose avatar Jan 15 '26 16:01 millimoose

We(me and Agents) implemented a similar pattern in our multi-agent system running on EC2. Instead of keyword triggers, we use Claude's native tool_choice="auto" for intent classification:

  • If stop_reason == "tool_use" → Work mode (initialize LSP, memory, tracing, load full toolset)
  • Else → Chat mode (respond directly, skip heavy initialization)

We also segment tools by agent role:

  • Worker agent: bash, file write, LSP (full access)
  • Codebase agent: file read, LSP (read-only verification)
  • Research agent: web_fetch, web_search only

Each agent only sees the tools it needs for its job. This naturally solves the token bloat problem - a verification pass doesn't load 40 tools when it only needs 3.

I'm updating with this native approach now. Thanks.

alistairyes avatar Jan 16 '26 16:01 alistairyes

Guys MCPSearch is live, as of a few days ago!

ariccio avatar Jan 17 '26 03:01 ariccio

This is basically what https://github.com/smart-mcp-proxy/mcpproxy-go does at proxy level - BM25 search means only relevant tools get sent to the model per query instead of loading everything upfront. Went from ~54K to under 1K tokens in practice.

Dumbris avatar Feb 06 '26 08:02 Dumbris

Closing for now — inactive for too long. Please open a new issue if this is still relevant.

github-actions[bot] avatar Mar 06 '26 22:03 github-actions[bot]

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

github-actions[bot] avatar Mar 14 '26 14:03 github-actions[bot]