Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
Updated README and mcp docs
chopratejas891d4d0cad20
diff --git a/README.md b/README.mdindex 3cb3eff61..f27318057 100644--- a/README.md+++ b/README.md@@ -296,7 +296,7 @@ Context compression is a new space. Here's how the approaches differ:| ASGI middleware | **Stable** | [Integration Guide](docs/integration-guide.md#asgi-middleware) || Proxy server | **Stable** | [Proxy Docs](docs/proxy.md) || Agno | **Stable** | [Agno Guide](docs/agno.md) |-| MCP (Claude Code) | **Stable** | [MCP Guide](docs/mcp.md) |+| MCP (Claude Code, Cursor, etc.) | **Stable** | [MCP Guide](docs/mcp.md) || Strands | **Stable** | [Strands Guide](docs/strands.md) || LangChain | **Experimental** | [LangChain Guide](docs/langchain.md) |@@ -318,8 +318,9 @@ headroom proxy --backend openrouter # OpenRouter (400+ model```bashpip install headroom-ai # Core librarypip install "headroom-ai[all]" # Everything including evals (recommended)-pip install "headroom-ai[proxy]" # Proxy server-pip install "headroom-ai[mcp]" # MCP for Claude Code+pip install "headroom-ai[proxy]" # Proxy server + MCP tools+pip install "headroom-ai[mcp]" # MCP tools only (no proxy)+pip install "headroom-ai[ml]" # ML compression (Kompress, requires torch)pip install "headroom-ai[agno]" # Agno integrationpip install "headroom-ai[langchain]" # LangChain (experimental)pip install "headroom-ai[evals]" # Evaluation framework only@@ -343,7 +344,7 @@ Python 3.10+| [Evals Framework](headroom/evals/README.md) | Prove compression preserves accuracy || [Memory](docs/memory.md) | Persistent memory || [Agno](docs/agno.md) | Agno agent framework |-| [MCP](docs/mcp.md) | Claude Code subscriptions |+| [MCP](docs/mcp.md) | Context engineering toolkit (compress, retrieve, stats) || [Learn](docs/learn.md) | Offline failure learning for coding agents || [Configuration](docs/configuration.md) | All options |diff --git a/docs/mcp.md b/docs/mcp.mdindex 5b8a65bee..e89816279 100644--- a/docs/mcp.md+++ b/docs/mcp.md@@ -1,164 +1,199 @@-# MCP Server for Claude Code Subscriptions+# MCP Server — Context Engineering Toolkit-Headroom's MCP (Model Context Protocol) server enables **CCR (Compress-Cache-Retrieve)** for Claude Code subscription users who don't have direct API access.+Headroom's MCP server exposes **compression, retrieval, and observability** as tools that any MCP-compatible AI coding tool can use — Claude Code, Cursor, Codex, and more.## Quick Start```bash-# Install MCP dependencies-pip install "headroom-ai[mcp]"+# Install (MCP is included with proxy, or standalone)+pip install "headroom-ai[proxy]" # Proxy + MCP tools+pip install "headroom-ai[mcp]" # MCP tools only (lightweight)-# Configure Claude Code (one-time)+# Register with Claude Code (one-time)headroom mcp install-# Start the proxy+# Start Claude Code — it now has headroom tools!+claude+```++That's it. Claude Code can now compress content on demand, retrieve originals, and check session stats — **no proxy required**.++For automatic compression of ALL traffic, also run the proxy:++```bash+# Terminal 1headroom proxy-# Use Claude Code - it now has headroom_retrieve!-claude+# Terminal 2+ANTHROPIC_BASE_URL=http://127.0.0.1:8787 claude```-## Why MCP?+## Tools-| Authentication | Custom Tools | Solution |-|----------------|--------------|----------|-| **API Key** | Direct injection via Messages API | Works automatically |-| **Subscription** | Claude Code's built-in tools only | MCP server |+The MCP server provides three tools:-Claude Code subscription users can't inject custom tools programmatically. MCP is Claude's official extension mechanism that works with subscriptions.+### headroom_compress-## How It Works+Compress content on demand. The LLM calls this when it wants to shrink large content before reasoning over it.```-┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐-│ Claude Code │────▶│ Headroom Proxy │────▶│ LLM Provider │-└────────┬────────┘ └────────┬────────┘ └─────────────────┘- │ │- │ MCP │ Stores compressed- │ │ content- ▼ ▼-┌─────────────────┐ ┌─────────────────┐-│ MCP Server │◀───▶│ Compression │-│ (headroom_ │ │ Store │-│ retrieve) │ └─────────────────┘-└─────────────────┘-```+Tool: headroom_compress-1. **Proxy compresses** large tool outputs (file listings, search results, logs)-2. **Claude sees** compressed summaries with hash markers: `[47 items compressed... hash=abc123]`-3. **When needed**, Claude calls `headroom_retrieve` to get the original content-4. **MCP server** fetches from the proxy's compression store+Parameters:+ - content (required): Text to compress (files, JSON, logs, search results, etc.)-## CLI Commands+Returns:+ - compressed: Compressed text+ - hash: Key for retrieving the original later+ - original_tokens / compressed_tokens / savings_percent+ - transforms: Which compression algorithms were applied+```-### Install MCP Configuration+Example — Claude reads a large file, then compresses it:-```bash-headroom mcp install```+Claude: Let me compress this large output to save context space.-This writes to `~/.claude/mcp.json`:+→ headroom_compress(content="[5000 lines of grep results...]")-```json-{- "mcpServers": {- "headroom": {- "command": "headroom",- "args": ["mcp", "serve"]- }+← {+ "compressed": "[key matches with context...]",+ "hash": "a1b2c3d4e5f6...",+ "original_tokens": 12000,+ "compressed_tokens": 3200,+ "savings_percent": 73.3,+ "transforms": ["router:search:0.27"]}-}```-Options:-- `--proxy-url URL` - Custom proxy URL (default: `http://127.0.0.1:8787`)-- `--force` - Overwrite existing configuration+The original is stored locally for the session (1-hour TTL). If Claude needs the full content later, it calls `headroom_retrieve`.-### Check Status--```bash-headroom mcp status-```+### headroom_retrieve-Shows:-- MCP SDK installation status-- Claude Code configuration status-- Proxy connectivity+Retrieve original uncompressed content by hash.-Example output:-```-Headroom MCP Status-========================================-MCP SDK: ✓ Installed-Claude Config: ✓ Configured- /Users/you/.claude/mcp.json-Proxy URL: http://127.0.0.1:8787-Proxy Status: ✓ Running at http://127.0.0.1:8787```+Tool: headroom_retrieve-### Uninstall+Parameters:+ - hash (required): Hash key from compression+ - query (optional): Search within the original to return only matching items-```bash-headroom mcp uninstall+Returns:+ - original_content (full retrieval) or results (search)+ - source: "local" or "proxy"```-Removes headroom from `~/.claude/mcp.json` while preserving other MCP servers.+Retrieval checks the local store first (content compressed via `headroom_compress`), then falls back to the proxy's store (content compressed automatically by the proxy). Hashes from either source work transparently.-### Manual Server Start+### headroom_stats-```bash-headroom mcp serve-```+Session compression statistics — including sub-agent stats and proxy cache info.-This is called by Claude Code automatically. For debugging:+```+Tool: headroom_stats-```bash-headroom mcp serve --debug+Returns:+ - compressions, retrievals, tokens_saved, savings_percent+ - estimated_cost_saved_usd+ - recent_events (last 10 compression/retrieval events)+ - sub_agents (stats from sub-agent MCP instances, if any)+ - combined (main + sub-agent totals)+ - proxy (request count, cache hits, cost saved — if proxy is running)```-## The headroom_retrieve Tool+Sub-agent stats are aggregated via a shared stats file (`~/.headroom/session_stats.jsonl`). Each MCP server instance (main session and sub-agents) writes events there, and `headroom_stats` reads across all of them.++## Architecture-When the MCP server is active, Claude has access to:+### MCP Only (no proxy)```-Tool: headroom_retrieve+┌─────────────────────────────────────────────┐+│ Claude Code / Cursor / Codex │+│ │+│ LLM calls headroom_compress on demand │+│ ↓ │+│ Compression happens locally in MCP process │+│ Original stored in local CompressionStore │+│ ↓ │+│ LLM calls headroom_retrieve when needed │+└─────────────────────────────────────────────┘+```-Parameters:- - hash (required): Hash key from compression marker- - query (optional): Search query to filter results+### MCP + Proxy (full setup)-Returns:- - Full original content, or- - Filtered results matching query```+┌─────────────────────────────────────────────┐+│ Claude Code │+│ │+│ 1. Sends request ──→ Proxy (auto-compress) │+│ 2. Gets response with compressed outputs │+│ 3. Can call headroom_compress for more │+│ 4. headroom_retrieve checks: │+│ local store → proxy store │+└──────────────────┬──────────────────────────┘+ │ MCP (stdio)+ ▼+┌─────────────────────────────────────────────┐+│ Headroom MCP Server │+│ ├── headroom_compress (local compression) │+│ ├── headroom_retrieve (local + proxy) │+│ └── headroom_stats (aggregated stats) │+└─────────────────────────────────────────────┘+```++No double-compression: the proxy compresses at the HTTP level (before the LLM sees content). MCP tools operate after the LLM receives content. They don't touch the same data.-Example interaction:+## CLI Commands++### Install+```bash+headroom mcp install # Default setup+headroom mcp install --proxy-url http://host:9000 # Custom proxy URL+headroom mcp install --force # Overwrite existing```-Claude sees:- [47 log entries compressed. Showing first 3 + anomalies.- Use headroom_retrieve(hash="a1b2c3") for full logs]-Claude calls:- headroom_retrieve(hash="a1b2c3", query="error")+### Status-Returns:- [All log entries containing "error"]+```bash+headroom mcp status```-## Custom Proxy URL+```+Headroom MCP Status+========================================+MCP SDK: ✓ Installed+Claude Config: ✓ Configured+ /Users/you/.claude/mcp.json+Proxy URL: http://127.0.0.1:8787+Proxy Status: ✓ Running at http://127.0.0.1:8787+```-If your proxy runs on a different port:+### Uninstall```bash-# During install-headroom mcp install --proxy-url http://localhost:9000+headroom mcp uninstall+```-# Or via environment variable-export HEADROOM_PROXY_URL=http://localhost:9000-headroom mcp serve+### Debug++```bash+headroom mcp serve --debug```+## Cross-Tool Compatibility++The MCP server works with any MCP-compatible host:++| Tool | MCP Support | Setup |+|------|-------------|-------|+| Claude Code | Native | `headroom mcp install` |+| Cursor | Supported | Add to Cursor MCP settings |+| Codex | If supported | Configure MCP server |+| Any MCP host | Yes | Point to `headroom mcp serve` |+## Troubleshooting### "MCP SDK not installed"@@ -167,26 +202,24 @@ headroom mcp servepip install "headroom-ai[mcp]"```-### "Proxy not running"--Start the proxy in another terminal:+### "Proxy not running" (when using proxy features)```bash-headroom proxy+headroom proxy # In another terminal```### "Entry not found or expired"-Compressed entries expire after 5 minutes (TTL). The proxy must be running continuously during your session.+- Content compressed via `headroom_compress`: stored for 1 hour (session TTL)+- Content compressed by the proxy: stored for 5 minutes (proxy TTL)+- The proxy must be running for proxy-compressed content-### Claude doesn't see headroom_retrieve+### Claude doesn't see headroom tools-1. Check status: `headroom mcp status`+1. Check: `headroom mcp status`2. Restart Claude Code after installing MCP-3. Verify `~/.claude/mcp.json` exists and contains headroom--## API Users+3. Verify with `/mcp` in Claude Code — should show 3 headroom tools-If you have an `ANTHROPIC_API_KEY`, you don't need MCP. The proxy automatically injects the `headroom_retrieve` tool into API requests.+### Sub-agent stats not showing-MCP is specifically for subscription users who authenticate via Claude Code's OAuth flow rather than an API key.+Sub-agent stats appear in `headroom_stats` only after sub-agents have run compressions. The shared stats file is at `~/.headroom/session_stats.jsonl`.