fix(mcp): broadcast roots/list_changed when session cwd changes#831
Open
apoc wants to merge 1 commit intocan1357:mainfrom
Open
fix(mcp): broadcast roots/list_changed when session cwd changes#831apoc wants to merge 1 commit intocan1357:mainfrom
apoc wants to merge 1 commit intocan1357:mainfrom
Conversation
When the user runs /move, MCP servers connected to the parent session were not notified of the new working directory because: - The client declared roots.listChanged: false in the initialize handshake - MCPManager.cwd was set at construction and never updated After /move, file-aware MCP servers continued to operate against the original repo root instead of the moved-to directory. Changes: - pi-utils: extract normalizeProjectPath helper so setProjectDir and MCPManager.setCwd cannot drift on macOS /private/... paths - MCPManager: add getCwd/setCwd; setCwd broadcasts notifications/roots/list_changed via best-effort fan-out - mcp/client: declare roots.listChanged: true (capability prerequisite); expose CLIENT_CAPABILITIES const for test pinning - mcp/types: add ROOTS_LIST_CHANGED to MCPNotificationMethods; annotate every entry with direction - mcp/roots: new shared helpers buildRootsList, notifyRootsChanged used by both manager and client default request handler - /move command: call mcpManager.setCwd after setProjectDir Tests: 17 cases across roots helpers, capabilities, and command wiring.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the user changes the session's working directory via
/move <path>, MCP servers connected to the session keep operating against the original repo root forever. Two reinforcing root causes:MCPManager.cwdis set at construction and never updated. Itsroots/listhandler answers fromthis.cwd, so connected servers see a stale path.roots.listChanged: falsein theinitializehandshake, which per MCP spec forfeits the ability to pushnotifications/roots/list_changed. Even if the manager's cwd were updated, there's no notification mechanism to tell servers to re-query.Net effect: file-aware MCP servers (code search, indexers, navigation) never learn about a
/moveand continue indexing/searching the original directory.Fix
MCPManager: addgetCwd()/setCwd(newCwd).setCwdnormalizes the path, no-ops on no-change, and broadcastsnotifications/roots/list_changedto every connected server (best-effort fan-out; per-connection failures logged at debug, never abort the broadcast).mcp/client: declareroots.listChanged: truein initialize capabilities (spec prerequisite for sending the notification). ExtractedCLIENT_CAPABILITIESconst so tests can pin the contract.mcp/types: addedROOTS_LIST_CHANGED: "notifications/roots/list_changed"toMCPNotificationMethods. Annotated every entry with direction + intended client behaviour for clarity.mcp/roots(new): sharedbuildRootsList(cwd)andnotifyRootsChanged(connections)helpers. BothMCPManager.#getRootsanddefaultRequestHandler(the probe-only fallback) now use the same builder so they cannot diverge.pi-utils: extractednormalizeProjectPathhelper. BothsetProjectDirandMCPManager.setCwdnow go through it, so they cannot drift on macOS/private/...paths (a single/moveto/private/tmp/foopreviously produced two differentfile://URIs depending on which code path servedroots/list)./movehandler: callsthis.ctx.mcpManager?.setCwd(resolvedPath)immediately aftersetProjectDir.Tests
17 cases across 3 files (all passing, full type-check clean):
test/mcp-roots.test.ts(12) —buildRootsList,notifyRootsChanged(fan-out, disconnected-skip, single-failure isolation),MCPManager.setCwd/getCwd(state, relative paths, no-op).test/mcp-client-capabilities.test.ts(2) — pinsCLIENT_CAPABILITIES.roots.listChanged === true.test/modes/controllers/command-controller-move.test.ts(5) — wiring of/move->mcpManager.setCwd, no-op whenmcpManagerundefined, no-op for invalid/missing path, no-op while streaming.E2E with a real subprocess MCP server was prototyped but excluded from the commit as too heavy for CI.
Limitations / out of scope
These are pre-existing or deliberate; not regressions of this PR. Could be follow-ups:
fuse-overlay,fuse-projfs,worktree) reuse the parentMCPManagervia proxy tools, so MCP servers still see the parent cwd, not the per-task isolation directory. The trade-off (fresh MCP connections per task vs. race-free shared state) is left as a known limitation.setCwdno-op check is strict-equality on the resolved path. Equivalent paths via arbitrary symlinks (other than the/private/...macOS case handled bynormalizeProjectPath) or case-insensitive filesystems could miss the no-op and produce a redundant broadcast. Acceptable since the broadcast is idempotent.Review
Three review rounds (P0 wire-method spelling, P3 const centralization, P2 macOS path drift, P3 doc-comment symmetry) — all findings resolved. Final pass: APPROVE (confidence 0.9).
Files changed
packages/coding-agent/src/mcp/manager.tspackages/coding-agent/src/mcp/client.tspackages/coding-agent/src/mcp/types.tspackages/coding-agent/src/mcp/roots.ts(new)packages/coding-agent/src/modes/controllers/command-controller.tspackages/coding-agent/CHANGELOG.mdpackages/utils/src/dirs.ts