云成本管理服务器

一款全面的云成本管理服务器,提供Kubernetes容器分析、预算预测和成本报告功能。

  • by eelzinaty
  • Python
  • 本地部署
  • 4 星标
  • 更新于
在 GitHub 查看 eelzinaty/cloudability-mcp-server

使用说明

云能力MCP服务器

用于Cloudability API的全面模型上下文协议(MCP)服务器,提供高级成本管理、Kubernetes容器分析和预算预测功能。

🌟 为什么使用此MCP服务器?

  • 🔥 集装箱优先:全面的Kubernetes成本分配和监控
  • 📊 高级分析:15个维度,8个指标,灵活的过滤和分组
  • 💰 预算管理:通过预测和警报完成预算生命周期
  • 🚀 生产就绪:全面测试覆盖、类型安全和错误处理
  • 🌍 多区域:支持美国、欧盟、亚太地区和中东地区的云计算能力
  • 🔐 灵活的身份验证:承载令牌、遗留基本身份验证或Frontdoor API密钥,用于自动获取令牌

🚀 快速开始

先决条件

  • Python 3.14+ (现代类型注释需要)
  • 紫外线 (推荐包管理器)
  • 可云性API访问 (中的承载令牌、API密钥或Frontdoor API密钥 .env)

安装

# Clone the repository
git clone https://github.com/eelzinaty/cloudability-mcp-server.git
cd cloudability-mcp-server

# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh

# Install dependencies
uv sync

# Set up configuration
cp .env.example .env
# Edit .env with your Cloudability credentials

# Run the server
uv run python main.py

MCP客户端配置

添加到MCP客户端配置中:

{
  "mcpServers": {
    "cloudability": {
      "command": "uv",
      "args": ["run", "python", "main.py"],
      "cwd": "/path/to/cloudability-mcp-server",
      "env": {
        "CLOUDABILITY_API_URL": "https://api.cloudability.com/v3",
        "CLOUDABILITY_ENVIRONMENT_ID": "your-environment-id",
        "CLOUDABILITY_DEFAULT_VIEW_ID": "12345",
        "CLOUDABILITY_KEY_ACCESS": "your-public-key",
        "CLOUDABILITY_KEY_SECRET": "your-private-key",
        "CLOUDABILITY_FRONTDOOR_URL": "https://frontdoor.apptio.com/service/apikeylogin"
      }
    }
  }
}

特性

🚀 API全面覆盖

  • 🔥 集装箱成本分配:完成Kubernetes成本分配和监控
  • 🔥 容器配置:群集设置和度量代理部署
  • 🔥 容器分析:使用模式、资源分配和效率分析
  • 🔥 容器发现:通过容器标签和资源库存_报告
  • 成本报告:灵活、强大的成本分析,包含15个维度和8个指标
  • 异步报告:将长时间运行的报告排队进行后台处理
  • 预算和预测:完成预算生命周期管理和支出预测
  • 预算订阅:预算阈值的电子邮件通知
  • 估计:本月支出预测及详细明细
  • 预测:具有置信区间的多月预测分析
  • 供应商账户:列出云提供商凭据帐户(AWS、Azure、GCP、IBM、OCI)

🔐 灵活的身份验证

  • 持有者令牌:现代apptio opentoken身份验证
  • 前门API钥匙:在环境中设置密钥时自动获取apptio opentoken
  • 基本认证:传统的API密钥身份验证
  • 多区域支持:美国、欧盟、亚太地区和ME Cloudability API和Frontdoor登录端点

📊 高级分析

  • 丰富的过滤:按集群、命名空间、工作负载类型等进行筛选
  • 灵活分组:按多个维度对结果进行分组
  • 排序和分页:高效处理大型数据集
  • 多种小部件类型:支持表格、KPI、图表和时间序列

