`--agent` CLI flag does not activate the agent's `mcpServers` on the main thread

Resolved 💬 3 comments Opened Apr 6, 2026 by don-san-sec Closed Apr 6, 2026

Summary

When using the --agent CLI flag (e.g., claude --agent my-agent), the agent's mcpServers frontmatter field is not activated on the main thread. The MCP servers defined in an agent's configuration are only initialized when the agent is spawned as a sub-agent via the Agent tool (runAgent.tsinitializeAgentMcpServers()).

This means there's no way to start a Claude Code session with agent-scoped MCP servers from the CLI using --agent.

Reproduction

  1. Create an agent file at .claude/agents/test-agent.md:
---
name: test-agent
description: An agent with a filesystem MCP server
tools:
  - "*"
mcpServers:
  - test-fs:
      command: npx
      args:
        - -y
        - "@modelcontextprotocol/server-filesystem"
        - "/tmp"
---

You are a helpful assistant with access to a filesystem MCP server.
  1. Run with --agent flag — MCP tools are NOT available:
claude --agent test-agent -p 'list tools starting with mcp__test'
# → NONE FOUND
  1. Spawn as a sub-agent via the Agent tool — MCP tools ARE available:
claude -p 'Use the Agent tool to spawn a sub-agent of type test-agent. Tell it to list all tools starting with mcp__test.'
# → 14 mcp__test-fs__* tools found

Root cause

In src/main.tsx, the mainThreadAgentDefinition is used to apply system prompt, model, tools, permissions, and initial prompt — but mainThreadAgentDefinition.mcpServers is never read or merged into dynamicMcpConfig.

The dynamicMcpConfig in main.tsx is built exclusively from:

  • --mcp-config CLI flags
  • Chrome integration (setupClaudeInChrome)
  • Computer-use setup (setupComputerUseMCP)

The agent MCP initialization code lives only in src/tools/AgentTool/runAgent.tsinitializeAgentMcpServers(), which is called when the Agent tool spawns a sub-agent — not when --agent starts a main-thread session.

Expected behavior

When --agent is used, the agent's mcpServers should be initialized on the main thread session, consistent with how other agent properties (system prompt, model, tools, permissions) are already applied.

Suggested fix

In src/main.tsx, after resolving mainThreadAgentDefinition, merge its mcpServers into dynamicMcpConfig. Something like:

// After resolving mainThreadAgentDefinition (~line 2062)
if (mainThreadAgentDefinition?.mcpServers?.length) {
  for (const spec of mainThreadAgentDefinition.mcpServers) {
    if (typeof spec === 'string') {
      // String references would need to be resolved from existing configs
      // (may already be loaded via other scopes)
    } else {
      // Inline definitions
      for (const [name, config] of Object.entries(spec)) {
        dynamicMcpConfig[name] = { ...config, scope: 'dynamic' } as ScopedMcpServerConfig
      }
    }
  }
}

Properties applied by --agent today

| Property | Applied on main thread? |
|---|---|
| System prompt | ✅ |
| Model override | ✅ |
| Tool allowlist/denylist | ✅ (via effectiveToolPermissionContext) |
| Permission mode | ✅ |
| Initial prompt | ✅ |
| MCP servers | ❌ Not applied |
| Hooks | ✅ |
| Max turns | N/A (sub-agent only) |

Environment

  • Claude Code version: 2.1.84
  • OS: macOS

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