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

[Bug] Codex device auth READY but resource tools return AUTH_REQUIRED for domestic ap-singapore env

Open yikson opened this issue 3 months ago • 1 comments

Summary

With @cloudbase/[email protected] in Codex, device authentication reports READY, but resource/control-plane tools return AUTH_REQUIRED after binding a domestic-site CloudBase environment whose region is ap-singapore.

Adding an explicit project config with site: "domestic" immediately fixes all resource tools without another login:

{
  "site": "domestic",
  "region": "ap-singapore",
  "envId": "<redacted-env-id>"
}

This looks like a site-resolution / credential-slot regression for the region shared by both domestic and international sites.

Environment

  • Host: Windows, Codex desktop/native plugin
  • CloudBase plugin wrapper: 0.2.0
  • MCP package: @cloudbase/[email protected]
  • npm package gitHead: 0f6b73dcc952fe518e31ed6346ae87521b697309
  • Node.js: v24.19.0
  • npm: 12.0.2
  • CloudBase site: domestic
  • CloudBase region: ap-singapore
  • tcb CLI was not installed or used

No credentials, account IDs, environment IDs, device codes, request IDs, or user identifiers are included in this report.

Steps to reproduce

  1. Start the local MCP server through the Codex plugin:
    {
      "command": "npx",
      "args": ["-y", "@cloudbase/cloudbase-mcp@latest"]
    }
    
  2. Use a domestic-site Tencent Cloud account with a CloudBase environment in ap-singapore.
  3. Do not create .cloudbase/project.json and do not set TCB_SITE.
  4. Run device authentication:
    auth(action="start_auth", authMode="device")
    
  5. Complete the browser authorization.
  6. Bind the known environment:
    auth(action="set_env", envId="<redacted-env-id>")
    
  7. Confirm auth status:
    {
      "auth_status": "READY",
      "credential_scope": "account",
      "current_region": "ap-singapore",
      "current_env_id": "<redacted-env-id>"
    }
    
  8. Call either:
    envQuery(action="info", envId="<redacted-env-id>")
    callCloudApi(service="tcb", action="DescribeEnvs", region="ap-singapore")
    

Actual behavior

Both resource/control-plane calls return:

{
  "ok": false,
  "code": "AUTH_REQUIRED",
  "message": "当前未登录,请先调用 auth 工具完成认证。"
}

Restarting Codex, explicitly rebinding the environment, logging out, and completing a fresh device authorization do not fix the issue.

Expected behavior

After auth(status) reports READY and set_env succeeds, resource tools should use the same valid credential and access the bound domestic-site Singapore environment.

If the site cannot be inferred safely, the tool should return an actionable SITE_REQUIRED / ambiguous-site error instead of incorrectly reporting that the user is not logged in.

Verified workaround

Create .cloudbase/project.json:

{
  "site": "domestic",
  "region": "ap-singapore",
  "envId": "<redacted-env-id>"
}

The next envQuery(action="info", ...) call succeeds immediately in the same MCP process and returns the expected environment state. No new login and no tcb CLI are required.

Source-level analysis

The behavior matches the published 2.32.2 source at 0f6b73dc...:

  1. Credentials are split into domestic / intl slots; legacy flat credentials are treated as domestic:
  2. The device branch of auth(start_auth) calls the toolbox loginByWebAuth() directly instead of the ensureLogin() path that performs site-slot merging:
  3. set_env applies the matched region, but does not persist a site:
  4. ap-singapore belongs to both sites; when site is unspecified, the resolver defaults the ambiguous region to intl:
  5. Resource managers read the site-specific login state and throw AUTH_REQUIRED when the inferred intl slot is empty:
  6. The project config workaround is an explicitly supported source of site, region, and envId:

Possible fixes

  • Persist the resolved/authenticated site together with the region during set_env.
  • Route the device-auth branch through the same site-slot merge used by ensureLogin(), or explicitly store the returned credential in the resolved site slot.
  • When ap-singapore is ambiguous and exactly one credential slot is usable, prefer that slot or return a structured ambiguous-site diagnostic.
  • Add a regression test for: domestic account + ap-singapore + device auth + set_env + envQuery(info).

yikson avatar Aug 26 '26 10:08 yikson

Thank you for this exceptionally detailed report 🙏 — especially the source-level analysis, which located the exact chain correctly and saved us a lot of investigation time.

We have verified every point against the latest main branch and confirmed the bug is real. The root cause matches your analysis:

  1. The device-flow branch calls toolbox's loginByWebAuth() directly, bypassing the site-slot merge inside ensureLogin(), so the credential stays in flat format (equivalent to the domestic slot)
  2. set_env only persists TCB_REGION and never writes any site information
  3. ap-singapore exists in the region lists of both domestic and intl sites, and resolveSite() defaults ambiguous regions to intl (to preserve existing international-site behavior)
  4. Resource tools resolve site=intl via getCloudBaseManager() → resolveSiteAndRegion(), find the intl slot empty, and incorrectly report AUTH_REQUIRED
  5. Meanwhile auth(status) calls peekLoginState() with no arguments, which defaults to the domestic slot, finds the flat credential, and reports READY

Root cause in one sentence: status reads the domestic slot while resource tools read the intl slot — the two sides disagree.

We adopt all of your proposed fix directions and plan to fix it as follows:

  • Persist the resolved site along with the region when set_env binds an environment (eliminating ambiguity for subsequent calls)
  • Merge the credential into the correct site slot after device-flow login completes (aligning with the ensureLogin() merge behavior)
  • When the region is ambiguous and only one slot has a usable credential, prefer that slot; when both slots are usable, return a structured ambiguity error (SITE_REQUIRED) instead of incorrectly reporting "not logged in"
  • Add a regression test: domestic account + ap-singapore + device auth + set_env + envQuery(info)

Thanks again — the workaround verification (explicit site: "domestic" in .cloudbase/project.json) was very valuable to us.

binggg avatar Aug 26 '26 11:08 binggg