Model Context Protocol (MCP) 可將大型語言模型 (LLM) 和 AI 應用程式/代理程式與外部資料來源的連線方式標準化。MCP 伺服器可讓您使用工具、資源和提示,從後端服務執行動作及取得更新資料。
本機 MCP 伺服器通常會在您的本機電腦上執行,並使用標準輸入和輸出串流 (stdio),在同一部裝置上的服務之間進行通訊。遠端 MCP 伺服器會在服務的基礎架構上執行,並為 AI 應用程式提供 HTTP 端點,供 AI MCP 用戶端與 MCP 伺服器通訊。如要進一步瞭解 MCP 架構,請參閱 MCP 架構。
本文說明如何使用 GKE 遠端模型內容通訊協定 (MCP) 伺服器,從 Gemini CLI、Gemini Code Assist 的代理程式模式、Claude Code 或您開發的 AI 應用程式等 AI 應用程式連線至 GKE。
如要瞭解 GKE 本機 MCP 伺服器,請參閱 GitHub 上的 GKE MCP 伺服器。
Google 和 Google Cloud 遠端 MCP 伺服器具備下列功能和優點:
- 簡化集中式探索作業。
- 管理全域或區域 HTTP 端點。
- 精細授權。
- 可選用 Model Armor 防護功能,確保提示詞和回覆安全無虞。
- 集中式稽核記錄。
如要瞭解其他 MCP 伺服器,以及 Google Cloud MCP 伺服器適用的安全性與控管措施,請參閱 Google Cloud MCP 伺服器總覽。
您可能基於下列原因而使用 GKE 本機 MCP 伺服器:
- 在本機開發及測試
- 離線使用 MCP
- 建立叢集和工作負載,包括產生 AI/機器學習工作負載的資訊清單
- 本機用戶端設定 (使用
kubeconfig) - 查詢記錄
- 取得 GKE 環境的成本和安全性建議
如要進一步瞭解如何使用本機 MCP 伺服器,請參閱「GKE MCP 伺服器」。下列章節僅適用於 GKE 遠端 MCP 伺服器。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Kubernetes Engine API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable container.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/container.clusterViewergcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Kubernetes Engine API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable container.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/container.clusterViewergcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
- 機構政策管理員 (
roles/orgpolicy.policyAdmin):在 Google Cloud 機構中授予此角色,即可建立自訂機構政策。 - 服務使用情形管理員 (
roles/serviceusage.serviceUsageAdmin):在 Google Cloud 專案中授予此角色,即可啟用遠端 MCP 服務端點。這個角色具備serviceusage.mcppolicy.get和serviceusage.mcppolicy.update權限。 - MCP 工具使用者 (
roles/mcp.toolUser): 授予權限,可對 MCP 伺服器端點發出工具呼叫。 - Kubernetes Engine 叢集檢視者 (
roles/container.clusterViewer):這個角色提供遠端伺服器工具所需的唯讀存取權。 - 使用者帳戶:使用者透過 Gemini CLI 等用戶端與 MCP 伺服器互動時。
- 建構呼叫 MCP 伺服器的自主代理程式或應用程式時,請使用服務帳戶。
- 伺服器名稱:GKE 遠端 MCP 伺服器
- 伺服器網址或端點:https://container.googleapis.com/mcp
- 傳輸:HTTP
- 驗證詳細資料:視驗證方式而定,您可以輸入 Google Cloud 憑證、OAuth 用戶端 ID 和密鑰,或是代理程式身分和憑證。如要進一步瞭解驗證,請參閱「向 MCP 伺服器進行驗證」。
- 檢查 GKE 叢集和節點集區的設定和狀態。舉例來說,使用提示:「Show me the details of my 'production-cluster' and list all of its node pools.」(顯示「production-cluster」的詳細資料,並列出所有節點集區)。
- 在叢集內查看 Kubernetes 資源設定和容器記錄,不必使用 kubectl。舉例來說,您可以使用以下提示:「Get the YAML for the 'frontend-deployment' in the 'default' namespace.」(取得「default」命名空間中「frontend-deployment」的 YAML)。
- 監控長時間執行的 GKE 作業狀態,例如叢集升級。舉例來說,使用提示:「列出我專案中過去一小時內的所有 GKE 作業。」
如要在Google Cloud 專案中啟用 Model Armor,請執行下列 gcloud CLI 指令:
gcloud services enable modelarmor.googleapis.com \ --project=PROJECT_ID將
PROJECT_ID替換為您的Google Cloud 專案 ID。如要設定建議的 Model Armor 底層設定,請執行下列 gcloud CLI 指令:
gcloud model-armor floorsettings update \ --full-uri='projects/PROJECT_ID/locations/global/floorSetting' \ --mcp-sanitization=ENABLED \ --malicious-uri-filter-settings-enforcement=ENABLED將
PROJECT_ID替換為您的Google Cloud 專案 ID。Model Armor 會掃描惡意網址嘗試。
如要進一步瞭解可設定的 Model Armor 篩選器,請參閱「Model Armor 篩選器」。
如要將 Model Armor 新增為 MCP 服務的內容安全防護供應商,請執行下列 gcloud CLI 指令:
gcloud beta services mcp content-security add modelarmor.googleapis.com \ --project=PROJECT_ID將
PROJECT_ID替換為Google Cloud 專案 ID。如要確認 MCP 流量是否傳送至 Model Armor,請執行下列指令:
gcloud beta services mcp content-security get \ --project=PROJECT_ID將
PROJECT_ID替換為Google Cloud 專案 ID。- 請參閱 GKE 遠端 MCP 參考說明文件。
- 進一步瞭解 Google Cloud MCP 伺服器。
必要的角色
如要執行一次性設定,啟用 GKE 遠端 MCP 伺服器,管理員必須具備下列角色:
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
使用這項服務的角色
主體必須具備存取 GKE 資源的權限,才能呼叫遠端 MCP 伺服器工具。這個主體可以是真人使用者或自動化服務帳戶。至少要在Google Cloud 專案中授予下列角色:
將這個角色授予:
啟用或停用 GKE 遠端 MCP 伺服器
您可以使用 gcloud beta services mcp enable 指令,在專案中啟用或停用 GKE 遠端 MCP 伺服器。詳情請參閱以下各節。
在專案中啟用 GKE 遠端 MCP 伺服器
如果您使用不同的專案做為用戶端憑證 (例如服務帳戶金鑰、OAuth 用戶端 ID 或 API 金鑰) 和資源主機,則必須在這兩個專案中啟用 GKE 服務和 GKE 遠端 MCP 伺服器。
如要在Google Cloud 專案中啟用 GKE 遠端 MCP 伺服器,請執行下列指令:
gcloud beta services mcp enable container.googleapis.com \
--project=PROJECT_ID
將 PROJECT_ID 替換為 Google Cloud 專案 ID。
您 Google Cloud 專案已啟用 GKE 遠端 MCP 伺服器。如果Google Cloud 專案尚未啟用 GKE 服務,系統會提示您先啟用服務,再啟用 GKE 遠端 MCP 伺服器。
為確保安全,建議您只為 AI 應用程式運作所需的服務啟用 MCP 伺服器。
在專案中停用 GKE 遠端 MCP 伺服器
如要在Google Cloud 專案中停用 GKE 遠端 MCP 伺服器,請執行下列指令:
gcloud beta services mcp disable SERVICE \
--project=PROJECT_ID
您的 Google Cloud 專案已停用 GKE 遠端 MCP 伺服器。
驗證及授權
GKE 遠端 MCP 伺服器會使用 OAuth 2.0 通訊協定搭配 Identity and Access Management (IAM) 進行驗證及授權。所有Google Cloud 身分都支援 MCP 伺服器驗證。
GKE 遠端 MCP 伺服器不接受 API 金鑰進行驗證。
建議您使用 MCP 工具為代理商建立個別身分,以便控管及監控資源存取權。如要進一步瞭解驗證,請參閱「向 MCP 伺服器進行驗證」。
GKE 遠端 MCP OAuth 範圍
OAuth 2.0 會使用範圍和憑證,判斷經過驗證的主體是否有權對資源執行特定動作。如要進一步瞭解 Google 的 OAuth 2.0 範圍,請參閱「使用 OAuth 2.0 存取 Google API」。
GKE 具有下列 MCP 工具 OAuth 範圍:
| gcloud CLI 的範圍 URI | 說明 |
|---|---|
https://www.googleapis.com/auth/cloud-platform |
授予 Google Cloud 專案的廣泛唯讀存取權。 |
在工具呼叫期間存取的資源可能需要其他範圍。如要查看 GKE 要求的範圍清單,請參閱 GKE API。
設定 MCP 用戶端以使用 GKE MCP 伺服器
主機程式 (例如 Claude 或 Gemini CLI) 可以例項化 MCP 用戶端,並連線至單一 MCP 伺服器。主機程式可以有多個連線至不同 MCP 伺服器的用戶端。如要連線至遠端 MCP 伺服器,MCP 用戶端至少須知道遠端 MCP 伺服器的網址。
在主機中,尋找連線至遠端 MCP 伺服器的方法。系統會提示您輸入伺服器的詳細資料,例如名稱和網址。
如果是 GKE 遠端 MCP 伺服器,請視需要輸入下列項目:
如需特定主機的指引,請參閱下列文章:
如需更多一般指引,請參閱「連線至遠端 MCP 伺服器」。
可用的工具
唯讀 MCP 工具的 MCP 屬性會設為 mcp.tool.isReadOnlytrue。您可能只想透過機構政策,在特定環境中允許唯讀工具。
如要查看 GKE MCP 伺服器的可用 MCP 工具詳細資料和說明,請參閱 GKE MCP 參考資料。
列出工具
使用 MCP 檢查器列出工具,或直接將 tools/list HTTP 要求傳送至 GKE 遠端 MCP 伺服器。tools/list 方法不需要驗證。
POST /mcp HTTP/1.1
Host: container.googleapis.com
Content-Type: application/json
{
"jsonrpc": "2.0",
"method": "tools/list",
}
應用實例
以下是 GKE 遠端 MCP 伺服器的範例用途:
選用的安全防護設定
MCP 工具可執行各種動作,因此會帶來新的安全風險和考量。為盡量降低及管理這些風險,Google Cloud 提供預設和可自訂的政策,控管機構或專案中 MCP 工具的使用情形。 Google Cloud
如要進一步瞭解 MCP 安全性和控管措施,請參閱這篇文章。
Model Armor
Model Armor 是一項Google Cloud 服務,可提高 AI 應用程式的安全性。這項功能會主動篩選 LLM 提示詞和回覆,防範各種風險,並支援負責任的 AI 做法。無論您是在雲端環境或外部雲端供應商部署 AI,Model Armor 都能協助您防範惡意輸入內容、驗證內容安全性、保護私密資料、維持法規遵循狀態,並在多元的 AI 環境中,持續落實 AI 安全與安全政策。
Model Armor 僅適用於特定地區。如果專案已啟用 Model Armor,且對該專案的呼叫來自不支援的區域,Model Armor 會進行跨區域呼叫。詳情請參閱Model Armor 位置。
啟用 Model Armor
如要啟用 Model Armor,請完成下列步驟:
Model Armor 記錄
如要瞭解 Model Armor 稽核和平台記錄,請參閱「Model Armor 稽核記錄」。
在專案中停用 Model Armor
如要停用 Google Cloud 專案的 Model Amor,請執行下列指令:
gcloud beta services mcp content-security remove modelarmor.googleapis.com \
--project=PROJECT_ID
將 PROJECT_ID 替換為Google Cloud 專案 ID。
系統不會掃描 Google Cloud 指定專案的 MCP 流量。
使用 Model Armor 停用掃描 MCP 流量
如果您仍想在專案中使用 Model Armor,但想停止使用 Model Armor 掃描 MCP 流量,請執行下列指令:
gcloud model-armor floorsettings update \
--full-uri='projects/PROJECT_ID/locations/global/floorSetting' \
--mcp-sanitization=DISABLED
將 PROJECT_ID 替換為Google Cloud 專案 ID。
Model Armor 不會掃描Google Cloud上的 MCP 流量。
機構層級 MCP 控制項
您可以建立自訂機構政策,透過 gcp.managed.allowedMCPService 限制條件控管機構中的 MCP 伺服器使用情形。 Google Cloud 如需更多資訊和使用範例,請參閱「Google Cloud MCP 伺服器:使用 IAM 控管存取權」。