🛠️ 开发者体验

  • FastMCP框架:现代MCP服务器实现
  • 综合测试:具有模拟功能的完整测试覆盖率
  • 类型安全:完整的类型注释,以获得更好的IDE支持
  • 整洁架构:独立的业务逻辑,便于维护

安装

此项目使用 uv 用于依赖性管理。确保你有 uv 安装。

# Install dependencies
uv sync

# Install development dependencies (for testing)
uv sync --dev

配置

创建 .env 文件基于 .env.example:

# Copy the example configuration
cp .env.example .env

环境变量

复制 .env.example 到 .env 并配置以下变量。

变量 必填 描述
CLOUDABILITY_API_URL 否(默认为美国) 您所在地区的Cloudability API基础URL
CLOUDABILITY_ENVIRONMENT_ID 是(用于承载身份验证) 来自访问管理的环境ID
CLOUDABILITY_KEY_ACCESS 否 用于自动获取代币的Frontdoor公共API密钥
CLOUDABILITY_KEY_SECRET 否 前门API私钥(与 CLOUDABILITY_KEY_ACCESS)
CLOUDABILITY_FRONTDOOR_URL 否(默认为美国) 您所在地区的Frontdoor API密钥登录端点
CLOUDABILITY_DEFAULT_VIEW_ID 是(适用于容器工具) 容器/报告和集群调用的默认视图ID
# Cloudability API Base URL (choose based on your region)
# US: https://api.cloudability.com/v3
# EU: https://api-eu.cloudability.com/v3
# APAC: https://api-au.cloudability.com/v3
# ME: https://api-me.cloudability.com/v3
CLOUDABILITY_API_URL=https://api.cloudability.com/v3

# Required for Bearer token authentication (apptio-opentoken)
# Get this from your Access Administration environment
CLOUDABILITY_ENVIRONMENT_ID=your-environment-id-here

# Optional: Frontdoor API keys for automatic apptio-opentoken acquisition
# When set, tools can omit the authorization parameter
# CLOUDABILITY_KEY_ACCESS=your-public-key-here
# CLOUDABILITY_KEY_SECRET=your-private-key-here

# Optional: Frontdoor login URL (defaults to US region)
# US: https://frontdoor.apptio.com/service/apikeylogin
# EU: https://frontdoor-eu.apptio.com/service/apikeylogin
# AU: https://frontdoor-au.apptio.com/service/apikeylogin
# CLOUDABILITY_FRONTDOOR_URL=https://frontdoor.apptio.com/service/apikeylogin

# Default view ID for containers API calls (required for containers/report and clusters)
CLOUDABILITY_DEFAULT_VIEW_ID=12345

区域端点

可云性API(CLOUDABILITY_API_URL)

区域 URL
美国 https://api.cloudability.com/v3
欧盟 https://api-eu.cloudability.com/v3
APAC https://api-au.cloudability.com/v3
我 https://api-me.cloudability.com/v3

前门登录(CLOUDABILITY_FRONTDOOR_URL)

区域 URL
美国 https://frontdoor.apptio.com/service/apikeylogin
欧盟 https://frontdoor-eu.apptio.com/service/apikeylogin
AU https://frontdoor-au.apptio.com/service/apikeylogin

认证方法

1.前门API密钥(可选,服务器端)

当 CLOUDABILITY_KEY_ACCESS 和 CLOUDABILITY_KEY_SECRET 设置后,服务器通过以下方式将它们交换为apptio opentoken CLOUDABILITY_FRONTDOOR_URL工具调用可以省略 authorization 参数。集 CLOUDABILITY_ENVIRONMENT_ID 同样,承载身份验证仍然需要它。

2.不记名代币

对于使用apptio opentoken的现代Cloudability环境:

  • 集 CLOUDABILITY_ENVIRONMENT_ID 在您的环境中
  • 通过 authorization: "Bearer your-apptio-opentoken" 工具调用(除非Frontdoor密钥为您处理身份验证)

3.基本身份验证(传统)

