GCP成本MCP服务器
> 备注:这是一个非官方项目,不隶属于谷歌或Genkit团队,也不得到谷歌或Genkit团队的认可或支持。
用于估算Google Cloud运行成本的MCP(模型上下文协议)服务器。
而不是手动使用 谷歌云定价计算器,您可以直接从Claude Desktop、Gemini CLI或Cursor等AI助手获得GCP成本估算。
特性
可用工具
| 工具 | 说明 |
|---|---|
get_estimation_guide |
从这里开始! 根据SKU分析为任何GCP服务动态生成估算指南 |
list_services |
列出所有可用的Google Cloud服务及其ID |
list_skus |
列出特定服务的SKU(可计费项目) |
get_sku_price |
获取特定SKU的定价详细信息 |
estimate_cost |
根据SKU和使用量计算成本, 自动扣除免费等级 |
工具关系
每个工具都是 独立和无国籍AI助手根据上下文自主决定调用哪些工具以及调用顺序:```mermaid
graph TB
guide["get_estimation_guide
─────────────────
IN: service_name
OUT: required params, pricing
factors, free tier info, tips
─────────────────
Internally resolves
service & SKU lookup"]
services["list_services
─────────────────
IN: name filter (opt)
OUT: service_id, display_name"]
skus["list_skus
─────────────────
IN: service_id, region, keyword
OUT: sku_id, display_name,
categories, regions"]
price["get_sku_price
─────────────────
IN: sku_id, currency
OUT: price/unit, pricing tiers"]
cost["estimate_cost
─────────────────
IN: sku_id, usage_amount
OUT: estimated cost with
automatic free tier deduction
─────────────────
Internally resolves pricing"]
services -- "service_id" --> skus
skus -- "sku_id" --> price
skus -- "sku_id" --> cost
> **实心箭头** 显示数据流——一个工具的输出为另一个工具提供输入。 `get_estimation_guide` 和 `estimate_cost` 是 **独立的**:它们在内部解决了自己的依赖关系,减少了往返。
|用例|人工智能如何使用工具|
|----------|----------------------|
| **快速估算** | `get_estimation_guide` → 收集用户需求→ `estimate_cost` |
| **多业务** |多个 `get_estimation_guide` + `estimate_cost` 电话 **并行** |
| **探索定价** | `list_services` → `list_skus` → `get_sku_price` |
| **直接计算** | `estimate_cost` 具有已知的SKU ID|
### 支持服务列表
`get_estimation_guide` 与...合作 **任何谷歌云服务**:
- **动态指南生成**:通过分析云计费目录API中的SKU动态生成指南
- **免费等级信息**:自动从GCP文档中提取并包含在指南中
- **普遍覆盖**:适用于所有GCP服务-没有硬编码的服务列表
该工具分析可用SKU以确定:
- 所需参数(区域、实例类型、存储等)
- 定价因素和计费维度
- 免费等级配额(如果可用)
- 成本优化提示
## 快速开始
### 先决条件
- 谷歌云SDK(`gcloud`)已安装
- 已配置应用程序默认凭据
> **备注**:无需设置Google Cloud项目或启用API。此服务器使用OAuth身份验证访问公共定价数据。
### 1.设置身份验证```bash
gcloud auth application-default login
2.安装
选择最适合您环境的安装方法:
选项A:自制(macOS/Linux)——推荐
在macOS或Linux上安装的最简单方法:```bash
brew tap nozomi-koborinai/tap
brew install gcp-cost-mcp-server
二进制文件将安装到 `/opt/homebrew/bin/gcp-cost-mcp-server` (苹果硅)或 `/usr/local/bin/gcp-cost-mcp-server` (英特尔/Linux)。
**升级到最新版本:**```bash
# Update tap to fetch the latest Formula
brew update
# Check the available version
brew info gcp-cost-mcp-server
# Upgrade to the latest version
brew upgrade gcp-cost-mcp-server
> 备注:始终运行 brew update 首先,确保您获得最新版本。如果没有它,Homebrew将使用缓存的配方信息。
选项B:下载预构建的二进制文件
下载自 GitHub发布 对于您的平台:
| 二进制 | 平台 | 架构 |
|---|---|---|
gcp-cost-mcp-server-darwin-arm64 |
macOS | 苹果硅(M1/M2/M3/M4) |
gcp-cost-mcp-server-darwin-amd64 |
macOS | 英特尔 |
gcp-cost-mcp-server-linux-amd64 |
Linux | x86_64 |
gcp-cost-mcp-server-windows-amd64.exe |
Windows | x86_64 |
> macOS用户:下载后,删除隔离属性:
>
>bash
> chmod +x gcp-cost-mcp-server-darwin-*
> xattr -d com.apple.quarantine gcp-cost-mcp-server-darwin-*
>
选项C:从源代码构建
需要Go 1.21+:```bash
git clone https://github.com/nozomi-koborinai/gcp-cost-mcp-server.git
cd gcp-cost-mcp-server
go build -o gcp-cost-mcp-server .
### 3.配置您的MCP客户端
先找到你的二进制路径:```bash
# If installed via Homebrew
which gcp-cost-mcp-server
# Output: /opt/homebrew/bin/gcp-cost-mcp-server (Apple Silicon)
# /usr/local/bin/gcp-cost-mcp-server (Intel/Linux)
克劳德桌面
增添 ~/Library/Application Support/Claude/claude_desktop_config.json:```json
{
"mcpServers": {
"gcp-cost": {
"command": "/opt/homebrew/bin/gcp-cost-mcp-server"
}
}
}
#### 光标
增添 `.cursor/mcp.json` 在您的项目或全局配置中:```json
{
"mcpServers": {
"gcp-cost": {
"command": "/opt/homebrew/bin/gcp-cost-mcp-server"
}
}
}
Gemini CLI
增添 ~/.gemini/settings.json:```json
{
"mcpServers": {
"gcp-cost": {
"command": "/opt/homebrew/bin/gcp-cost-mcp-server"
}
}
}
> **备注**:替换 `/opt/homebrew/bin/gcp-cost-mcp-server` 如果不同,请使用实际的二进制路径。
## 使用示例
### 基本成本估算(对话式)
从一个简单的问题开始——人工智能将指导你完成细节:```
You: "How much would Cloud Run cost for 2 instances?"
AI: "To estimate Cloud Run costs accurately, I need a few more details:
- Region: Where will this be deployed? (e.g., asia-northeast1, us-central1)
- vCPU: How many vCPUs per instance? (e.g., 1, 2, 4)
- Memory: How much memory per instance? (e.g., 1 GiB, 2 GiB)
- Billing type: Instance-based or request-based?
- Monthly usage: Will it run 24/7 (730 hours) or less?"
You: "Tokyo region, 1 vCPU, 2GB memory, instance-based, 24/7"
AI: "Based on your requirements for 2 Cloud Run instances in Tokyo (asia-northeast1):
📊 Cost Breakdown:
- vCPU: 2 vCPU × 2,628,000 sec = 5,256,000 vCPU-seconds
Free tier applied: -180,000 vCPU-seconds
Billable: 5,076,000 vCPU-seconds × $0.000024 = $121.82/month
- Memory: 4 GiB × 2,628,000 sec = 10,512,000 GiB-seconds
Free tier applied: -360,000 GiB-seconds
Billable: 10,152,000 GiB-seconds × $0.0000025 = $25.38/month
💰 Total estimated: ~$147.20/month (free tier already applied)
💡 Tip: Cloud Run's free tier (180,000 vCPU-sec + 360,000 GiB-sec/month)
has been automatically deducted from your estimate."
探索服务和SKU```
You: "What's the service ID for Compute Engine?"
You: "List the available SKUs for Compute Engine in Tokyo region"
You: "How much would an n2-standard-4 instance cost for 730 hours?"
### 任何GCP服务
所有服务均支持动态指南生成:```
You: "How much would Vertex AI cost for my ML training?"
AI: "I'll generate an estimation guide for Vertex AI by analyzing available SKUs...
Based on my analysis, to estimate Vertex AI costs, I need:
1. Region: Where will training run?
2. Machine type: What accelerator type? (e.g., NVIDIA T4, A100)
3. Training hours: How long will training take?
4. Storage: How much training data?
Free tier note: Vertex AI offers $300 in free credits for new users.
Would you like me to list the specific SKUs available for Vertex AI?"
架构图估算(多服务)
上传架构图并获得完整的成本明细:```
You: [Upload architecture diagram image]
"Please estimate the monthly cost for this architecture"
AI: "I've analyzed your architecture diagram. Here are the GCP services identified:
- Cloud Run (Application layer)
- Cloud SQL (Database)
- Cloud Storage (Static files)
- Cloud Load Balancing (Traffic distribution)
To provide an accurate estimate, I need some details:
🌏 Common Settings:
- Region: Is asia-northeast1 (Tokyo) okay?
📦 Cloud Run:
- vCPUs: 1? 2? 4?
- Memory: 1GB? 2GB? 4GB?
- Instance count: How many instances?
- Uptime: 24/7 or specific hours only?
🗄️ Cloud SQL:
- Database type: MySQL? PostgreSQL?
- Machine type: Dev (db-f1-micro)? Production (db-custom-2-4096)?
- Storage: How many GB needed?
- High Availability (HA): Required?"
You: "Tokyo region, Cloud Run with 1vCPU/2GB × 2 instances running 24/7,
Cloud SQL PostgreSQL db-custom-2-4096, 100GB SSD with HA,
Cloud Storage 50GB Standard"
AI: "Thank you! Here's your estimate (free tiers automatically applied):
| Service | Configuration | Monthly Cost |
|---|---|---|
| Cloud Run | 2 × 1vCPU × 2GB (24/7) | $147.20 ✨ |
| Cloud SQL | PostgreSQL, 2vCPU/4GB, 100GB HA | $178.50 |
| Cloud Storage | 50GB Standard | $0.00 ✨ |
| Cloud Load Balancing | Base + traffic | $18.00 |
| Total | $343.70 |
✨ = Free tier applied
💡 Cost Optimization Tips:
- Cloud Run: Set min instances to 0 to reduce idle costs
- Cloud SQL: Disable HA for ~50% savings (dev environments)
- Consider 1yr/3yr Committed Use Discounts (CUDs) for up to 57% off"
此工作流程:
1. AI分析架构图以识别GCP服务
1. AI首先询问共享参数(地区),然后询问服务特定的详细信息
1. AI使用MCP工具计算每项服务的成本
1. AI提供了一个包含总体和优化提示的综合细分
## 身份验证选项
### 使用应用程序默认凭据(推荐)
这是个人使用的最简单的选择:```bash
gcloud auth application-default login
使用服务帐户
对于生产或自动化环境,请使用服务帐户:```json
{
"mcpServers": {
"gcp-cost": {
"command": "/path/to/gcp-cost-mcp-server",
"env": {
"GOOGLE_APPLICATION_CREDENTIALS": "/path/to/service-account.json"
}
}
}
}
**所需IAM角色** (仅适用于定制/合同定价):
- `roles/billing.viewer` (账单账户查看器)
> 对于公开定价数据,不需要IAM角色。
______________________________________________________________________
## 建筑
### 项目结构```
gcp-cost-mcp-server/
├── main.go # Entry point, tool registration
├── internal/
│ ├── freetier/ # Free tier information retrieval
│ │ ├── service.go # FreeTierService with 24h cache
│ │ ├── search.go # DuckDuckGo search client
│ │ ├── scraper.go # GCP documentation scraper
│ │ └── patterns.go # Regex patterns for extraction
│ ├── pricing/
│ │ └── client.go # Cloud Billing Catalog API client
│ ├── tools/
│ │ ├── get_estimation_guide.go # Dynamic guide generator
│ │ ├── estimate_cost.go # Cost calc + free tier
│ │ ├── list_services.go
│ │ ├── list_skus.go
│ │ └── get_sku_price.go
│ └── mcp/
│ └── server.go # MCP server wrapper
工具设计```mermaid
flowchart TB
subgraph MCP["MCP Server Tools"]
direction TB
Guide["get_estimation_guide
─────────────────
• Dynamic SKU analysis
• Free tier info included
• Self-contained"]
Services["list_services
─────────────────
Returns: service IDs"]
SKUs["list_skus
─────────────────
Returns: SKU IDs, regions"]
Price["get_sku_price
─────────────────
Returns: price/unit, tiers"]
Cost["estimate_cost
─────────────────
• Auto free tier deduction
• Tiered pricing support
• Self-contained"]
Services --> SKUs
SKUs --> Price
SKUs --> Cost
end
subgraph External["External Services"]
Billing["Google Cloud Billing API v2beta
cloudbilling.googleapis.com"]
Docs["GCP Documentation
(for free tier info)"]
DDG["DuckDuckGo Search
(fallback for doc discovery)"]
end
MCP --> Billing
Guide -.-> Docs
Guide -.-> DDG
Cost -.-> Docs
Cost -.-> DDG
### 数据流```mermaid
sequenceDiagram
participant User
participant AI as AI Assistant
participant MCP as MCP Server
participant API as Cloud Billing API
participant Docs as GCP Docs
User->>AI: "How much would Cloud Run cost?"
AI->>MCP: get_estimation_guide("Cloud Run")
MCP->>API: ListSKUs(Cloud Run)
API-->>MCP: SKU list
MCP->>Docs: Fetch free tier info
Docs-->>MCP: Free tier data
MCP-->>AI: Dynamic guide + free tier
AI->>User: "I need: region, vCPU, memory..."
User->>AI: "Tokyo, 1vCPU, 2GB, 24/7"
AI->>MCP: list_services()
MCP->>API: ListServices
API-->>MCP: Service IDs
AI->>MCP: list_skus(service_id)
MCP->>API: ListSKUs
API-->>MCP: SKU details
AI->>MCP: estimate_cost(sku_id, usage)
MCP->>MCP: Apply free tier deduction
MCP-->>AI: Cost with free tier applied
AI->>User: "Estimated: $147.20/month (free tier applied)"
关键组件
| 组件 | 描述 |
|---|---|
| get_estimation_guide | 通过分析云计费API的SKU动态生成指南。包括从GCP文档中获取的免费等级信息。 |
| list_services | 查询云计费API中的所有可用服务。返回查询SKU所需的服务ID。 |
| list_skus | 列出特定服务的SKU。可按地区和类别筛选。 |
| get_sku_price | 获取特定SKU的详细定价。支持多种货币(美元、日元、欧元等)。 |
| 估算成本 | 通过自动扣除免费等级来计算最终成本。处理分层定价计算。 |
| FreeTier服务 | 通过DuckDuckGo搜索+GCP文档抓取获取免费等级信息。将结果缓存24小时。 |
发展
构建```bash
go build -o gcp-cost-mcp-server .
### 测试```bash
# Run all tests
go test -v ./...
# With coverage
go test -cover ./...
使用MCP检查员进行测试```bash
npx @modelcontextprotocol/inspector ./gcp-cost-mcp-server
### 交叉编译```bash
# macOS (Apple Silicon)
GOOS=darwin GOARCH=arm64 go build -o dist/gcp-cost-mcp-server-darwin-arm64 .
# macOS (Intel)
GOOS=darwin GOARCH=amd64 go build -o dist/gcp-cost-mcp-server-darwin-amd64 .
# Linux (x86_64)
GOOS=linux GOARCH=amd64 go build -o dist/gcp-cost-mcp-server-linux-amd64 .
# Windows (x86_64)
GOOS=windows GOARCH=amd64 go build -o dist/gcp-cost-mcp-server-windows-amd64.exe .
发布过程
通过以下方式自动发布 GoRelease GitHub Actions。
要创建新版本,请执行以下操作:```bash
1. Create and push a tag
git tag v0.6.0
git push origin v0.6.0
这将自动:
1. 为所有平台构建二进制文件(darwin/linux/windows、amd64/arm64)
1. 使用changelog创建GitHub版本
1. 更新 自制水龙头 公式
**自制水龙头自动化的先决条件:**
- GitHub个人访问令牌(PAT) `repo` 范围
- 将其存储为 `HOMEBREW_TAP_TOKEN` 存储库中的机密
### 本地开发```bash
# Build
go build -o gcp-cost-mcp-server .
# Run locally
./gcp-cost-mcp-server
# Test GoReleaser config (dry run)
goreleaser release --snapshot --clean
为什么选择Genkit for Go?
此MCP服务器由以下组件构建 Genkit for Go 而不是使用生的 mcp走 图书馆直接。原因如下:
类型安全工具定义
Genkit自动从Go结构标签生成JSON模式,消除了手动模式定义:go
// Genkit: Type-safe with auto-generated schema
genkit.DefineTool(g, "list_skus", "Lists SKUs for a service",
func(ctx *ai.ToolContext, input struct {
ServiceID string `json:"service_id" jsonschema_description:"The service ID"`
PageSize int `json:"page_size,omitempty"`
}) (*Output, error) {
// Implementation
})
自动MCP电桥
Genkit的MCP插件会自动从注册表中发现工具并将其转换为MCP格式,无需手动注册。
统一的生态系统
| 特点 | 优点 |
|---|---|
| Genkit用户界面 | 在开发过程中直观地调试和测试工具 |
| 追踪 | 自动执行跟踪和可观察性 |
| AI模型集成 | 与Gemini、Bedrock、OpenAI无缝连接 |
| MCP主机 | 使用同一代码库中的其他MCP服务器 |
刀具中断支持
Genkit的 ToolContext 为长时间运行的操作提供中断/恢复功能,这对用户确认流很有用。
面向未来
工具定义与以下工具定义相同:
- MCP服务器工具(适用于Claude Desktop、Cursor、Gemini CLI)
- Genkit Flow组件(用于AI代理工作流)
- HTTP API端点(通过
genkit.Handler)
有关更多详细信息,请参阅 Genkit MCP插件文档.
许可证
MIT许可证
