MCP Hub
Back to servers

lean-ctx

Reduce AI coding costs by 99% — MCP Server + Shell Hook for Cursor, Claude Code, Copilot, Windsurf, Gemini CLI & 24 tools. Single Rust binary, zero telemetry.

GitHub
Stars
496
Forks
64
Updated
Apr 8, 2026
Validated
Apr 10, 2026
  ██╗     ███████╗ █████╗ ███╗   ██╗     ██████╗████████╗██╗  ██╗
  ██║     ██╔════╝██╔══██╗████╗  ██║    ██╔════╝╚══██╔══╝╚██╗██╔╝
  ██║     █████╗  ███████║██╔██╗ ██║    ██║        ██║    ╚███╔╝ 
  ██║     ██╔══╝  ██╔══██║██║╚██╗██║    ██║        ██║    ██╔██╗ 
  ███████╗███████╗██║  ██║██║ ╚████║    ╚██████╗   ██║   ██╔╝ ██╗
  ╚══════╝╚══════╝╚═╝  ╚═╝╚═╝  ╚═══╝     ╚═════╝   ╚═╝   ╚═╝  ╚═╝
             The Intelligence Layer for AI Coding

Reduce Claude Code, Cursor & Copilot Token Costs by 99% — Open Source MCP Server

Shell Hook + Context Server · 28 tools · 90+ patterns · Single Rust binary

CI Security crates.io Downloads npm pi-lean-ctx AUR License Discord X/Twitter Zero Telemetry

Website · Install · How It Works · Tools · Patterns · Changelog · Discord



lean-ctx reduces LLM token consumption by up to 99% through three complementary strategies in a single binary — making AI coding faster, cheaper, and more effective.


⚡ How lean-ctx Reduces AI Token Costs

  Without lean-ctx:                              With lean-ctx:

  LLM ──"read auth.ts"──▶ Editor ──▶ File       LLM ──"ctx_read auth.ts"──▶ lean-ctx ──▶ File
    ▲                                  │           ▲                           │            │
    │      ~2,000 tokens (full file)   │           │   ~13 tokens (cached)     │ cache+hash │
    └──────────────────────────────────┘           └────── (compressed) ───────┴────────────┘

  LLM ──"git status"──▶  Shell  ──▶  git        LLM ──"git status"──▶  lean-ctx  ──▶  git
    ▲                                 │            ▲                       │              │
    │     ~800 tokens (raw output)    │            │   ~150 tokens         │ compress     │
    └─────────────────────────────────┘            └────── (filtered) ─────┴──────────────┘
StrategyHowImpact
Shell HookTransparently compresses CLI output (90+ patterns) before it reaches the LLM60-95% savings
Context Server28 MCP tools for cached reads, mode selection, deltas, dedup, memory, multi-agent sharing74-99% savings
AI Tool HooksOne-command integration via lean-ctx init --agent <tool>Works everywhere

🎯 Token Savings — Real Numbers

OperationFreqWithoutWith lean-ctxSaved
File reads (cached)15×30,00019599%
File reads (map mode)10×20,0002,00090%
ls / find6,4001,28080%
git status/log/diff10×8,0002,40070%
grep / rg8,0002,40070%
cargo/npm build5,0001,00080%
Test runners10,0001,00090%
curl (JSON)1,50016589%
docker ps/build90018080%
Session total~89,800~10,62088%

Based on typical Cursor/Claude Code sessions with medium TypeScript/Rust projects. Cached re-reads cost ~13 tokens.


Why lean-ctx?

AI coding tools like Cursor, Claude Code, GitHub Copilot, Windsurf, and Gemini CLI send massive amounts of context to LLMs with every request. Each file read, git command, or directory listing consumes tokens — and tokens cost money. lean-ctx sits transparently between your AI tool and the LLM, compressing everything before it reaches the model. No workflow changes needed. Install once, save on every API call.

Common questions lean-ctx solves:

  • "How do I reduce Claude Code token costs?" → Shell hook + MCP server compress all context automatically
  • "Why is Cursor using so many tokens?" → Uncompressed file reads and CLI output waste 60-99% of tokens
  • "How to optimize MCP server performance?" → lean-ctx IS an optimized MCP server with 28 intelligent tools
  • "Best way to reduce AI coding API costs?" → Context compression saves $30-100+/month for active developers

