[Bug]: MCP server writes to main repo instead of git worktree
Description
When using Backlog.md MCP server from within a git worktree, task files are created in the main repository's backlog/tasks/ directory instead of the current worktree's directory.
Steps to Reproduce
- Set up a project with Backlog.md and a protected
mainbranch - Create a git worktree:
git worktree add .worktrees/feature-branch -b feature-branch main - Open Claude Code (or any MCP client) with CWD set to the worktree
- Create a task via the MCP
task_createtool
Expected: Task file created in .worktrees/feature-branch/backlog/tasks/
Actual: Task file created in the main repo's backlog/tasks/
Impact
Since main is protected, the task files created by MCP can't be committed. The workaround is to manually copy files from the main repo to the worktree, then delete the originals — which defeats the purpose of MCP automation.
Suggested Fix
Resolve the project root using process.cwd() or git rev-parse --show-toplevel at the time of each file operation, rather than at server startup. In a worktree, --show-toplevel correctly returns the worktree path.
Environment
- Backlog.md: latest (installed via homebrew)
- MCP config:
backlog mcp start(stdio, user scope) - Git worktrees via
git worktree add - macOS
Hi @mrkre yeah this is not the desired flow.
I will work on a fix. In the meantime can you try to use the manual CWD override https://github.com/MrLesk/Backlog.md?tab=readme-ov-file#manual-config?
Yup already doing that @MrLesk thanks!
Alex's Agent: This likely overlaps the current CWD/MCP roots fixes that landed after the report (including BACK-391 and BACK-406). Could you retest on the latest release with the same git worktree setup? If it still writes to the main repo instead of the worktree, this should become a fresh regression task with the exact MCP launch command.
Since I use the project .mcp.json it would be very good if I could write the CWD relative to the project root.
cat .mcp.json
{
"mcpServers": {
"backlog": {
"type": "stdio",
"command": "backlog",
"args": [
"mcp",
"start"
],
"env": {"BACKLOG_CWD": "./backlog"}
}
}
}
So the backlog directory is a clean work tree mounting the backlog branch, and /backlog is in .gitignore.
I hit the same thing with a user-scope server (claude mcp add backlog --scope user -- backlog mcp start) and dug into why it survives BACK-391/406/434. Retested on main.
Repro (no worktree needed):
cd ~ && backlog init --backlog-dir .backlogso~/.backlogexists.- Register a user-scope server:
claude mcp add backlog --scope user -- backlog mcp start. - Open Claude Code in any other initialized project and create a task via MCP.
Expected: the task lands in that project's backlog/. Actual: it lands in ~/.backlog/ (whatever directory the server process happened to start in). The git-worktree case in this issue is the same bug with ~ replaced by the main checkout.
Root cause is in src/mcp/server.ts. createMcpServer resolves the root once at startup: if the launch directory has a config it registers the full toolset and freezes that root for the process lifetime, and only the no-config branch calls enableRootsDiscovery(). ensureRootsResolved() then early-returns unless discovery was enabled, so when the server starts on top of an existing project (a global ~/.backlog, or the main checkout while you work in a worktree) the client's MCP roots are never consulted. BACK-391/406/434 fixed the fallback path; the normal path still ignores roots.
BACKLOG_CWD works around it by pinning, but a shared/user-scope server cannot pin per project, which is the whole point of one shared server name.
I put together a fix that unifies the two paths into a single request-scoped resolver with this precedence: an explicit --cwd/BACKLOG_CWD pin wins; otherwise the client's MCP roots are authoritative and re-resolved on roots/list_changed; a launch-directory project is kept only when it is nested inside an advertised root (monorepo packages), so an unrelated global ~/.backlog is dropped rather than written to; clients without the roots capability keep today's process.cwd() behavior. This closes both the worktree case and the shared-server case, with tests for each.
Opened #687 with the fix and tests for all of the above.