Skip to content

fix(cli): reload .agents MCP servers after project picker selection - #973

Closed
bunnysayzz wants to merge 1 commit into
CodebuffAI:mainfrom
bunnysayzz:fix/mcp-config-project-picker-957
Closed

fix(cli): reload .agents MCP servers after project picker selection#973
bunnysayzz wants to merge 1 commit into
CodebuffAI:mainfrom
bunnysayzz:fix/mcp-config-project-picker-957

Conversation

@bunnysayzz

Copy link
Copy Markdown

Summary

Fixes #957. MCP servers from a project's .agents/mcp.json were only loaded once at CLI startup from the launch cwd. When the CLI is started from an ancestor directory and the project is selected via the project picker, the registry was never re-initialized, so the selected project's MCP servers stayed invisible to the main agent.

Root cause

initializeAgentRegistry() runs once at startup and calls loadMCPConfigSync(), which reads {cwd}/.agents/mcp.json. handleProjectChange in cli/src/index.tsx changes the working directory and resets the codebuff client, but never re-runs the agent registry, so mcpServersCache stays empty and loadAgentDefinitions() merges nothing into the base agents.

Changes

  • cli/src/utils/local-agent-registry.ts: new reloadLocalAgentRegistry() that clears the derived caches (agent listings and the resolved .agents directory) and re-runs initializeAgentRegistry().
  • cli/src/index.tsx: handleProjectChange now awaits reloadLocalAgentRegistry() right after process.chdir and setProjectRoot.

Verification

  • New regression test in cli/src/__tests__/integration/local-agents.test.ts reproduces the report exactly: registry initialized in a directory without mcp.json, then chdir + setProjectRoot into a project that has one, then reload. It fails on the original code (the export does not exist) and passes with the fix.
  • Local Agent Integration suite: 37/37 pass.
  • tsc --noEmit on cli/: no errors in the changed files.

The MCP servers from a project's .agents/mcp.json were only loaded once at
CLI startup from the launch cwd. When the CLI is started from an ancestor
directory and the project is selected via the project picker, the registry
was never re-initialized, so the selected project's MCP servers stayed
invisible to the main agent (issue CodebuffAI#957).

handleProjectChange now calls reloadLocalAgentRegistry() after chdir +
setProjectRoot, which clears the derived caches and re-runs
initializeAgentRegistry() so user agents and MCP servers reflect the newly
selected working directory.

Co-Authored-By: Codebuff <noreply@codebuff.com>
@chrismindpower369

Copy link
Copy Markdown

Thanks for the clear root-cause analysis and cache-reload approach. PR #966 appears to solve the same #957 bug and tests the project-picker path through activateProject(), including propagation into the base agent definition. It may be best to consolidate the cache-reset details here into #966 and close one PR as a duplicate, rather than merge both.

@bunnysayzz

Copy link
Copy Markdown
Author

Closing this as a duplicate of #966, which predates it and covers the same #957 fix with the same approach (reload the local agent registry after project selection, regression test included, CI green). No need to keep two competing PRs for one issue; #966 is the one to review.

@bunnysayzz bunnysayzz closed this Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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)

2 participants