Skip to content

fix(cli): reload MCP config after project selection - #966

Closed
luantaraschi wants to merge 8216 commits into
CodebuffAI:mainfrom
luantaraschi:fix/reload-mcp-after-project-change
Closed

fix(cli): reload MCP config after project selection#966
luantaraschi wants to merge 8216 commits into
CodebuffAI:mainfrom
luantaraschi:fix/reload-mcp-after-project-change

Conversation

@luantaraschi

Copy link
Copy Markdown

Summary

  • Reload the local agent registry after the project picker changes the working directory, so the selected project's .agents/mcp.json is loaded before the client is recreated.
  • Preserve the existing --agent override behavior.
  • Add a regression test that verifies a project MCP server reaches the base agent definition after project selection.

Fixes #957

Testing

  • Project picker and local agent registry tests: 41 passed
  • CLI typecheck
  • SDK build
  • Freebuff Windows binary build
  • Freebuff version and help E2E tests: 5 passed

codebuff public sync bot added 30 commits August 2, 2026 08:06
Source: CodebuffAI/freebuff-private@455596ee766ba73118788120f3ec9e353cbcf153
Source: CodebuffAI/freebuff-private@b1da43dfd83fb07493538a794012650c31a48539
Source: CodebuffAI/freebuff-private@0f2b495d355a0090347dcf2502fa179946ed89b3
Source: CodebuffAI/freebuff-private@648fbe7abb907b73dbbce79e60350545f12f2a5e
Source: CodebuffAI/freebuff-private@5cfb8e29aafd1373ebff2fc729d30c02ded8762e
Source: CodebuffAI/freebuff-private@7b02bcf9edb2e755cde3c6901a4171dac6c1b8c7
Source: CodebuffAI/freebuff-private@29c1c7b1abef99b25fec4cb90c5d6017b2c1c71c
Source: CodebuffAI/freebuff-private@52921bfce62c02c522dbfbd4aaeebb2df65136e7
Source: CodebuffAI/freebuff-private@c28e04fe81c1b7a6eaf13b407cbb145990a5cdaf
Source: CodebuffAI/freebuff-private@5676fb7cbf3d00fd0c9c3538d0259566dc668940
Source: CodebuffAI/freebuff-private@50bac2d3d963004ba6059a2795ff6a35c67381ac
Source: CodebuffAI/freebuff-private@ad1083b367a3fc29a6f612442f9870482a7185bc
Source: CodebuffAI/freebuff-private@c8249b5ccd9a1b2be9379672e92ffef10ff4897f
Source: CodebuffAI/freebuff-private@4b86502de1ed4882f15674ac14f7e84be3d9b58e
Source: CodebuffAI/freebuff-private@c7290ad48d71bb54682bd20d48863b741b35fcfc
Source: CodebuffAI/freebuff-private@2a09095ca7d9326fc5c4d3b3549d4772592711a5
Source: CodebuffAI/freebuff-private@866435ce7c3e6a3a5fc981dc03bed20c6b9c2eb0
Source: CodebuffAI/freebuff-private@84a23adc880c1e23633156dc004110ffecd03593
Source: CodebuffAI/freebuff-private@796b3e0b97cd7ad767e1d1708f597825c4b1c90b
Source: CodebuffAI/freebuff-private@23aaf9ef7774a9c7ed917aeff1cb126e83132468
Source: CodebuffAI/freebuff-private@1d362ea933a1a93f7064d82f4890ec402c2015a8
Source: CodebuffAI/freebuff-private@0dfe68ca3146fef7ece681786a7b6f966e05df57
Source: CodebuffAI/freebuff-private@022a380f3badd9850407e53b2a6a4cb6575f2035
Source: CodebuffAI/freebuff-private@e77ec9b87dba4de75ba19fb254c17fb16d718dc9
Source: CodebuffAI/freebuff-private@1a0b1105f13b283e66e72e5ee646cb99386cfbd1
Source: CodebuffAI/freebuff-private@8c44ea347507795dec3b5044cf5177f76c96d410
Source: CodebuffAI/freebuff-private@c853800b120a14823cc734eb920264d839feb376
Source: CodebuffAI/freebuff-private@ffa95ba73cba74ef367f26740fe81ed06b7a3f85
Source: CodebuffAI/freebuff-private@a89e78c516c43e1cd9b48913e3db8ebd90ab089f
Source: CodebuffAI/freebuff-private@dd137f0f66364db6bd1666c2d1126cb6d9f7e48f
codebuff public sync bot and others added 13 commits August 10, 2026 02:09
Source: CodebuffAI/freebuff-private@d1b2f0391c7d840ee20faf545ffdce0f9dc67685
Source: CodebuffAI/freebuff-private@a921e43c182e08d16612f2f054b34a34d6cce014
Source: CodebuffAI/freebuff-private@893daab93cdaf672061a81a5be163d7504919208
Source: CodebuffAI/freebuff-private@acf40ca98f1be4ed62c07abb261cfb267dce5364
Source: CodebuffAI/freebuff-private@2fd0ee2c027ea71c2814908aac00e9db07a8da3c
Source: CodebuffAI/freebuff-private@9f88777e422eb21d18fb9497b53228e5c24815b0
Source: CodebuffAI/freebuff-private@40759dcc4e86eea7874d3762dc744e0ac16ca42b
Source: CodebuffAI/freebuff-private@f9d585f7951f6f58bd55527a3474c1c4bb1a3ae3
Source: CodebuffAI/freebuff-private@7b1a370bc0bc45722e94e0cc0ead5ef2e213c62f
Source: CodebuffAI/freebuff-private@67a8bb39d0a85e467e91cb03a9cde41e566d2a01
Source: CodebuffAI/freebuff-private@6282027bf68035afa9da4ad9352ab3a673f197e4
Source: CodebuffAI/freebuff-private@1cae9492a65f772e22fad41d9ec391dcd89a16fc
Copilot Bot lite review requested due to automatic review settings August 10, 2026 17:29

ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@chrismindpower369

