关于 infracost
infracost 是通过 Infracost CLI 估算、比较和发布基础设施即代码成本变化的 MCP 服务,面向云基础设施开发与审查流程。它提供成本报告、拉取请求评论及 CLI 配置等工具。
主要功能
- 01生成基础设施成本基线
- 02比较计划变更的成本差异
- 03渲染 JSON、表格或 HTML 报告
- 04发布拉取请求成本评论
- 05上传报告并管理 CLI 配置
- 06生成用量文件模板
适用场景
- 01评估 Terraform 项目的月度成本
- 02审查拉取请求中的成本影响
- 03将成本报告上传至 Infracost Cloud
- 04为资源类型生成用量文件
MCP 相关
该 MCP 服务向客户端提供 8 种 Infracost CLI 工具,用于成本估算、差异比较、报告、协作和配置操作。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
8收录的工具列表 · 更新于 2 周前
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| path | string | 否 | Terraform/IaC 目录或计划 JSON 文件的路径。 |
| apiKey | string | 否 | Infracost API 密钥(也可使用 INFRACOST_API_KEY 环境变量)。 |
| fields | array | 否 | 要包含在输出中的字段(all、price、monthlyQuantity、unit、hourlyCost、monthlyCost)。 |
| format | string | 否 | 输出格式:table、json、html、diff、github-comment、gitlab-comment、azure-repos-comment、slack-message 等。 |
| showSkus | boolean | 否 | 在输出中显示 SKU 详细信息。 |
| compareTo | string | 否 | 用于比较的基准 Infracost JSON 文件路径。 |
| extraArgs | array | 否 | 其他 CLI 参数。 |
| usageFile | string | 否 | 用于资源用量估算的 Infracost 用量文件路径。 |
| configFile | string | 否 | Infracost 配置文件路径。 |
| syncUsageFile | boolean | 否 | 将用量文件与缺失的资源同步。 |
| timeoutSeconds | number | 否 | 命令超时时间(秒)。 |
| workingDirectory | string | 否 | 命令执行的目录。默认为当前目录。 |
| terraformPlanFlags | string | 否 | 传递给 'terraform plan' 的标志。 |
| terraformWorkspace | string | 否 | 要使用的 Terraform 工作区。 |
详细介绍
Infracost MCP 服务
**注意:**这是一个由社区维护的 MCP 服务,并非 Infracost 官方提供的 Model Context Protocol 服务。
一个 Model Context Protocol(MCP)服务,用于运行 Infracost CLI,以估算、比较并发布 Terraform 和其他基础设施即代码项目中的基础设施成本变更。
概览
此服务提供八种 MCP 工具,涵盖估算、报告、协作和运维工作流:
infracost_breakdown:生成基准成本估算。infracost_diff:比较计划中的成本变更。infracost_output:以不同格式呈现现有的 Infracost JSON。infracost_comment:在支持的 SCM 平台上发布 PR 评论。infracost_upload:将报告上传到 Infracost Cloud。infracost_configure:获取或设置 CLI 配置。infracost_auth:运行身份验证流程。infracost_generate_usage:使用合理的默认值生成用量文件。
平台兼容性
此 MCP 服务与平台无关,可在本地和 CI 环境中运行:
- 本地开发
- GitHub Actions
- GitLab CI/CD
- Azure DevOps
- CircleCI、Jenkins 和 Bitbucket Pipelines
- AI Agent 和 Copilot 工作流
主要依赖项是 Node.js 和 Infracost CLI 二进制文件。
工具矩阵
| 工具 | 类别 | 典型结果 |
|---|---|---|
infracost_breakdown | 估算 | 当前 IaC 的每月成本基准 |
infracost_diff | 比较 | 计划变更造成的成本差异 |
infracost_output | 报告 | 呈现为表格/JSON/HTML/评论的输出 |
infracost_comment | 协作 | 包含估算影响的 PR 评论 |
infracost_upload | 云端同步 | 可在 Infracost Cloud 中查看的成本报告 |
infracost_configure | 配置 | 已保存的 CLI 配置更新/读取结果 |
infracost_auth | 身份验证 | Infracost Cloud 登录流程 |
infracost_generate_usage | 实用工具 | 已填入合理默认值的用量文件模板 |
工具
infracost_breakdown
运行 infracost breakdown。
输入:
workingDirectory(string,可选):命令的工作目录。path(string,可选):Terraform/IaC 目录或计划 JSON 的路径。configFile(string,可选):Infracost 配置文件的路径。usageFile(string,可选):用量文件的路径。format(string,可选):输出格式。默认值:table。fields(string[],可选):要包含在输出中的字段。showSkus(boolean,可选):包含 SKU 详情。syncUsageFile(boolean,可选):将用量文件与检测到的资源同步。compareTo(string,可选):基准 Infracost JSON 路径。terraformWorkspace(string,可选):Terraform 工作区。terraformPlanFlags(string,可选):传递给terraform plan的标志。timeoutSeconds(number,可选):超时时间(秒)。extraArgs(string[],可选):其他 CLI 参数。
infracost_diff
运行 infracost diff。
输入:
workingDirectory(字符串,可选):命令工作目录。path(字符串,可选):Terraform/IaC 目录或计划 JSON 文件的路径。configFile(字符串,可选):Infracost 配置文件的路径。usageFile(字符串,可选):用量文件的路径。format(字符串,可选):输出格式。默认值:table。compareTo(字符串,可选):基准 Infracost JSON 路径。showSkus(布尔值,可选):包含 SKU 详情。syncUsageFile(布尔值,可选):将用量文件与检测到的资源同步。terraformWorkspace(字符串,可选):Terraform 工作区。terraformPlanFlags(字符串,可选):terraform plan的标志。timeoutSeconds(数字,可选):超时时间(秒)。extraArgs(字符串数组,可选):其他 CLI 参数。
infracost_output
运行 infracost output。
输入:
workingDirectory(字符串,可选):命令工作目录。path(字符串,可选):Infracost JSON 文件路径。省略时自动检测。format(字符串,可选):输出格式。默认值:table。fields(字符串数组,可选):要包含在输出中的字段。showSkus(布尔值,可选):包含 SKU 详情。timeoutSeconds(数字,可选):超时时间(秒)。extraArgs(字符串数组,可选):其他 CLI 参数。
infracost_comment
运行 infracost comment。
输入:
workingDirectory(字符串,可选):命令工作目录。platform(字符串,可选):平台(github、gitlab、azure-repos、bitbucket)。默认值:github。path(字符串,可选):Infracost JSON 文件路径。省略时自动检测。repo(字符串,可选):代码仓库(owner/repo)。pullRequest(字符串,可选):拉取请求编号。commit(字符串,可选):提交 SHA。token(字符串,可选):平台令牌。tag(字符串,可选):评论标签。behavior(字符串,可选):评论更新行为。dryRun(布尔值,可选):生成评论但不发布。timeoutSeconds(数字,可选):超时时间(秒)。extraArgs(字符串数组,可选):其他 CLI 参数。
infracost_upload
运行 infracost upload。
输入:
workingDirectory(字符串,可选):命令工作目录。path(字符串,可选):Infracost JSON 文件路径。省略时自动检测。timeoutSeconds(数字,可选):超时时间(秒)。extraArgs(字符串数组,可选):其他 CLI 参数。
infracost_configure
运行 infracost configure。
输入:
workingDirectory(字符串,可选):命令工作目录。set(字符串,可选):设置配置值。get(字符串,可选):读取配置值。timeoutSeconds(数字,可选):超时时间(秒)。extraArgs(字符串数组,可选):其他 CLI 参数。
infracost_auth
运行 infracost auth login。
输入:
workingDirectory(字符串,可选):命令工作目录。timeoutSeconds(数字,可选):超时时间(秒)。extraArgs(字符串数组,可选):其他 CLI 参数。
infracost_generate_usage
为指定资源类型生成包含合理默认值的 Infracost 用量文件。这是一款实用工具,可与 infracost_configure 搭配使用,为你提供可根据工作负载自定义的预填充模板。返回原始 JSON 和格式化的使用指南。
输入:
resourceTypes(string[],必填):要包含的资源类型(例如,["aws_lambda_function", "aws_s3_bucket"])。outputFilePath(string,可选):保存用量文件的路径(仅供指南参考)。includeGuidance(boolean,可选):是否包含格式化的使用指南。默认值:true。onlyJson(boolean,可选):是否仅返回原始 JSON(不含使用指南)。默认值:false。
支持的资源类型:aws_lambda_function、aws_instance、aws_db_instance、aws_s3_bucket、aws_dynamodb_table、aws_ebs_volume、aws_cloudfront_distribution、aws_lb、aws_nat_gateway、google_compute_instance、google_storage_bucket、azurerm_windows_virtual_machine、azurerm_linux_virtual_machine、azurerm_storage_account、azurerm_mssql_database。
提示词手册
在 Copilot Chat 中使用 @infracost 搭配这些简短提示词。
未指定路径时,CLI 工具默认使用当前工作区根目录。
如果将文件或文件夹添加为 Copilot 上下文(#file 或 #folder),请在提示词中引用它,工具将以该路径为目标。
服务会自动检测常见的报告文件(infracost-diff.json、infracost-base.json、infracost.json),因此通常无需为 infracost_output、infracost_comment 或 infracost_upload 传入 path。
快速提示词
@infracost run infracost_breakdown
@infracost run infracost_diff
@infracost run infracost_output
@infracost run infracost_comment with dryRun true
@infracost run infracost_upload
运行成本明细(infracost_breakdown)
@infracost run infracost_breakdown
预期输出:基准成本估算,包含命令输出、退出代码、stdout 和 stderr。
运行差异分析(infracost_diff)
@infracost run infracost_diff
预期输出:计划变更相较于当前基准的成本差额。
渲染输出(infracost_output)
@infracost run infracost_output with format html
预期输出:根据现有 Infracost JSON 生成的格式化输出(如果可能,将自动检测)。
发布 PR 评论(infracost_comment)
@infracost run infracost_comment with repo owner/repo and pullRequest 123 and dryRun true
预期输出:生成的评论负载;若未启用试运行模式,则为已发布的评论。平台默认为 github。
上传报告(infracost_upload)
@infracost run infracost_upload
预期输出:确认报告已上传至 Infracost Cloud。
配置 CLI(infracost_configure)
@infracost run infracost_configure with:
- set: currency=USD
预期输出:CLI 返回的配置更新确认信息。
身份验证(infracost_auth)
@infracost run infracost_auth
预期输出:身份验证流程提示或登录成功详情。
生成用量文件(infracost_generate_usage)
@infracost run infracost_generate_usage with:
- resourceTypes: ["aws_lambda_function", "aws_s3_bucket", "aws_dynamodb_table"]
- outputFilePath: ./infracost-usage.json
- includeGuidance: true
预期输出:包含合理默认值的预填充用量文件 JSON,以及有关自定义和使用方法的指南。使用 onlyJson: true 可仅返回 JSON,以便通过程序处理。
依赖项
系统依赖项
- Node.js
>=24.14.0 - npm(随 Node.js 捆绑)
- 已安装且可在
PATH中使用的 Infracost CLI(或通过INFRACOST_BINARY_PATH指定)
npm 依赖项
运行时:
@modelcontextprotocol/sdk(MCP 服务 SDK)
开发:
typescript(构建/编译)tsx(开发运行器)vitest(单元测试运行器)@types/node(Node.js 类型定义)
环境变量
INFRACOST_API_KEY:用于云端功能的 Infracost API 密钥。INFRACOST_BINARY_PATH:覆盖 Infracost 二进制文件的路径。
安装
npm install
npm run build
配置
在 VS Code Copilot Chat 中使用
此工作区已在 .vscode/mcp.json 中预先配置:
{
"servers": {
"infracost": {
"type": "stdio",
"command": "node",
"args": ["./dist/index.js"]
}
}
}
更改 MCP 配置后,请重新加载 VS Code(Cmd+Shift+P -> Developer: Reload Window)。
然后在 Copilot Chat 中使用 @infracost 查询服务,例如:
@infracost run infracost_breakdown
@infracost run infracost_diff
在其他 MCP 客户端中使用
使用 stdio 传输,并指定已构建的入口点:
{
"name": "infracost-mcp-server",
"type": "stdio",
"command": "node",
"args": ["/absolute/path/to/infracost-mcp/dist/index.js"]
}
先运行 npm run build 进行构建,然后启动 MCP 客户端。
运行
npm start
开发模式:
npm run dev
构建模式:
npm run start
调试
使用 .vscode/launch.json:
Debug MCP Server(运行npm run dev)Debug MCP Server (Built)(构建后运行dist/index.js)
在 src/index.ts 中设置断点,然后按 F5。
测试
请参阅 docs/TESTING.md,了解 Copilot Chat 场景、手动 JSON-RPC 检查和故障排除指南。
Copilot Chat 中的快速验证提示词:
@infracost run infracost_breakdown
交互式 VS Code 工作流
1. PR 成本影响审查
- 生成基线:
@infracost run infracost_breakdown with:
- path: .
- format: json
- 生成差异:
@infracost run infracost_diff with:
- path: .
- format: diff
- 起草 PR 评论:
@infracost run infracost_comment with:
- platform: github
- path: infracost-diff.json
- repo: owner/repo
- pullRequest: 123
- dryRun: true
2. 报告流水线设置
- 为人类读者生成报告:
@infracost run infracost_output with:
- path: infracost-diff.json
- format: html
- 上传以进行集中跟踪:
@infracost run infracost_upload with:
- path: infracost-diff.json
3. 环境引导
- 配置默认值:
@infracost run infracost_configure with:
- set: currency=USD
- 进行身份验证:
@infracost run infracost_auth
最佳实践
- 在单体仓库中运行时,明确设置
workingDirectory。 - 使用
--format json进行机器处理,使用--format table进行人工排查。 - 设置足够高的
timeoutSeconds,以处理大型 Terraform 计划。 - 在向生产 PR 讨论串发帖前,优先使用
dryRun=true的infracost_comment。 - 使用
infracost_upload在 Infracost Cloud 中查看历史记录。
其他用例
- 在合并前的拉取请求中设置成本门禁。
- 在 CI 中定期生成基线成本快照。
- 为发布列车生成成本变动报告。
- 基于上传的报告创建团队级 FinOps 仪表板。
相关项目
Infracost 资源
Model Context Protocol
社区与贡献
署名与许可证
由 Carl Dawson 在 Down At The Bottom Of The Mole Hole 组织下维护。
开发
npm run lint
npm test
npm run build
许可证
依据 MIT 许可证授权。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。