🚀 Get Started (60 seconds)

# 1. Install (pick one)
curl -fsSL https://leanctx.com/install.sh | sh     # universal, no Rust needed
brew tap yvgude/lean-ctx && brew install lean-ctx    # macOS / Linux
npm install -g lean-ctx-bin                          # Node.js
cargo install lean-ctx                               # Rust

# 2. Setup (auto-configures shell + ALL detected editors)
lean-ctx setup

# 3. Verify
lean-ctx doctor
Troubleshooting
ProblemFix
Commands broken?Run lean-ctx-off (fixes current session)
Permanent fix?Run lean-ctx uninstall (removes all hooks)
Binary missing?Aliases auto-fallback to original commands (safe)
Manual fix?Edit ~/.zshrc, remove the lean-ctx shell hook block
Preview changes?lean-ctx init --global --dry-run
Diagnose?lean-ctx doctor

lean-ctx creates a backup of your shell config before modifying it (~/.zshrc.lean-ctx.bak).

Supported editors (auto-detected by lean-ctx setup)
EditorMethodStatus
CursorMCP + hooks + rules✅ Auto
Claude CodeMCP + PreToolUse hooks + rules✅ Auto
GitHub CopilotMCP✅ Auto
WindsurfMCP + rules✅ Auto
VS CodeMCP + rules✅ Auto
ZedContext Server (settings.json)✅ Auto
Codex CLIconfig.toml + AGENTS.md✅ Auto
Gemini CLIMCP + hooks + rules✅ Auto
OpenCodeMCP + rules✅ Auto
Pipi-lean-ctx npm package✅ Auto
Qwen CodeMCP + rules✅ Auto
TraeMCP + rules✅ Auto
Amazon Q DeveloperMCP + rules✅ Auto
JetBrains IDEsMCP + rules✅ Auto
Google AntigravityMCP + rules✅ Auto
Cline / Roo CodeMCP + rules✅ Auto
AiderShell hook + rules✅ Auto
AmpShell hook + rules✅ Auto
AWS KiroMCP + rules✅ Auto
ContinueMCP + rules✅ Auto
CrushMCP + rules✅ Auto

🧠 Three Intelligence Protocols

CEP

Cognitive Efficiency Protocol

Adaptive LLM communication with compliance scoring (0-100), task complexity classification, quality scoring, auto-validation pipeline.

Measurable efficiency gains

CCP

Context Continuity Protocol

Cross-session memory that persists tasks, findings, decisions across chats. LITM-aware positioning for optimal attention placement.

-99.2% cold-start tokens

TDD

Token Dense Dialect

Symbol shorthand (λ § τ ε) and ROI-based identifier mapping for compact LLM communication.

8-25% extra savings


🛠 28 Intelligent Tools

Core

ToolPurposeSavings
ctx_readFile reads — 7 modes + lines:N-M, caching, fresh=true74-99%
ctx_multi_readMultiple file reads in one round trip74-99%
ctx_treeDirectory listings (ls, find, Glob)34-60%
ctx_shellShell commands with 90+ compression patterns60-90%
ctx_searchCode search (Grep)50-80%
ctx_compressContext checkpoint for long conversations90-99%

Intelligence

ToolWhat it does
ctx_smart_readAdaptive mode — auto-picks full/map/signatures/diff based on file type and cache
ctx_deltaIncremental updates — only sends changed hunks via Myers diff
ctx_dedupCross-file deduplication — finds shared imports and boilerplate
ctx_fillPriority-based context filling — maximizes info within a token budget
ctx_intentSemantic intent detection — classifies queries and auto-loads files
ctx_responseResponse compression — removes filler, applies TDD
ctx_contextMulti-turn session overview — tracks what the LLM already knows
ctx_graphProject intelligence graph — dependency analysis + related file discovery
ctx_discoverShell history analysis — finds missed compression opportunities
ctx_editSearch-and-replace file editing — works without native Read/Edit tools
ctx_overviewTask-relevant project map — use at session start
ctx_preloadProactive context loader — caches task-relevant files, returns compact summary
ctx_semantic_searchBM25 code search by meaning — finds symbols and patterns across the project

