prompt-compressor-mcp
# Prompt Compressor
> Rule-based prompt compression โ reduce LLM token usage without a model, without an API, without sending data anywhere.
[](https://www.npmjs.com/package/prompt-compressor)
[](https://www.npmjs.com/package/prompt-compressor-mcp)
[](https://prompt-compressor.com)
โญ **Find it useful? [Star it on GitHub](https://github.com/kettertom/prompt-compressor)** ยท ๐ **Found a bug or have an idea? [Open an issue](https://github.com/kettertom/prompt-compressor/issues/new)**
---
## Three ways to use it
### Web tool
**[prompt-compressor.com](https://prompt-compressor.com)** โ paste a prompt, pick a risk level, review every change individually, download.
### npm package
```bash
npm install prompt-compressor
```
```js
import { compress } from 'prompt-compressor';
const { output, stats } = compress(systemPrompt, 'low');
// { tokensBefore: 1200, tokensAfter: 820, saved: 380, reductionPct: 31 }
```
### MCP server (Claude, Cursor, VS Code, Windsurf)
Give your AI assistant compression tools. No install step, because your client runs it with `npx`.
**Claude Code:**
```bash
claude mcp add prompt-compressor -- npx -y prompt-compressor-mcp
```
**Claude Desktop / Cursor / Windsurf** (`claude_desktop_config.json`, `~/.cursor/mcp.json`, `~/.codeium/windsurf/mcp_config.json`):
```json
{
"mcpServers": {
"prompt-compressor": {
"command": "npx",
"args": ["-y", "prompt-compressor-mcp"]
}
}
}
```
**VS Code** (`.vscode/mcp.json`):
```json
{
"servers": {
"prompt-compressor": { "type": "stdio", "command": "npx", "args": ["-y", "prompt-compressor-mcp"] }
}
}
```
| Tool | What it does |
|---|---|
| `compress_prompt` | Compress text at a chosen risk level and return token stats |
| `compress_file` | Compress a local file (read-only, the file is not modified) |
| `compare_levels` | Show the savings at every risk level side by side |
| `estimate_tokens` | Quick token count |
Then just ask: *"Compress the system prompt in `prompts/support.md` at low risk and show how many tokens it saves."* More details are in the [MCP package README](src/mcp-package/README.md).
---
## Why
Token costs on Bedrock (and other LLM APIs) add up fast โ system prompts, tool descriptions, conversation history, RAG context. All of it gets sent on every call.
Most compression tools require a model to compress, which defeats the purpose. This uses deterministic rules only โ no model, no API, nothing leaves your process.
---
## Risk levels
Pick a level โ every algorithm at or below that risk runs automatically.
| Level | What runs | Typical saving |
|---|---|---|
| `'none'` | Whitespace, unicode normalization, indentation | 5โ15% |
| `'very-low'` | + Filler removal, phrase shortening, JSON/SQL minify | 15โ30% |
| `'low'` | + Hedging removal, code comments, deduplication, stack traces | 25โ45% |
| `'medium'` | + TF-IDF, TextRank, fuzzy/n-gram/Levenshtein dedup | 30โ70% |
---
## npm package usage
```js
// Simple
const { output, stats } = compress(text, 'low');
// With overrides
const { output, stats } = compress(text, {
level: 'low',
tfidf: true,
tfidfRatio: 0.7,
codeComments: false, // disable a step
});
// Individual functions
import { removeFiller, tfidfCompress, deduplicateLines } from 'prompt-compressor';
```
### Inside your own MCP server or Bedrock pipeline
```js
import { compress } from 'prompt-compressor';
// Before every API call:
const { output } = compress(systemPrompt, 'low');
const response = await bedrock.invokeModel({ body: JSON.stringify({ system: output, messages }) });
```
### Variable protection
Prompt variables are automatically detected and preserved โ compression never breaks templates.
Supported: `{{var}}`, `{var}`, `${var}`, `<tag>`, `[[var]]`, `%VAR%`
---
## Algorithms (30+)
| Algorithm | Risk | What it does |
|---|---|---|
| `compressWhitespace` | none | Collapse spaces, tabs, excess blank lines |
| `normalizeUnicode` | none | Smart quotes, em-dash, zero-width chars |
| `cleanPunctuation` | none | `!!!` โ `!`, `???` โ `?` |
| `stripLicenseHeaders` | none | Remove copyright blocks from code |
| `normalizeIndentation` | none | 4-space โ 2-space in code blocks |
| `removeFiller` | very-low | Remove "please note that", "e.g.", "i.e." clauses |
| `shortenPhrases` | very-low | "in order to" โ "to", "utilize" โ "use" (50+ rules) |
| `numberWordsToNumerals` | very-low | "twenty" โ "20" |
| `normalizeDates` | very-low | "Monday, Dec 25th 2023" โ "2023-12-25" |
| `minifyJSON` | very-low | Minify JSON code blocks |
| `minifySQL` | very-low | Remove SQL comments, collapse whitespace |
| `removeHedging` | low | Remove "perhaps", "it seems", "one might say" |
| `removeMetaCommentary` | low | Remove "Here is...", "I hope this helps" |
| `removeParentheticals` | low | Remove "(as mentioned above)", "(see below)" |
| `stripCodeComments` | low | Remove `//`, `/* */`, `#` comments |
| `stripDocstrings` | low | Remove Python/JSDoc documentation blocks |
| `removeNullJsonFields` | low | Strip null/empty fields from JSON |
| `stripBase64AndHex` | low | Replace long hashes with placeholders |
| `truncateStackTraces` | low | Keep 4 frames, replace rest with count |
| `deduplicateLines` | low | Remove duplicate lines |
| `deduplicateSentences` | low | Remove duplicate sentences |
| `fuzzyDeduplicate` | medium | Jaccard-based near-duplicate removal |
| `ngramDeduplicate` | medium | Bigram/trigram near-duplicate removal |
| `levenshteinDeduplicate` | medium | Edit-distance near-duplicate removal |
| `tfidfCompress` | medium | Keep top sentences by TF-IDF score |
| `textRankCompress` | medium | PageRank-style sentence centrality |
| `extractiveCompress` | medium | Keep top sentences by Term Frequency |
---
## Repository structure
```
/ โ web app (React + Vite)
โโโ src/
โ โโโ compression.js โ all compression logic
โ โโโ App.jsx โ UI
โ โโโ ReviewDiffView.jsx
โ โโโ ...
โโโ npm-package/ โ npm package `prompt-compressor` (separate, zero shared code)
โ โโโ src/
โ โ โโโ algorithms.js
โ โ โโโ protect.js
โ โโโ test/
โ โโโ index.js
โ โโโ index.d.ts
โ โโโ package.json
โโโ mcp-package/ โ MCP server `prompt-compressor-mcp` (uses the npm package)
โโโ index.js โ tools: compress_prompt, compress_file, compare_levels, estimate_tokens
โโโ bin.js โ stdio entry point
โโโ test/
โโโ server.json โ MCP registry manifest
```
### Releasing
Bump `version` in `src/npm-package/package.json` and/or `src/mcp-package/package.json`, then merge to `main`. The **Release to npm** workflow publishes any version that isn't on npm yet, library first and then the MCP server. It needs the `NPM_TOKEN` secret. Pushing an `npm-v*` / `mcp-v*` tag or using "Run workflow" also works.
---
## Run the web app locally
```bash
npm install
npm run dev
```
Open [http://localhost:5173](http://localhost:5173)
---
## License
MIT โ [Tom Ketter](https://github.com/kettertom)
TDQS
Scored across 4 tools
Each tool has a distinct input mode (raw text vs. file path) and a distinct action (compress, compare, estimate), so misselection is unlikely. The only mild overlap is that estimate_tokens returns token counts that the compress tools also report, but the descriptions make the fast-heuristic-only purpose clear.
All four names use snake_case with a verb_noun pattern (compress_prompt, compress_file, compare_levels, estimate_tokens). The two compress_* tools share a prefix that correctly signals a shared operation with different targets.
Four tools is well-scoped for a narrow prompt-compression utility: text compression, file compression, level comparison, and token estimation. Each tool covers a genuinely different need with no filler.
The core lifecycle (estimate cost, compress input, compress file, tune the risk/savings trade-off) is fully covered with no dead ends. Minor gaps remain around batch/directory processing and a way to inspect or restore what was changed, but agents can work around these with repeated calls.