Skip to content

[BUG] MCP servers not properly terminated when Claude Code exits, causing orphaned processes #1935

@noomz

Description

@noomz

Environment

  • Platform (select one):
    • Anthropic API
    • AWS Bedrock
    • Google Vertex AI
    • Other:
  • Claude CLI version: 1.0.18 (Claude Code)
  • Operating System: macOS Darwin 24.5.0
  • Terminal: Terminal/zsh

Bug Description

Claude Code does not properly clean up MCP server processes when exiting, leading to orphaned processes that continue consuming system resources indefinitely. These
processes accumulate over multiple sessions and are not cleaned up even when MCP servers are removed from configuration.

Steps to Reproduce

  1. Configure one or more MCP servers using claude mcp add <name> <command>
  2. Start Claude Code and use it normally (any session that might initialize MCP servers)
  3. Exit Claude Code normally
  4. Check running processes with ps aux | grep mcp
  5. Observe orphaned MCP server processes still running
  6. Optional: Try claude mcp remove <name> - processes still persist

Expected Behavior

MCP server processes should be gracefully terminated when Claude Code exits. All spawned MCP server processes should be cleaned up, leaving no orphaned processes consuming
system resources.

Actual Behavior

MCP server processes remain running as orphaned processes after Claude Code exits. These processes:

  • Continue consuming system resources (CPU, memory)
  • Accumulate over time with each Claude Code session
  • Are not cleaned up even when running claude mcp remove <server-name>
  • Persist across system restarts in some cases

Additional Context

Evidence from testing:

  • Found 40+ orphaned MCP server processes from previous Claude Code sessions
  • Processes dating back several days (some running since Monday/Tuesday)
  • Process types include:
    • Docker containers: docker run -i --rm mcp/time, mcp/github, mcp/memory, etc.
    • Python processes: chroma-mcp, cli-mcp-server
    • Node.js processes: context7-mcp

Example orphaned processes (sanitized):
user 90260 0.0 0.0 411858096 12272 s001 S+ 11:19AM 0:00.56 docker run -i --rm mcp/time
user 88027 0.0 0.0 411869888 12224 ?? S 11:18AM 0:00.53 docker run -i --rm mcp/time
user 54415 0.0 0.0 411896512 4576 ?? S Tue12PM 0:02.82 docker run -i --rm mcp/time
user 90680 0.0 0.0 411871728 10912 ?? S Mon11AM 0:04.81 docker run -i --rm mcp/time
user 54413 0.0 0.0 411889136 4080 ?? S Tue12PM 0:02.74 docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN=*** mcp/github
user 54418 0.0 0.0 411896816 4128 ?? S Tue12PM 0:02.85 docker run --rm -i --mount type=bind,src=/Users/user/Projects/workspace,dst=/projects/workspace mcp/git

Impact:

  • Resource leak with long-running orphaned processes
  • Potential system performance degradation over time
  • User confusion when debugging MCP server issues
  • Accumulating Docker containers consuming disk space

Potential root cause:
The MCP protocol specification includes a graceful shutdown phase, but Claude Code appears to not properly invoke cleanup handlers during application exit to terminate
spawned MCP server child processes.

Metadata

Metadata

Labels

area:mcpbugSomething isn't workinghas reproHas detailed reproduction stepsplatform:macosIssue specifically occurs on macOS

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions