使用 GKE 远程 MCP 服务器

Model Context Protocol (MCP) 可标准化大语言模型 (LLM) 和 AI 应用或代理连接到外部数据源的方式。借助 MCP 服务器,您可以使用其工具、资源和提示来执行操作,并从其后端服务获取更新后的数据。

本地 MCP 服务器通常在本地机器上运行,并使用标准输入和输出流 (stdio) 在同一设备上的服务之间进行通信。远程 MCP 服务器在服务的基础设施上运行,并为 AI 应用提供 HTTP 端点,以便在 AI MCP 客户端和 MCP 服务器之间进行通信。如需详细了解 MCP 架构,请参阅 MCP 架构

本文档介绍了如何使用 GKE 远程 Model Context Protocol (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 服务器。

准备工作

  1. 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.
  2. Install the Google Cloud CLI.

  3. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  4. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

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

  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable container.googleapis.com
  8. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/container.clusterViewer

    gcloud 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.
  9. Install the Google Cloud CLI.

  10. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  11. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  12. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

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

  14. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable container.googleapis.com
  15. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/container.clusterViewer

    gcloud 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.
  16. 所需的角色

    如需执行一次性设置以启用 GKE 远程 MCP 服务器,管理员需要具备以下角色:

    • Organization Policy Administrator (roles/orgpolicy.policyAdmin):针对 Google Cloud 组织授予此角色,以允许创建自定义组织政策。
    • Service Usage Admin (roles/serviceusage.serviceUsageAdmin):针对 Google Cloud 项目授予此角色,以允许启用远程 MCP 服务端点。 此角色可提供 serviceusage.mcppolicy.getserviceusage.mcppolicy.update 权限。

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

    使用相应服务的角色

    对远程 MCP 服务器工具进行调用的主账号需要具有访问 GKE 资源的权限。此主账号可以是人类用户,也可以是自动化服务账号。请至少授予Google Cloud 项目的以下角色:

    向以下账号授予此角色:

    • 用户通过 Gemini CLI 等客户端与 MCP 服务器交互时的用户账号
    • 构建调用 MCP 服务器的自主代理或应用时的服务账号

    启用或停用 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。

    GKE 远程 MCP 服务器已启用,可在您的 Google Cloud 项目中使用了。如果没有为Google Cloud 项目启用 GKE 服务,系统会提示您先启用该服务,然后再启用 GKE 远程 MCP 服务器。

    作为安全性最佳实践,我们建议您仅为 AI 应用正常运行所需的服务启用 MCP 服务器。

    在项目中停用 GKE 远程 MCP 服务器

    如需在Google Cloud 项目中停用 GKE 远程 MCP 服务器,请运行以下命令:

    gcloud beta services mcp disable SERVICE \
        --project=PROJECT_ID
    

    GKE 远程 MCP 服务器已停用,无法在 Google Cloud 项目中使用。

    身份验证和授权

    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 服务器,请根据需要输入以下内容:

    • 服务器名称:GKE 远程 MCP 服务器
    • 服务器网址端点:https://container.googleapis.com/mcp
    • 传输:HTTP
    • 身份验证详细信息:您可以根据所需的身份验证方式,输入 Google Cloud 凭证、OAuth 客户端 ID 和密钥,或代理身份和凭证。如需详细了解身份验证,请参阅向 MCP 服务器进行身份验证

    如需查看特定于主机的指导,请参阅以下内容:

    如需查看更通用的指导,请参阅连接到远程 MCP 服务器

    可用的工具

    只读 MCP 工具的 MCP 属性 mcp.tool.isReadOnly 设置为 true。您可能希望通过组织政策仅允许在特定环境中使用只读工具。

    如需查看 GKE MCP 服务器的可用 MCP 工具及其说明的详细信息,请参阅 GKE MCP 参考文档

    列出工具

    使用 MCP 检查器列出工具,或直接向 GKE 远程 MCP 服务器发送 tools/list HTTP 请求。tools/list 方法不需要进行身份验证。

    POST /mcp HTTP/1.1
    Host: container.googleapis.com
    Content-Type: application/json
    
    {
      "jsonrpc": "2.0",
      "method": "tools/list",
    }
    

    示例应用场景

    以下是 GKE 远程 MCP 服务器的示例使用场景:

    • 检查 GKE 集群和节点池的配置和状态。例如,使用提示:“Show me the details of my 'production-cluster' and list all of its node pools.”
    • 无需使用 kubectl,即可查看集群内的 Kubernetes 资源配置和容器日志。例如,使用提示:“Get the YAML for the 'frontend-deployment' in the 'default' namespace.”
    • 监控长时间运行的 GKE 操作(例如集群升级)的状态。例如,使用提示:“List all the GKE operations in my project from the last hour.”

    可选的安全配置

    由于 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,请完成以下步骤:

    1. 如需在Google Cloud 项目中启用 Model Armor,请运行以下 gcloud CLI 命令:

      gcloud services enable modelarmor.googleapis.com \
          --project=PROJECT_ID
      

      PROJECT_ID 替换为您的Google Cloud 项目 ID。

    2. 如需配置建议的 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 过滤条件

    3. 如需将 Model Armor 添加为 MCP 服务的内容安全服务提供商,请运行以下 gcloud CLI 命令:

      gcloud beta services mcp content-security add modelarmor.googleapis.com \
          --project=PROJECT_ID
      

      PROJECT_ID 替换为Google Cloud 项目 ID。

    4. 如需确认 MCP 流量已发送到 Model Armor,请运行以下命令:

      gcloud beta services mcp content-security get \
          --project=PROJECT_ID
      

      PROJECT_ID 替换为Google Cloud 项目 ID。

    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 扫描以用于指定项目。

    禁止使用 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 组织中的使用。如需了解详情和使用示例,请参阅使用 IAM 进行 Google Cloud MCP 服务器访问权限控制

    后续步骤