AI Token Cost Optimization MCP Server
Provides tools to count tokens, estimate API costs, optimize prompts, and compare AI model pricing for multiple LLMs.
README
AI Token Cost Optimization — MCP Server
A production-ready Model Context Protocol (MCP) server that provides 4 tools to count tokens, estimate API costs, optimize prompts, and compare AI model pricing. Compatible with Cline, Claude Desktop, and any MCP-compatible client.
Tools Overview
| Tool | Description |
|---|---|
| count_tokens | Count tokens in any text for GPT-4o, GPT-4o-mini, Claude 3.5 Sonnet, Claude 3.5 Haiku, or Gemini 1.5 Flash. Includes cost estimation. |
| estimate_cost | Estimate the full API call cost: input cost + output cost. Supports USD and INR. |
| optimize_prompt | Analyze a prompt and get suggestions to reduce token usage. Also recommends the cheapest model for your text. |
| compare_models | Compare all 5 models side-by-side for your prompt, sorted from cheapest to most expensive. |
Project Structure
my-mcp-server/
├── src/
│ ├── index.ts # MCP server (4 tools)
│ ├── types.ts # TypeScript interfaces
│ ├── pricing.ts # Model pricing data (USD/INR)
│ └── tokenizer.ts # Token counting (gpt-tokenizer o200k_base)
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── README.md # This file
└── DOCUMENTATION.md # Full process documentation
Prerequisites
- Node.js v18 or higher
- npm (comes with Node.js)
Quick Start
# Navigate to the project directory
cd C:\Users\ghosh\Documents\MSS\my-mcp-server
# Install dependencies
npm install
# Run in development mode
npm run dev
Usage
Development Mode (hot-reload with tsx)
npm run dev
Development Mode (with auto-reload)
npm run dev:watch
Production Mode
npm run build # Compile TypeScript to dist/
npm start # Run compiled version
TypeScript Type-Check
npx tsc --noEmit
Testing the Server
Option 1: MCP Inspector (Recommended)
npx @modelcontextprotocol/inspector npx tsx src/index.ts
Opens a web UI at http://localhost:5173 — browse tools, call them, see responses.
Option 2: Quick Command-Line Test
Test all 4 tools in one command:
cd C:\Users\ghosh\Documents\MSS\my-mcp-server
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0.0"}}}
{"jsonrpc":"2.0","method":"notifications/initialized"}
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"count_tokens","arguments":{"text":"Hello World!","model":"gpt-4o"}}}
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"compare_models","arguments":{"prompt":"Hello","estimated_output_tokens":100}}}' | npx tsx src/index.ts 2>/dev/null
Cline Configuration
Add to your Cline MCP settings:
{
"mcpServers": {
"hello-world": {
"command": "npx",
"args": [
"tsx",
"C:\\Users\\ghosh\\Documents\\MSS\\my-mcp-server\\src\\index.ts"
]
}
}
}
macOS/Linux: Use forward slashes:
"/Users/ghosh/Documents/MSS/my-mcp-server/src/index.ts"
Tool Details
count_tokens
Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
text |
string | ✅ | The text to count tokens for |
model |
string | ✅ | gpt-4o, gpt-4o-mini, claude-3.5-sonnet, claude-3.5-haiku, gemini-1.5-flash |
currency |
string | ❌ | USD (default) or INR |
Example response:
{
"model": "gpt-4o",
"token_count": 7,
"character_count": 31,
"tokenizer": "OpenAI o200k_base (via gpt-tokenizer) — exact",
"estimated_input_cost": "$0.000018",
"currency": "USD"
}
estimate_cost
Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
prompt |
string | ✅ | The input prompt text |
estimated_output_tokens |
number | ❌ | Default: 500 |
model |
string | ✅ | Model identifier |
currency |
string | ❌ | USD (default) or INR |
Example response:
{
"model": "gpt-4o",
"provider": "OpenAI",
"input_tokens": 6,
"output_tokens": 200,
"input_cost": "$0.000015",
"output_cost": "$0.002000",
"total_cost": "$0.002015",
"currency": "USD"
}
optimize_prompt
Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
prompt |
string | ✅ | The prompt to analyze |
model |
string | ✅ | Model identifier |
Example response:
{
"original_token_count": 15,
"character_count": 62,
"suggested_improvements": [
{ "suggestion": "Replace verbose phrases...", "estimated_savings_percent": 5 },
{ "suggestion": "Consider using GPT-4o-mini...", "estimated_savings_percent": 60 }
],
"best_model_recommendation": "Switch to gpt-4o-mini for ~60% cost savings."
}
compare_models
Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
prompt |
string | ✅ | The input prompt text |
estimated_output_tokens |
number | ❌ | Default: 500 |
currency |
string | ❌ | USD (default) or INR |
Example response:
{
"prompt_character_count": 11,
"estimated_output_tokens": 100,
"currency": "USD",
"comparisons": [
{ "model": "gpt-4o-mini", "total_cost": "$0.000060" },
{ "model": "claude-3.5-haiku", "total_cost": "$0.000503" }
],
"summary": {
"cheapest": "gpt-4o-mini",
"most_expensive": "claude-3.5-sonnet"
}
}
Supported Models & Pricing
| Model | Provider | Input (per 1K tokens) | Output (per 1K tokens) |
|---|---|---|---|
| GPT-4o | OpenAI | $0.00250 | $0.01000 |
| GPT-4o-mini | OpenAI | $0.00015 | $0.00060 |
| Claude 3.5 Sonnet | Anthropic | $0.00300 | $0.01500 |
| Claude 3.5 Haiku | Anthropic | $0.00100 | $0.00500 |
| Gemini 1.5 Flash | $0.00150 | $0.00900 |
Technical Details
| Field | Value |
|---|---|
| SDK | @modelcontextprotocol/sdk ^1.29.0 |
| Token Counting | gpt-tokenizer (o200k_base) for OpenAI; character-based for Claude/Gemini |
| Transport | StdioServerTransport |
| Module System | ES Modules ("type": "module") |
| Runtime | Node.js >= 18 |
| Dev Runner | tsx for TypeScript execution |
Example Cline Prompts
After connecting the server, try asking Cline:
"Count the tokens in 'Hello World from my MCP Server!' using GPT-4o"
"Estimate the cost of sending a 100-token prompt with 500 output tokens on Claude 3.5 Sonnet in INR"
"Which is the cheapest model for this prompt: 'Write a poem about artificial intelligence' with 200 output tokens?"
"Analyze this prompt for optimization: 'I would like you to please write a detailed report'"
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 模型以安全和受控的方式获取实时的网络信息。