使用 Compute Engine 远程 MCP 服务器

本文档介绍了如何使用 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 服务器有何区别?

    本地 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 服务器概览

    准备工作

    1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
    2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

      Go to project selector

    3. Verify that billing is enabled for your Google Cloud project.

    4. 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

      1. In the Google Cloud console, go to the IAM page.

        Go to IAM
      2. Select the project.
      3. 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.

      4. 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

      1. In the Google Cloud console, go to the IAM page.

        Go to IAM
      2. Select the project.
      3. Click Grant access.
      4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

      5. Click Select a role, then search for the role.
      6. To grant additional roles, click Add another role and add each additional role.
      7. Click Save.
    5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

      Go to project selector

    6. Verify that billing is enabled for your Google Cloud project.

    7. 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

      1. In the Google Cloud console, go to the IAM page.

        Go to IAM
      2. Select the project.
      3. 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.

      4. 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

      1. In the Google Cloud console, go to the IAM page.

        Go to IAM
      2. Select the project.
      3. Click Grant access.
      4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

      5. Click Select a role, then search for the role.
      6. To grant additional roles, click Add another role and add each additional role.
      7. Click Save.
    8. 启用 Compute Engine API。

      启用 Compute Engine API

    所需的角色

    如需获得使用 Compute Engine 远程 MCP 服务器所需的权限,请让您的管理员为您授予 Google Cloud 项目的以下 IAM 角色:

    如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

    这些预定义角色包含使用 Compute Engine 远程 MCP 服务器所需的权限。如需查看所需的确切权限,请展开所需权限部分:

    所需权限

    如需使用 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 服务器

    Claude 或 Gemini CLI 等 AI 应用和代理可以实例化连接到单个 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 检查器列出工具,或直接向 Compute Engine 远程 MCP 服务器发送 tools/list HTTP 请求。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 资源:

    • 检查和管理资源。例如,如需了解项目中的资源分配和配置,您可以列出所有计算实例。您还可以查找可用区中附加了特定加速器的所有正在运行的计算实例,并显示其位置和名称以进行资源管理。
    • 清理未使用的资源以降低运营成本。例如,识别并清理可用区中不再与源磁盘关联的磁盘快照,或者识别并删除挂接了昂贵 GPU 资源的已停止虚拟机实例。
    • 优化实例性能。例如,将配置不足的虚拟机实例调整为同一系列中更大的机器类型,并确认更新成功。
    • 灵活选择可用区,为 AI 工作负载预配专用虚拟机。例如,在指定区域内提供特定 GPU 加速器的任何可用区中,创建一个挂接了该加速器的虚拟机实例。
    • 排查和验证实例配置。例如,检索作业冻结的特定虚拟机实例的配置详细信息,重新启动该实例,并确认底层加速器和磁盘已连接。

    示例提示

    以下是一些示例提示,您可以使用这些提示通过 Compute Engine MCP 服务器执行任务:

    • 列出 PROJECT_ID 中的所有虚拟机,包括虚拟机名称和可用区。
    • 显示 VM_NAME 的实例详情。
    • REGION 中,找到所有来源磁盘已不存在的磁盘快照。
    • VM_NAME 的机器类型更改为同一机器家族中的下一个最大机器类型,在 VM_NAME 重新上线时发送通知,并确认新机器类型。
    • 查找 REGION 中所有运行的 NVIDIA 加速器虚拟机,并显示这些虚拟机的可用区和名称。
    • ZONE 中创建一台挂接了 NVIDIA T4 加速器的虚拟机。将虚拟机命名为 my-nvidiat4-vm
    • 查找 REGION 中所有已停止且具有 NVIDIA Tesla T4 加速器的虚拟机,然后将其删除。

    替换以下内容:

    • PROJECT_ID: Google Cloud 项目 ID。
    • REGION:资源所在的区域的名称。
    • ZONE:虚拟机所在的可用区的名称。
    • VM_NAME:虚拟机实例的名称。

    可选的安全配置

    由于 MCP 工具可执行各种操作,因此 MCP 会引发新的安全风险和注意事项。为了最大限度地降低这些风险并进行管理,Google Cloud 提供了默认设置和可自定义的政策,用于控制 MCP 工具在 Google Cloud组织或项目中的使用。

    如需详细了解 MCP 安全性和治理,请参阅 AI 安全性

    使用 Model Armor

    Model Armor 是一项Google Cloud 服务,旨在增强 AI 应用的安全性。它通过主动筛选 LLM 提示和回答来防范各种风险,并支持 Responsible AI 实践。无论您是在云环境还是外部云服务提供商中部署 AI,Model Armor 都能帮助您防止恶意输入、验证内容安全性、保护敏感数据、保持合规性,并在各种 AI 环境中以一致的方式实施 AI 安全政策。

    Model Armor 仅在特定区域位置提供。如果为项目启用了 Model Armor,并且对该项目的调用来自不受支持的区域,则 Model Armor 会进行跨区域调用。如需了解详情,请参阅 Model Armor 位置

    启用 Model Armor

    您必须先启用 Model Armor API,然后才能使用 Model Armor。

    控制台

    1. 启用 Model Armor API。

      启用 API 所需的角色

      如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (roles/serviceusage.serviceUsageAdmin),该角色包含 serviceusage.services.enable 权限。了解如何授予角色

      启用 API

    2. 选择要启用 Model Armor 的项目。

    gcloud

    在开始之前,请使用 Google Cloud CLI 和 Model Armor API 按照以下步骤操作:

    1. 在 Google Cloud 控制台中,激活 Cloud Shell。

      激活 Cloud Shell

      Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。

    2. 运行以下命令,为 Model Armor 服务设置 API 端点。

      gcloud config set api_endpoint_overrides/modelarmor "https://modelarmor.LOCATION.rep.googleapis.com/"

      LOCATION 替换为您要使用 Model Armor 的区域。

    为 Google 和 Google Cloud 远程 MCP 服务器配置保护

    为了帮助保护您的 MCP 工具调用和响应,您可以使用 Model Armor 下限设置。下限设置用于定义适用于整个项目的最低安全过滤条件。此配置会对项目中的所有 MCP 工具调用和响应应用一组一致的过滤条件。

    设置启用了 MCP 清理功能的 Model Armor 下限设置。如需了解详情,请参阅配置 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:Responsible AI - Dangerous 过滤设置的置信度。您可以修改此设置,但较低的值可能会导致出现更多假正例。如需了解详情,请参阅 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 使用情况

    Identity and Access Management (IAM) 拒绝政策有助于保护 Google Cloud 远程 MCP 服务器。配置这些政策以阻止不需要的 MCP 工具访问。

    例如,您可以根据以下条件拒绝或允许访问:

    • 正文
    • 工具属性(例如只读)
    • 应用的 OAuth 客户端 ID

    如需了解详情,请参阅使用 Identity and Access Management 控制 MCP 的使用

    后续步骤