对于传统的API密钥验证:

  • 通过 authorization: "Basic your-api-key:" 工具调用
  • 无需环境ID

用法

运行服务器

uv run python main.py

MCP参考资源

建筑成本报告的只读参考数据(缓存,默认TTL 15分钟,通过 CLOUDABILITY_RESOURCE_CACHE_TTL_SECONDS):

URI 描述
cloudability://config 服务器配置(API URL,默认视图ID,身份验证模式-无机密)
cloudability://measures 成本报告维度和指标目录
cloudability://measures/allocated 由成本分配支持的措施
cloudability://filter-operators 筛选器运算符参考(==, =@, []=, …)
cloudability://saved-reports 节省的成本报告定义

使用 resources/read 在MCP客户端中,或调用匹配工具(get_available_measures等等)用于相同的数据。

可用工具

🔥 集装箱成本分配工具

provision_kubernetes_cluster

设置新集群 用于通过自动化Metrics Agent部署进行云能力监控。

get_cluster_deployment_yaml

获取部署配置 用于在集群中安装Cloudability Metrics代理。

containers_report

主要集装箱成本分配工具 --代替退休人员 /containers/allocations 和 /containers/counts API与 POST /v3/containers/report.

主要特点:

  • 共享资源分配:按命名空间、工作负载、集群和标签公平共享和分配成本指标
  • 弹性分组:命名空间、workload_type、workload_name、pod、标签等。
  • 过滤:集群UUID、命名空间、工作负载类型等

参数:

  • group:对维度进行分组(例如,[“命名空间”]、[“cldy:labels:team”])
  • metrics:报告指标(例如,[“总成本”、“总成本效率”])
  • filters:范围分析(例如,[“cluster==uuid”,“namespace==product”])
  • cost_type:“调整后”或“总计调整后摊销”

退货:

  • result.data 具有分组维度和度量的行
  • 分页通过 result.pagination.nextToken
get_container_resource_usage

日常使用趋势 用于容量规划和规模调整分析。

discover_container_labels

查找可用的Kubernetes标签 用于自定义成本分配分组。

get_detailed_cluster_info

全面的集群元数据 具有节点详细信息和数据收集状态。

🚀 主要工具

list_clusters

获取所有Kubernetes集群 以及它们的UUID和元数据。

list_budgets

获取所有预算配置 具有当前状态和阈值。

get_budget

获取详细的预算信息 包括支出跟踪和警报。

list_aws_accounts

获取AWS供应商凭据帐户 在Cloudability中配置(GET /v3/vendors/AWS/accounts?viewId=0).

list_azure_accounts

获取Azure供应商凭据帐户 在Cloudability中配置(GET /v3/vendors/azure/accounts?viewId=0).

list_gcp_accounts

获取GCP供应商凭据帐户 在Cloudability中配置(GET /v3/vendors/gcp/accounts?viewId=0).

list_ibm_accounts

获取IBM Cloud供应商凭据帐户 在Cloudability中配置(GET /v3/vendors/ibm/accounts?viewId=0).

list_oci_accounts

获取OCI供应商凭据帐户 在Cloudability中配置(GET /v3/vendors/oci/accounts?viewId=0).

📊 预算和预测工具

get_spending_estimate

本月支出预测 详细的服务明细和每日进度。

主要特点:

  • 基于本月迄今使用情况的实时支出估算
  • 服务级别支出驱动因素(AWS EC2、RDS等)
  • 每日累计支出进展
  • 与上月实际值比较
  • 速率限制:10个请求/用户/分钟,20/org/分钟

响应格式: 标准Cloudability v3信封。所有字段都在下面 result (非顶级):

{
  "result": {
    "estimatedSpend": 35615446.36,
    "previousMonthSpend": 33819722.57,
    "previousMonthFinalized": true,
    "currentDate": "2026-05-24",
    "cumulativeMtdSpend": [{"date": "2026-05-01", "spend": 1615288.82}],
    "details": [
      {
        "serviceName": "Azure Compute",
        "estimatedSpend": 5568954.48,
        "mtdSpend": 4128094.29,
        "previousMonthSpend": 5409219.11,
        "usageFamily": "Instance Usage"
      }
    ]
  }
}

