[FEATURE] MCP servers should reinitialize when session enters a git worktree

Resolved 💬 4 comments Opened Mar 8, 2026 by rmolines Closed Apr 10, 2026

Problem

When EnterWorktree is called mid-session, MCP servers are not reinitialized for the new working directory. They remain bound to the directory where Claude Code was originally launched.

This breaks workflows that depend on MCP servers that are path-sensitive — most notably XcodeBuildMCP, which detects and builds the Xcode project found in the working directory at startup.

Concrete example

  1. Launch Claude Code from /project (main repo) — XcodeBuildMCP initializes, finds no .xcodeproj
  2. Call EnterWorktree → session moves to /project/.claude/worktrees/my-feature/ which has a freshly generated .xcodeproj
  3. Ask Claude to build — XcodeBuildMCP still looks at the original path, fails or builds wrong target

Current workaround (painful)

Exit Claude Code, open a new terminal inside the worktree, and run claude from there. This loses conversation context and requires manual coordination.

We've added a PostToolUse hook on EnterWorktree that opens a new iTerm2 tab with claude running inside the worktree — but this starts a fresh session with no context from the current conversation.

The ideal behavior is session-level MCP reinitialization when the working directory changes to a different git root/worktree.

Expected behavior

After EnterWorktree, MCP servers that support it should either:

  • Reinitialize with the new working directory as context, or
  • Expose a setWorkingDirectory capability that Claude Code calls after worktree switch

Alternatives considered

  • Branch-based workflow (no worktrees) — MCP works, but loses isolation between parallel feature work
  • Worktrees as siblings (../feature/) instead of nested — doesn't solve the root cause
  • PostToolUse hook to open new terminal session — works but loses conversation context

Related issues

  • #16089 — project context not following CWD changes (closed/inactive, same root cause)
  • #12885 — working directory mismatch with worktrees

Impact

Any developer using git worktrees with path-sensitive MCP servers (Xcode, filesystem-based tools, project-specific servers). Worktree-based workflows are increasingly common with Claude Code's own EnterWorktree tool — making this a first-party friction point.

View original on GitHub ↗

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