-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·744 lines (662 loc) · 25.6 KB
/
index.js
File metadata and controls
executable file
·744 lines (662 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#!/usr/bin/env node
/**
* mcp-compress — First MCP Server for Data Compression
* =====================================================
* Gives any AI agent (Claude Code, OpenClaw, etc.) the ability to
* compress and decompress data through Model Context Protocol.
*
* 10,000+ MCP servers exist. Zero for compression. This is the first.
*
* Tools provided:
* compress — Compress text/JSON/CSV data (zstd, gzip, lz4, brotli)
* decompress — Decompress back to original
* analyze — Show compressibility, best algorithm, estimated savings
* store — Compress and store to disk with metadata
* retrieve — Decompress and retrieve from disk
* stats — Show compression stats across all stored data
*
* Usage:
* npx @shipitandpray/mcp-compress
*
* Add to Claude Code settings.json:
* "mcpServers": {
* "compress": {
* "command": "npx",
* "args": ["@shipitandpray/mcp-compress"]
* }
* }
*/
import { createInterface } from 'readline';
import { createGzip, createGunzip, gzipSync, gunzipSync, brotliCompressSync, brotliDecompressSync, deflateSync, inflateSync } from 'zlib';
import { writeFileSync, readFileSync, existsSync, mkdirSync, readdirSync, statSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
import { createHash } from 'crypto';
// ═══════════════════════════════════════════════════════════
// TurboQuant — Extreme numerical compression
// Based on Google Research's TurboQuant (ICLR 2026)
// Random rotation + quantization for vectors/numbers
// ═══════════════════════════════════════════════════════════
class TurboQuant {
constructor(bits = 4, blockSize = 32, seed = 42) {
this.bits = bits;
this.blockSize = blockSize;
this.seed = seed;
this.levels = 1 << bits;
}
_rng(seed) {
// Deterministic PRNG (mulberry32)
let s = seed | 0;
return () => { s = (s + 0x6D2B79F5) | 0; let t = Math.imul(s ^ (s >>> 15), 1 | s); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
}
_hadamard(x, inverse, seed) {
const n = x.length;
const result = [...x];
const rounds = 3;
const allSigns = [];
const rng = this._rng(seed);
for (let r = 0; r < rounds; r++) {
const signs = [];
for (let i = 0; i < n; i++) signs.push(rng() > 0.5 ? 1 : -1);
allSigns.push(signs);
}
const order = inverse ? [2, 1, 0] : [0, 1, 2];
for (const r of order) {
const signs = allSigns[r];
if (!inverse) {
for (let i = 0; i < n; i++) result[i] *= signs[i];
let h = 1;
while (h < n) {
for (let i = 0; i < n; i += h * 2)
for (let j = i; j < i + h; j++) {
const a = result[j], b = result[j + h];
result[j] = a + b; result[j + h] = a - b;
}
h *= 2;
}
const norm = Math.sqrt(n);
for (let i = 0; i < n; i++) result[i] /= norm;
} else {
let h = 1;
while (h < n) {
for (let i = 0; i < n; i += h * 2)
for (let j = i; j < i + h; j++) {
const a = result[j], b = result[j + h];
result[j] = a + b; result[j + h] = a - b;
}
h *= 2;
}
const norm = Math.sqrt(n);
for (let i = 0; i < n; i++) result[i] = result[i] / norm * signs[i];
}
}
return result;
}
compress(numbers) {
const bs = this.blockSize;
// Delta encode for correlated data
const first = numbers[0];
const deltas = [0];
for (let i = 1; i < numbers.length; i++) deltas.push(numbers[i] - numbers[i - 1]);
// Pad to block size
const padLen = (bs - (deltas.length % bs)) % bs;
const padded = [...deltas, ...Array(padLen).fill(0)];
const blockMins = [], blockMaxs = [], quantized = [];
for (let start = 0; start < padded.length; start += bs) {
const block = padded.slice(start, start + bs);
const rotated = this._hadamard(block, false, this.seed + start);
let mn = Infinity, mx = -Infinity;
for (const v of rotated) { if (v < mn) mn = v; if (v > mx) mx = v; }
blockMins.push(mn);
blockMaxs.push(mx);
const range = mx - mn || 1e-10;
for (const v of rotated) {
let q = Math.round((v - mn) / range * (this.levels - 1));
q = Math.max(0, Math.min(this.levels - 1, q));
quantized.push(q);
}
}
// Pack bits
const totalBits = quantized.length * this.bits;
const packed = new Uint8Array(Math.ceil(totalBits / 8));
let bitPos = 0;
for (const val of quantized) {
for (let b = 0; b < this.bits; b++) {
if (val & (1 << b)) packed[bitPos >> 3] |= (1 << (bitPos & 7));
bitPos++;
}
}
return {
packed: Buffer.from(packed).toString('base64'),
blockMins, blockMaxs,
first, originalLen: numbers.length, padLen,
bits: this.bits, blockSize: bs, seed: this.seed
};
}
decompress(compressed) {
const { packed: b64, blockMins, blockMaxs, first, originalLen, padLen, bits, blockSize: bs, seed } = compressed;
this.bits = bits;
this.blockSize = bs;
this.seed = seed;
this.levels = 1 << bits;
const packed = Buffer.from(b64, 'base64');
const totalQ = blockMins.length * bs;
// Unpack bits
const quantized = [];
let bitPos = 0;
for (let i = 0; i < totalQ; i++) {
let val = 0;
for (let b = 0; b < bits; b++) {
if (packed[bitPos >> 3] & (1 << (bitPos & 7))) val |= (1 << b);
bitPos++;
}
quantized.push(val);
}
// Dequantize and inverse rotate
const deltas = [];
for (let i = 0; i < blockMins.length; i++) {
const mn = blockMins[i], mx = blockMaxs[i];
const range = mx - mn || 1e-10;
const dequant = [];
for (let j = 0; j < bs; j++) {
dequant.push(mn + quantized[i * bs + j] / (this.levels - 1) * range);
}
const restored = this._hadamard(dequant, true, seed + i * bs);
deltas.push(...restored);
}
// Delta decode
const result = [first];
for (let i = 1; i < originalLen; i++) result.push(result[i - 1] + deltas[i]);
return result;
}
}
// ═══════════════════════════════════════════════════════════
// MCP Protocol Implementation (JSON-RPC over stdio)
// ═══════════════════════════════════════════════════════════
const STORE_DIR = join(homedir(), '.mcp-compress');
class MCPCompressServer {
constructor() {
this.version = '0.1.0';
this.stats = { total_compressed: 0, total_saved_bytes: 0, operations: 0 };
if (!existsSync(STORE_DIR)) mkdirSync(STORE_DIR, { recursive: true });
}
// ── Compression algorithms ──
handleQuantize(args) {
const { numbers, bits = 4 } = args;
if (!numbers) return { error: 'Missing "numbers" — provide a JSON array of numbers or comma-separated values' };
let nums;
try {
nums = typeof numbers === 'string' ? JSON.parse(numbers) : numbers;
if (!Array.isArray(nums)) nums = numbers.split(',').map(Number);
} catch (e) {
try { nums = numbers.split(',').map(s => parseFloat(s.trim())); }
catch (e2) { return { error: 'Could not parse numbers. Provide JSON array or comma-separated values.' }; }
}
if (nums.length < 2) return { error: 'Need at least 2 numbers' };
if (nums.some(n => isNaN(n))) return { error: 'All values must be numbers' };
const tq = new TurboQuant(bits, Math.min(32, Math.pow(2, Math.ceil(Math.log2(Math.min(nums.length, 32))))));
const compressed = tq.compress(nums);
const restored = tq.decompress(compressed);
const rawBytes = nums.length * 8; // float64
const compBytes = Buffer.from(compressed.packed, 'base64').length + compressed.blockMins.length * 16;
const ratio = rawBytes / compBytes;
let totalErr = 0, maxErr = 0;
for (let i = 0; i < nums.length; i++) {
const err = Math.abs(nums[i] - restored[i]);
totalErr += err;
if (err > maxErr) maxErr = err;
}
const mae = totalErr / nums.length;
const mape = nums.reduce((s, v, i) => s + (v !== 0 ? Math.abs(v - restored[i]) / Math.abs(v) : 0), 0) / nums.length * 100;
this.stats.operations++;
return {
algorithm: `turboquant-${bits}bit`,
original_count: nums.length,
original_bytes: rawBytes,
compressed_bytes: compBytes,
ratio: `${ratio.toFixed(1)}x`,
saved_percent: `${((1 - compBytes / rawBytes) * 100).toFixed(1)}%`,
mean_absolute_error: mae.toFixed(6),
max_error: maxErr.toFixed(6),
mape_percent: `${mape.toFixed(3)}%`,
bits_per_value: bits,
lossless: false,
compressed_data: JSON.stringify(compressed),
sample_original: nums.slice(0, 5),
sample_restored: restored.slice(0, 5).map(v => parseFloat(v.toFixed(6))),
};
}
handleDequantize(args) {
const { compressed_data } = args;
if (!compressed_data) return { error: 'Missing "compressed_data"' };
try {
const compressed = JSON.parse(compressed_data);
const tq = new TurboQuant();
const restored = tq.decompress(compressed);
this.stats.operations++;
return { numbers: restored.map(v => parseFloat(v.toFixed(8))), count: restored.length };
} catch (e) {
return { error: `Dequantize failed: ${e.message}` };
}
}
compressData(data, algorithm = 'auto') {
const buf = Buffer.from(data, 'utf-8');
const results = {};
if (algorithm === 'auto' || algorithm === 'gzip') {
try {
results.gzip = gzipSync(buf, { level: 9 });
} catch (e) { /* skip */ }
}
if (algorithm === 'auto' || algorithm === 'brotli') {
try {
results.brotli = brotliCompressSync(buf);
} catch (e) { /* skip */ }
}
if (algorithm === 'auto' || algorithm === 'deflate') {
try {
results.deflate = deflateSync(buf, { level: 9 });
} catch (e) { /* skip */ }
}
// TurboQuant: convert text to byte vector, quantize, compare
if (algorithm === 'auto' || algorithm === 'turboquant') {
try {
const bytes = Array.from(buf);
const tq = new TurboQuant(4, 32);
const tqCompressed = tq.compress(bytes);
const tqBytes = Buffer.from(tqCompressed.packed, 'base64').length +
tqCompressed.blockMins.length * 16 + 50; // metadata overhead
results.turboquant = Buffer.from(JSON.stringify(tqCompressed));
// Store actual compressed size for comparison
results._turboquant_size = tqBytes;
} catch (e) { /* skip if data too small */ }
}
if (algorithm === 'auto') {
// Pick the smallest
let best = null;
let bestSize = Infinity;
for (const [alg, compressed] of Object.entries(results)) {
if (alg.startsWith('_')) continue;
const size = alg === 'turboquant' ? (results._turboquant_size || compressed.length) : compressed.length;
if (size < bestSize) {
bestSize = size;
best = alg;
}
}
return {
algorithm: best || 'gzip',
compressed: results[best || 'gzip'],
originalSize: buf.length,
compressedSize: bestSize,
ratio: buf.length / bestSize,
allResults: Object.fromEntries(
Object.entries(results).filter(([k]) => !k.startsWith('_')).map(([k, v]) => {
const size = k === 'turboquant' ? (results._turboquant_size || v.length) : v.length;
return [k, { size, ratio: (buf.length / size).toFixed(2) }];
})
)
};
}
const compressed = results[algorithm];
if (!compressed) throw new Error(`Algorithm ${algorithm} failed`);
return {
algorithm,
compressed,
originalSize: buf.length,
compressedSize: compressed.length,
ratio: buf.length / compressed.length,
};
}
decompressData(base64Data, algorithm) {
const buf = Buffer.from(base64Data, 'base64');
let decompressed;
switch (algorithm) {
case 'gzip': decompressed = gunzipSync(buf); break;
case 'brotli': decompressed = brotliDecompressSync(buf); break;
case 'deflate': decompressed = inflateSync(buf); break;
case 'turboquant': {
const tqData = JSON.parse(buf.toString('utf-8'));
const tq = new TurboQuant();
const bytes = tq.decompress(tqData);
decompressed = Buffer.from(bytes.map(b => Math.round(Math.max(0, Math.min(255, b)))));
break;
}
default: throw new Error(`Unknown algorithm: ${algorithm}`);
}
return decompressed.toString('utf-8');
}
// ── Tool handlers ──
handleCompress(args) {
const { data, algorithm = 'auto' } = args;
if (!data) return { error: 'Missing "data" parameter' };
const result = this.compressData(data, algorithm);
this.stats.total_compressed++;
this.stats.total_saved_bytes += result.originalSize - result.compressedSize;
this.stats.operations++;
return {
compressed_base64: result.compressed.toString('base64'),
algorithm: result.algorithm,
original_size: result.originalSize,
compressed_size: result.compressedSize,
ratio: `${result.ratio.toFixed(1)}x`,
saved_bytes: result.originalSize - result.compressedSize,
saved_percent: `${((1 - result.compressedSize / result.originalSize) * 100).toFixed(1)}%`,
...(result.allResults ? { all_algorithms: result.allResults } : {})
};
}
handleDecompress(args) {
const { compressed_base64, algorithm } = args;
if (!compressed_base64) return { error: 'Missing "compressed_base64" parameter' };
if (!algorithm) return { error: 'Missing "algorithm" parameter' };
this.stats.operations++;
const original = this.decompressData(compressed_base64, algorithm);
return { data: original, size: original.length };
}
handleAnalyze(args) {
const { data } = args;
if (!data) return { error: 'Missing "data" parameter' };
const result = this.compressData(data, 'auto');
const buf = Buffer.from(data, 'utf-8');
// Estimate compressibility characteristics
const uniqueChars = new Set(data).size;
const entropy = this.shannonEntropy(data);
return {
original_size: buf.length,
best_algorithm: result.algorithm,
best_compressed_size: result.compressedSize,
best_ratio: `${result.ratio.toFixed(1)}x`,
all_algorithms: result.allResults,
entropy_bits_per_char: entropy.toFixed(3),
unique_characters: uniqueChars,
total_characters: data.length,
compressibility: entropy < 3 ? 'HIGH' : entropy < 5 ? 'MEDIUM' : 'LOW',
recommendation: result.ratio > 5 ? 'Highly compressible — compress everything'
: result.ratio > 2 ? 'Moderately compressible — compress for storage/transit'
: 'Low compressibility — data is already dense/random'
};
}
handleStore(args) {
const { data, name, algorithm = 'auto' } = args;
if (!data) return { error: 'Missing "data" parameter' };
const key = name || createHash('sha256').update(data).digest('hex').slice(0, 16);
const result = this.compressData(data, algorithm);
const metadata = {
key,
algorithm: result.algorithm,
originalSize: result.originalSize,
compressedSize: result.compressedSize,
ratio: result.ratio,
storedAt: new Date().toISOString(),
hash: createHash('sha256').update(data).digest('hex'),
};
const dataPath = join(STORE_DIR, `${key}.bin`);
const metaPath = join(STORE_DIR, `${key}.json`);
writeFileSync(dataPath, result.compressed);
writeFileSync(metaPath, JSON.stringify(metadata, null, 2));
this.stats.total_compressed++;
this.stats.total_saved_bytes += result.originalSize - result.compressedSize;
this.stats.operations++;
return {
key,
stored_at: dataPath,
original_size: result.originalSize,
compressed_size: result.compressedSize,
ratio: `${result.ratio.toFixed(1)}x`,
saved: `${result.originalSize - result.compressedSize} bytes`
};
}
handleRetrieve(args) {
const { key } = args;
if (!key) return { error: 'Missing "key" parameter' };
const dataPath = join(STORE_DIR, `${key}.bin`);
const metaPath = join(STORE_DIR, `${key}.json`);
if (!existsSync(dataPath) || !existsSync(metaPath)) {
return { error: `Key "${key}" not found` };
}
const metadata = JSON.parse(readFileSync(metaPath, 'utf-8'));
const compressed = readFileSync(dataPath);
const original = this.decompressData(compressed.toString('base64'), metadata.algorithm);
this.stats.operations++;
return {
key,
data: original,
original_size: metadata.originalSize,
compressed_size: metadata.compressedSize,
algorithm: metadata.algorithm,
stored_at: metadata.storedAt
};
}
handleStats() {
// Scan store directory
let totalStored = 0;
let totalOriginal = 0;
let totalCompressed = 0;
let fileCount = 0;
if (existsSync(STORE_DIR)) {
const files = readdirSync(STORE_DIR).filter(f => f.endsWith('.json'));
for (const f of files) {
try {
const meta = JSON.parse(readFileSync(join(STORE_DIR, f), 'utf-8'));
totalOriginal += meta.originalSize || 0;
totalCompressed += meta.compressedSize || 0;
fileCount++;
} catch (e) { /* skip corrupted */ }
}
}
return {
stored_items: fileCount,
total_original_bytes: totalOriginal,
total_compressed_bytes: totalCompressed,
total_saved_bytes: totalOriginal - totalCompressed,
overall_ratio: totalCompressed > 0 ? `${(totalOriginal / totalCompressed).toFixed(1)}x` : 'N/A',
session_operations: this.stats.operations,
store_path: STORE_DIR
};
}
handleList() {
if (!existsSync(STORE_DIR)) return { items: [] };
const files = readdirSync(STORE_DIR).filter(f => f.endsWith('.json'));
const items = files.map(f => {
try {
const meta = JSON.parse(readFileSync(join(STORE_DIR, f), 'utf-8'));
return {
key: meta.key,
algorithm: meta.algorithm,
original_size: meta.originalSize,
compressed_size: meta.compressedSize,
ratio: `${meta.ratio.toFixed(1)}x`,
stored_at: meta.storedAt
};
} catch (e) { return null; }
}).filter(Boolean);
return { items, count: items.length };
}
// ── Utility ──
shannonEntropy(str) {
const freq = {};
for (const c of str) freq[c] = (freq[c] || 0) + 1;
const len = str.length;
let entropy = 0;
for (const count of Object.values(freq)) {
const p = count / len;
if (p > 0) entropy -= p * Math.log2(p);
}
return entropy;
}
// ── MCP Protocol ──
getToolDefinitions() {
return [
{
name: 'compress',
description: 'Compress text/JSON/CSV data. Returns base64-encoded compressed data with compression ratio. Use algorithm="auto" to pick the best compression.',
inputSchema: {
type: 'object',
properties: {
data: { type: 'string', description: 'The data to compress (text, JSON, CSV, etc.)' },
algorithm: { type: 'string', enum: ['auto', 'gzip', 'brotli', 'deflate'], description: 'Compression algorithm. Default: auto (picks best)' }
},
required: ['data']
}
},
{
name: 'decompress',
description: 'Decompress previously compressed data. Requires the base64 compressed data and the algorithm used.',
inputSchema: {
type: 'object',
properties: {
compressed_base64: { type: 'string', description: 'Base64-encoded compressed data' },
algorithm: { type: 'string', enum: ['gzip', 'brotli', 'deflate'], description: 'Algorithm used for compression' }
},
required: ['compressed_base64', 'algorithm']
}
},
{
name: 'analyze',
description: 'Analyze how compressible data is. Shows best algorithm, compression ratio, entropy, and recommendation. Use this before deciding whether to compress.',
inputSchema: {
type: 'object',
properties: {
data: { type: 'string', description: 'The data to analyze' }
},
required: ['data']
}
},
{
name: 'store',
description: 'Compress data and store it to disk with a key. Retrieve later with the key. Like a compressed key-value store for agents.',
inputSchema: {
type: 'object',
properties: {
data: { type: 'string', description: 'Data to compress and store' },
name: { type: 'string', description: 'Key name for retrieval. Auto-generated if not provided.' },
algorithm: { type: 'string', enum: ['auto', 'gzip', 'brotli', 'deflate'], description: 'Compression algorithm' }
},
required: ['data']
}
},
{
name: 'retrieve',
description: 'Retrieve and decompress previously stored data by key.',
inputSchema: {
type: 'object',
properties: {
key: { type: 'string', description: 'The key used when storing the data' }
},
required: ['key']
}
},
{
name: 'list',
description: 'List all stored compressed items with their keys, sizes, and compression ratios.',
inputSchema: { type: 'object', properties: {} }
},
{
name: 'stats',
description: 'Show compression statistics: total items stored, bytes saved, overall compression ratio.',
inputSchema: { type: 'object', properties: {} }
},
{
name: 'quantize',
description: 'TurboQuant: Extreme compression for numerical data (prices, sensor readings, embeddings, vectors). Based on Google TurboQuant (ICLR 2026). Converts numbers to 1-4 bits using random rotation + quantization. Lossy but near-zero error on correlated data.',
inputSchema: {
type: 'object',
properties: {
numbers: { type: 'string', description: 'JSON array of numbers, e.g., "[1.5, 2.3, 3.1]" or comma-separated "1.5,2.3,3.1"' },
bits: { type: 'number', description: 'Bits per value: 1, 2, 3, or 4. Lower = more compression, more error. Default: 4' }
},
required: ['numbers']
}
},
{
name: 'dequantize',
description: 'Decompress TurboQuant-compressed numerical data back to numbers.',
inputSchema: {
type: 'object',
properties: {
compressed_data: { type: 'string', description: 'The compressed_data string from a quantize result' }
},
required: ['compressed_data']
}
}
];
}
handleRequest(request) {
const { method, params, id } = request;
switch (method) {
case 'initialize':
return {
jsonrpc: '2.0', id,
result: {
protocolVersion: '2024-11-05',
capabilities: { tools: {} },
serverInfo: { name: 'mcp-compress', version: this.version }
}
};
case 'notifications/initialized':
return null; // No response needed
case 'tools/list':
return {
jsonrpc: '2.0', id,
result: { tools: this.getToolDefinitions() }
};
case 'tools/call': {
const toolName = params?.name;
const args = params?.arguments || {};
let result;
try {
switch (toolName) {
case 'compress': result = this.handleCompress(args); break;
case 'decompress': result = this.handleDecompress(args); break;
case 'analyze': result = this.handleAnalyze(args); break;
case 'store': result = this.handleStore(args); break;
case 'retrieve': result = this.handleRetrieve(args); break;
case 'list': result = this.handleList(); break;
case 'stats': result = this.handleStats(); break;
case 'quantize': result = this.handleQuantize(args); break;
case 'dequantize': result = this.handleDequantize(args); break;
default: result = { error: `Unknown tool: ${toolName}` };
}
} catch (e) {
result = { error: e.message };
}
return {
jsonrpc: '2.0', id,
result: {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }]
}
};
}
default:
return {
jsonrpc: '2.0', id,
error: { code: -32601, message: `Method not found: ${method}` }
};
}
}
run() {
const rl = createInterface({ input: process.stdin, terminal: false });
let buffer = '';
rl.on('line', (line) => {
buffer += line;
try {
const request = JSON.parse(buffer);
buffer = '';
const response = this.handleRequest(request);
if (response) {
process.stdout.write(JSON.stringify(response) + '\n');
}
} catch (e) {
// Incomplete JSON, keep buffering
if (e instanceof SyntaxError) return;
buffer = '';
const errorResponse = {
jsonrpc: '2.0', id: null,
error: { code: -32700, message: `Parse error: ${e.message}` }
};
process.stdout.write(JSON.stringify(errorResponse) + '\n');
}
});
process.stderr.write('mcp-compress server running\n');
}
}
const server = new MCPCompressServer();
server.run();