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

Add dynamic loading/unloading of MCP servers during active sessions

Open izpavlovich opened this issue 1 year ago • 15 comments

Problem Statement

Currently, MCP servers are loaded at session start and remain in the context window for the entire session, consuming tokens even when not actively needed. This is particularly problematic for specialized tools like Linear that are heavily used during planning phases but rarely needed during implementation.

Current behavior:

  • Linear MCP server: ~14k tokens (7% of 200k context window)
  • Loaded for entire session regardless of usage
  • No way to dynamically enable/disable during session

Use Case

Typical development workflow:

  1. Planning phase (5-15 minutes): Heavy Linear usage - create issues, update tickets, organize work
  2. Implementation phase (1-3 hours): Primarily coding - Linear context unused but consuming tokens
  3. Occasional planning (sporadic): Brief Linear usage for updates

Current workarounds are suboptimal:

  • Session restarts lose conversation context
  • Separate sessions fragment workflow
  • Manual MCP add/remove requires CLI context switching

Proposed Solution

Add dynamic MCP server management with session commands:

/mcp enable linear # Load MCP server mid-session /mcp disable linear # Unload and free context tokens /mcp list # Show active/available servers /mcp status # Show token usage by server

Benefits:

  • Preserve conversation context while managing resources
  • Optimize context window usage for long development sessions
  • Enable phase-based workflows (planning → implementation → review)
  • Maintain security controls (approval prompts still apply)

Implementation Considerations

  • Context cleanup: Properly remove server tools/schema from context
  • State management: Handle graceful enable/disable without breaking ongoing conversations
  • Security: Maintain existing approval workflows for project-scoped servers
  • UI feedback: Clear indication of active/inactive servers in status line
  • Persistence: Remember user preferences per session

Alternative Solutions Considered

  1. Improved scoping: More granular scope controls (feature-based, time-based)
  2. Lazy loading: Load servers on first use rather than session start
  3. Context compression: Compress unused MCP server definitions

Related Issues

  • #4986 (Dynamic Subagent Loading) - Shows interest in dynamic loading concepts
  • #6632, #6616, #6621 - Context optimization concerns from users

Impact

This feature would significantly improve context efficiency for users with multiple MCP servers, especially in long development sessions where tool usage patterns change over time.

izpavlovich avatar Aug 27 '25 04:08 izpavlovich

Let me illustrate this:

Image

Would be very nice to be able to add an mcp during the session, and even by the agent itself.

nibzard avatar Aug 27 '25 11:08 nibzard

This is a fantastic feature request. I run into the exact same problem, especially with heavy-duty MCP servers that are essential for one part of my workflow but just take up valuable context for the rest. Your breakdown of the "planning vs. implementation" phases is spot on.

While we wait for a feature like this, I wanted to share a couple of workarounds I've found based on the current documentation, and also offer an explanation for the behavior you're seeing in your screenshot.

Explaining the Screenshot: claude mcp add vs. /mcp

Based on the official documentation on MCP, the command claude mcp add is run from your shell (outside of an active Claude Code session). It modifies the configuration files that Claude Code reads when it starts up.

It looks like you ran claude mcp add... in your terminal, and it correctly modified your local configuration file. However, your existing Claude Code session was already running and won't see that change until you restart it. When you then ran the slash command /mcp inside the session, it reported "No MCP servers configured" because it was still operating on the configuration it had when it was launched.

This is the very problem you're highlighting: configuration is static for the life of the session.

Workaround 1: Using Project Scopes

The most direct way to manage this now is by using project-scoped MCP servers.

  1. Add your Linear server to a project-specific config file that you can check into git. This makes it easy for the whole team to use.

    # Run this from your shell in the project root
    claude mcp add linear --scope project --transport sse https://mcp.linear.app/sse 
    

    This will create a .mcp.json file in your project directory.

  2. When you're done with your "planning" phase and want to free up tokens, you can:

    • Exit the current Claude session.
    • Temporarily rename the file: mv .mcp.json .mcp.json.bak
    • Start a new session with claude --continue to resume your conversation.

The new session will load without the Linear MCP server, freeing up those ~14k tokens. It's not as elegant as your proposed /mcp disable, but it lets you keep your conversation history.

Workaround 2: Specialized Sub-Agents

A potentially more powerful (but also more complex) workaround is to use Sub-Agents.