Memory & Multi-Agent

ToolWhat it does
ctx_sessionCross-session memory — persist task, findings, decisions across chats
ctx_knowledgePersistent project knowledge — remember facts, recall by query/category
ctx_agentMulti-agent coordination — register, post/read scratchpad, handoff tasks, sync status
ctx_shareMulti-agent context sharing — push/pull cached file contexts between agents
ctx_wrappedShareable savings report — "Spotify Wrapped" for your tokens

Analysis

ToolWhat it does
ctx_benchmarkSingle-file or project-wide benchmark with preservation scores
ctx_metricsSession statistics with USD cost estimates
ctx_analyzeShannon entropy analysis + mode recommendation
ctx_cacheCache management: status, clear, invalidate

Integrations

PackageWhat it provides
VS Code Extension (packages/vscode-lean-ctx)Status bar token savings, one-click setup, MCP auto-config
Chrome Extension (packages/chrome-lean-ctx)Auto-compress pastes in ChatGPT, Claude, Gemini via native messaging

📖 ctx_read Modes

ModeWhen to useToken cost
fullFiles you will edit (cached re-reads ≈ 13 tokens)100% first, ~0% cached
mapUnderstanding a file — deps + exports + API~5-15%
signaturesAPI surface with more detail than map~10-20%
diffRe-reading files that changedchanged lines only
aggressiveLarge files with boilerplate~30-50%
entropyRepetitive patterns (Shannon + Jaccard filtering)~20-40%
lines:N-MSpecific ranges (e.g. lines:10-50,80-90)proportional

🔌 Shell Hook Patterns (90+)

Pattern-based compression for 90+ commands across 34 categories:

View all 34 categories
CategoryCommandsSavings
Git (19)status, log, diff, add, commit, push, pull, fetch, clone, branch, checkout, switch, merge, stash, tag, reset, remote, blame, cherry-pick70-95%
Docker (10)build, ps, images, logs, compose ps/up/down, exec, network, volume, inspect70-90%
npm/pnpm/yarn (6)install, test, run, list, outdated, audit70-90%
Cargo (3)build, test, clippy80%
GitHub CLI (9)pr list/view/create/merge, issue list/view/create, run list/view60-80%
Kubernetes (8)get pods/services/deployments, logs, describe, apply, delete, exec, top, rollout60-85%
Python (7)pip install/list/outdated/uninstall/check, ruff check/format60-80%
Ruby (4)rubocop, bundle install/update, rake test, rails test60-85%
Linters (4)eslint, biome, prettier, stylelint60-70%
Build Tools (3)tsc, next build, vite build60-80%
Test Runners (8)jest, vitest, pytest, go test, playwright, cypress, rspec, minitest90%
Terraforminit, plan, apply, destroy, validate, fmt, state, import, workspace60-85%
Makemake targets, parallel jobs, dry-run60-80%
Maven / Gradlecompile, test, package, install, clean, dependency trees60-85%
.NETdotnet build, test, restore, run, publish, pack60-85%
Flutter / Dartflutter pub, analyze, test, build; dart pub, analyze, test60-85%
Poetry / uvinstall, sync, lock, run, add, remove; uv pip/sync/run60-85%
AWS (7)s3, ec2, lambda, cloudformation, ecs, logs, sts60-80%
Databases (2)psql, mysql/mariadb50-80%
Prisma (6)generate, migrate, db push/pull, format, validate70-85%
Helm (5)list, install, upgrade, status, template60-80%
Bun (3)test, install, build60-85%
Deno (5)test, lint, check, fmt, task60-85%
Swift (3)test, build, package resolve60-80%
Zig (2)test, build60-80%
CMake (3)configure, build, ctest60-80%
Ansible (2)playbook recap, task summary60-80%
Composer (3)install, update, outdated60-80%
Mix (5)test, deps, compile, format, credo/dialyzer60-80%
Bazel (3)test, build, query60-80%
systemd (2)systemctl, journalctl50-80%
Utils (5)curl, grep/rg, find, ls, wget50-89%
Data (3)env (filtered), JSON schema extraction, log dedup50-80%

After lean-ctx init --global, 23 commands are transparently compressed via shell aliases:

