🦾 Token-Optimise
A middleware MCP server that sits between Claude Desktop and your downstream MCP servers — reducing token consumption through semantic caching, dynamic tool selection, and response trimming.
🎯 What Problem This Solves
Every Claude Desktop session sends the full schema of every connected MCP tool with every prompt. With 23+ tools registered, that's thousands of tokens injected per query — even when only one tool is needed.
Token-Optimise optimises at three levels:
- Semantic Tool Selection — Only the most relevant tool schema is sent to Claude per query (ChromaDB similarity search)
- Semantic Caching — Repeated or similar queries skip tool calls entirely and return cached answers (similarity ≥ 0.8)
- Response Trimming — Verbose tool responses are trimmed before entering the context window
Real result from testing: 91.8% schema token reduction per call.
📁 Project Structure
.
├── README.md
├── config.py # Settings & env loading
├── requirements.txt # Python dependencies
├── Dockerfile # For EC2 dashboard deployment
├── pyproject.toml # PyPI package definition
│
├── token_optimise/
│ ├── init.py
│ ├── main.py # Entry point (pip install)
│ └── config.py # Settings & env loading
|
├── src/
│ ├── front.py # Streamlit dashboard
│ ├── admin_front.py # Admin dashboard
│ ├── core/
│ │ ├── cache.py # Semantic cache
│ │ ├── trim.py # Response trimmer
│ │ ├── tool_selection.py # Tool selection
│ │ ├── client.py # Groq/Claude client
│ │ ├── document_search.py # RAG (index + search)
│ │ └── db.py # SQLite audit logging
│ │
│ └── mcp/
│ ├── server.py # Main MCP server
│ ├── server_http.py # FastAPI + metrics
│ └── dash_api.py # Lightweight API for EC2 deployment
|
└── storage/
├── chroma_db/ # ChromaDB persistent store
└── token_audit.db # SQLite audit logs
Our Test Case
- list files on /Users/../Desktop
- list files on /Users/../Desktop
- index /Users/../Desktop/leave_policy.pdf doc_id leave
- what is the leave policy?
- what is the leave policy?
- read a file
🏗️ System Architecture
Claude Desktop
│
▼
┌──────────────────────────────────────────────────────────┐
│ Token-Optimised MCP Server │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Exposed Tools │ │
│ │ • find_tool (orchestrator) │ │
│ │ • index_document / ask_document │ │
│ │ • search_all_documents / index_documents_folder │ │
│ └─────────────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Optimization Layer │ │
│ │ ┌──────────────┐ ┌──────────────────────────────┐ │ │
│ │ │ Semantic │ │ Tool Selection │ │ │
│ │ │ Cache │ │ (ChromaDB → top-k tools) │ │ │
│ │ │ (sim ≥ 0.8) │ │ │ │ │
│ │ └──────────────┘ └──────────────────────────────┘ │ │
│ │ ┌──────────────┐ ┌──────────────────────────────┐ │ │
│ │ │ Response │ │ Token Audit │ │ │
│ │ │ Trimmer │ │ (SQLite audit log) │ │ │
│ │ │ (≤500 tok) │ │ │ │ │
│ │ └──────────────┘ └──────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────┘ │
└────────────┬──────────────────┬──────────────────────────┘
│ │
▼ ▼
┌─────────────┐ ┌──────────────┐
│ Filesystem │ │ Memory MCP │
│ MCP │ │ (remote) │
│ (14 tools) │ │ (9 tools) │
└─────────────┘ └──────────────┘
🚀 Install — One Command
Prerequisites
- Python 3.10+
- Node.js + npx
- An API key from any supported provider (or Ollama for local, no key needed)
- Claude Desktop (latest)
New users — two commands
pip install token-optimise
token-optimise
The installer will:
- Ask which AI provider you want to use
- Ask for your API key
- Ask which folder it can access on your machine
- Write the MCP config into Claude Desktop automatically
- Start the server in the background
Switch provider later
token-optimise --change-provider
Then restart with token-optimise.
📊 Dashboard
Local (your metrics only)
http://localhost:7738
🔧 Ports
| Service | Port |
|---|---|
| FastAPI / MCP | 7737 |
| Streamlit Dashboard | 7738 |
These are intentionally non-default to avoid conflicts with other projects.
💬 For Best Results — Tell Claude to Use Token-Optimise
Claude Desktop has its own built-in tools (memory, web search) that it may prefer by default. For consistent routing through Token-optimise:
Add this to Settings → Instructions for Claude:
You have access to a token MCP server. Follow these rules strictly:
For ALL tasks — files, Gmail, Notion, memory, or anything else — always use token:execute first before calling any other tool.
For PDF questions always call token:list_indexed_documents first then token:ask_document.
After every response call token:wick_track.
DOCUMENT/PDF TASKS:
- ALWAYS call list_indexed_docs first, then ask_document immediately.
- NEVER ask the user for clarification.
AFTER EVERY RESPONSE: Call wick_track.
You can start your conversation with :
Use token:execute for every task. Never call other tools directly.
For PDF tasks specifically:
First call token:list_indexed_documents, then token:ask_document.
Why is this needed? Claude decides which tool to call — Token-Optimise can't force it. The system prompt in the config nudges Claude, but an explicit instruction in the chat is the most reliable way to ensure Token-Optimise is used. This is an honest limitation of how Claude Desktop works, not a bug in Token-Optimise.
Dashboard Metrics
| Metric | What it means |
|---|---|
| Schema Tokens — Estimated Baseline | Tokens Claude would receive with all tool schemas |
| Schema Tokens — With Token-Optimise | Tokens Claude actually received (1 selected schema) |
| Total Tokens Saved | Schema savings + response trim savings |
| Cache Hit Rate | % of queries that returned cached answers |
| Groq Cost (actual ✅) | Real measured cost at Groq pricing |
| Claude Cost Saved (estimated ⚠️) | Range across Haiku→Opus (model unknown) |
| Live USD/INR Rate | Fetched live every hour |
🐛 Troubleshooting
Server not starting
cat ~/.token_optimise/server.log
Port 7737 in use
lsof -i :7737
kill <PID>
token-optimise
Claude not using token:execute
- Check Settings → Developer → MCP Servers —
tokenshould show green - Add explicit instruction at start of conversation
Dashboard offline
token-optimise
Want to change your API key or provider
token-optimise --change-provider
Check your current config
cat ~/.token_optimise/.env
⚠️ Known Limitations
- Claude's built-in tools take priority — memory and web search bypass Token-Optimise because Claude prefers its native tools
- Cost estimates are approximate — based on Sonnet 4.6 pricing; varies by model
- PDF files only — document indexing supports PDF format only
Metadata
Release files for token-optimise 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| token_optimise-0.2.0.tar.gz | 45.5 kB | Without provenance | Details for token_optimise-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| token_optimise-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for token_optimise-0.2.0-py3-none-any.whl |
Total release size: 85.2 kB
Release files / token_optimise-0.2.0.tar.gz
| Download URL | token_optimise-0.2.0.tar.gz |
|---|---|
| Size | 45.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b16164e34100130ad784e47d8a1649052712360b3226e15ea51949f68c5c9ec2
|
|
BLAKE2b-256 checksum How to use checksums |
c38329a722d6619a7b6557b8627450a7dc991181bb0850e273e619b9fa9affd1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.3
|
Release files / token_optimise-0.2.0-py3-none-any.whl
| Download URL | token_optimise-0.2.0-py3-none-any.whl |
|---|---|
| Size | 39.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2e6b1e310710bcf2891d934f77fc90ceef3cd9ba5eca56ac025c4190953313ea
|
|
BLAKE2b-256 checksum How to use checksums |
ab2e4972f47080281f15b0f33e17af22a65b55684f0322b8888675f7bf22d34b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.3
|
Security for token-optimise 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.