You could create a dedicated "planning" sub-agent that is the only thing with access to the Linear MCP server.

  1. Create a file at .claude/agents/planner.md:

    ---
    name: planner
    description: A sub-agent for planning, creating, and updating tickets in Linear.
    tools: mcp__linear__create_issue, mcp__linear__update_issue, Read, WebSearch
    ---
    
    You are a project management assistant. Your primary job is to interact with our Linear project board. When asked to plan work, create tickets, or update status, you must use the provided Linear tools.
    
  2. When you start your main Claude Code session, it will have the planner sub-agent available. During your planning phase, you can invoke it:

    > use the planner to create a new ticket for the auth refactor
    

The planner sub-agent runs in its own context window, which gets populated with the Linear MCP tool definitions. Your main conversation context remains clean. During the "implementation" phase, you simply wouldn't invoke the planner sub-agent, and its context cost would never be incurred.


Again, these are just stopgaps. Your proposal for /mcp enable and /mcp disable would be a massive quality-of-life improvement for anyone juggling multiple complex tools. +1 for this feature.

coygeek avatar Aug 27 '25 17:08 coygeek

+1 on this request ..

The way I work is to group a set of mcp server (tools) in a given group .. and enable and disable groups of them (i do that using a script). What I noticed in my workflow is that the MCP server's I use are not specific to the project I am working, but on what task I am working on. So if I am in research mode, I use a given set of tools So if I am in debug mode, I use a given set of tools So if I am in testing mode, I use a given set of tools etc .. It use the same technique for subagents .. I don't have them all enabled (or available) at the same time .. but I select a "team" based on what I am doing .. As in real life, there is no point to have a project manager around during a brainstorming meeting, or have a front end dev present when you are working in SQL backend stuff.

pal-hexrays avatar Sep 02 '25 04:09 pal-hexrays

I've implemented a proof-of-concept for intelligent lazy loading that goes beyond manual enable/disable:

🔗 Repository: https://github.com/machjesusmoto/claude-lazy-loading 📝 Full discussion: #7336

Key improvements over manual loading:

  • Automatic detection: Analyzes input to load only needed tools
  • Registry-based: 5k token overhead vs 14k for Linear alone
  • Keyword intelligence: Loads Linear only when planning keywords detected
  • Session caching: Keeps loaded tools for 30 minutes

Example for your Linear use case:

# Planning phase - Linear auto-loads
"Let's review our Linear tasks" → Loads Linear MCP (14k)

# Implementation phase - Linear not loaded
"Build the authentication system" → Linear stays unloaded, saves 14k

This achieves what you're asking for - phase-appropriate tool loading without manual intervention.

machjesusmoto avatar Sep 10 '25 19:09 machjesusmoto

Need this as well

antonlvovych avatar Sep 30 '25 18:09 antonlvovych

This issue should be closed, as it has been resolved with the release of Claude Code 2.0.10:

2.0.10

  • Rewrote terminal renderer for buttery smooth UI
  • Enable/disable MCP servers by @mentioning, or in /mcp
  • Added tab completion for shell commands in bash mode
  • PreToolUse hooks can now modify tool inputs
  • Press Ctrl-G to edit your prompt in your system's configured text editor
  • Fixes for bash permission checks with environment variables in the command

lukemmtt avatar Oct 10 '25 18:10 lukemmtt

@lukemmtt but can we have mcps started disabled and then enable them if needed?

BenNewman100 avatar Oct 30 '25 14:10 BenNewman100

@BenNewman100 100%, you can view and toggle them in realtime without restarting the session

lukemmtt avatar Oct 30 '25 15:10 lukemmtt

@BenNewman100 100%, you can view and toggle them in realtime without restarting the session

yeah, but can you start a new session with them disabled? so they don't try to connect until you need them? that is my current problem

BenNewman100 avatar Oct 30 '25 18:10 BenNewman100

@BenNewman100

yeah, but can you start a new session with them disabled? so they don't try to connect until you need them?

That's exactly how it behaves for me; mcp enablement statuses persist across sessions. If you're not seeing that, I recommended filing a bug report

lukemmtt avatar Oct 30 '25 19:10 lukemmtt

I've just found this thread and appreciate the functionality is now available. It would be a great enhancement for claude itself to be able to dynamically enable and disable them as needed. "Please post my PR to slack" "I see you want me to post your PR to slack...I'll need to enable the slack mcp first" "...enabling slack mcp...posting to slack"

afkelsall avatar Dec 03 '25 06:12 afkelsall

export ENABLE_EXPERIMENTAL_MCP_CLI=true was doing it for me.

cmin764 avatar Dec 17 '25 14:12 cmin764

This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

github-actions[bot] avatar Jan 18 '26 10:01 github-actions[bot]

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

github-actions[bot] avatar Feb 14 '26 10:02 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 Feb 22 '26 14:02 github-actions[bot]