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