Skip to content

Solution: Use MCP Protocol to bypass Claude Code nested session restriction #125

Description

@dohapark81

Problem

Claude Code v2.1.39+ blocks nested sessions by checking CLAUDECODE environment variable, causing EPIPE errors when running crewx x @agent inside Claude Code.

Root Cause

  • Claude Code sets CLAUDECODE=1 env variable
  • Child processes spawning claude CLI detect this and exit immediately
  • CrewX subprocess spawning fails with EPIPE error

Solution: MCP Protocol Workaround

Key Insight: MCP servers run as separate processes, not as child processes of Claude Code. This means:

  • No CLAUDECODE env variable inherited
  • Can spawn claude/gemini CLI without restrictions
  • Uses Anthropic's official extension protocol (politically safe)

Architecture

Claude Code (VSCode)
  ├── MCP Protocol (stdio/sse)
  │     └── CrewX MCP Server (별도 프로세스) ← CLAUDECODE env 없음!
  │           └── tool call: "crewx_executeAgent"
  │                 └── claude spawn (정상 실행) ✅
  │
  └── Bash tool
        └── crewx x @agent → EPIPE ❌ (nested session blocked)

Implementation Status

Already Implemented! CrewX already has complete MCP infrastructure:

  • crewx_queryAgent - Query agents (read-only)
  • crewx_executeAgent - Execute tasks through agents
  • crewx_executeAgentParallel - Parallel agent execution
  • crewx_listAgents - List available agents
  • MCP stdio and HTTP modes both supported

Setup

Option 1: stdio mode (recommended)

Add to Claude Code config (~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "crewx": {
      "command": "npx",
      "args": ["-y", "@sowonai/crewx-cli@next"],
      "env": {
        "CREWX_CONFIG": "/path/to/your/crewx.yaml"
      }
    }
  }
}

Option 2: HTTP mode (more stable)

  1. Run CrewX MCP server:

    crewx serve --port 3100 --protocol HTTP --log
  2. Add to Claude Code config:

    {
      "mcpServers": {
        "crewx": {
          "url": "http://localhost:3100/mcp",
          "transport": "http"
        }
      }
    }

Usage in Claude Code

Use crewx_executeAgent tool:
- agentId: "crewx_claude_dev"
- task: "Implement feature X"
- projectPath: "/Users/doha/git/crewx"

Why This Is Perfect

  1. Technically sound: Separate process space, no env inheritance
  2. Politically safe: Using Anthropic's official MCP protocol, not "bypassing security"
  3. Zero new code: Existing MCP infrastructure works as-is
  4. Performance: No overhead compared to direct CLI calls

Documentation

See: docs/mcp-nested-session-workaround.md

Related Issues

Next Steps

  1. Update CrewX documentation to recommend MCP usage in Claude Code
  2. Add troubleshooting guide for common MCP setup issues
  3. Consider deprecating direct CLI calls in favor of MCP tools

Activity

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

    documentationImprovements or additions to documentationenhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions