token-optimise 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ EMBEDDER="all-MiniLM-L6-v2"
2
+ ALLOWED_DIR=your/allowed/dir/path
3
+ GROQ_API_KEY=you_groq_api_key
@@ -0,0 +1,21 @@
1
+ .env
2
+ venv/
3
+ __pycache__/
4
+ *.pyc
5
+ memory.db
6
+ chroma_db/
7
+ .DS_Store
8
+ storage/
9
+ server.log
10
+ extenstion/
11
+ *.py[cod]
12
+ .pytest_cache/
13
+
14
+ .vscode/
15
+ .idea/
16
+ tests/
17
+ adapters/
18
+ *.log
19
+ .env
20
+ .env.*
21
+ !.env.example
@@ -0,0 +1,32 @@
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Set PYTHONPATH so all imports resolve correctly
6
+ ENV PYTHONPATH=/app
7
+
8
+ RUN pip install --no-cache-dir \
9
+ streamlit==1.35.0 \
10
+ streamlit-autorefresh==1.0.1 \
11
+ requests==2.31.0 \
12
+ pandas==2.2.0 \
13
+ plotly==5.22.0 \
14
+ psycopg2-binary==2.9.9 \
15
+ pydantic==2.7.0 \
16
+ pydantic-settings==2.2.0 \
17
+ python-dotenv==1.0.0 \
18
+ fastapi==0.110.0 \
19
+ uvicorn==0.29.0
20
+
21
+ # Copy as proper packages so imports work
22
+ COPY token_optime/ ./token_optime/
23
+ COPY src/ ./src/
24
+
25
+ RUN mkdir -p /root/.streamlit
26
+ RUN printf '[server]\nport = 7738\naddress = "0.0.0.0"\nheadless = true\n\n[browser]\ngatherUsageStats = false\n' \
27
+ > /root/.streamlit/config.toml
28
+
29
+ EXPOSE 7737
30
+ EXPOSE 7738
31
+
32
+ CMD ["python", "src/mcp/dash_api.py"]
@@ -0,0 +1,498 @@
1
+ Metadata-Version: 2.5
2
+ Name: token-optimise
3
+ Version: 0.1.0
4
+ Summary: Token-optimised MCP proxy server for Claude Desktop
5
+ Requires-Python: >=3.10
6
+ Requires-Dist: chromadb>=1.5.9
7
+ Requires-Dist: fastapi>=0.140.13
8
+ Requires-Dist: fastmcp>=3.4.5
9
+ Requires-Dist: groq>=1.6.0
10
+ Requires-Dist: httpx>=0.28.1
11
+ Requires-Dist: huggingface-hub>=1.28.0
12
+ Requires-Dist: langchain-chroma>=1.1.0
13
+ Requires-Dist: langchain-community>=0.4.2
14
+ Requires-Dist: langchain-core>=1.6.0
15
+ Requires-Dist: langchain-huggingface>=1.2.2
16
+ Requires-Dist: langchain-text-splitters>=1.1.2
17
+ Requires-Dist: mcp>=1.29.0
18
+ Requires-Dist: numpy>=2.5.1
19
+ Requires-Dist: pandas>=3.0.5
20
+ Requires-Dist: plotly>=6.9.0
21
+ Requires-Dist: pydantic-settings>=2.14.2
22
+ Requires-Dist: pydantic>=2.13.4
23
+ Requires-Dist: pymupdf4llm>=1.28.0
24
+ Requires-Dist: pymupdf>=1.28.0
25
+ Requires-Dist: python-dotenv>=1.2.2
26
+ Requires-Dist: requests>=2.34.2
27
+ Requires-Dist: scipy>=1.18.0
28
+ Requires-Dist: sentence-transformers>=5.6.1
29
+ Requires-Dist: sse-starlette>=3.4.6
30
+ Requires-Dist: streamlit-autorefresh>=1.0.1
31
+ Requires-Dist: streamlit>=1.61.1
32
+ Requires-Dist: tiktoken>=0.13.0
33
+ Requires-Dist: uvicorn>=0.51.0
34
+ Description-Content-Type: text/markdown
35
+
36
+ # 🦾 Token-optimeee
37
+
38
+ 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.
39
+
40
+ **LLM Backend:** Groq | **Vector Store:** ChromaDB | **Dashboard:** Streamlit
41
+
42
+ ---
43
+
44
+ ## 🎯 What Problem This Solves
45
+
46
+ 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.
47
+
48
+ Token-optimeee optimises at three levels:
49
+
50
+ 1. **Semantic Tool Selection** — Only the most relevant tool schema is sent to Claude per query (ChromaDB similarity search)
51
+ 2. **Semantic Caching** — Repeated or similar queries skip tool calls entirely and return cached answers (similarity ≥ 0.8)
52
+ 3. **Response Trimming** — Verbose tool responses are trimmed to ≤200 tokens before entering the context window
53
+
54
+ **Real result from testing:** 91.8% schema token reduction per call.
55
+
56
+ ---
57
+
58
+ ## 📁 Project Structure
59
+ ```
60
+ .
61
+ ├── README.md
62
+ ├── config.py # Settings & env loading
63
+ ├── start.sh # One-click setup and launch
64
+ ├── stop.sh # One-click
65
+ ├── requirements.txt # Python dependencies
66
+ ├── remote_servers.json # Remote MCP URLs
67
+
68
+ ├── src/
69
+ │ ├── front.py # Streamlit dashboard
70
+ │ ├── core/
71
+ │ │ ├── cache.py # Semantic cache
72
+ │ │ ├── trim.py # Response trimmer
73
+ │ │ ├── tool_selection.py # Tool selection
74
+ │ │ ├── client.py # Groq/Claude client
75
+ │ │ ├── document_search.py # RAG (index + search)
76
+ │ │ └── db.py # SQLite audit logging
77
+ │ │
78
+ │ └── mcp/
79
+ │ ├── server.py # Main MCP server
80
+ │ └── server_http.py # FastAPI + metrics
81
+
82
+ └── storage/
83
+ ├── chroma_db/ # ChromaDB persistent store
84
+ └── token_audit.db # SQLite audit logs
85
+ ```
86
+
87
+
88
+ ---
89
+
90
+ ## Our Test Case
91
+
92
+ 1. list files on /Users/../Desktop
93
+ 2. list files on /Users/../Desktop
94
+
95
+ 3. index /Users/../Desktop/leave_policy.pdf doc_id leave
96
+ 4. what is the leave policy?
97
+ 5. what is the leave policy?
98
+ 6. read a file
99
+
100
+
101
+ ---
102
+
103
+ ## 🏗️ System Architecture
104
+
105
+ ```
106
+ Claude Desktop
107
+
108
+
109
+ ┌──────────────────────────────────────────────────────────┐
110
+ │ Token-Optimised MCP Server │
111
+ │ ┌─────────────────────────────────────────────────────┐ │
112
+ │ │ Exposed Tools │ │
113
+ │ │ • find_tool (orchestrator) │ │
114
+ │ │ • index_document / ask_document │ │
115
+ │ │ • search_all_documents / index_documents_folder │ │
116
+ │ └─────────────────────────────────────────────────────┘ │
117
+ │ ┌─────────────────────────────────────────────────────┐ │
118
+ │ │ Optimization Layer │ │
119
+ │ │ ┌──────────────┐ ┌──────────────────────────────┐ │ │
120
+ │ │ │ Semantic │ │ Tool Selection │ │ │
121
+ │ │ │ Cache │ │ (ChromaDB → top-k tools) │ │ │
122
+ │ │ │ (sim ≥ 0.8) │ │ │ │ │
123
+ │ │ └──────────────┘ └──────────────────────────────┘ │ │
124
+ │ │ ┌──────────────┐ ┌──────────────────────────────┐ │ │
125
+ │ │ │ Response │ │ Token Audit │ │ │
126
+ │ │ │ Trimmer │ │ (SQLite audit log) │ │ │
127
+ │ │ │ (≤500 tok) │ │ │ │ │
128
+ │ │ └──────────────┘ └──────────────────────────────┘ │ │
129
+ │ └─────────────────────────────────────────────────────┘ │
130
+ └────────────┬──────────────────┬──────────────────────────┘
131
+ │ │
132
+ ▼ ▼
133
+ ┌─────────────┐ ┌──────────────┐
134
+ │ Filesystem │ │ Memory MCP │
135
+ │ MCP │ │ (remote) │
136
+ │ (14 tools) │ │ (9 tools) │
137
+ └─────────────┘ └──────────────┘
138
+ ```
139
+
140
+
141
+ ---
142
+
143
+ ## 🚀 Setup — One Command
144
+
145
+ ### Prerequisites
146
+ - Python 3.10+
147
+ - Node.js + npx
148
+ - Groq API key (free at [console.groq.com](https://console.groq.com))
149
+ - Claude Desktop (latest)
150
+
151
+ ### Run
152
+
153
+ ## Run
154
+
155
+ ```bash
156
+ chmod +x token.sh
157
+ ./token.sh start # start
158
+ ./token.sh stop # stop
159
+ ./token.sh restart # restart
160
+ ./token.sh status # check if running
161
+ ```
162
+
163
+ That's it. The script will:
164
+ - Check Python and Node are installed
165
+ - Create a virtual environment and install packages (first run: 4-5 mins)
166
+ - Create `.env` from `.env.example` and prompt you to add your Groq key
167
+ - Close Claude Desktop automatically
168
+ - Write the MCP config
169
+ - Start the server in the background (terminal can be closed)
170
+ - Open the dashboard in your browser
171
+ - Reopen Claude Desktop
172
+
173
+ ### Stop
174
+
175
+ ```bash
176
+ ./stop.sh
177
+ ```
178
+
179
+ Or click the **⏹ Stop Token-optime** button in the dashboard.
180
+
181
+ ---
182
+
183
+ ## 🔧 Ports
184
+
185
+ | Service | Port |
186
+ |---|---|
187
+ | FastAPI / MCP | 7737 |
188
+ | Streamlit Dashboard | 7738 |
189
+
190
+ These are intentionally non-default to avoid conflicts with other projects.
191
+
192
+ ---
193
+
194
+ ## 💬 For Best Results — Tell Claude to Use Token-optime
195
+
196
+ Claude Desktop has its own built-in tools (memory, web search) that it may prefer by default. For consistent routing through Token-optime, start each conversation with:
197
+
198
+ > Use token:execute for every task. Never call other tools directly.
199
+
200
+ For PDF tasks specifically:
201
+ > First call token:list_indexed_documents, then token:ask_document.
202
+
203
+ **Why is this needed?** Claude decides which tool to call — Token-optime 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-optime is used. This is an honest limitation of how Claude Desktop works, not a bug in Token-optime.
204
+
205
+ ---
206
+
207
+ ## 📊 Dashboard
208
+
209
+ Access at `http://localhost:7738`
210
+
211
+ | Metric | What it means |
212
+ |---|---|
213
+ | **Schema Tokens Without Token-optime** | Tokens Claude would receive with all tool schemas |
214
+ | **Schema Tokens With Token-optime** | Tokens Claude actually received (1 selected schema) |
215
+ | **Total Tokens Saved** | Schema savings + response trim savings |
216
+ | **Cache Hit Rate** | % of queries that returned cached answers |
217
+ | **Real Groq Token Usage** | Exact token counts from Groq API (not estimates) |
218
+ | **Cost Saved** | Estimated savings based on Sonnet 4.6 pricing |
219
+
220
+ > **Note:** Cost estimates use Claude Sonnet 4.6 pricing ($3/1M tokens, ₹84/$). Actual cost depends on which model you use in Claude Desktop.
221
+
222
+
223
+
224
+ ---
225
+
226
+ ## 🐛 Troubleshooting
227
+
228
+ **Token-optime already running error**
229
+ ./stop.sh
230
+ ./start.sh
231
+
232
+ **Port 7737 in use**
233
+ ```bash
234
+ lsof -i :7737
235
+ kill <PID>
236
+ ```
237
+
238
+ **Claude not using token:execute**
239
+ - Check Settings → Developer → MCP Servers — `token` should show green
240
+ - Add explicit instruction at start of conversation (see above)
241
+
242
+ **Tool not found for query**
243
+ - Rephrase more specifically — e.g. "list files in /Users/name/Desktop" instead of "show my stuff"
244
+ - Check `server_out.log` for similarity scores
245
+
246
+ **Dashboard offline**
247
+ ```bash
248
+ cat server_out.log | tail -50
249
+ ```
250
+
251
+ ---
252
+
253
+ ## ⚠️ Known Limitations
254
+
255
+ - **Claude's built-in tools take priority** — memory and web search bypass Token-optime because Claude prefers its native tools
256
+ - **Cost estimates are approximate** — based on Sonnet 4.6 pricing; varies by model
257
+ - **Windows not supported** — Mac and Linux only
258
+ - **Tilde paths** — if a tool call fails with "file not found", use the full path e.g. `/Users/name/Desktop/file.pdf`
259
+
260
+ ---
261
+
262
+ ## 📖 Docs
263
+
264
+ - `docs/COMPONENTS.md` — Detailed components guide
265
+ - `docs/QUICKSTART.md` — 5-minute quick start
266
+
267
+
268
+
269
+ ## 📁 Project Structure
270
+ ```
271
+ .
272
+ ├── README.md
273
+ ├── config.py # Settings & env loading
274
+ ├── install.sh # One-command installer for new users
275
+ ├── stop.sh # One-click
276
+ ├── requirements.txt # Python dependencies
277
+ ├── Dockerfile # For EC2 dashboard deployment
278
+ ├── pyproject.toml # PyPI package definition
279
+
280
+ ├── src/
281
+ │ ├── front.py # Streamlit dashboard
282
+ │ ├── dash_api.py # Lightweight API for EC2 deployment
283
+ │ ├── core/
284
+ │ │ ├── cache.py # Semantic cache
285
+ │ │ ├── trim.py # Response trimmer
286
+ │ │ ├── tool_selection.py # Tool selection
287
+ │ │ ├── client.py # Groq/Claude client
288
+ │ │ ├── document_search.py # RAG (index + search)
289
+ │ │ └── db.py # SQLite audit logging
290
+ │ │
291
+ │ └── mcp/
292
+ │ ├── server.py # Main MCP server
293
+ │ └── server_http.py # FastAPI + metrics
294
+
295
+ └── storage/
296
+ ├── chroma_db/ # ChromaDB persistent store
297
+ └── token_audit.db # SQLite audit logs
298
+ ```
299
+
300
+
301
+ ---
302
+
303
+ ## Our Test Case
304
+
305
+ 1. list files on /Users/../Desktop
306
+ 2. list files on /Users/../Desktop
307
+
308
+ 3. index /Users/../Desktop/leave_policy.pdf doc_id leave
309
+ 4. what is the leave policy?
310
+ 5. what is the leave policy?
311
+ 6. read a file
312
+
313
+
314
+ ---
315
+
316
+ ## 🏗️ System Architecture
317
+
318
+ ```
319
+ Claude Desktop
320
+
321
+
322
+ ┌──────────────────────────────────────────────────────────┐
323
+ │ Token-Optimised MCP Server │
324
+ │ ┌─────────────────────────────────────────────────────┐ │
325
+ │ │ Exposed Tools │ │
326
+ │ │ • find_tool (orchestrator) │ │
327
+ │ │ • index_document / ask_document │ │
328
+ │ │ • search_all_documents / index_documents_folder │ │
329
+ │ └─────────────────────────────────────────────────────┘ │
330
+ │ ┌─────────────────────────────────────────────────────┐ │
331
+ │ │ Optimization Layer │ │
332
+ │ │ ┌──────────────┐ ┌──────────────────────────────┐ │ │
333
+ │ │ │ Semantic │ │ Tool Selection │ │ │
334
+ │ │ │ Cache │ │ (ChromaDB → top-k tools) │ │ │
335
+ │ │ │ (sim ≥ 0.8) │ │ │ │ │
336
+ │ │ └──────────────┘ └──────────────────────────────┘ │ │
337
+ │ │ ┌──────────────┐ ┌──────────────────────────────┐ │ │
338
+ │ │ │ Response │ │ Token Audit │ │ │
339
+ │ │ │ Trimmer │ │ (SQLite audit log) │ │ │
340
+ │ │ │ (≤500 tok) │ │ │ │ │
341
+ │ │ └──────────────┘ └──────────────────────────────┘ │ │
342
+ │ └─────────────────────────────────────────────────────┘ │
343
+ └────────────┬──────────────────┬──────────────────────────┘
344
+ │ │
345
+ ▼ ▼
346
+ ┌─────────────┐ ┌──────────────┐
347
+ │ Filesystem │ │ Memory MCP │
348
+ │ MCP │ │ (remote) │
349
+ │ (14 tools) │ │ (9 tools) │
350
+ └─────────────┘ └──────────────┘
351
+ ```
352
+
353
+
354
+
355
+ ---
356
+
357
+ ## 🚀 Install — One Command
358
+
359
+ ### Prerequisites
360
+ - Python 3.10+
361
+ - Node.js + npx
362
+ - Groq API key — free at [console.groq.com](https://console.groq.com)
363
+ - Claude Desktop (latest)
364
+
365
+ ### New users — two commands
366
+
367
+ ```bash
368
+ pip install token-optime
369
+ token-optime
370
+ ```
371
+
372
+ The installer will:
373
+ - Check Python and Node are installed
374
+ - Ask for your Groq API key
375
+ - Write the MCP config into Claude Desktop automatically
376
+ - Restart Claude Desktop
377
+
378
+ ### Existing users / developers
379
+
380
+ ```bash
381
+ git clone https://github.com/yourrepo/tom.git
382
+ cd tom
383
+ cp .env.example .env # add your GROQ_API_KEY
384
+ ./token.sh start
385
+ ```
386
+
387
+ ### token.sh commands
388
+
389
+ ```bash
390
+ ./token.sh start # start
391
+ ./token.sh stop # stop
392
+ ./token.sh restart # restart
393
+ ./token.sh status # check if running
394
+ ```
395
+
396
+ ---
397
+
398
+ ## 📊 Dashboard
399
+
400
+ ### Local (your metrics only)
401
+ http://localhost:7738
402
+
403
+
404
+ ---
405
+
406
+ ## 🔧 Ports
407
+
408
+ | Service | Port |
409
+ |---|---|
410
+ | FastAPI / MCP | 7737 |
411
+ | Streamlit Dashboard | 7738 |
412
+
413
+ These are intentionally non-default to avoid conflicts with other projects.
414
+
415
+ ---
416
+
417
+ ## 💬 For Best Results — Tell Claude to Use Token-optime
418
+
419
+ Claude Desktop has its own built-in tools (memory, web search) that it may prefer by default. For consistent routing through Token-optime, start each conversation with:
420
+
421
+ Add Instructions for Claude :
422
+ > Paste it here Settings → Instruction for Claude
423
+ ```
424
+ You have access to a token MCP server. Follow these rules strictly:
425
+ For ALL tasks — files, Gmail, Notion, memory, or anything else — always use token:execute first before calling any other tool.
426
+ For PDF questions always call token:list_indexed_documents first then token:ask_document.
427
+ After every response call token:wick_track.
428
+
429
+ DOCUMENT/PDF TASKS:
430
+ - ALWAYS call list_indexed_docs first, then ask_document immediately.
431
+ - NEVER ask the user for clarification.
432
+
433
+ AFTER EVERY RESPONSE: Call wick_track.
434
+ ```
435
+
436
+ > Use token:execute for every task. Never call other tools directly.
437
+
438
+ For PDF tasks specifically:
439
+ > First call token:list_indexed_documents, then token:ask_document.
440
+
441
+ **Why is this needed?** Claude decides which tool to call — Token-optime 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-optime is used. This is an honest limitation of how Claude Desktop works, not a bug in Token-optime.
442
+
443
+ ---
444
+ # Dashboard Metrics
445
+
446
+ | Metric | What it means |
447
+ |--------|--------------|
448
+ | Schema Tokens — Estimated Baseline | Tokens Claude would receive with all tool schemas |
449
+ | Schema Tokens — With Token-optime | Tokens Claude actually received (1 selected schema) |
450
+ | Total Tokens Saved | Schema savings + response trim savings |
451
+ | Cache Hit Rate | % of queries that returned cached answers |
452
+ | Groq Cost (actual ✅) | Real measured cost at Groq pricing |
453
+ | Claude Cost Saved (estimated ⚠️) | Range across Haiku→Opus (model unknown) |
454
+ | Live USD/INR Rate | Fetched live every hour |
455
+
456
+
457
+
458
+ ---
459
+
460
+ ## 🐛 Troubleshooting
461
+
462
+ **Server not starting**
463
+ ```bash
464
+ tail -50 server_out.log
465
+ ```
466
+
467
+ **Port 7737 in use**
468
+ ```bash
469
+ lsof -i :7737
470
+ kill <PID>
471
+ ./token.sh start
472
+ ```
473
+
474
+ **Claude not using token:execute**
475
+ - Check Settings → Developer → MCP Servers — `token` should show green
476
+ - Add explicit instruction at start of conversation
477
+
478
+ **Dashboard offline**
479
+ ```bash
480
+ ./token.sh status
481
+ ./token.sh restart
482
+ ```
483
+
484
+ ---
485
+
486
+ ## ⚠️ Known Limitations
487
+
488
+ - **Claude's built-in tools take priority** — memory and web search bypass Token-optime because Claude prefers its native tools
489
+ - **Cost estimates are approximate** — based on Sonnet 4.6 pricing; varies by model
490
+ - **Windows not supported** — Mac and Linux only
491
+ - **Tilde paths** — if a tool call fails with "file not found", use the full path e.g. `/Users/name/Desktop/file.pdf`
492
+
493
+ ---
494
+
495
+ ## 📖 Docs
496
+
497
+ - `docs/COMPONENTS.md` — Detailed components guide
498
+ - `docs/QUICKSTART.md` — 5-minute quick start