Kanji
・雲端架構工程師 / 自由職業 ・1993年出生 ・愛媛縣出身 / 現居東京都澀谷區 ・5年 AWS 實戰經驗 個人檔案詳情
目錄
在 Cloud Run 上運行自主型 AI 代理(如 Antigravity、Claude Code、OpenCode 或基於 LangGraph 的代理平台),並將其與暴露內部資料庫和維運工具的遠端 MCP(Model Context Protocol)伺服器對接時,第一線工程師必定會遇到一個痛點:「認證樣板程式碼對業務邏輯的侵入與污染」。
在使用 Google Cloud IAM 進行零信任存取控制時,呼叫端(用戶端)必須請求中繼資料伺服器( http://metadata.google.internal/... )取得 OIDC ID 權杖,並手動在請求標頭中注入 Authorization: Bearer <ID_TOKEN> 。
http://metadata.google.internal/...
Authorization: Bearer <ID_TOKEN>
然而,從工程實務角度來看,這種模式存在三大痛點:
https://my-mcp-backend-abc123-an.a.run.app
google-auth
google-auth-library
為了將認證邏輯徹底剝離到應用程式碼之外,Google Cloud 在 2026 年 11 月推出了 Cloud Run 預覽版新功能 「Ambient Networking」 與 「Express Links」 。
▲ 基於 Cloud Run Ambient Networking 與 Express Links 的自主型 AI 代理服務網格整體架構
Ambient Networking 是 Cloud Run 的一項基礎設施能力,它會在容器執行個體內部自動注入由 Google 全代管的 Envoy 邊車代理(Sidecar Proxy) 。
開發者無需在 Dockerfile 中撰寫 Envoy 設定檔,也無需像 Kubernetes Istio 那樣維護繁瑣的邊車注入清單。只需一個 gcloud 旗標,透明代理層就會隨容器執行個體一同啟動,並接管進出容器的網路流量。
gcloud
Express Links 以 Ambient Networking 的代管 Envoy 邊車作為資料平面, 完全自動化 Cloud Run 服務間的路由與 Google Cloud IAM 認證(OIDC ID 權杖注入) 。
呼叫端 AI 代理完全不需要感知目標服務的實體 URL 或 ID 權杖的存在,只需透過本地純 HTTP 向 http://mcp-backend/mcp 這樣的抽象邏輯主機名稱發起請求。
http://mcp-backend/mcp
Envoy 邊車會攔截該通訊,在背景向中繼資料伺服器取得帶有正確受眾(Audience,即目標 Cloud Run 服務 URL)的 OIDC ID 權杖,自動加上 Authorization 標頭,並將連線透明升級為 HTTPS 轉發給目標服務。
Authorization
https://*.a.run.app
http://mcp-backend
以下時序圖展示了在用戶端徹底移除認證程式碼後,請求是如何在底層安全中繼的:
該流程最大的安全優勢在於: 「ID 權杖字串甚至不會進入代理應用容器的行程空間」 。即便 AI 代理遭受提示詞注入(Prompt Injection)攻擊導致環境變數或行程記憶體被傾印,用於跨服務呼叫的 ID 權杖也僅存在於獨立的 Envoy 邊車記憶體中,進而大幅降低橫向移動(Lateral Movement)風險。
下面完整重現我在驗證環境中建置該架構的操作步驟。
agent-worker
sa-agent@my-project.iam.gserviceaccount.com
mcp-filesystem
asia-northeast1
使用 --ambient-networking 與 --ambient-scope=global 更新來源服務版本:
--ambient-networking
--ambient-scope=global
gcloud beta run services update agent-worker \ --region=asia-northeast1 \ --ambient-networking \ --ambient-scope=global \ --project=my-project
--matches-hostname
接著使用 gcloud beta network-services express-links create 指令建立 Express Link 路由定義。
gcloud beta network-services express-links create
這裡最關鍵的參數是 --matches-hostname 旗標 。預設情況下,Express Links 攔截發往目標服務確定性 URL 的流量;而明確指定 --matches-hostname="mcp-backend" 後,即可將該連結的目標映射替換為抽象的自訂邏輯主機名稱(不含協定頭的 mcp-backend )。
--matches-hostname="mcp-backend"
mcp-backend
63 字元 DNS 標籤上限與 --matches-hostname 的強制條件 : 當目標 Cloud Run 服務名稱與 Google Cloud 專案編號拼接後的預設主機名稱首段標籤超過 63 個字元 時,若不設定 --matches-hostname ,建立 Express Link 會因 DNS 標籤長度檢驗失敗而報錯。在微服務和遠端 MCP 架構中,透過 --matches-hostname 綁定簡短的邏輯主機名稱是正式環境的必備最佳實踐。
gcloud beta network-services express-links create link-agent-to-mcp \ --location=global \ --source-services="//run.googleapis.com/projects/my-project/locations/asia-northeast1/services/agent-worker" \ --destination-services="//run.googleapis.com/projects/my-project/locations/asia-northeast1/services/mcp-filesystem" \ --matches-hostname="mcp-backend" \ --project=my-project
透過 describe (或 export )檢視建立好的 Express Link,可以確認 sourceServices 、 destinationServices 與 matchesHostname 已在全域作用域生效:
describe
export
sourceServices
destinationServices
matchesHostname
# gcloud beta network-services express-links describe link-agent-to-mcp --location=global name: "projects/my-project/locations/global/expressLinks/link-agent-to-mcp" sourceServices: - "//run.googleapis.com/projects/my-project/locations/asia-northeast1/services/agent-worker" destinationServices: - "//run.googleapis.com/projects/my-project/locations/asia-northeast1/services/mcp-filesystem" matchesHostname: - "mcp-backend" state: ACTIVE
roles/run.invoker
在目標服務 mcp-filesystem 上,向呼叫端服務的執行階段服務帳戶 sa-agent 授予呼叫權限:
sa-agent
gcloud run services add-iam-policy-binding mcp-filesystem \ --region=asia-northeast1 \ --member="serviceAccount:sa-agent@my-project.iam.gserviceaccount.com" \ --role="roles/run.invoker" \ --project=my-project
在傳統方式下,必須引入 google-auth-library 並在每次請求時取得 ID 權杖拼接標頭:
import { GoogleAuth } from 'google-auth-library'; import fetch from 'node-fetch'; const auth = new GoogleAuth(); const targetUrl = process.env.MCP_SERVER_URL!; // https://mcp-filesystem-abc123-an.a.run.app async function callMcpTool(method: string, params: Record<string, unknown>) { // 1. 從 GCP 中繼資料伺服器取得 OIDC ID 權杖 const client = await auth.getIdTokenClient(targetUrl); const headers = await client.getRequestHeaders(targetUrl); // 2. 手動附加 Authorization 標頭並發起 HTTPS 請求 const response = await fetch(`${targetUrl}/mcp`, { method: 'POST', headers: { ...headers, 'Content-Type': 'application/json', 'Mcp-Method': method, }, body: JSON.stringify(params), }); return await response.json(); }
引入 Express Links 後,徹底不再需要 Google Cloud SDK。只需使用標準 fetch 或通用 MCP SDK 用戶端向邏輯主機名稱發送純 HTTP 請求:
fetch
// 無需匯入 google-auth-library! const MCP_ENDPOINT = 'http://mcp-backend/mcp'; // Express Link 映射的邏輯主機名稱 async function callMcpTool(method: string, params: Record<string, unknown>) { // 權杖取得、標頭注入與 HTTPS 升級全部由代管邊車自動完成 const response = await fetch(MCP_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Mcp-Method': method, }, body: JSON.stringify(params), }); return await response.json(); }
這不僅大幅精簡了程式碼,還讓本地開發環境的 Mock 替換(如 localhost:8080 )和 CI/CD 整合測試變得極其簡單。
localhost:8080
▲ 遠端驗證環境中的 gcloud 指令執行結果與 Express Links 路由設定實機驗證日誌
在實際建置過程中,我遇到了三個非常典型的陷阱,在此分享排障紀錄。
ENOTFOUND
初次設定時,我在 gcloud beta run services update 中只加上了 --ambient-networking ,省略了作用域旗標(預設值為 regional )。
gcloud beta run services update
regional
此時從容器內部請求 http://mcp-backend/mcp ,立即拋出如下 DNS 解析失敗錯誤:
FetchError: request to http://mcp-backend/mcp failed, reason: getaddrinfo ENOTFOUND mcp-backend at ClientRequest.<anonymous> (/app/node_modules/node-fetch/lib/index.js:1491:11) at Socket.emit (node:events:517:28) at emitErrorNT (node:internal/streams/destroy:151:8)
原因與對策 : Express Links 目前作為全域資源( locations/global )進行管理。因此呼叫端 Cloud Run 服務必須明確指定 --ambient-scope=global ,否則代管 Envoy 無法攔截自訂邏輯主機名稱,請求會回退到容器的 Linux DNS 解析器向外部查詢並失敗。
locations/global
在設定權限時,如果不小心將 roles/run.invoker 授予了目標服務 mcp-filesystem 自身的服務帳戶,呼叫將直接被 403 Forbidden 拒絕:
403 Forbidden
HTTP/1.1 403 Forbidden content-type: text/html; charset=UTF-8 date: Wed, 27 Nov 2026 06:15:22 GMT content-length: 315 <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> Your client does not have permission to get URL <code>/mcp</code> from this server. </body></html>
在 Express Links 機制下, 呼叫端服務( agent-worker )的執行階段服務帳戶才是 OIDC 權杖的主體( sub ) ,因此務必將 roles/run.invoker 綁定給呼叫端服務帳戶( sa-agent )。
sub
當 MCP 工具執行耗時較長的推論或批次任務時,若沿用舊版長連線 SSE 模式,容易觸發 Cloud Run 預設逾時(300 秒)或 Envoy 閒置連線逾時。正式環境中強烈建議結合 MCP 2026-07-28 規範推薦的無狀態 MRTR(Multi Round-Trip Requests)模式。
我實測了代管邊車常駐帶來的資源與延遲開銷:
512MiB
1GiB
2GiB
▲ 終端機實機驗證介面:作用域遺漏(ENOTFOUND)與 IAM 主體錯誤(403 Forbidden)重現,以及 Express Links 1,000 次請求 RTT 基準測試(平均 +1.4ms)
Cloud Run 的「Ambient Networking × Express Links」不僅是一次網路功能升級。隨著企業內部遠端 MCP 伺服器擴展到數十甚至上百個,由基礎設施層透明接管零信任認證、讓應用程式碼回歸純粹的協定與業務邏輯,將成為企業級 AI 代理服務網格的標準範式。
Real-time AI quant analysis of corporate earnings reports.
提供逼真 AI 模擬通話與多維反饋,結合 16 講實戰社交學院與工作室級 AI 肖像生成,全面提升社交互動品質。
拍發票即可完成高精度智慧拆單,支援 LINE 登入無縫結算團隊聚會支出,告別繁雜帳單分攤。
占卜師、管家、調酒師等 AI 角色即時引導話題與聚會遊戲,讓線下與線上聚餐氛圍瞬間升溫。
人工甘味料や添加物のNG除外と有効成分あたりの実質単価比較データベース。