CloudBase-MCP icon indicating copy to clipboard operation
CloudBase-MCP copied to clipboard

[Bug] .cloudbase/project.json envId is ignored and binding is lost across Git worktrees

Open yikson opened this issue 3 months ago • 1 comments

🐞 Bug report

Summary

CloudBase MCP does not reliably preserve a project environment binding across new MCP processes or Git worktrees.

A repository can contain:

{
  "site": "domestic",
  "region": "ap-singapore",
  "envId": "env-project-a"
}

in .cloudbase/project.json, but a fresh MCP process still has no pinned environment and asks the agent to call auth(action="set_env") again when the account can access multiple environments.

This is especially visible in Codex worktrees: every worktree starts a new stdio MCP process, so the binding performed in the main checkout is lost.

Environment

  • @cloudbase/cloudbase-mcp: 2.32.5
  • Also reproduced by source inspection on current main: 1938cad8f9fac50c346e48d096ce2b90d80992b7
  • Client: Codex Desktop / stdio MCP
  • OS: Windows
  • Account has multiple CloudBase environments
  • Authentication: device-code account login
  • Both example environments use site=domestic, region=ap-singapore

No credentials or real environment IDs are included in this report.

Reproduction

  1. Install the official CloudBase plugin/MCP at user scope.
  2. Log in once with device-code authentication.
  3. In repository A, commit .cloudbase/project.json with:
    • site=domestic
    • region=ap-singapore
    • envId=env-project-a
  4. Call auth(action="set_env", envId="env-project-a") in the main checkout.
  5. Create a new Git worktree for the same repository and start a new Codex task there.
  6. Call a CloudBase resource tool.

Actual behavior

  • The global account credential is reusable.
  • The environment binding is not reusable.
  • The new MCP process reports an unbound/multiple-environment state and asks for set_env again.
  • Repeating this for every worktree causes repeated environment-selection prompts.
  • A separate repository B can therefore also start unbound and risks selecting repository A's environment.

Expected behavior

  • .cloudbase/project.json.envId should pin the default environment when the MCP process starts.
  • All worktrees of repository A should automatically use env-project-a.
  • Repository B, whose project file contains env-project-b, should automatically use env-project-b.
  • No repeated login or set_env call should be needed per worktree.
  • Project bindings must not leak between unrelated repositories.

Source evidence

Current source declares envId?: string in the project config type and documents .cloudbase/project.json as a site/region/envId source:

  • mcp/src/utils/project-config.ts
  • mcp/src/utils/site-map.ts

However:

  1. resolveSiteAndRegion() consumes only projectConfig.site and projectConfig.region.
  2. projectConfig.envId is not passed into getCloudBaseManager() or the environment resolution chain.
  3. EnvironmentManager._setCachedEnvId() only updates the in-memory cache and process.env.CLOUDBASE_ENV_ID.
  4. Therefore auth(set_env) is process-local and disappears when a new stdio MCP process starts.

The documentation and implementation currently disagree about .cloudbase/project.json.envId.

Confirmed workaround

A Codex project-scoped .codex/config.toml can pass the native MCP environment variables to every worktree when that file is committed:

[mcp_servers.cloudbase-mcp.env]
CLOUDBASE_ENV_ID = "env-project-a"
TCB_SITE = "domestic"
TCB_REGION = "ap-singapore"

This works without embedding credentials, but it duplicates the environment binding already declared in .cloudbase/project.json.

Suggested fix

Use one central environment resolver with this priority:

  1. Explicit cloudBaseOptions.envId
  2. CLOUDBASE_ENV_ID
  3. .cloudbase/project.json.envId
  4. Current process cache / login-state fallback
  5. Environment selection prompt

Please also add integration coverage for:

  • repository root -> new Git worktree, same .cloudbase/project.json
  • two unrelated repositories with different EnvIds
  • domestic-site ap-singapore
  • no repeated set_env after a fresh MCP process starts

If environment binding is persisted outside the repository, Git worktrees should share the binding using the Git common directory identity rather than each worktree's absolute path.

Related

This is distinct from #960. That issue covered domestic/intl site ambiguity; this issue is about the EnvId field being ignored and process-local binding being lost across worktrees.

yikson avatar Sep 02 '26 19:09 yikson

🤖 AI Fix Attempt

I created a PR for this bug: https://github.com/TencentCloudBase/CloudBase-AI-Toolkit/pull/983

Please review the generated changes before merging.

github-actions[bot] avatar Sep 03 '26 01:09 github-actions[bot]