[Bug] .cloudbase/project.json envId is ignored and binding is lost across Git worktrees
🐞 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
- Install the official CloudBase plugin/MCP at user scope.
- Log in once with device-code authentication.
- In repository A, commit
.cloudbase/project.jsonwith:site=domesticregion=ap-singaporeenvId=env-project-a
- Call
auth(action="set_env", envId="env-project-a")in the main checkout. - Create a new Git worktree for the same repository and start a new Codex task there.
- 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_envagain. - 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.envIdshould 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 useenv-project-b. - No repeated login or
set_envcall 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.tsmcp/src/utils/site-map.ts
However:
resolveSiteAndRegion()consumes onlyprojectConfig.siteandprojectConfig.region.projectConfig.envIdis not passed intogetCloudBaseManager()or the environment resolution chain.EnvironmentManager._setCachedEnvId()only updates the in-memory cache andprocess.env.CLOUDBASE_ENV_ID.- 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:
- Explicit
cloudBaseOptions.envId CLOUDBASE_ENV_ID.cloudbase/project.json.envId- Current process cache / login-state fallback
- 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_envafter 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.
🤖 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.