UNPKG

tonl-mcp-bridge

Version:

Reduce LLM token costs by 30-60% with TONL format. TypeScript library & CLI with MCP Server for bidirectional JSON/YAML ↔ TONL conversion.

457 lines (355 loc) 11.4 kB
# TONL-MCP Bridge 🌉 > Reduce LLM token costs by 40-60% with TONL format [![npm version](https://img.shields.io/npm/v/tonl-mcp-bridge.svg)](https://www.npmjs.com/package/tonl-mcp-bridge) [![npm downloads](https://img.shields.io/npm/dm/tonl-mcp-bridge.svg)](https://www.npmjs.com/package/tonl-mcp-bridge) [![Tests](https://img.shields.io/badge/tests-167%20passing-brightgreen)](https://github.com/kryptomrx/tonl-mcp-bridge) [![TypeScript](https://img.shields.io/badge/TypeScript-5.3-blue)](https://www.typescriptlang.org/) [![License](https://img.shields.io/npm/l/tonl-mcp-bridge.svg)](https://github.com/kryptomrx/tonl-mcp-bridge/blob/main/LICENSE) ## What is this? A TypeScript library, CLI tool, and MCP server that converts JSON/YAML data to TONL (Token Optimized Natural Language) format, reducing token usage for LLM context windows by 40-60%. Perfect for: - 🤖 RAG (Retrieval-Augmented Generation) systems - 📊 MCP (Model Context Protocol) servers - 💬 AI chat applications with large context needs - 🗄️ Vector database query optimization (Milvus, Qdrant) - 📝 Prompt libraries and templates **Not suitable for:** - Single objects (header overhead makes it inefficient) - Highly inconsistent schemas - Systems that require standard JSON output [📚 Read full documentation](https://tonl-mcp-bridge-docs.vercel.app/) ## The Problem When sending data to LLMs, JSON is verbose and wastes tokens: ```json [ { "id": 1, "name": "Alice Johnson", "age": 25, "email": "alice@example.com", "active": true }, { "id": 2, "name": "Bob Smith", "age": 30, "email": "bob@example.com", "active": false } ] ``` **118 tokens** 💸 ## The Solution TONL format is compact and structured: ```tonl data[2]{id:i32,name:str,age:i32,email:str,active:bool}: 1, "Alice Johnson", 25, alice@example.com, true 2, "Bob Smith", 30, bob@example.com, false ``` **75 tokens** **Result: 36.4% token savings!** (scales to 60%+ with larger datasets) ## Features **Bidirectional Conversion** - JSON ↔ TONL - YAML ↔ TONL - Lossless round-trip conversion **Smart Handling** - Automatic schema detection - Smart quote handling (only when needed) - All primitive types supported (string, number, boolean, null) **CLI Tool** - Convert files from command line - Auto-detect format based on extension (.json, .yaml, .yml, .tonl) - Optional token savings statistics **MCP Server** (New in v0.9.0) - HTTP/SSE transport for remote connections - Bearer token authentication - Session management - Graceful shutdown handling - [📖 MCP Server Guide](https://tonl-mcp-bridge-docs.vercel.app/guide/mcp-server) **Vector Database Integration** (New in v0.9.0) - Milvus adapter with automatic TONL conversion - Qdrant adapter with search optimization - Built-in token statistics - [📖 Milvus Guide](https://tonl-mcp-bridge-docs.vercel.app/guide/milvus) | [📖 Qdrant Guide](https://tonl-mcp-bridge-docs.vercel.app/guide/qdrant) **Privacy & Compliance** (New in v0.9.0) - Field-level data anonymization - GDPR/HIPAA-ready redaction - Configurable sensitive field masking - [📖 Privacy Guide](https://tonl-mcp-bridge-docs.vercel.app/guide/privacy) **Type Safety** - Full TypeScript support - Comprehensive type definitions - Runtime type checking **Battle Tested** - 167/167 unit tests passing - Edge cases handled - Production-ready code ## Installation ### Global Installation (CLI) ```bash npm install -g tonl-mcp-bridge ``` ### Local Installation (Library) ```bash npm install tonl-mcp-bridge ``` ### MCP Server ```bash npm install -g tonl-mcp-bridge export TONL_AUTH_TOKEN=your-secure-token npx tonl-mcp-server ``` ### Docker ```bash docker run -d \ -p 3000:3000 \ -e TONL_AUTH_TOKEN=your-token \ ghcr.io/kryptomrx/tonl-mcp-bridge:latest ``` ## CLI Usage ### Convert JSON to TONL ```bash tonl convert data.json # With statistics tonl convert data.json -s # Output: # 📄 Converting JSON → TONL... # ✅ Converted successfully! # 📁 Output: data.tonl # 📊 Token Statistics: # Input: 118 tokens # Output: 75 tokens # Saved: 43 tokens (36.4%) ``` ### Convert YAML to TONL ```bash tonl convert prompts.yaml -s # Auto-detects .yaml and .yml files ``` ### Convert TONL back to JSON/YAML ```bash # To JSON (default) tonl convert data.tonl # To YAML (specify extension) tonl convert data.tonl output.yaml ``` ### Custom Collection Name ```bash tonl convert users.json --name users # Creates: users[5]{...}: instead of data[5]{...}: ``` ### Specify Output Path ```bash tonl convert input.json output.tonl ``` ## Programmatic Usage ### JSON → TONL ```typescript import { jsonToTonl } from 'tonl-mcp-bridge'; const users = [ { id: 1, name: "Alice", age: 25 }, { id: 2, name: "Bob", age: 30 } ]; const tonl = jsonToTonl(users, "users"); console.log(tonl); // users[2]{id:i32,name:str,age:i32}: // 1, Alice, 25 // 2, Bob, 30 ``` ### TONL → JSON ```typescript import { tonlToJson } from 'tonl-mcp-bridge'; const tonl = `users[2]{id:i32,name:str}: 1, Alice 2, Bob`; const json = tonlToJson(tonl); console.log(json); // [ // { id: 1, name: "Alice" }, // { id: 2, name: "Bob" } // ] ``` ### YAML ↔ TONL ```typescript import { yamlToTonl, tonlToYaml } from 'tonl-mcp-bridge'; // YAML → TONL const yamlStr = ` - role: storyteller context: fantasy tone: dramatic `; const tonl = yamlToTonl(yamlStr, 'prompts'); // TONL → YAML const yaml = tonlToYaml(tonl); ``` ### Token Savings ```typescript import { calculateSavings } from 'tonl-mcp-bridge'; const jsonStr = JSON.stringify(data); const tonlStr = jsonToTonl(data); const savings = calculateSavings(jsonStr, tonlStr); console.log(`Saved ${savings.savingsPercent}% tokens!`); ``` ### Vector Database Integration (New in v0.9.0) ```typescript import { MilvusAdapter } from 'tonl-mcp-bridge/sdk/vector'; const milvus = new MilvusAdapter({ address: 'localhost:19530', username: 'root', password: 'milvus' }); await milvus.connect(); // Search with automatic TONL conversion const result = await milvus.searchToTonl( 'documents', queryEmbedding, { limit: 10 } ); console.log(result.tonl); console.log(`Saved ${result.stats.savingsPercent}% tokens`); ``` [📖 See full vector database documentation](https://tonl-mcp-bridge-docs.vercel.app/guide/milvus) ### Privacy & Anonymization (New in v0.9.0) ```typescript import { jsonToTonl } from 'tonl-mcp-bridge'; const users = [ { id: 1, name: 'Alice', email: 'alice@company.com', ssn: '123-45-6789' } ]; // Redact sensitive fields const tonl = jsonToTonl(users, 'users', { anonymize: ['email', 'ssn'] }); // Output: users[1]{id:i32,name:str,email:str,ssn:str}: // 1, Alice, "[REDACTED]", "[REDACTED]" ``` [📖 See privacy documentation](https://tonl-mcp-bridge-docs.vercel.app/guide/privacy) ## Benchmarks | Dataset Size | JSON Tokens | TONL Tokens | Savings | |--------------|-------------|-------------|---------| | 2 items | 12 | 12 | 0% | | 5 items | 118 | 75 | 36.4% | | 10 items | 247 | 134 | 45.7% | | 100 items | 2,470 | 987 | 60.0% | | 1000 items | 24,700 | 9,870 | 60.0% | *Based on GPT-4 tokenizer (~4 chars = 1 token)* **Key Insight:** Savings increase with dataset size! 📈 ## Real-World Impact ### YAML Prompts (Tested) ```yaml # Original YAML: 75 tokens - role: storyteller context: fantasy_world tone: dramatic setting: dark_forest goal: create_mystery ``` ```tonl # TONL: 61 tokens (18.7% savings) data[3]{role:str,context:str,tone:str,setting:str,goal:str}: storyteller, fantasy_world, dramatic, dark_forest, create_mystery ``` ### Enterprise Scale Example **Scenario:** AI platform with vector database RAG **Before (JSON):** ``` - 1M queries/day - 1000 results per query - ~500KB JSON per response - ~125K tokens per query - $3.75 per query (GPT-4) - Daily cost: $3.75M 💸 ``` **After (TONL Bridge):** ``` - Same 1M queries/day - Same 1000 results - ~200KB TONL per response (60% smaller) - ~50K tokens per query - $1.50 per query - Daily cost: $1.5M - Monthly savings: $67.5M ``` ## Development ```bash # Clone repository git clone https://github.com/kryptomrx/tonl-mcp-bridge.git cd tonl-mcp-bridge # Install dependencies npm install # Run tests npm test # Run tests in watch mode npm test -- --watch # Build npm run build # Test CLI locally npm run cli convert file.json ``` ## Tech Stack - **TypeScript 5.3** - Type-safe development - **Vitest** - Lightning fast unit testing - **Commander.js** - CLI framework - **js-yaml** - YAML parsing - **Express** - HTTP server (v0.9.0+) - **@modelcontextprotocol/sdk** - MCP integration (v0.9.0+) - **Node.js 18+** - Modern JavaScript runtime ## Roadmap - [x] **Phase 1: Core Engine** - [x] Token counter - [x] Type detector - [x] JSON → TONL converter - [x] TONL → JSON parser - [x] YAML support - [x] CLI tool - [x] npm package published - [x] **Phase 2: Advanced Features** (v0.6.0 - v0.9.0) - [x] MCP Server integration - [x] Vector database adapters (Milvus, Qdrant) - [x] Privacy & anonymization - [x] Batch processing - [x] Query analysis - [x] Schema drift detection - [x] Docker support - [ ] **Phase 3: Production Infrastructure** - [ ] Kubernetes manifests - [ ] Health check endpoints - [ ] Prometheus metrics - [ ] Grafana dashboards - [ ] **Phase 4: Ecosystem** - [ ] VS Code extension - [ ] Langchain integration - [ ] LlamaIndex plugin - [ ] Serverless deployments (AWS Lambda, Cloudflare Workers) **Current Status:** Phase 2 complete! Production-ready for RAG systems and vector databases. [📖 View detailed roadmap](https://tonl-mcp-bridge-docs.vercel.app/guide/roadmap) ## Documentation Full documentation available at: **https://tonl-mcp-bridge-docs.vercel.app/** **Guides:** - [Getting Started](https://tonl-mcp-bridge-docs.vercel.app/guide/getting-started) - [MCP Server](https://tonl-mcp-bridge-docs.vercel.app/guide/mcp-server) - [Milvus Integration](https://tonl-mcp-bridge-docs.vercel.app/guide/milvus) - [Qdrant Integration](https://tonl-mcp-bridge-docs.vercel.app/guide/qdrant) - [Privacy & Compliance](https://tonl-mcp-bridge-docs.vercel.app/guide/privacy) - [Token Savings](https://tonl-mcp-bridge-docs.vercel.app/guide/token-savings) **API Reference:** - [Core API](https://tonl-mcp-bridge-docs.vercel.app/api/core) - [Server API](https://tonl-mcp-bridge-docs.vercel.app/api/server) - [Vector Adapters](https://tonl-mcp-bridge-docs.vercel.app/api/vector) ## Contributing Contributions are welcome! Please feel free to submit a Pull Request. 1. Fork the repository 2. Create your feature branch (`git checkout -b feature/AmazingFeature`) 3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) 4. Push to the branch (`git push origin feature/AmazingFeature`) 5. Open a Pull Request ## License MIT © [kryptomrx](https://github.com/kryptomrx) ## Links - **npm:** https://www.npmjs.com/package/tonl-mcp-bridge - **GitHub:** https://github.com/kryptomrx/tonl-mcp-bridge - **Documentation:** https://tonl-mcp-bridge-docs.vercel.app/ - **Issues:** https://github.com/kryptomrx/tonl-mcp-bridge/issues --- **Built with ❤️ by a developer who was tired of wasting tokens on verbose JSON** 🚀 *If this saved you money, consider giving it a ⭐ on GitHub!*