git · npm · pnpm · yarn · cargo · docker · docker-compose · kubectl · k
gh · pip · pip3 · ruff · go · golangci-lint · eslint · prettier · tsc
ls · find · grep · curl · wget

👀 Examples

Directory listing — 239 → 46 tokens (-81%)
# ls -la src/                               # lean-ctx -c "ls -la src/"
total 96                                     core/
drwxr-xr-x  4 user staff  128 ...           tools/
drwxr-xr-x  11 user staff 352 ...           cli.rs  9.0K
-rw-r--r--  1 user staff  9182 ...           main.rs  4.0K
-rw-r--r--  1 user staff  4096 ...           server.rs  11.9K
...                                          shell.rs  5.2K
                                             4 files, 2 dirs
                                             [lean-ctx: 239→46 tok, -81%]
File reading (map mode) — 2,078 → ~30 tokens (-99%)
# Full read (284 lines, ~2078 tokens)       # lean-ctx read stats.rs -m map (~30 tokens)
use serde::{Deserialize, Serialize};         stats.rs [284L]
use std::collections::HashMap;                 deps: serde::
use std::path::PathBuf;                        exports: StatsStore, load, save, record, format_gain
                                               API:
#[derive(Serialize, Deserialize)]                cl ⊛ StatsStore
pub struct StatsStore {                          fn ⊛ load() → StatsStore
    pub total_commands: u64,                     fn ⊛ save(store:&StatsStore)
    pub total_input_tokens: u64,                 fn ⊛ record(command:s, input_tokens:n, output_tokens:n)
    ...                                          fn ⊛ format_gain() → String
(284 more lines)                             [2078 tok saved (100%)]
curl (JSON) — 127 → 14 tokens (-89%)
# curl -s httpbin.org/json                   # lean-ctx -c "curl -s httpbin.org/json"
{                                            JSON (428 bytes):
  "slideshow": {                             {
    "author": "Yours Truly",                   slideshow: {4K}
    "date": "date of publication",           }
    "slides": [                              [lean-ctx: 127→14 tok, -89%]
      {
        "title": "Wake up to WonderWidgets!",
        ...
Visual terminal dashboard
$ lean-ctx gain

  ◆ lean-ctx  Token Savings Dashboard
  ────────────────────────────────────────────────────────

   1.7M          76.8%         520          $33.71
   tokens saved   compression    commands       USD saved

  Cost Breakdown  (@ $2.50/M input, $10/M output)
  ────────────────────────────────────────────────────────
  Without lean-ctx    $44.75  ($5.79 input + $38.96 output)
  With lean-ctx       $11.04  ($1.76 input + $9.28 output)
  Saved               $33.71  ($4.03 input + $29.68 output)

  Top Commands
  ────────────────────────────────────────────────────────
  curl                48x  ████████████████████ 728.1K  97%
  git commit          34x  ██████████▎          375.2K  50%
  ctx_read           103x  █▌                    59.1K  38%
    ... +33 more commands

  lean-ctx v2.19.0  |  leanctx.com  |  lean-ctx dashboard

🔬 Scientific Compression Engine

Built on information theory and attention modeling (v2.6):

FeatureWhat it doesImpact
Adaptive EntropyPer-language BPE entropy + Jaccard thresholds with Kolmogorov adjustment10-25%
Attention ModelHeuristic U-curve positional weighting + structural importance scoring↑ comprehension
TF-IDF CodebookCross-file pattern dedup via cosine similarity5-15%
Feedback LoopLearns optimal thresholds per language/file type across sessionsauto-improving
Info BottleneckEntropy + task-relevance filtering (Tishby et al., 2000)20-40%
ctx_overviewMulti-resolution project map with graph-based relevance tiers90%+

🌳 tree-sitter Signature Engine

AST-based signature extraction for 18 languages: TypeScript, JavaScript, Rust, Python, Go, Java, C, C++, Ruby, C#, Kotlin, Swift, PHP, Bash, Dart, Scala, Elixir, Zig.

CapabilityRegex (old)tree-sitter
Multi-line signatures
Arrow functions
Nested classes/methodsHeuristicAST scope
Languages414

Build without tree-sitter for a smaller binary (~5.7 MB vs ~17 MB):

cargo install lean-ctx --no-default-features

📊 CLI Commands

Shell Hook
lean-ctx -c "git status"       # Execute + compress output
lean-ctx exec "cargo build"    # Same as -c
lean-ctx shell                 # Interactive REPL with compression
File Operations
lean-ctx read file.rs                         # Full content (structured header)
lean-ctx read file.rs -m map                  # Deps + API signatures (~10% tokens)
lean-ctx read file.rs -m signatures           # Function/class signatures only
lean-ctx read file.rs -m aggressive           # Syntax-stripped (~40% tokens)
lean-ctx read file.rs -m entropy              # Shannon entropy filtered (~30%)
lean-ctx read file.rs -m "lines:10-50,80-90"  # Specific line ranges
lean-ctx diff file1.rs file2.rs               # Compressed file diff
lean-ctx grep "pattern" src/                  # Grouped search results
lean-ctx find "*.rs" src/                     # Compact find results
lean-ctx ls src/                              # Token-optimized directory listing
lean-ctx deps .                               # Project dependencies summary
Setup & Analytics
lean-ctx setup                 # One-command setup: shell + editors + verify
lean-ctx init --global         # Install 23 shell aliases
lean-ctx init --agent claude   # Claude Code hook
lean-ctx init --agent cursor   # Cursor hooks.json
lean-ctx init --agent gemini   # Gemini CLI hook
lean-ctx init --agent codex    # Codex AGENTS.md
lean-ctx init --agent windsurf # .windsurfrules
lean-ctx init --agent cline    # .clinerules
lean-ctx init --agent crush    # Crush by Charmbracelet
lean-ctx init --agent pi       # Pi Coding Agent extension
lean-ctx gain                  # Visual terminal dashboard
lean-ctx gain --live           # Live auto-updating dashboard
lean-ctx gain --graph          # ASCII chart (30 days)
lean-ctx gain --daily          # Day-by-day breakdown
lean-ctx gain --json           # Raw JSON export
lean-ctx dashboard             # Web dashboard (localhost:3333)
lean-ctx cheatsheet            # Quick reference
lean-ctx discover              # Find uncompressed commands
lean-ctx doctor                # Diagnostics
lean-ctx update                # Self-update
lean-ctx wrapped               # Shareable savings report
lean-ctx benchmark run         # Real project benchmark
lean-ctx benchmark report      # Markdown report
Multi-Agent Launcher
lctx                              # Auto-detect agent, current dir
lctx --agent claude               # Launch Claude Code with lean-ctx
lctx --agent cursor               # Configure Cursor
lctx --agent gemini               # Launch Gemini CLI
lctx /path/to/project "prompt"    # Project + prompt
lctx --scan-only                  # Build project graph only

⚙️ Editor Configuration

lean-ctx setup handles this automatically. Manual config below is only needed for edge cases.

Cursor

~/.cursor/mcp.json:

{
  "mcpServers": {
    "lean-ctx": { "command": "lean-ctx" }
  }
}
GitHub Copilot

.github/copilot/mcp.json:

{
  "servers": {
    "lean-ctx": { "command": "lean-ctx" }
  }
}
Claude Code
claude mcp add lean-ctx lean-ctx
Windsurf

~/.codeium/windsurf/mcp_config.json:

{
  "mcpServers": {
    "lean-ctx": { "command": "lean-ctx" }
  }
}

If tools don't load, use the full path (e.g., /Users/you/.cargo/bin/lean-ctx). Windsurf spawns MCP servers with a minimal PATH.

Zed

~/.config/zed/settings.json:

{
  "context_servers": {
    "lean-ctx": {
      "source": "custom",
      "command": "lean-ctx",
      "args": [],
      "env": {}
    }
  }
}
OpenAI Codex

~/.codex/config.toml:

[mcp_servers.lean-ctx]
command = "lean-ctx"
args = []
Gemini CLI

~/.gemini/settings/mcp.json:

{
  "mcpServers": {
    "lean-ctx": { "command": "lean-ctx" }
  }
}
Pi Coding Agent
lean-ctx init --agent pi
# Or: pi install npm:pi-lean-ctx

Pi's bash, read, grep, find, and ls tools are automatically routed through lean-ctx. Supports 55+ file extensions with auto mode selection.

OpenCode

~/.config/opencode/opencode.json:

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "lean-ctx": {
      "type": "local",
      "command": ["lean-ctx"],
      "enabled": true
    }
  }
}

🏆 lean-ctx vs Alternatives (Rust Token Killer, context-mode)

FeatureRust Token Killerlean-ctx
ArchitectureShell hook onlyShell hook + MCP server
Process modelSpawns per commandPersistent server (no EAGAIN)
CLI patterns~5090+
File readingSignatures only7 modes (full, map, signatures, diff, aggressive, entropy, lines)
File caching✓ (re-reads ≈ 13 tokens)
Signature engineRegex (4 langs)tree-sitter AST (18 langs)
Dependency maps
Context checkpoints
Token countingEstimatedtiktoken-exact
Entropy analysis
Cost tracking✓ (USD estimates)
TDD mode✓ (8-25% extra)
Thinking reduction✓ (CRP v2)
Cross-session memory✓ (CCP)
LITM positioning
Multi-agent sharing
Project knowledge store
Web dashboard
Savings reports✓ (wrapped)
Raw mode / bypass✓ (raw=true, --raw, lean-ctx-raw)
User-defined filtersTOML rulesTOML rules (priority over builtins)
Full output recoverytee✓ (tee_mode: always/failures/never)
Truncation warnings✓ (transparent markers)
ANSI auto-strip✓ (pre-compression)
TelemetryDefault ON (PII)Zero. None. Ever.
Editor support3 editors24 editors/tools

🔐 Privacy & Security

lean-ctx is privacy-first by design:

  • Zero telemetry — no data collection, no analytics, no phone-home, ever
  • Zero network requests — everything runs locally on your machine
  • No PII exposure — no hostnames, usernames, or project paths leave your system
  • Fully auditable — MIT-licensed, single Rust binary, no hidden dependencies

See SECURITY.md.

Note on VirusTotal: Rust binaries are frequently flagged by ML-based heuristic scanners. This is a known issue. Build from source with cargo install lean-ctx to verify.


🗑 Uninstall

lean-ctx init --global   # See what was added, then remove from shell profile
cargo uninstall lean-ctx # Remove binary
rm -rf ~/.lean-ctx       # Remove stats + config

❓ Frequently Asked Questions

How much money does lean-ctx save?

Based on real usage data: active developers save $30-100+ per month on API costs. The exact amount depends on your AI tool, usage patterns, and pricing tier. lean-ctx shows your exact savings with lean-ctx gain.

Does lean-ctx work with Claude Code / Cursor / Copilot?

Yes — lean-ctx supports 24 AI coding tools out of the box. Run lean-ctx setup and it auto-detects and configures all installed editors. No manual configuration needed.

Does it slow down my AI tool?

No. lean-ctx adds <1ms overhead per operation. The MCP server runs as a persistent process (no cold starts), and the shell hook uses compiled Rust patterns. Most users report their AI tools feel faster because less data means faster LLM responses.

Is it safe? Does it send data anywhere?

lean-ctx has zero telemetry — no data collection, no analytics, no network requests, ever. Everything runs 100% locally. The code is MIT-licensed and fully auditable. See SECURITY.md.

What's the difference between lean-ctx and Rust Token Killer (RTK)?

lean-ctx is a hybrid architecture (shell hook + MCP server) while Rust Token Killer is shell-hook only. lean-ctx offers 28 tools vs RTK's basic compression, supports 24 editors vs 3, has tree-sitter AST parsing for 18 languages, cross-session memory, multi-agent coordination, and — critically — zero telemetry (RTK has default-on telemetry with PII).

How do I reduce my Claude Code / Cursor API bill?
  1. Install lean-ctx: npm install -g lean-ctx-bin
  2. Run lean-ctx setup (auto-configures everything)
  3. Use your AI tool normally — lean-ctx compresses context transparently
  4. Check savings: lean-ctx gain

Most users see 70-90% reduction in token usage from day one.


🤝 Contributing

Contributions welcome! Open an issue or PR on GitHub.

Discord · 𝕏 / Twitter · Buy me a coffee ☕


📄 License

MIT — see LICENSE.


Built with 🦀 Rust · Made in Switzerland 🇨🇭

Reviews

No reviews yet

Sign in to write a review