ghost commented Aug 12, 2026

Copy link
Copy Markdown

This looks like the stronger fix for #957 because the regression test exercises the actual activateProject() flow and verifies that the selected project's MCP server reaches the base agent definition.

PR #973 appears to address the same root cause with overlapping changes. Before merging, could we confirm that this PR also clears every derived local-agent cache that depends on the previous cwd/project root? If yes, I would suggest merging #966 and closing #973 as a duplicate to avoid two competing fixes.

@luantaraschi

ghost commented Aug 12, 2026

Copy link
Copy Markdown
Author

Thanks for catching this. The local agent registry has two derived caches that depend on the previous working directory: cachedAgentsByMode and cachedAgentsDir. The new reloadLocalAgentRegistry() helper clears both before initializeAgentRegistry() refreshes the agent definitions, file paths, and MCP configuration.

I also expanded the regression test to warm the caches in the launch project, switch projects through activateProject(), and verify that the selected project supplies the agent directory, local agent list, MCP server, and base agent definition. The old project's agent must no longer be present.

Validation on the new head (2f246ab):

  • 41 related tests passed
  • CLI typecheck passed
  • Freebuff Windows binary build passed
  • 5 --version and --help E2E tests passed
  • Prettier and git diff --check passed

This incorporates the cache-reset part of #973 while keeping the project-picker coverage in this PR.

@chrismindpower369

ghost commented Aug 12, 2026

Copy link
Copy Markdown

Perfect, thanks for the thorough follow-up. Clearing both cwd-dependent caches and testing the full activateProject() transition addresses the concern. The added assertion that the old project's agent is absent is especially valuable.

This now seems like the right PR to merge for #957. I’d suggest closing #973 as a duplicate once a maintainer has reviewed this update.

@codebuff-team

ghost commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Good catch and clean fix. The root cause is real: handleProjectChange in cli/src/index.tsx previously called setProjectRoot/resetCodebuffClient but never re-scanned .agents/ for the newly selected directory, so cachedAgentsDir/cachedAgentsByMode in local-agent-registry.ts kept serving the launch directory's MCP config. Wrapping the chdir/setProjectRoot/registry-reload/reset sequence into a single activateProject() helper in project-picker.ts is a sensible consolidation and removes the duplicated logic from index.tsx.

The added test in project-picker.test.ts is a genuine regression test — it writes real .agents/*.ts and mcp.json files into temp launch/project directories, calls activateProject, and asserts the base agent definition picks up the new project's MCP server while dropping the launch project's local agent. That's exactly the kind of test this bug needed.

Two things worth double-checking before porting:

  1. reloadLocalAgentRegistry only clears cachedAgentsByMode and cachedAgentsDir — please confirm there isn't a third cache (e.g. for MCP servers specifically, or directory-scan results) elsewhere in local-agent-registry.ts that also needs clearing, since getLoadedMCPServers() presumably reads from one of these.
  2. The hasAgentOverride skip path (reloadAgentRegistry: !hasAgentOverride) isn't exercised by the new test — a second test case confirming the override path doesn't reload would strengthen this further, though the existing description of preserving that behavior is plausible from the diff.

No forbidden paths touched, no scope creep — solid, focused bug fix.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Aug 20, 2026
@luantaraschi

ghost commented Aug 20, 2026

Copy link
Copy Markdown
Author

Both checked.

On the first one, there is no third cache. The module keeps five mutable ones: userAgentsCache, userAgentFilePaths and mcpServersCache at the top, and cachedAgentsDir and cachedAgentsByMode further down. initializeAgentRegistry() reassigns the first three on both the success and the failure path, including mcpServersCache from loadMCPConfigSync(), which reads the cwd, so getLoadedMCPServers() is covered as long as the chdir happens first. That is why reloadLocalAgentRegistry() only clears the other two: they are derived and nothing else resets them. __resetLocalAgentRegistryForTests lists all five, which is a handy way to confirm the count did not grow.

On the second one, you were right that the skip path had no coverage. Added a test for it: with { reloadAgentRegistry: false } the chdir and project root still move, and the launch project's agent is still the one served. I checked it is worth something by making activateProject reload unconditionally, and the new test is the only one that fails.

@victorxheng

ghost commented Aug 31, 2026

Copy link
Copy Markdown

Apologies — this PR was auto-closed by GitHub when we force-pushed a history rewrite of this repository (repository maintenance; every commit SHA changed). That was not a judgment on this PR, and GitHub does not allow us to reopen it because the commits it was based on no longer exist in the new history.

If you'd like to continue with this change: rebase your branch onto the new main (or recreate it from a fresh clone) and open a new PR — feel free to link back to this one for context, and we'll pick up the review there.

Sorry for the churn, and thanks for contributing.

@luantaraschi

ghost commented Sep 1, 2026

Copy link
Copy Markdown
Author

No problem, and thanks for explaining what happened rather than leaving it to look like a silent close.

Recreated as #1179, rebased onto d4902003. Same change, with the two follow-ups from this thread already folded in: the cache count and the skip-path test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] MCP servers from .agents/mcp.json are not loaded when the project is selected via the project picker (CLI started from an ancestor directory)

6 participants