使用 result["details"] 用于供应商或服务故障(例如,过滤器线路 serviceName 以...开始 Azure).省略 view_id 或从 cloudability://config 资源 default_view_id 使用服务器配置的默认视图时(0 指所有组织成本数据)。

get_spending_forecast

多月预测分析 具有置信区间和历史比较。

参数:

  • months_back (3-24):建模的历史数据
  • months_forward (1-24):预测期
  • use_current_estimate:在模型中包含当前月份
  • remove_credits:从分析中排除学分
  • remove_one_time_charges:过滤掉一次性成本

退货: Cloudability v3信封,其中包含以下预测数据 result (例如 result.forecast, result.forecastDetail, result.actual, result.parameters).

create_new_budget

创建预算 具有每月阈值和成本基础配置。

modify_budget

更新现有预算 具有新的阈值或配置更改。

remove_budget

删除预算 从系统中永久删除。

create_budget_alert

设置电子邮件通知 违反预算门槛。

通知类型:

  • notify_exceeded:实际支出超过预算时发出警报
  • notify_expected:当预计支出超过预算时发出警报
list_budget_alerts

列出预算订阅 以及通知偏好。

modify_budget_alert / remove_budget_alert

更新或删除 预算通知订阅。

📊 成本报告工具

execute_cost_report

主要成本报告工具 具有高级分析和灵活的配置。

主要特点:

  • 多达15个维度:供应商、地区、服务名称、资源标识符等。
  • 多达8个指标:总成本、摊销成本、使用小时等。
  • 高级过滤:12个运算符(==、!=、>、<、=@、[]=等)
  • 灵活排序:带ASC/DESC的多列
  • 自动分页:处理10000+行数据集
  • 相对日期:“上个月初”,“本季度末”
  • 图表格式:基于日期的可视化支持
  • 视图集成:应用已保存的视图和分配

参数:

  • dimensions:分组维度列表(最多15个)
  • metrics:成本/使用指标列表(最多8个)
  • filters:筛选表达式(例如,[“transaction_type==用法”])
  • sort:对表达式进行排序(例如,[“total_costDESC”])
  • start_date/end_date:日期范围或相对日期
  • view_id:应用已保存的视图(0表示无限制)
  • apply_allocations:包括员额分配费用
queue_cost_report

异步报告 用于长时间运行或复杂的报告。

优点:

  • 后台处理:无超时限制
  • 大型数据集:分页为30000行vs 10000行
  • 状态跟踪:监控进度 check_report_status
  • 速率限制:每个用户保护20个请求
list_saved_cost_reports

发现可用报告 具有完整的配置和共享信息。

get_available_measures

探索维度和指标 具有详细的元数据和分组。

get_filter_operators

参考指南 对于所有支持的过滤器比较运算符。

check_report_status / get_queued_report_results

管理异步报告 从排队到完成。

测试

运行测试套件:

# Run unit tests (mocked HTTP; integration tests are skipped by default)
uv run pytest tests/ -v

# Run tests with coverage
uv run pytest tests/ -v --cov=main --cov-report=term-missing

# Run live MCP + Cloudability API integration tests (requires .env credentials)
cp .env.example .env   # set CLOUDABILITY_KEY_ACCESS, CLOUDABILITY_KEY_SECRET, CLOUDABILITY_ENVIRONMENT_ID
uv run pytest tests/test_mcp_integration.py -v -m integration

集成测试涵盖了10个核心只读MCP工具(预算、成本报告、容器)、3个在租户无法访问时跳过的可选工具,以及一个链式工具 get_budget 测试。

例子

集装箱成本分配(最重要!)

