From 642b61dc9f28f414422f0b1898d164d10f6aa557 Mon Sep 17 00:00:00 2001 From: uncle6me-web Date: Sun, 14 Jun 2026 22:45:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(cli):=20deploy=20=E6=B3=A8=E5=85=A5=20MULTI?= =?UTF-8?q?=5FTENANT=3Dfalse=20=E5=88=B0=20self-hosted=20worker=EF=BC=88?= =?UTF-8?q?=E4=BF=AE=20MCP=20401=20=E6=B3=A8=E5=85=A5=E7=BC=BA=E5=8F=A3?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因(非 code bug):partner-auth.ts MULTI_TENANT 分支邏輯對,但部署沒注入 → worker c.env.MULTI_TENANT===undefined → 走 partner-key → self-hosted 401。 mcp/wrangler.toml 的 MULTI_TENANT 原是註解掉的,injectWranglerConfig 注了 KV/WORKER_SUBDOMAIN/D1 卻漏 MULTI_TENANT。只取消註解不夠(只修手動 fork, 沒修 acr update 自動部署這條 mira 走的路)。 修法(方案①,注 vars 非 secret,self-hosted 零填寫): - deploy.ts:DeployContext 加 selfHosted;新增 injectMultiTenant(active/註解/無行三態 → 加進 [vars]);injectWranglerConfig 在 selfHosted 時呼叫。 - init.ts:deployCtx selfHosted:true(本就是 --self-hosted 分支)。 - update.ts:ctx selfHosted = mode==='self-hosted' || multi_tenant===false(mira 走這條)。 - mcp/wrangler.toml:# [vars] 改 active [vars](官方不含 MULTI_TENANT=多租戶; 注入加行在 [vars] 下,結構正確)。 本地驗注入(真實 export 函式 dry-run):mcp/cypher 注入後各 1 行 active MULTI_TENANT="false" 在 active [vars] 下 → PASS。cli tsc exit 0。 端到端交棒 mira:leo21c 重跑 acr update → curl Bearer leo /mcp 應 200。 SDD: mcp-account-source.md §5.5.1。 Co-Authored-By: Claude Opus 4.8 (1M context) --- cli/src/commands/init.ts | 4 +++- cli/src/commands/update.ts | 3 +++ cli/src/lib/deploy.ts | 39 ++++++++++++++++++++++++++++++++++++++ mcp/wrangler.toml | 14 +++++++------- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/cli/src/commands/init.ts b/cli/src/commands/init.ts index e0ccbd2..f1321d0 100644 --- a/cli/src/commands/init.ts +++ b/cli/src/commands/init.ts @@ -250,7 +250,9 @@ async function initSelfHosted( // 4. 下載 repo 部署物(含預編譯 wasm)+ 注入 KV id + wrangler deploy 全部 Worker console.log(chalk.gray('\n → 下載部署物 + 部署 Worker(從 GitHub 拉預編譯 wasm,用你的 CF token 部署)...')); - const deployCtx: DeployContext = { accountId, apiToken: cfApiToken, workerSubdomain, kvNamespaceIds, d1DatabaseId }; + // selfHosted: true → deploy 注入 MULTI_TENANT="false"(mcp-account-source §5.5,修 MCP 401)。 + // init.ts 這條本就是 --self-hosted 分支(config.mode 稍後寫 'self-hosted')。 + const deployCtx: DeployContext = { accountId, apiToken: cfApiToken, workerSubdomain, kvNamespaceIds, d1DatabaseId, selfHosted: true }; const deploy = await downloadAndDeploy(deployCtx); const cypherUrl = deploy.cypherExecutorUrl ?? (workerSubdomain ? `https://arcrun-cypher-executor.${workerSubdomain}.workers.dev` : ''); diff --git a/cli/src/commands/update.ts b/cli/src/commands/update.ts index f409cbd..5fbdca5 100644 --- a/cli/src/commands/update.ts +++ b/cli/src/commands/update.ts @@ -79,6 +79,9 @@ export async function cmdUpdate(opts: { force?: boolean } = {}): Promise { workerSubdomain: extractSubdomain(config.cypher_executor_url), kvNamespaceIds, d1DatabaseId: d1DatabaseId || undefined, + // self-hosted → 注入 MULTI_TENANT="false"(mcp-account-source §5.5,修 acr update 部署的 MCP 401)。 + // config 源頭:init 寫 multi_tenant:false + mode:'self-hosted'。acr update 只在 self-hosted 跑。 + selfHosted: config.mode === 'self-hosted' || config.multi_tenant === false, }; const result = await downloadAndDeploy(ctx, 'main', { force: opts.force }); diff --git a/cli/src/lib/deploy.ts b/cli/src/lib/deploy.ts index 3a5328c..854bece 100644 --- a/cli/src/lib/deploy.ts +++ b/cli/src/lib/deploy.ts @@ -98,6 +98,10 @@ export interface DeployContext { workerSubdomain: string; kvNamespaceIds: Record; // title → id d1DatabaseId?: string; // KBDB Base D1 (arcrun-kbdb); injected into kbdb wrangler.toml + // self-hosted 單租戶旗標。true(self-hosted)→ 注入 MULTI_TENANT="false" 到 worker [vars], + // 讓 MCP partner-auth 走 namespace 明碼分支(mcp-account-source §5.5)。 + // 未設 / false → 不注入(官方 SaaS 多租戶,行為不變)。 + selfHosted?: boolean; } export interface DeployResult { @@ -388,11 +392,46 @@ function injectWranglerConfig(tomlPath: string, ctx: DeployContext): void { ); } + // self-hosted:注入 MULTI_TENANT="false" 到 [vars](mcp-account-source §5.5)。 + // 修「部署沒注入 → worker c.env.MULTI_TENANT===undefined → MCP 走 partner-key → 401」。 + // 只對有 [vars] 的 worker(mcp / cypher-executor)生效;其餘無 [vars] 的不動。 + if (ctx.selfHosted) { + toml = injectMultiTenant(toml); + } + toml = stripOfficialOnlyBindings(toml); writeFileSync(tomlPath, toml, 'utf8'); } +/** + * self-hosted:確保 worker [vars] 有 `MULTI_TENANT = "false"`。處理三種既有狀態: + * 1. 已有 active `MULTI_TENANT = "..."` → 改成 "false" + * 2. 有註解的 `# MULTI_TENANT = "false"`(mcp/cypher toml 預設這樣)→ 取消註解 + * 3. 無此行但有 `[vars]` → 在 [vars] header 下一行加進去 + * 4. 無 `[vars]`(該 worker 不吃此 var)→ 不動 + * 純文字操作,與 WORKER_SUBDOMAIN/KV 注入同層級(mcp-account-source §5.5)。 + */ +export function injectMultiTenant(toml: string): string { + // 1. 已有 active 行 → 設 false + if (/^\s*MULTI_TENANT\s*=/m.test(toml)) { + return toml.replace(/^(\s*MULTI_TENANT\s*=\s*")[^"]*(".*)$/m, `$1false$2`); + } + // 2. 註解掉的行 → 取消註解(保留原縮排) + if (/^\s*#\s*MULTI_TENANT\s*=/m.test(toml)) { + return toml.replace(/^(\s*)#\s*(MULTI_TENANT\s*=\s*)"[^"]*"(.*)$/m, `$1$2"false"$3`); + } + // 3. 有 [vars] → 在其後插入 + if (/^\s*\[vars\]\s*$/m.test(toml)) { + return toml.replace( + /^(\s*\[vars\]\s*)$/m, + `$1\nMULTI_TENANT = "false" # self-hosted 單租戶(acr update 注入,mcp-account-source §5.5)`, + ); + } + // 4. 無 [vars] → 不動(該 worker 不用此 var) + return toml; +} + /** * 移除 self-hosted fork 帳號沒有、會導致 wrangler deploy 失敗的官方專屬 TOML 區塊: * - `[[routes]]`(含 pattern/zone_name):fork 沒有 arcrun.dev zone diff --git a/mcp/wrangler.toml b/mcp/wrangler.toml index dc13bfa..eb30ccb 100644 --- a/mcp/wrangler.toml +++ b/mcp/wrangler.toml @@ -5,13 +5,13 @@ compatibility_flags = [ "nodejs_compat" ] workers_dev = true # 對齊 arcrun 部署慣例(rule 05):deploy 掃描自動啟用 workers.dev URL # ── 租戶模式(self-hosted fork 必看)───────────────────────────────────────────── -# 官方 SaaS:不設 MULTI_TENANT(預設多租戶)→ MCP 走 partner-key 驗證(pk_live)。 -# self-hosted 單租戶:fork 後設 MULTI_TENANT = "false" → MCP 接受 Bearer = namespace 明碼, -# 不打 KBDB partner 驗證,直接當 org_namespace(與 cypher-executor 的 MULTI_TENANT=false 對齊)。 -# self-hosted 用戶用 namespace 明碼即可連 MCP(不需平台 partner key)。 -# SDD: sdk-and-website/mcp-account-source.md(self-hosted 認證);HANDOFF §3b。 -# [vars] -# MULTI_TENANT = "false" +# 官方 SaaS:[vars] 不含 MULTI_TENANT(預設多租戶)→ MCP 走 partner-key 驗證(pk_live)。 +# self-hosted 單租戶:acr init/update 部署時 **自動注入** MULTI_TENANT="false" 進此 [vars] +# (cli/src/lib/deploy.ts injectMultiTenant,依 config.mode='self-hosted')→ MCP 接受 Bearer = +# namespace 明碼,不打 KBDB partner 驗證,直接當 org_namespace(與 cypher 的 MULTI_TENANT=false 對齊)。 +# 用戶零填寫(不必手動取消註解)。手動 fork 不走 CLI 者,自行在此加 MULTI_TENANT = "false"。 +# SDD: sdk-and-website/mcp-account-source.md §5.5;HANDOFF §3b。 +[vars] # Service Bindings # 2026-05-07:COMPONENT_REGISTRY 從 inkstone-component-registry 改為 arcrun-registry