A lightweight, zero-dependency CLI tool built natively with Node.js ES Modules to aggressively optimize data files, code snippets, and raw logs before pasting them into LLM context windows (like Claude, ChatGPT, or Gemini).
Save up to 60%+ on token usage, prevent context dilution, and keep your AI interactions incredibly fast and accurate.
Large Language Models (LLMs) charge you, slow down, and lose focus based on the volume of tokens in your prompt. Standard production logs, raw JSON payloads, and source code are filled with structural whitespace, repetitive arrays, and verbose metadata that exhaust your context window.
mcx shreds the bloat while preserving data schemas and critical logic architectures, allowing you to feed maximum structural context to an AI in the absolute minimum number of tokens.
The project relies entirely on native Node.js ESM imports/exports without a single external npm dependency.
This file houses the underlying compression algorithms for processing JSON structural truncation, log sanitization, and code stripping.
/**
* Intelligently minifies JSON strings and truncates heavy arrays
* to prevent token flooding while preserving schema shape.
*/
export function compressJson(rawData) {
const parsed = JSON.parse(rawData);
// Custom replacer to locate arrays and truncate elements safely
const walker = (key, value) => {
if (Array.isArray(value) && value.length > 2) {
const firstItem = value[0];
const lastItem = value[value.length - 1];
const truncatedCount = value.length - 2;
return [firstItem, `... [Truncated ${truncatedCount} items] ...`, lastItem];
}
return value;
};
const structuralTarget = JSON.parse(JSON.stringify(parsed, walker));
return JSON.stringify(structuralTarget);
}
/**
* Condenses verbose application logs by dropping excess whitespace,
* stripping repetitive noise, and isolating error profiles.
*/
export function compressLog(rawLogs) {
return rawLogs
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0)
.join('\n');
}
/**
* Eliminates comments, inline documentation, and formatting structures
* to isolate pure, dense functional logic.
*/
export function compressCode(rawCode) {
// Simple regex sequence to clear out single-line and block comments
return rawCode
.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1')
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0)
.join(' ');
}