# Provision a new cluster for monitoring
cluster = provision_kubernetes_cluster(
    cluster_name="production-k8s",
    kubernetes_version="1.28",
    authorization="Bearer your-token"
)

# Get deployment YAML for Metrics Agent
deployment = get_cluster_deployment_yaml(
    cluster_id=cluster["result"]["id"],
    authorization="Bearer your-token"
)
# Save deployment["deployment_yaml"] and apply: kubectl apply -f deployment.yaml

# Analyze cost allocation by namespace (primary use case)
allocation = containers_report(
    start_date="2024-01-01",
    end_date="2024-01-31",
    group=["namespace"],
    metrics=["total_cost", "total_cost_efficiency"],
    authorization="Bearer your-token"
)

# Team-based cost allocation using labels
team_costs = containers_report(
    start_date="2024-01-01",
    end_date="2024-01-31",
    group=["cldy:labels:team", "namespace"],
    metrics=["total_cost", "total_cost_allocated"],
    filters=["cluster==your-cluster-uuid"],
    cost_type="total_adjusted_amortized",
    authorization="Bearer your-token"
)

# Workload-level cost breakdown for a namespace
service_costs = containers_report(
    start_date="2024-01-01",
    end_date="2024-01-31",
    group=["workload_name", "workload_type"],
    metrics=["total_cost"],
    filters=["namespace==production", "cluster==your-cluster-uuid"],
    authorization="Bearer your-token"
)

集装箱资源分析

# Daily usage trends for capacity planning
usage_trends = get_container_resource_usage(
    start_date="2024-01-01",
    end_date="2024-01-31",
    metrics=["cpu/reserved", "memory/reserved_rss", "filesystem/usage"],
    filters=["cluster==your-cluster-uuid"],
    authorization="Bearer your-token"
)

# Discover available labels for cost allocation
labels = discover_container_labels(
    start_date="2024-01-01",
    end_date="2024-01-31",
    filters=["cluster==your-cluster-uuid"],
    authorization="Bearer your-token"
)

# Get detailed cluster information
cluster_info = get_detailed_cluster_info(
    start_date="2024-01-01",
    end_date="2024-01-31",
    authorization="Bearer your-token"
)

基本成本分析

# Get cost breakdown by namespace for the last month
result = containers_report(
    start_date="2024-01-01",
    end_date="2024-01-31",
    metrics=["total_cost", "total_cost_efficiency"],
    group=["namespace"],
    authorization="Bearer your-token"
)

高级筛选

# Get cost data for specific cluster and production workloads
result = containers_report(
    start_date="2024-01-01",
    end_date="2024-01-31",
    filters=[
        "cluster==your-cluster-uuid",
        "namespace==production",
        "workload_type[]=deployment,statefulset"
    ],
    group=["workload_name"],
    sort=[{"sortMetric": "total_cost", "sortOrder": "desc"}],
    limit=10,
    authorization="Bearer your-token"
)

预算监控

# Check all budgets and their current status
budgets = list_budgets(authorization="Basic your-api-key:")

# Get detailed information for a specific budget
budget_details = get_budget(
    budget_id="budget-123",
    authorization="Basic your-api-key:"
)

支出估算和预测

# Get current month spending estimate (fields are under result)
estimate = get_spending_estimate(
    view_id="0",
    basis="cash",
    authorization="Bearer your-token"
)
summary = estimate["result"]
print(summary["estimatedSpend"], summary["currentDate"])

# Azure month-end estimate from service breakdown
azure_estimate = sum(
    row["estimatedSpend"]
    for row in summary["details"]
    if row["serviceName"].startswith("Azure")
)

# Generate 12-month forecast based on 6 months of history
forecast = get_spending_forecast(
    view_id="0",
    basis="cash",
    months_back=6,
    months_forward=12,
    use_current_estimate=True,
    remove_one_time_charges=True,
    authorization="Bearer your-token"
)
forecast_rows = forecast["result"]["forecast"]

