CosTrack MCP
Enables AI agents to track LLM costs, enforce budgets, compare models, and estimate expenses through simple tool calls.
README
CosTrack MCP — Agent Cost Control Layer
Lightweight MCP server that gives any AI agent or developer instant cost tracking, spend analysis, budget enforcement, and model cost optimization for LLM operations.
What It Does
CosTrack sits between simple price-lookup tools and full observability platforms. It's an MCP-native cost control layer — log costs, get reports, check budgets, compare models — all via tool calls.
Tools
| Tool | Description |
|---|---|
cost_log |
Record a cost event for an LLM call or agent operation |
cost_report |
Generate cost summary with breakdowns by model, agent, task |
cost_compare |
Compare costs side-by-side for models, agents, or periods |
budget_check |
Check spend vs budget with end-of-period projection |
cost_estimate |
Estimate cost for planned calls with cheaper alternatives |
pricing_table |
Get current pricing data for 17+ supported models |
Supported Models
- Anthropic: Claude Opus 4, Opus 4.6, Sonnet 4, Sonnet 4.6, Haiku 4
- OpenAI: GPT-4o, GPT-4o Mini, GPT-4 Turbo, o1, o1-mini
- Google: Gemini 2.5 Pro, Gemini 2.5 Flash, Gemini 2.0 Flash
- Meta: Llama 4 Scout, Llama 4 Maverick
- DeepSeek: DeepSeek V3, DeepSeek R1
Model names are automatically normalized — use aliases like sonnet, gpt-4o, haiku etc.
Quick Start
Connect via Claude Desktop / Claude Code
Add to your MCP server configuration:
{
"mcpServers": {
"costrack": {
"url": "https://costrack-mcp.<your-subdomain>.workers.dev/mcp"
}
}
}
Example Usage
Log a cost event:
cost_log(model: "claude-sonnet-4", input_tokens: 1500, output_tokens: 800, agent_id: "my-agent")
→ { cost_usd: 0.0165, running_session_total: 1.23 }
Check budget:
budget_check(budget_usd: 100, scope: "all", period: "30d")
→ { current_spend_usd: 65.0, status: "PROJECTED_OVERAGE", projected_end_of_period_spend_usd: 130.1 }
Estimate before calling:
cost_estimate(model: "claude-opus-4", estimated_input_tokens: 5000, estimated_output_tokens: 2000)
→ { cost_per_call_usd: 0.225, alternatives: [{ model: "anthropic/claude-sonnet-4", savings_percent: 90.0 }] }
Features
- Model Normalization —
sonnet,claude-sonnet-4,anthropic/claude-sonnet-4all resolve to the same model - Idempotency — Pass
idempotency_keyto prevent duplicate cost logging - Hard Limit Signaling — Set
hard_limit_usdto get alerts when spend exceeds threshold (fail-safe, not fail-stop) - Price Snapshots — Each event stores the price at time of logging; historical costs never change
- Budget Projection — Predicts end-of-period spend based on daily average
- Alternative Suggestions —
cost_estimaterecommends cheaper models in same capability tier
Deployment
Prerequisites
- Node.js 18+
- Cloudflare account with Workers enabled
- Wrangler CLI (
npm install -g wrangler)
Deploy
# Install dependencies
npm install
# Login to Cloudflare
wrangler login
# Create KV namespace
wrangler kv:namespace create COSTRACK_EVENTS
# Update wrangler.toml with the returned namespace ID
# Deploy
wrangler deploy
Local Development
npm run dev
Type Check
npm run build
Architecture
- Runtime: Cloudflare Workers (TypeScript)
- Storage: Hybrid — Durable Objects (real-time aggregations) + KV (raw event audit trail)
- Protocol: MCP (Model Context Protocol) over Streamable HTTP
- Pricing: Built-in table, single-file source of truth (
src/pricing/pricing-table.ts)
Configuration
Edit wrangler.toml to set:
COSTRACK_EVENTSKV namespace ID (Rich fills in afterwrangler kv:namespace create)- Production environment variables
Project Structure
costrack-mcp/
├── src/
│ ├── tools/ # 6 tool implementations
│ ├── pricing/ # Pricing data + model aliases
│ ├── normalize/ # Model name normalization
│ ├── storage/ # Durable Object + KV + DO client
│ ├── utils/ # Cost calculator, tier check
│ ├── types/ # Shared TypeScript types
│ ├── index.ts # MCP server core
│ └── worker.ts # Cloudflare Workers entry point
├── wrangler.toml # CF Workers config
├── package.json
├── tsconfig.json
└── README.md
License
MIT
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。