Skip to content

fix(cli): reload MCP servers and skills after project change in picker - #1328

Open
sohailnajar147 wants to merge 1 commit into
CodebuffAI:mainfrom
sohailnajar147:fix/reload-mcp-skills-project-picker
Open

fix(cli): reload MCP servers and skills after project change in picker#1328
sohailnajar147 wants to merge 1 commit into
CodebuffAI:mainfrom
sohailnajar147:fix/reload-mcp-skills-project-picker

Conversation

@sohailnajar147

Copy link
Copy Markdown

Summary

Fixes #957.

When the CLI is started outside a project (such as in ~) and a project is selected through the Project Picker, project-specific MCP servers and skills were not loaded because the registries remained initialized from the original working directory.

This fix reloads both initializeAgentRegistry() and initializeSkillRegistry() after changing projects, adds debug logging, and clears the cached local-agent state so the new project's .agents/ configuration is picked up correctly.

Changes

  • Reload registries: In cli/src/index.tsx, invoke initializeAgentRegistry() and initializeSkillRegistry() after updating the project root in handleProjectChange.

  • Cache invalidation: In cli/src/utils/local-agent-registry.ts, reset cachedAgentsDir and cachedAgentsByMode when reinitializing so directory lookups and the @ menu don't serve stale cache from the launch directory.

  • Observability: Added a debug log confirming MCP servers and skills were reloaded for the project.

  • Regression tests: Added cli/src/__tests__/integration/project-change-reload.test.ts covering bug reproduction, MCP and skill reloading, project-to-project switching, and cache clearing.

Testing

Ran the new and affected test suites:

bun test cli/src/__tests__/integration/project-change-reload.test.ts
bun test cli/src/__tests__/integration/local-agents.test.ts
bun test cli/src/commands/__tests__/skill-command.test.ts
bun test sdk/src/__tests__/load-mcp-config.test.ts

Result: 58 tests passed.

Also manually verified the original issue before and after the fix, including switching between projects with different .agents/ configurations.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good bug fix with a clear root cause: handleProjectChange in cli/src/index.tsx updated projectRoot but never re-ran initializeAgentRegistry()/initializeSkillRegistry(), so switching projects from a non-project cwd (e.g. ~) left the old project's .agents/ config (or none) cached. The two-line cache-clear in local-agent-registry.ts (cachedAgentsByMode.clear(), cachedAgentsDir = null) at the top of initializeAgentRegistry() correctly forces a fresh directory lookup instead of serving stale paths.

The new integration test project-change-reload.test.ts is thorough: it reproduces the bug first (asserting the broken behavior without the fix), then verifies the fix, project-to-project switching, and cache invalidation for custom agents. That's exactly the kind of test coverage this kind of fix needs.

A couple of things worth double-checking before porting:

  1. initializeAgentRegistry/initializeSkillRegistry are called in index.tsx without an import diff shown — confirm they're already imported at the top of that file (likely, since they're used at startup) so this isn't a silent compile break.
  2. Consider whether reloading synchronously in the picker's change handler introduces noticeable UI latency on large .agents/ trees — worth a quick manual check, though probably fine given it already happens at startup.
  3. The debug log is a reasonable addition for observability.

Overall: correct diagnosis, minimal fix, in-scope files, good tests. Recommending as a port candidate.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Sep 12, 2026
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)

2 participants