Apache Airflow的MCP服务器
模型上下文协议(MCP)服务器,提供与Apache Airflow的REST API的全面集成。该服务器允许AI助手与Airflow工作流交互,监控DAG运行,并以编程方式管理任务。
特性
- DAG管理:列出、查看详细信息、暂停和取消暂停DAGs
- DAG运行操作:触发新运行,列出现有运行,并获取详细的运行信息
- 任务实例监控:查看任务实例及其执行详细信息
- 通用兼容性:适用于所有流行的Airflow托管平台:
- 天文学家 - 谷歌云编辑器 - 亚马逊 MWAA - 自托管Airflow实例
- 综合录井:访问和监视日志以进行调试和故障排除:
- 单个任务的实时日志检索 - 整个DAG运行的聚合日志 - 智能日志跟踪最近的活动摘要 - 自动日志格式化和解码
可用工具
DAG管理
- 气流列表_天 -列出所有带分页和排序的DAG
- 气流get-dag -获取特定DAG的详细信息
- 气流_触发器_dag -使用可选配置触发新的DAG运行
- 气流_使用量_dag -暂停DAG
- 气流损失 --不均匀的DAG
DAG运行监控
- 气流_时间_行程 -列出特定DAG运行的DAG
- 气流速度 -获取特定DAG运行的详细信息
- 气流列表任务状态 -列出DAG运行的任务实例
- 气流_任务_状态 -获取详细的任务实例信息
日志记录和调试
- 气流_get_task_logs -获取特定任务实例的完整日志
- 气流_风速_风速_风向 -获取DAG运行中所有任务的日志
- airflow_tail_dag_run -使用最近的活动和日志跟踪/监视DAG运行
安装和部署
地方发展
通过NPX(建议用于Claude Desktop)
npx mcp-server-airflowHTTP服务器(建议用于云部署)
npx mcp-server-airflow-http来源
git clone https://github.com/tomnagengast/mcp-server-airflow.git
cd mcp-server-airflow
npm install
npm run build
# For stdio mode (Claude Desktop)
npm start
# For HTTP mode (cloud deployment)
npm run start:http云部署(推荐)
此服务器支持流式HTTP传输,这是MCP服务器的当前最佳实践。部署到您首选的云平台:
快速部署
npm run deploy此交互式脚本将指导您部署到:
- 谷歌云平台(云运行)
- 亚马逊网络服务(ECS Fargate)
- 数字海洋应用平台
- Netlify(无服务器功能)
手动部署选项
🌐 Google Cloud Platform (Cloud Run)
# Build and push to Container Registry
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/mcp-server-airflow
# Create secrets
echo "https://your-airflow-instance.com" | gcloud secrets create airflow-base-url --data-file=-
echo "your_token_here" | gcloud secrets create airflow-token --data-file=-
# Deploy to Cloud Run
gcloud run deploy mcp-server-airflow \
--image gcr.io/YOUR_PROJECT_ID/mcp-server-airflow \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--port 3000 \
--memory 512Mi \
--set-secrets AIRFLOW_BASE_URL=airflow-base-url:latest,AIRFLOW_TOKEN=airflow-token:latest☁️ Amazon Web Services (ECS Fargate)
# Create ECR repository
aws ecr create-repository --repository-name mcp-server-airflow
# Build and push image
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com
docker build -t mcp-server-airflow .
docker tag mcp-server-airflow:latest YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/mcp-server-airflow:latest
docker push YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/mcp-server-airflow:latest
# Create secrets in Secrets Manager
aws secretsmanager create-secret --name airflow-config --secret-string '{"base_url":"https://your-airflow-instance.com","token":"your_token_here"}'
# Register task definition and create service (use provided template)
aws ecs register-task-definition --cli-input-json file://deploy/aws-ecs-fargate.json🌊 DigitalOcean App Platform
- 将此存储库分叉到您的GitHub帐户
- 在DigitalOcean应用平台中创建新应用
- 连接您的分叉存储库
- 使用提供的应用程序规范:
deploy/digitalocean-app.yaml - 在仪表板中设置环境变量:
- AIRFLOW_BASE_URL - AIRFLOW_TOKEN (或 AIRFLOW_USERNAME 和 AIRFLOW_PASSWORD)
⚡ Netlify (Serverless Functions)
Netlify提供了出色的无服务器部署,内置了CI/CD和全局CDN。
快速部署
# Interactive deployment script (includes environment setup)
node scripts/deploy-netlify.js
# Or manage environment variables separately
npm run env:netlify手动部署
# Install Netlify CLI
npm install -g netlify-cli
# Authenticate with Netlify
netlify login
# Build for Netlify
npm run build:netlify
# Initialize site (first time only)
netlify init
# Deploy to production
netlify deploy --prod环境变量
选项1:使用Netlify CLI(推荐)
# Interactive environment setup
npm run env:netlify
# Or manually set variables
netlify env:set AIRFLOW_BASE_URL "https://your-airflow-instance.com"
netlify env:set AIRFLOW_TOKEN "your_api_token"
# For basic auth instead of token
netlify env:set AIRFLOW_USERNAME "your_username"
netlify env:set AIRFLOW_PASSWORD "your_password"
# List current variables
netlify env:list选项2:Netlify仪表板
在Netlify站点仪表板中设置这些(站点设置→ 环境变量):
AIRFLOW_BASE_URL:您的Airflow实例URLAIRFLOW_TOKEN:您的Airflow API代币(推荐)
或者对于基本身份验证:
AIRFLOW_USERNAME:您的Airflow用户名AIRFLOW_PASSWORD:您的气流密码
地方发展
# Install dependencies
npm install
# Start local Netlify development server
npm run dev:netlify您的MCP服务器将在 http://localhost:8888/.netlify/functions/mcp
Docker部署
# Build image
npm run docker:build
# Run with environment file
npm run docker:run
# Or with docker-compose
docker-compose up配置
服务器需要通过环境变量进行身份验证配置:
选项1:API代币(推荐)
export AIRFLOW_BASE_URL="https://your-airflow-instance.com"
export AIRFLOW_TOKEN="your_api_token_here"选项2:基本身份验证
export AIRFLOW_BASE_URL="https://your-airflow-instance.com"
export AIRFLOW_USERNAME="your_username"
export AIRFLOW_PASSWORD="your_password"环境变量
| 变量 | 必填 | 描述 |
|---|---|---|
AIRFLOW_BASE_URL | 是 | 您的Airflow实例的基本URL |
AIRFLOW_TOKEN | 没有用于身份验证的\* | API令牌 |
AIRFLOW_USERNAME | 否\* | 基本身份验证的用户名 |
AIRFLOW_PASSWORD | 否\* | 基本身份验证密码 |
\*要么 AIRFLOW_TOKEN 或两者 AIRFLOW_USERNAME 和 AIRFLOW_PASSWORD 必须提供。
平台特定设置
天文学家
export AIRFLOW_BASE_URL="https://your-deployment.astronomer.io"
export AIRFLOW_TOKEN="your_astronomer_api_token"谷歌云编辑器
export AIRFLOW_BASE_URL="https://your-composer-environment-web-server-url"
export AIRFLOW_TOKEN="your_gcp_access_token"亚马逊 MWAA
export AIRFLOW_BASE_URL="https://your-environment-name.airflow.region.amazonaws.com"
# Use AWS credentials with appropriate IAM permissions测试
局部测试
测试stdio和HTTP模式:
# Set required environment variables
export AIRFLOW_BASE_URL="https://your-airflow-instance.com"
export AIRFLOW_TOKEN="your_api_token_here"
# Run comprehensive local tests
npm run test:localHTTP API测试
部署后,测试您的HTTP端点:
# Health check
curl https://your-deployed-url/health
# MCP initialization
curl -X POST https://your-deployed-url/ \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test", "version": "1.0.0"}}}'
# List available tools
curl -X POST https://your-deployed-url/ \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}'Claude桌面集成
标准模式(地方发展)
将此添加到您的Claude Desktop MCP设置中:
{
"mcpServers": {
"airflow": {
"command": "npx",
"args": ["mcp-server-airflow"],
"env": {
"AIRFLOW_BASE_URL": "https://your-airflow-instance.com",
"AIRFLOW_TOKEN": "your_api_token_here"
}
}
}
}HTTP模式(云部署)
对于可流式传输的HTTP,请配置Claude以使用您部署的端点:
{
"mcpServers": {
"airflow": {
"transport": {
"type": "http",
"url": "https://your-deployed-url"
}
}
}
}特定于平台的端点:
- Netlify:
https://your-site.netlify.app/mcp - 谷歌云运行:
https://your-service-url.run.app/ - AWS/数字海洋:
https://your-deployed-url/
使用示例
连接后,您可以使用自然语言与Airflow进行交互:
DAG管理
- “列出我的所有DAGs”
- “显示data_pipeline DAG的详细信息”
- “使用自定义配置触发daily_etl DAG”
- “暂停有问题的dag”
监控和状态
- “my_workflow的最新运行状态如何?”
- “显示上次运行中所有失败的任务实例”
- “列出从今天起my_data_pipeline的所有DAG运行”
日志记录和调试
- “显示run-daily_etl_2024_01_15中extract_data任务的日志”
- “每天获取失败的DAG运行的所有日志_etl_2024_01_15”
- “跟踪当前的DAG运行,并向我展示发生了什么”
- “显示正在运行的data_pipeline的最近活动”
高级示例
- “获取DAG'etl_pipeline'中任务'transform_data'的日志,运行'manual_2024_01_15',尝试编号2”
- “监视DAG运行'scheduled_2024_01_15',并显示每个任务的最后100条日志行”
- “显示DAG运行失败后前5个任务的日志”
身份验证要求
该服务器使用Airflow的稳定REST API(v1),这需要身份验证。API支持:
- 承载令牌身份验证:最安全,建议用于生产
- 基本认证:用户名/密码,对开发有用
- 会话身份验证:使用基于web的令牌时自动处理
安全注意事项
- 安全地存储凭据,永远不要将其提交给版本控制
- 使用环境变量或安全的秘密管理系统
- 对于生产部署,首选API令牌,而不是用户名/密码
- 确保您的Airflow实例具有适当的网络安全性(TLS、VPC等)
- 应用适当的速率限制和监控
- 将HTTPS端点用于生产部署
- 在负载均衡器/网关级别实施适当的身份验证和授权
性能和扩展
HTTP模式的优点
- 无状态:每个请求都是独立的,允许水平缩放
- 缓存:响应可以在CDN/代理级别缓存
- 负载平衡:多个实例可以处理请求
- 监控:标准HTTP监控工具开箱即用
- 调试:使用标准HTTP工具易于测试和调试
推荐的生产设置
- 自动缩放:根据CPU/内存使用情况配置云平台以进行扩展
- 健康检查:使用
/health负载平衡器运行状况检查的端点 - 监控:设置日志记录和指标收集
- 缓存:考虑缓存频繁访问的DAG信息
- 速率限制:实施速率限制以保护您的Airflow实例
API兼容性
此服务器与Apache Airflow 2.x REST API兼容。它已经过以下测试:
- 阿帕奇气流2.7+
- 天文学家软件和云
- 谷歌云编辑器2
- 亚马逊MWAA(所有支持的Airflow版本)
发展
# Clone the repository
git clone https://github.com/tomnagengast/mcp-server-airflow.git
cd mcp-server-airflow
# Install dependencies
npm install
# Build the project
npm run build
# Run in development mode
npm run dev贡献
欢迎投稿!请随时提交拉取请求。
许可证
MIT许可证-请参阅 许可证 文件以获取详细信息。
