AI Token Cost Optimization MCP Server
README.md
# AI Token Cost Optimization — MCP Server
A production-ready [Model Context Protocol (MCP)](https://modelcontextprotocol.io) 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](https://nodejs.org/) **v18 or higher**
- npm (comes with Node.js)
---
## Quick Start
```bash
# 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)
```bash
npm run dev
```
### Development Mode (with auto-reload)
```bash
npm run dev:watch
```
### Production Mode
```bash
npm run build # Compile TypeScript to dist/
npm start # Run compiled version
```
### TypeScript Type-Check
```bash
npx tsc --noEmit
```
---
## Testing the Server
### Option 1: MCP Inspector (Recommended)
```bash
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:
```bash
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:
```json
{
"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:**
```json
{
"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:**
```json
{
"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:**
```json
{
"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:**
```json
{
"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 | Google | $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
This server cannot be deployed
Maintenance
ActivityStale
ResponsivenessNo issues