预算管理

# Create a quarterly budget
budget = create_new_budget(
    name="Q1 2024 Budget",
    basis="adjusted",
    view_id="0",
    months=[
        {"month": "2024-01", "threshold": 50000},
        {"month": "2024-02", "threshold": 55000},
        {"month": "2024-03", "threshold": 60000}
    ],
    authorization="Bearer your-token"
)

# Set up budget alerts
alert = create_budget_alert(
    budget_id=budget["result"]["id"],
    notify_exceeded=True,
    notify_expected=True,
    authorization="Bearer your-token"
)

# Update budget thresholds
updated_budget = modify_budget(
    budget_id=budget["result"]["id"],
    months=[
        {"month": "2024-01", "threshold": 45000},
        {"month": "2024-02", "threshold": 50000}
    ],
    authorization="Bearer your-token"
)

高级成本报告

# Basic cost breakdown by vendor and region
basic_report = execute_cost_report(
    start_date="2024-01-01",
    end_date="2024-01-31",
    dimensions=["vendor", "region"],
    metrics=["total_amortized_cost", "usage_hours"],
    authorization="Bearer your-token"
)

# Advanced filtering and sorting
filtered_report = execute_cost_report(
    start_date="beginning of last month",
    end_date="end of last month",
    dimensions=["vendor", "service_name", "resource_identifier"],
    metrics=["total_amortized_cost", "total_cost_efficiency"],
    filters=[
        "transaction_type==usage",
        "total_amortized_cost>100",
        "region=@us-east"
    ],
    sort=["total_amortized_costDESC", "vendorASC"],
    limit=1000,
    authorization="Bearer your-token"
)

# Asynchronous reporting for large datasets
report_id = queue_cost_report(
    start_date="2024-01-01",
    end_date="2024-03-31",
    dimensions=["resource_identifier", "service_name", "region"],
    metrics=["total_amortized_cost", "usage_hours"],
    filters=["vendor==Amazon"],
    authorization="Bearer your-token"
)

# Check status and retrieve results
status = check_report_status(
    report_id=report_id["id"],
    authorization="Bearer your-token"
)

if status["status"] == "finished":
    results = get_queued_report_results(
        report_id=report_id["id"],
        authorization="Bearer your-token"
    )

# Discover available dimensions and metrics
measures = get_available_measures(authorization="Bearer your-token")
operators = get_filter_operators(authorization="Bearer your-token")

发展

项目结构

cloudability-mcp-server/
├── main.py                      # Main MCP server with tool definitions
├── cloudability_tools.py        # Core API implementation
├── tests/
│   └── test_cloudability_tools.py # Comprehensive API tests
├── .env.example                 # Environment configuration template
├── pyproject.toml              # Project configuration
└── README.md                   # This documentation

建筑

  • main.py:使用FastMCP装饰器清理MCP工具定义
  • cloudability_tools.py:分离的业务逻辑,全面覆盖API
  • 灵活的身份验证:支持Bearer令牌和基本身份验证
  • 综合测试:HTTP模拟的完整测试覆盖率
  • 类型安全:完整的类型注释,以获得更好的开发体验

🤝 贡献

我们欢迎捐款!请查看我们的 贡献指南 了解详情。

快速贡献步骤

  1. 分叉存储库
  2. 创建要素分支: git checkout -b feature/amazing-feature
  3. 通过测试进行更改
  4. 运行质量检查: uv run black . && uv run pytest
  5. 提交拉取请求

开发环境

# Clone your fork
git clone https://github.com/eelzinaty/cloudability-mcp-server.git
cd cloudability-mcp-server

# Install development dependencies
uv sync --group dev

# Run tests
uv run pytest tests/ -v

# Format code
uv run black . && uv run isort .

📚 文档

🐛 问题与支持

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

🙏 致谢

🔗 相关项目


由以下材料制成❤️ 面向FinOps和Kubernetes社区