Serena edits land in primary checkout with Claude Code Agent Teams worktrees?
When using the Serena MCP server with Claude Code Agent Teams, worktree-isolated teammates' Serena symbolic edits (
replace_content,replace_symbol_body, insert/rename) land in the primary git checkout instead of the teammate worktree (e.g..claude/worktrees/<unit>). Serena runs via stdio with--context=claude-code --project-from-cwd; built-in Claude Code file tools and shell commands inside the worktree behave correctly. Tested on Claude Code v2.1.219:git -C <worktree> diffshows no edits whilegit -C <primary-checkout> diffshows them. Why?Originally asked on GitHub: https://github.com/oraios/serena/issues/1496
4 answers
Status: closed as explained (unfixed). The maintainer concluded this is a Claude Code problem, not a Serena one: Serena supports worktrees in general, and a session started directly inside the worktree with
--project-from-cwdshould work. For Agent Teams, he suspects Claude Code is reusing the MCP server instance it spawned for the primary checkout instead of starting a new one per worktree, and notes that MCP clients must treat servers as stateful; he hadn't investigated the Claude Code internals himself.A commenter who instrumented Claude Code v2.1.219 confirmed the project-scoped
--project/--project-from-cwdapproach does not fix Agent Teams teammates. They patched Serena to listen for MCPnotifications/roots/list_changedand rebind to the reported worktree; it worked with a real stdio MCP client that sends the notification, but Claude Code never sent that notification for any in-session worktree path they tested: plaingit worktree add, worktree-isolated subagents, named teammates (samesession_id, i.e. same MCP session), or the nativeEnterWorktreetool.EnterWorktreeonly appears to work if it is the very first Serena tool call in the session; after any earlier Serena call, edits resolve against the stale root.The maintainer's proposed workaround: enable Serena's
activate_projecttool (custom mode or project.yml) and instruct Claude Code to call it with the full path to the current directory whenever it switches to a worktree. Nobody in the thread confirmed this works. Reporting the behavior upstream to Anthropic as a Claude Code bug was recommended, though the maintainer was not optimistic.Did this work for you?What @jimmazur tried on GitHub (Partly worked)
A commenter patched Serena to listen for MCP
notifications/roots/list_changedand rebind the active project to the reported worktree. It worked with a real stdio MCP client that sends the notification, but Claude Code v2.1.219 never sent it for any tested worktree-creation path, so it did not fix real sessions.Source: https://github.com/oraios/serena/issues/1496#issuecomment-5082020068 (opens external site)
Did this work for you?What a GitHub user tried on GitHub (Outcome unclear)
The reporter investigated project-scoped
.mcp.jsonwith--project .or--project-from-cwd, noting MCP startup may happen before the teammate enters its worktree, but reached no conclusion.Source: https://github.com/oraios/serena/issues/1496 (opens external site)
Did this work for you?What @opcode81 tried on GitHub (Outcome unclear)
The maintainer suggested enabling Serena's
activate_projecttool and instructing Claude Code to call it with the full path to the current directory whenever it switches to a worktree. No one in the thread reported trying it.Source: https://github.com/oraios/serena/issues/1496#issuecomment-4961585652 (opens external site)
Did this work for you?
Related questions
- Serena MCP server SSE mode: dynamic client registration fails with HTTP 404
- Serena C# language server download fails: missing BuildHost.runtimeconfig.json on Windows
- Serena MCP server hangs at "Parsing all gitignore files" when project contains a large venv
- Serena MCP server fails to connect in Claude Code after installing with uvx
- Serena replace_symbol_body duplicates code in Claude Code — how to disable it?