MCP TokenSage

MCP TokenSage

Enables token counting, usage tracking, and cost calculation for LLM APIs, with a proxy server mode to automatically intercept and monitor API requests from clients like Cursor and Windsurf.

Category
Visit Server

README

MCP TokenSage

A Model Context Protocol (MCP) server for token counting, usage tracking, and cost calculation for LLM APIs.

Now with Proxy Server Mode - Automatically track token usage by intercepting API requests from Cursor, Windsurf, or any LLM client!

Features

  • ๐Ÿš€ Proxy Server Mode (NEW!): Intercept API requests and auto-track token usage - no cookies needed!
  • ๐Ÿ”ข Token Counting: Accurate token counting using tiktoken
  • ๐Ÿ“Š Usage Tracking: Track input/output tokens per session with detailed statistics
  • ๐Ÿ’ฐ Cost Calculation: Calculate costs based on real pricing from major LLM providers
  • ๐Ÿ“ˆ Model Comparison: Compare costs across different models
  • ๐ŸŽฏ Project Estimation: Estimate monthly/yearly costs for your AI projects
  • ๐Ÿ”„ Auto-Update: Crawl latest model data from OpenRouter API
  • ๐Ÿ“ฑ Real-time Dashboard: View usage stats in a beautiful web dashboard

Quick Start - Proxy Mode

The easiest way to track your token usage from Cursor/Windsurf:

# Start the proxy server
npm run proxy:dev

# Or production mode
npm run proxy

Then configure your IDE:

  • Proxy URL: http://localhost:4000
  • Dashboard: http://localhost:4001

Configure Cursor/Windsurf

Set the API base URL to the proxy:

# For OpenAI models
OPENAI_BASE_URL=http://localhost:4000/v1

# For Anthropic models  
ANTHROPIC_BASE_URL=http://localhost:4000/v1

All your API requests will now be automatically tracked with:

  • โœ… Token count (input + output)
  • โœ… Cost calculation
  • โœ… Model detection
  • โœ… Latency monitoring
  • โœ… Persistent storage

Supported Models

350+ Models from 15+ Providers

Provider Models
OpenAI GPT-4o, GPT-4o Mini, GPT-4 Turbo, o1, o3-mini, Embeddings
Anthropic Claude 3.5 Sonnet/Haiku, Claude 3 Opus/Sonnet/Haiku
Google Gemini 2.0, Gemini 1.5 Pro/Flash
Meta Llama 3.3, 3.2, 3.1, Code Llama
Mistral Mistral Large/Medium/Small, Mixtral, Codestral
DeepSeek DeepSeek V3, Chat, Coder
Alibaba Qwen Max/Plus/Turbo, Qwen 2.5
xAI Grok 2, Grok Vision
Cohere Command R+, Command R
Amazon Nova Pro/Lite/Micro, Titan
AI21 Jamba 1.5, Jurassic-2
+ More Perplexity, Yi, GLM, Inflection...

Installation

# Clone the repository
git clone https://github.com/quangminh1212/MCP_TokenSage.git
cd MCP_TokenSage

# Install dependencies
npm install

# Build
npm run build

Usage

As MCP Server

Add to your MCP client configuration:

{
  "mcpServers": {
    "tokensage": {
      "command": "node",
      "args": ["path/to/MCP_TokenSage/dist/index.js"]
    }
  }
}

Update Model Data

# Update pricing data from OpenRouter API
npm run update-models

# Or use the batch script (Windows)
update-models.bat

Development

# Run in development mode
npm run dev

# Run tests
npm test

# Lint
npm run lint

Project Structure

MCP_TokenSage/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ index.ts          # MCP Server vแป›i 10 tools
โ”‚   โ”œโ”€โ”€ tokenCounter.ts   # Token counting vแป›i tiktoken
โ”‚   โ”œโ”€โ”€ costCalculator.ts # Cost calculation vแป›i pricing data
โ”‚   โ”œโ”€โ”€ usageTracker.ts   # Session usage tracking
โ”‚   โ”œโ”€โ”€ crawler.ts        # OpenRouter API crawler
โ”‚   โ”œโ”€โ”€ modelLoader.ts    # Data loader vแป›i caching
โ”‚   โ”œโ”€โ”€ config.ts         # Configuration constants
โ”‚   โ”œโ”€โ”€ types.ts          # TypeScript type definitions
โ”‚   โ””โ”€โ”€ test.ts           # Test suite
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ models.json       # Full model data (tแปซ crawler)
โ”‚   โ”œโ”€โ”€ pricing.json      # Pricing data
โ”‚   โ””โ”€โ”€ encodings.json    # Token encoding mappings
โ”œโ”€โ”€ dist/                 # Build output
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tsconfig.json
โ”œโ”€โ”€ update-models.bat     # Windows script ฤ‘แปƒ update data
โ””โ”€โ”€ README.md

Available Tools

count_tokens

Count tokens in a text string.

{
  "text": "Hello, how are you?",
  "model": "gpt-4",
  "include_tokens": false
}

count_tokens_batch

Count tokens for multiple texts at once.

{
  "texts": ["Hello", "World"],
  "model": "gpt-4"
}

record_usage

Record token usage for a request.

{
  "model": "gpt-4o",
  "input_tokens": 150,
  "output_tokens": 500,
  "request_id": "req_123"
}

get_usage_stats

Get usage statistics for the current session.

{
  "limit": 10
}

calculate_cost

Calculate cost for a request.

{
  "model": "gpt-4o",
  "input_tokens": 1000,
  "output_tokens": 2000
}

compare_models

Compare costs across different models.

{
  "input_tokens": 10000,
  "output_tokens": 20000,
  "models": ["gpt-4o", "gpt-4o-mini", "claude-3.5-sonnet"]
}

get_pricing

Get pricing information for all supported models.

estimate_project

Estimate project costs.

{
  "model": "gpt-4o",
  "daily_input_tokens": 100000,
  "daily_output_tokens": 200000,
  "days": 30
}

get_supported_models

Get list of models supported for token counting.

reset_usage

Reset usage statistics.

Example Output

Cost Calculation

{
  "model": "gpt-4o",
  "inputTokens": 1000,
  "outputTokens": 2000,
  "totalTokens": 3000,
  "inputCost": 0.0025,
  "outputCost": 0.02,
  "totalCost": 0.0225,
  "currency": "USD",
  "pricing": {
    "name": "GPT-4o",
    "inputPricePer1M": 2.5,
    "outputPricePer1M": 10,
    "contextWindow": 128000
  }
}

Model Comparison (Top 5 Cheapest)

1. Gemini 2.0 Flash Exp: $0.0000
2. DeepSeek Chat: $0.0700
3. GPT-4o Mini: $0.1350
4. Claude 3 Haiku: $0.2750
5. Mistral Small: $0.8000

Configuration

Configuration is centralized in src/config.ts:

  • Cache timeout: 5 minutes
  • Default encoding: cl100k_base
  • Cost decimals: 6 places
  • API endpoints: OpenRouter

Data Sources

  • Primary: OpenRouter API - 350+ models with real-time pricing
  • Fallback: Hardcoded data in costCalculator.ts - Updated December 2024

License

MIT

Author

quangminh1212

Recommended Servers

playwright-mcp

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.

Official
Featured
TypeScript
Magic Component Platform (MCP)

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.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

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.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

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.

Official
Featured
TypeScript
Kagi MCP Server

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.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

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.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured