Skip to content

feat: support ZCode official MCP auth headers and ZCODE_BASE_URL injection - #411

Merged
william0wang merged 2 commits into
william0wang:mainfrom
ggllzz04:feat/official-mcp-auth
Nov 23, 2026
Merged

william0wang merged 2 commits into
william0wang:mainfrom
ggllzz04:feat/official-mcp-auth

Conversation

@ggllzz04

Copy link
Copy Markdown
Contributor

Summary

Enable ZCode official plugin MCP servers (such as tianyancha@zcode-plugins-official and zcode-site:sites) that require auth: { type: "zcode_official", provider: "jwt_token" } and ${ZCODE_BASE_URL} URL expansion when running headless behind zcode-acp-server.

Root causes addressed

  1. Unresolved ${ZCODE_BASE_URL} in plugin .mcp.json: mergeEnvWithCreds() rightly strips any inherited ZCODE_BASE_URL (which could be a provider model API URL), leaving ZCODE_BASE_URL unset when zcode app-server resolves official plugin manifests like ${ZCODE_BASE_URL}/api/v1/mcp/server/finance_tianyancha. Those MCP servers were skipped during plugin resolution.
  2. Unhandled interaction/requestOfficialMcpAuthHeaders: When official MCP servers connect or invoke tools, zcode app-server sends a server→client interaction/requestOfficialMcpAuthHeaders request to the host. Previously this fell through to the unhandled-request path, causing official_auth_unavailable.

Changes

  • src/config/official-mcp-auth.ts:
    • resolveOfficialZCodeBaseUrl() resolves the authoritative ZCode service origin (ZCODE_ENDPOINT_ORIGIN or default https://zcode.z.ai, ignoring any inherited ZCODE_BASE_URL).
    • isTrustedOfficialMcpOrigin() validates targetOrigin before reading credentials (matching ZCode desktop's fail-closed origin check, with optional ZCODE_OFFICIAL_MCP_DEV_TRUSTED_ORIGINS loopback support).
    • resolveOfficialMcpAuthHeaders() reads zcodejwttoken and active individual Coding Plan oauth:<family>:access_token from the existing local credential store (readCredentialStoreSync) and builds Authorization, X-Bigmodel-Authorization, and Bigmodel-Target-Type: PERSONAL without logging any token values.
  • src/backend/client.ts & src/handlers/server-requests.ts & src/server.ts:
    • Wire an arrival-time officialMcpAuthHeadersResponder on ZcodeBackend (plus turn-loop / orphan fallbacks) mirroring providerRuntimeHeadersResponder.
    • Inject authoritative ZCODE_BASE_URL: resolveOfficialZCodeBaseUrl() in ensureBackend() while keeping mergeEnvWithCreds() unchanged so inherited model URLs remain stripped.
  • tests/official-mcp-auth.test.ts:
    • Add 10 unit tests covering origin validation, ZCODE_BASE_URL resolution, full coding-plan headers, identity-only fallback, missing credentials, untrusted-origin rejection, and answerOfficialMcpAuthHeaders dispatch.

Verification

  • pnpm typecheck, pnpm lint, pnpm build, and full pnpm test pass.
  • End-to-end verified against ZCode Desktop 3.18.2 (zcode 0.17.1) in an ACP client (T3 Code): plugin:tianyancha:tianyancha registered and executed search_companies, get_company_registration_info, and get_company_basic_profile successfully with zero unhandled server→client request warnings.

Copilot AI balanced review requested due to automatic review settings November 22, 2026 21:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Translate the PR's comments and test titles to English (repo convention:
code and comments are English-only) and extend the coding-plan resolution
with the upstream wire rules for team plans: a bigmodel team selection
sends Bigmodel-Target-Type: TEAM plus the atomic Bigmodel-Organization /
Bigmodel-Project pair, a zai team sends the plan token with no scope
headers, and a malformed bigmodel team selection falls back to
identity-only instead of half-sending the pair.
@william0wang

Copy link
Copy Markdown
Owner

Thanks for the clean implementation — the review found no security issues (enc:v2 decrypt path, fail-closed origin trust, arrival-time responder wiring, and ZCODE_BASE_URL injection all check out against upstream 3.18.2).

I pushed one maintainer commit (9d1c02d) addressing two findings before merge:

  1. Comments/test titles translated to English — this repo keeps code and comments English-only (mechanical change, no logic touched).
  2. Team Coding Plan support — resolveActiveCodingPlan now also accepts team-coding-plan selections and mirrors upstream officialMcpCredentials.ts wire rules: a bigmodel team sends Bigmodel-Target-Type: TEAM with the atomic Bigmodel-Organization/Bigmodel-Project pair, a zai team sends the plan token with no scope headers, and a malformed bigmodel team selection (org/project pair incomplete) falls back to identity-only instead of half-sending the pair. Three new tests cover these paths.

Verified on the branch: pnpm typecheck, pnpm lint, pnpm build, and the full suite (1306 tests) all pass.

@william0wang
william0wang merged commit af6cda8 into william0wang:main Nov 23, 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.

3 participants