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
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.