fix(cli): MCP tools read config from projectDir, not the spawn dir

Merged
TO
1d ago
+145-11

Summary

Customer report (Windows 11, Claude Code, cmd /c npx -y @capgo/cli@latest mcp, app in a repo subfolder): capgo_set_project_dir says "Capacitor config found", but capgo_doctor says "No capacitor config file found" and start_capgo_live_update_onboarding returns no-capacitor-project, with or without projectDir. npx @capgo/cli doctor from the same folder works.

Root cause: the MCP wrapper already moves into the selected project with withCwd, but withCwd (and init) changed directory with the named chdir export of node:process. That export is the raw binding. It does not clear the value process.cwd() caches. Capacitor's loadConfig reads process.cwd(), so once anything had called process.cwd() at startup, config kept loading from the server spawn dir. It only bites projects that have their own typescript in node_modules (the normal case), because projects without one take the CLI's own loader, which uses the uncached named cwd(). That is why the report looked flaky and why terminal runs work (no chdir there). The cause is Node's cwd cache, not the path style, so it is not Windows specific; Windows users hit it because Claude Code launches the server from another dir.

All tools go through one wrapper (registerTool in mcp/server.ts: explicit projectDir/cwd, else capgo_set_project_dir state, else roots, else spawn cwd), so the fix is in the shared withCwd rather than per tool.

Changes:

  • cli/src/build/cwd.ts: process.chdir / process.cwd() instead of the named exports (covers every MCP tool, build request, SDK callers).
  • cli/src/init/command.ts: same swap for the three chdir call sites (monorepo switch, temporary cwd, upload from the selected app), same stale-cache bug for capgo init.
  • cli/test/test-mcp-project-dir-cwd-cache.mjs (wired into test:mcp-project-dir).

Test plan

New e2e test spawns the built server with real Node (Bun has no cwd cache and would hide the bug), with spawn dir different from the app, config only in the app, and a project-local typescript:

  • capgo_doctor with projectDir
  • capgo_doctor with the cwd alias
  • capgo_doctor, start_capgo_live_update_onboarding, start_capgo_builder_onboarding after capgo_set_project_dir (no args)
  • live-update and builder onboarding start with projectDir
  • switching projectDir between two apps across calls loads each one

On main all 5 fail ("No capacitor config file found", no-capacitor-project); with the fix all 5 pass. bun run test:mcp-project-dir passes, tsc --noEmit clean.

Not covered: a literal D:\ path run (CI has no Windows MCP runner here); the failure does not depend on path syntax.

Screenshots

Not visual.

Checklist

  • My code follows the code style of this project and passes bun run lint:backend && bun run lint. (Not run in full. eslint on changed files: changed lines clean; the 8 errors in cli/src/init/command.ts already exist on main and are untouched.)
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • My change has adequate E2E test coverage.
  • I have tested my code manually, and I have provided steps how to reproduce my tests
  • No GHSA ids, unpublished advisory bodies or PoCs, or embargoed exploit detail in this public PR (title, body, commits, branch name).

Summary by CodeRabbit

  • Bug Fixes
    • Improved project configuration detection when the MCP server starts in a different directory from the app. Project selection is now followed across calls, including when the project directory changes.
    • Onboarding tools can locate projects using saved project state or an explicitly provided project directory.
  • Tests
    • Added integration coverage for project detection across different startup directories and project-selection changes.