本文說明如何使用 Compute Engine 遠端 Model Context Protocol (MCP) 伺服器,連線至 Gemini CLI、ChatGPT、Claude 等 AI 應用程式,以及您正在開發的自訂應用程式。Compute Engine 遠端 MCP 伺服器提供完整的功能,可讓 LLM 代理執行各種基礎架構管理工作,包括:
- 管理虛擬機器 (VM) 執行個體。
- 管理執行個體群組管理員和執行個體範本。
- 管理磁碟和快照。
- 擷取預留項目和承諾使用合約的相關資訊。
。
啟用 Compute Engine API 時,系統會啟用 Compute Engine 遠端 MCP 伺服器。
Model Context Protocol (MCP) 標準可將大型語言模型 (LLM) 和 AI 應用程式/代理程式連結至外部資料來源的方式標準化。MCP 伺服器可讓您使用工具、資源和提示,從後端服務採取行動及取得更新資料。
本機和遠端 MCP 伺服器有何不同?
- 本機 MCP 伺服器
- 通常在本機執行,並使用標準輸入和輸出串流 (stdio) 在同一部裝置上的服務之間通訊。
- 遠端 MCP 伺服器
- 在服務的基礎架構上執行,並為 AI 應用程式提供 HTTP 端點,供 AI MCP 用戶端與 MCP 伺服器通訊。如要進一步瞭解 MCP 架構,請參閱 MCP 架構。
Google 和 Google Cloud 遠端 MCP 伺服器
Google 和 Google Cloud 遠端 MCP 伺服器具備下列功能和優點:- 簡化集中式探索作業。
- 管理全域或區域 HTTP 端點。
- 精細授權。
- 可選用 Model Armor 防護功能,確保提示詞和回覆安全無虞。
- 集中式稽核記錄。
如要瞭解其他 MCP 伺服器,以及 Google Cloud MCP 伺服器適用的安全性與控管措施,請參閱 Google Cloud MCP 伺服器總覽。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
-
Make sure that you have the following role or roles on the project: Compute Instance Admin (v1), Compute Security Admin, Service Account User, Service Usage Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
-
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
-
Make sure that you have the following role or roles on the project: Compute Instance Admin (v1), Compute Security Admin, Service Account User, Service Usage Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
- 啟用 Compute Engine API。
必要的角色
如要取得使用 Compute Engine 遠端 MCP 伺服器所需的權限,請要求管理員在 Google Cloud 專案中授予下列 IAM 角色:
-
發出 MCP 工具呼叫:
MCP 工具使用者 (
roles/mcp.toolUser)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
這些預先定義的角色具備使用 Compute Engine 遠端 MCP 伺服器所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:
所需權限
如要使用 Compute Engine 遠端 MCP 伺服器,必須具備下列權限:
-
發出 MCP 工具呼叫:
mcp.tools.call
您還需要執行 Compute Engine 作業所需的角色和權限。詳情請參閱「Compute Engine 角色和權限」。
驗證及授權
Compute Engine MCP 伺服器會使用 OAuth 2.0 通訊協定搭配 Identity and Access Management (IAM) 進行驗證和授權。所有Google Cloud 身分皆支援 MCP 伺服器的驗證。
建議您為使用 MCP 工具的代理商建立個別身分,以便控管及監控資源存取權。如要進一步瞭解驗證,請參閱「向 MCP 伺服器進行驗證」。
Compute Engine MCP OAuth 範圍
OAuth 2.0 會使用範圍和憑證,判斷經過驗證的主體是否有權對資源執行特定動作。如要進一步瞭解 Google 的 OAuth 2.0 範圍,請參閱「使用 OAuth 2.0 存取 Google API」。
Compute Engine 具有下列 MCP 工具 OAuth 範圍:
gcloud CLI 的範圍 URI 說明 https://www.googleapis.com/auth/compute.read-only僅允許讀取資料的權限。 https://www.googleapis.com/auth/compute.read-write有權讀取及修改資料。 在工具呼叫期間存取的資源可能需要其他範圍。如要查看 Compute Engine 必要的範圍清單,請參閱 Compute Engine API。
設定 MCP 用戶端,以使用 Compute Engine MCP 伺服器
AI 應用程式和代理程式 (例如 Claude 或 Gemini CLI) 可以例項化 MCP 用戶端,連線至單一 MCP 伺服器。AI 應用程式可有多個連至不同 MCP 伺服器的用戶端。如要連線至遠端 MCP 伺服器,MCP 用戶端必須知道遠端 MCP 伺服器的網址。
在 AI 應用程式中,尋找連線至遠端 MCP 伺服器的方法。系統會提示你輸入伺服器的詳細資料,例如名稱和網址。
如果是 Compute Engine MCP 伺服器,請視需要輸入下列資訊:
- 伺服器名稱:Compute Engine MCP 伺服器
- 伺服器網址或端點:https://compute.googleapis.com/mcp
- 傳輸:HTTP
- 驗證詳細資料:視驗證方式而定,您可以輸入 Google Cloud 憑證、OAuth 用戶端 ID 和密鑰,或是代理程式身分和憑證。如要進一步瞭解驗證,請參閱「向 MCP 伺服器進行驗證」。
如需設定及連線至 MCP 伺服器的特定主機指南,請參閱下列內容:
如需更多一般指引,請參閱下列資源:
可用的工具
如要查看 Compute Engine MCP 伺服器的可用 MCP 工具詳細資料和說明,請參閱 Compute Engine MCP 參考資料。
列出工具
使用 MCP 檢查器列出工具,或直接將
tools/listHTTP 要求傳送至 Compute Engine 遠端 MCP 伺服器。tools/list方法不需要驗證。POST /mcp HTTP/1.1 Host: compute.googleapis.com Content-Type: application/json { "jsonrpc": "2.0", "method": "tools/list", }應用實例
下列範例使用案例說明如何使用 Compute Engine MCP 伺服器管理 Compute Engine 資源:
- 檢查及管理資源。舉例來說,如要瞭解專案中的資源配置和設定,可以列出所有運算執行個體。您也可以找出可用區中所有已附加特定加速器的執行中運算執行個體,並顯示其位置和名稱,方便您管理資源。
- 清除未使用的資源,降低營運成本。舉例來說,您可以找出並清理區域中不再與來源磁碟相關聯的磁碟快照,或是找出並刪除已停止的 VM 執行個體,這些執行個體已連結昂貴的 GPU 資源。
- 最佳化執行個體效能。舉例來說,您可以將資源不足的 VM 執行個體調整為同一系列中較大的機型,並確認更新成功。
- 為 AI 工作負載佈建專用 VM,並享有可用區彈性。舉例來說,您可以在指定區域中提供 GPU 加速器的任何可用區,建立附加特定 GPU 加速器的 VM 執行個體。
- 排解及驗證執行個體設定。舉例來說,您可以擷取工作凍結的特定 VM 執行個體設定詳細資料、重新啟動該執行個體,並確認已附加基礎加速器和磁碟。
提示範例
以下是範例提示,可用於透過 Compute Engine MCP 伺服器執行工作:
- 列出
PROJECT_ID中的所有 VM,包括 VM 名稱和可用區。 - 顯示「
VM_NAME」的執行個體詳細資料。 - 在
REGION中,找出來源磁碟已不存在的所有磁碟快照。 - 將
VM_NAME的機型變更為相同機器家族中次大的機型,在恢復連線時傳送通知,並確認新的機型。 - 找出
REGION中所有搭載 NVIDIA 加速器的執行中 VM,並顯示這些 VM 的可用區和名稱。 - 在
ZONE中建立 VM,並附加 NVIDIA T4 加速器。將 VM 命名為my-nvidiat4-vm。 - 找出
REGION中所有已停止的 VM,並刪除這些 VM。
更改下列內容:
PROJECT_ID: Google Cloud 專案 ID。REGION:資源所在的區域名稱。ZONE:VM 所在的可用區名稱。VM_NAME:VM 執行個體的名稱。
選用的安全防護設定
由於 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 API,才能使用 Model Armor。
控制台
gcloud
開始前,請使用 Google Cloud CLI 搭配 Model Armor API 執行下列步驟:
在 Google Cloud 控制台中啟用 Cloud Shell。
Google Cloud 主控台底部會開啟一個 Cloud Shell 工作階段,並顯示指令列提示。Cloud Shell 是已安裝 Google Cloud CLI 的殼層環境,並已針對您目前的專案設定好相關值。工作階段可能要幾秒鐘的時間才能初始化。
-
執行下列指令,為 Model Armor 服務設定 API 端點。
gcloud config set api_endpoint_overrides/modelarmor "https://modelarmor.LOCATION.rep.googleapis.com/"
將
LOCATION替換為要使用 Model Armor 的區域。
設定 Google 和遠端 MCP 伺服器的防護功能 Google Cloud
如要保護 MCP 工具呼叫和回應,可以使用 Model Armor 底限設定。底限設定會定義專案適用的最低安全性篩選條件。這項設定會對專案中的所有 MCP 工具呼叫和回應套用一致的篩選器。
設定 Model Armor 底限設定,並啟用 MCP 消毒功能。詳情請參閱「設定 Model Armor 底限設定」。
請參閱下列指令範例:
gcloud model-armor floorsettings update \ --full-uri='projects/PROJECT_ID/locations/global/floorSetting' \ --enable-floor-setting-enforcement=TRUE \ --add-integrated-services=GOOGLE_MCP_SERVER \ --google-mcp-server-enforcement-type=INSPECT_AND_BLOCK \ --enable-google-mcp-server-cloud-logging \ --malicious-uri-filter-settings-enforcement=ENABLED \ --add-rai-settings-filters='[{"confidenceLevel": "MEDIUM_AND_ABOVE", "filterType": "DANGEROUS"}]'
將
PROJECT_ID替換為 Google Cloud 專案 ID。請注意下列設定:
INSPECT_AND_BLOCK:強制執行類型,可檢查 Google MCP 伺服器的內容,並封鎖符合篩選條件的提示和回應。ENABLED:啟用篩選器或強制執行的設定。MEDIUM_AND_ABOVE:負責任的 AI 技術 - 危險篩選器設定的信心水準。您可以修改這項設定,但較低的值可能會導致更多誤判。詳情請參閱「Model Armor 信心水準」。
使用 Model Armor 停用掃描 MCP 流量
如要停止使用 Model Armor 掃描 Google MCP 流量,請執行下列指令:
gcloud model-armor floorsettings update \ --full-uri='projects/PROJECT_ID/locations/global/floorSetting' \ --remove-integrated-services=GOOGLE_MCP_SERVER將
PROJECT_ID替換為 Google Cloud 專案 ID。Model Armor 不會掃描專案中的 MCP 流量。
使用 IAM 拒絕政策控管 MCP 使用情形
身分與存取權管理 (IAM) 拒絕政策可協助您保護遠端 MCP 伺服器。 Google Cloud 設定這些政策,封鎖不必要的 MCP 工具存取權。
舉例來說,您可以根據下列條件拒絕或允許存取:
- 主體
- 工具屬性 (例如唯讀)
- 應用程式的 OAuth 用戶端 ID
詳情請參閱「使用 Identity and Access Management 控制 MCP 使用情形」。
後續步驟
- 請參閱 Compute Engine MCP 參考說明文件。
- 進一步瞭解 Google Cloud MCP 伺服器。