使用 Managed Service for Apache Airflow 远程 MCP 服务器

Managed Airflow(第 3 代) | Managed Airflow(第 2 代) | Managed Airflow(旧版第 1 代)

本文档介绍了如何使用 Managed Service for Apache Airflow 远程 Model Context Protocol (MCP) 服务器从 Gemini CLI、ChatGPT、Claude 或您正在开发的 AI 应用等 AI 应用连接到 Managed Service for Apache Airflow。 借助 Managed Airflow MCP 服务器,您可以管理 Managed Airflow 环境,并获取有关已执行的 DAG 运行和 Airflow 任务的详细信息。

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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Enable the Managed Airflow 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.

    Enable the API

  5. 安装 Google Cloud CLI。

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

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

    gcloud init
  8. 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

  9. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  10. Enable the Managed Airflow 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.

    Enable the API

  11. 安装 Google Cloud CLI。

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

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

    gcloud init

所需的角色

如需获得启用 Managed Service for Apache Airflow MCP 服务器所需的权限,请让管理员向您授予要在其中启用 Managed Service for Apache Airflow MCP 服务器的项目中的以下 IAM 角色:

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

您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。

身份验证和授权

Managed Service for Apache Airflow 远程 MCP 服务器使用 OAuth 2.0 协议和 Identity and Access Management (IAM) 进行身份验证和授权。支持所有 Google Cloud 身份 用于向 MCP 服务器进行身份验证。

我们建议您为使用 MCP 工具的智能体创建单独的身份,以便控制和监控对资源的访问权限。如需详细了解 身份验证,请参阅 向 MCP 服务器进行身份验证

Managed Service for Apache Airflow MCP OAuth 范围

OAuth 2.0 使用范围和凭证来确定经过身份验证的主账号是否有权对资源执行特定操作。如需详细了解 Google 的 OAuth 2.0 范围,请参阅 使用 OAuth 2.0 访问 Google API

Managed Service for Apache Airflow 具有以下 MCP 工具 OAuth 范围:

gcloud CLI 的范围 URI 说明
https://www.googleapis.com/auth/cloudcomposer.readonly 仅允许访问以读取数据。
https://www.googleapis.com/auth/cloudcomposer 允许访问以读取和修改数据。

配置 MCP 客户端以使用 Managed Service for Apache Airflow MCP 服务器

Claude 或 Antigravity 等 AI 应用和智能体可以实例化连接到单个 MCP 服务器的 MCP 客户端。一个 AI 应用可以有多个连接到不同 MCP 服务器的客户端。 如果您的应用未列在 特定于客户端的指南中,则可以使用 以下信息从大多数应用进行连接。

在 AI 应用中,查找添加或连接到远程 MCP 服务器的方法。 对于 Managed Service for Apache Airflow MCP 服务器,请根据需要输入以下信息:

  • 服务器名称:Managed Service for Apache Airflow MCP 服务器
  • 端点:composer.{region}.rep.googleapis.com/mcp
  • 传输:HTTP
  • 身份验证详细信息:您可以根据所需的身份验证方式, 输入凭证、OAuth 客户端 ID 和密钥,或代理身份和凭证。 Google Cloud 如需详细了解 身份验证,请参阅 向 MCP 服务器进行身份验证
  • OAuth 范围:您在连接到 Managed Service for Apache Airflow MCP 服务器时要使用的 OAuth 2.0 范围

如需了解有关设置和连接到 MCP 服务器的应用专用指南, 请参阅 特定于客户端的指南

如需了解更通用的指南,请参阅以下资源:

可用的工具

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

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

列出工具

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

POST /mcp HTTP/1.1
Host: composer.{region}.rep.googleapis.com/mcp
Content-Type: application/json

{
  "jsonrpc": "2.0",
  "method": "tools/list",
}

应用场景示例

以下是 Managed Service for Apache Airflow MCP 服务器的应用场景示例:

描述环境状态

在此示例应用场景中,您将询问有关项目中的环境的问题。

查找 us-central1 中当前未处于运行状态的所有 Managed Airflow 环境。 如果其中任何一个处于错误状态,请告诉我 此环境上次更新的时间以及环境的 工作负载配置。

工作流:描述 Managed Airflow 环境可能如下所示 。

  • 查看环境列表:智能体使用 list_environments 获取 指定区域中的环境列表,以及有关上次更新时间的信息 。

使用自定义 PyPI 软件包创建新的 Managed Airflow 环境

在此示例应用场景中,您将创建一个新的 Managed Airflow 环境 ,然后将 自定义 PyPI 软件包安装到其中。

提示示例

在我的项目中创建一个新的 Managed Airflow(第 3 代)环境,其中包含 Airflow 2。然后将 nltk[machine_learning] 软件包安装到其中。 使用 example-account@example-project。iam.gserviceaccount.com 服务帐号作为环境。

工作流:创建新的 Managed Airflow 环境,然后 将自定义 PyPI 软件包安装到其中可能如下所示。

  • 创建环境:智能体使用 create_environment 通过提供的配置参数创建新 环境。智能体会询问其他配置参数,例如允许访问 Airflow 界面的 IP 地址列表。

  • 安装软件包:智能体调用 manage_pypi_packages 以安装 指定的 PyPI 软件包。

排查 DAG 运行和任务失败的问题

检查 us-central1 中的 example-environment-name Managed Airflow 环境。example_dag 失败,我想知道原因以及具体失败的任务。此外,请告诉我过去 24 小时内此 环境中失败的任何其他 DAG。

工作流:排查 DAG 运行失败的问题可能如下所示。

  • 获取失败的 DAG 运行:智能体使用 find_last_failed_dag_runs 获取指定 环境中 example_dag DAG 的失败 DAG 运行列表。智能体使用相同的工具获取所有失败 DAG 运行的列表。

  • 检查失败的 DAG 运行:智能体调用 list_failed_task_instances 以获取处于失败状态的 DAG 运行的任务实例列表 。

  • 分析失败的任务日志:智能体使用 get_task_instance 获取 失败的任务实例的详细信息,包括检索日志所需的数据 。

  • 检查 DAG 源代码:智能体使用 get_dag_source_code 分析失败的任务源代码中的错误。

可选的安全配置

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

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

使用 Model Armor

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

如果启用了 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 不会自动将此项目的下限设置中定义的规则应用于任何 Google MCP 服务器流量。

Model Armor 下限设置和常规配置可能会影响 MCP 以外的其他内容。由于 Model Armor 与 Vertex AI 等服务集成,因此您对下限设置所做的任何更改都可能会影响所有集成服务(而不仅仅是 MCP)的流量扫描和安全行为。

使用 IAM 拒绝政策控制 MCP 的使用

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

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

  • 主账号
  • 工具属性,例如只读
  • 应用的 OAuth 客户端 ID

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

后续步骤