All notes

An MCP server in a Claude Code worktree session reads the main checkout

Claude CodeMCPGit worktrees

Ask the client for its MCP roots and use the first one. When the Claude desktop app runs a session in a git worktree, it starts the session’s MCP servers in the main checkout. A server that finds the repository from its working directory or from CLAUDE_PROJECT_DIR then reads the wrong folder: the main checkout’s branch, commit and files, while the session edits .claude/worktrees/<name>.

What each source names

Measured with Claude Code 2.1.321, in a desktop session with its own worktree:

Source Folder
The server’s working directory The main checkout
CLAUDE_PROJECT_DIR in the server’s environment The main checkout
The first root the client lists The session’s worktree
cwd in a hook’s input The session’s worktree

Hooks are fine, then. Only the MCP server is misled.

I found it through a server of mine that records the commit and branch a session is on. In worktree sessions it recorded the main checkout’s commit, and it took the branch from there too.

Read the first root

Roots are folders the client tells the server it works in, as file: URIs. Claude Code lists the session’s own folder first and the ones added with --add-dir after it. Take the first and never guess among the others:

import { fileURLToPath } from "node:url";

function folderOfRoots(roots) {
  const first = roots[0];
  if (!first) return null;
  try {
    return fileURLToPath(first.uri);
  } catch {
    return null; // not a local file: URI
  }
}

async function sessionFolder(server) {
  const fallback = process.env.CLAUDE_PROJECT_DIR ?? process.cwd();
  if (!server.server.getClientCapabilities()?.roots) return fallback;
  try {
    const { roots } = await server.server.listRoots(undefined, { timeout: 5000 });
    return folderOfRoots(roots) ?? fallback;
  } catch {
    return fallback;
  }
}

server is the TypeScript SDK’s McpServer. Keep the fallback, since a client that offers no roots, or doesn’t answer in five seconds, should get the old behaviour.

Ask again when the client sends notifications/roots/list_changed. A session can move into a worktree after it starts, with Claude Code’s EnterWorktree, and an answer cached at startup would then be stale.

On the newest protocol revision

With version 2.3.1 of the SDK’s server package, Claude Code 2.1.321 speaks MCP revision 2026-07-28 to my server, and there the roots travel differently. The server doesn’t send a roots/list request of its own. A tool call answers with an input_required result that asks for the roots, and the client makes the call again with them attached:

return inputRequired({ inputRequests: { roots: inputRequired.listRoots() } });

So on that revision my server asks on every tool call, which also follows a session that changes folder. It reads what the client supports from each request’s envelope, because over stdio getClientCapabilities() comes back empty there. The older path above still serves clients on a 2025 revision.

Check it

I started a headless Claude Code the way the desktop app starts a worktree session and had it make one tool call. Before the change the server recorded the main checkout’s commit. After it, the record carried the worktree’s commit and the branch the worktree is on. Tests cover both revisions over real git worktrees.

For the worktree side of this on Windows, see reusing node_modules in a git worktree.