Feature Request: CLI Commands for MCP Server Enable/Disable (Hook Automation Support)
Feature Request: CLI Commands for MCP Server Enable/Disable (Hook Automation Support)
Problem Statement
Claude Code v2.0.10 introduced the ability to enable/disable MCP servers within a session via the @ UI menu. This is excellent for manual control, but there's currently no way to programmatically toggle MCP servers via CLI commands. This prevents automation through hooks and scripts.
Current Limitations:
- The
claude mcpCLI only supportsadd,remove,list, andget- noenable/disable/toggle - MCP servers consume context window even when not actively needed
- No way to automate MCP lifecycle based on project context, skills, or workflows
- The
@menu toggle is UI-only with no programmatic equivalent
Use Case: Skill-Based MCP Management
I have several Claude Code skills that require specific MCP servers, but only occasionally:
Example Skills:
accessibility-compliance-expertskill → Needs Playwright MCP for visual testinglibrary-documentationresearch → Needs Context7 MCP for API docsdatabase-analysistasks → Needs database MCP tools
Current Problem:
- All MCP servers load at session start, consuming 15-20% of context window
- Playwright MCP alone: ~8-10k tokens, but only needed for visual testing tasks
- Context7 MCP: ~5k tokens, but only needed for documentation lookup
- These servers sit idle 90% of the time, wasting valuable context
Desired Workflow:
# SessionStart hook - enable only baseline MCPs
claude mcp disable playwright
claude mcp disable context7
# PreToolUse hook - enable when specific skill triggers
if [[ "$context" =~ "accessibility-compliance-expert" ]]; then
claude mcp enable playwright
fi
# Stop hook - disable when skill completes to free context
claude mcp disable playwright
Proposed CLI Commands
Add these commands to the claude mcp CLI:
# Enable an MCP server in current session (doesn't modify config files)
claude mcp enable <server-name>
# Disable an MCP server in current session (doesn't modify config files)
claude mcp disable <server-name>
# Toggle an MCP server's state
claude mcp toggle <server-name>
# Show current enabled/disabled state of all servers
claude mcp status
Example Usage:
$ claude mcp status
✓ pieces-mcp enabled (3.2k tokens)
✗ playwright disabled
✓ sequential-thinking enabled (1.8k tokens)
✗ context7 disabled
✓ stripe enabled (4.1k tokens)
Total active: 3/5 servers (9.1k tokens)
$ claude mcp disable stripe
✓ Disabled stripe (freed 4.1k tokens)
$ claude mcp enable playwright
✓ Enabled playwright (loaded 8.3k tokens)
Hook Integration Example
SessionStart Hook (.claude/hooks/session-start.sh):
#!/bin/bash
# Disable rarely-used MCPs at session start
claude mcp disable playwright
claude mcp disable context7
# Enable based on project type
if [[ -f "package.json" ]] && grep -q "@playwright/test" package.json; then
claude mcp enable playwright
echo "📊 Enabled Playwright MCP for testing project"
fi
PreToolUse Hook (.claude/hooks/pre-tool-use.sh):
#!/bin/bash
# Enable MCP when specific skill triggers
# Read context from environment
context="$CLAUDE_CONTEXT"
if [[ "$context" =~ "accessibility-compliance-expert" ]]; then
# Enable Playwright only when accessibility skill is active
claude mcp enable playwright
fi
SubagentStop Hook (.claude/hooks/subagent-stop.sh):
#!/bin/bash
# Free up context when specialized subagent completes
subagent_name="$CLAUDE_SUBAGENT_NAME"
if [[ "$subagent_name" == "accessibility-compliance-expert" ]]; then
claude mcp disable playwright
echo "♻️ Freed context: disabled Playwright MCP"
fi
Benefits
- Context Window Optimization
- Load MCP servers on-demand instead of all-at-once
- Free context by disabling servers when done
- Optimize for long development sessions with varying needs
- Skill-Based Automation
- Skills can automatically enable their required MCPs
- Clean up after themselves to avoid context bloat
- Smooth integration with subagent workflows
- Project-Based Profiles
- SessionStart hooks enable relevant MCPs based on project type
- Testing projects get Playwright, database projects get DB tools
- No manual toggling required
- Hook Ecosystem Integration
- PreToolUse, Stop, SubagentStop hooks manage MCP lifecycle
- Automated workflows without manual intervention
- ADHD-friendly: reduces decision fatigue
Implementation Considerations
Session-Level Only:
- These commands affect current session only (not config files)
- Changes don't persist across session restarts
- Config files (
.mcp.json,settings.json) remain unchanged
Relationship to @ Menu:
- CLI commands should mirror the behavior of the UI
@toggle - Both methods (UI and CLI) modify the same session state
claude mcp statusshould reflect current state regardless of how it was set
Security:
- Respect existing approval workflows for project-scoped servers
- Honor
managed-mcp.jsonenterprise policies - Enabling a disabled server may still prompt for project approval if not yet granted
Error Handling:
$ claude mcp enable nonexistent-server
❌ Error: MCP server 'nonexistent-server' not found in configuration
Available servers: pieces-mcp, playwright, sequential-thinking
$ claude mcp disable playwright
✓ Disabled playwright
$ claude mcp disable playwright
⚠️ Warning: playwright is already disabled
Why This is Different from Existing Issues
While several issues request runtime toggling (#6309, #6638), this request specifically focuses on:
- CLI automation (not just UI toggle, which already exists)
- Hook integration (SessionStart, PreToolUse, Stop, SubagentStop)
- Skill-based workflows (automated MCP lifecycle management)
- Concrete implementation (specific command syntax and examples)
Alternative Solutions Considered
- Project-scoped
.mcp.jsonwithenabledMcpjsonServers
- Problem: Only works at session start, not mid-session
- Problem: Requires session restart to change
- PreToolUse hooks blocking MCP tools
- Problem: Doesn't free context, server still loaded
- Problem: Shows confusing errors instead of smooth workflow
- Multiple Claude Code profiles/sessions
- Problem: Fragments workflow across sessions
- Problem: Loses conversation context when switching
Related Issues
- #6309 - Runtime MCP Server Toggle (general request)
- #6638 - Dynamic loading/unloading (context optimization focus)
- #7328 - Tool-level filtering (related but different scope)
- #3036 - MCP servers eating context window
Priority
High - This enables a powerful automation workflow that significantly improves context management and integrates with Claude Code's existing hook system.
---
Environment:
- Claude Code Version: 2.0.22 (latest)
- MCP Servers: 5-7 servers configured per project
- Context Impact: 15-20k tokens consumed by idle MCP servers
15 Comments
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
@claude
I don't think this fully encapsulates this issue which would allow for running in hooks
That's exactly what I'm looking for
It's exactly what I need. It could be the massive update for context management.
Anthropic introduced some great tools in the API SDK recently:
https://www.anthropic.com/engineering/advanced-tool-use
CC should use these features.
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.
Looking for this as well -- this would enable context auto load / offload dependent on hooks
I think that's an really excellent feature and will massively enhance context control
+1 on this
I currently run two MCP servers (Things + Google Calendar) that together consume ~12.4K tokens of context on every session. After analysing my usage history across all sessions, I found that roughly 3K tokens worth of tools have never been called once, and the remaining tools are only needed in maybe 30-40% of my sessions (planning and daily reviews, not coding).
Combined with the 33K autocompact buffer and ~20K of system prompts/tools, I'm losing nearly 65K tokens of my 200K context window before I've even started working. That's a third of the window gone.
The ability to
claude mcp enable/disablemid-session — especially via hooks — would let me:This would effectively give me back ~12K tokens in sessions where I don't need these integrations, which is most of them. That's a meaningful difference — it delays autocompaction and extends the useful life of a conversation.
The
@menu toggle already proves this works at the UI level. Having a CLI equivalent for hooks would make context management significantly more practical.+1
+1
+1
For the management side of this — knowing which MCP servers are configured at which scope and cleaning up the ones you don't need — I'm building a visual dashboard that shows all your MCP servers (and memories, skills, hooks) across every scope in a hierarchy view (global, workspace, project).
You can move servers between scopes with drag-and-drop and delete them directly. Doesn't give you runtime enable/disable mid-session yet, but it helps with the "I have 12K tokens of MCP servers loaded and I don't even know which scope they're coming from" problem.
We just launched and this kind of MCP management is exactly what we're building towards — give us a ⭐ to follow along: https://github.com/mcpware/claude-code-organizer
I had the exact same frustration — 12K+ tokens of MCP tool definitions eating my context window every session, with no way to toggle them programmatically.
I ended up building a gateway that sits between Claude Code and all MCP servers. Instead of exposing 60+ tools at startup, it exposes just 7 meta-tools (
airis-find,airis-exec,airis-schema) and routes to the right server on demand. Tool definitions went from ~42K tokens to ~1.4K — a 97% reduction.Servers start lazily on first call and auto-terminate after 120s idle, so there's no resource waste either. Effectively gives you the enable/disable behavior at the tool level without needing CLI commands.
Repo: https://github.com/agiletec-inc/airis-mcp-gateway
Not a replacement for native CLI enable/disable (which I still want too), but it solved the context window problem for me today.
A hook-based approach can toggle MCP servers programmatically:
Or use a wrapper script for CLI use:
Combine with a Notification start hook to auto-disable context-inappropriate servers:
It would be great to see this feature in the CLI.
The Claude Code app seems to be able to programmatically restart mcp servers (in my case, a stdio app that needs to recompile).