lossless-context-mcp

lossless-context-mcp

MCP server providing lossless file reads with deduplication, per-repo token metering, and HMAC-signed context receipts. Enables auditable, vendor-neutral measurement of what an AI agent saw.

Category
访问服务器

README

lossless-context-mcp

CI

A context ledger for agent file reads: provably-lossless dedup of re-reads, per-repo token metering with dollar costs, and HMAC-signed receipts of exactly which file versions the model was shown.

Reality check: as a token saver, on real sessions this saves ~0%

Measured over 1,839 real Claude Code transcripts (16,823 Read calls, 64 MB) with the bundled bench/real-session.ts harness: −1.4% with a real tokenizer (i.e. slightly worse than baseline), because only 7–8% of reads are re-reads and the median session re-reads 0% of its files. Claude Code's native file-state cache already avoids redundant re-reads, so there is almost nothing left for this to save, and on changed re-reads a unified diff can be larger than the file. It is not a token-savings product for normal use. Reproduce: npx tsx bench/real-session.ts.

Re-measured 2026-08-06 on a grown corpus (3,363 transcripts, 26,041 reads, 116 MB): −2.6%, losslessness clean. The computed ceiling for any naive intra-session read-dedup tool on that corpus is about 0.2% — the big re-reads are already gone before any tool sees them.

v1.2 fixes the economics: the engine emits whichever is smaller (marker vs content, diff vs content), making it structurally unable to cost more than native reads. Same corpus, same day: +0.4% — almost entirely from large files re-read after small edits, the one dedup win that still exists. Small, real, and never negative. The ledger remains the reason this exists.

The earlier ~72% figure is a synthetic edit-loop that re-reads the same files dozens of times — a ceiling, not a typical workload (see BENCHMARK.md for the full methodology and range). The dedup layer still helps the niche workflows that genuinely re-read files many times in one context — and clients without native file-state caching — but it is not the reason this exists.

What it is (v1.1): measurement and evidence, not just savings

Token-saving tricks get absorbed by the platforms (the reality check above is that story: Claude Code's native cache already ate the opportunity). What doesn't get absorbed is vendor-neutral measurement and provenance. v1.1 builds both on the same ledger that makes reads lossless:

  1. Lossless reads (read_file, read_files) — full content on first contact; a tiny reuse-marker or a unified diff on re-reads, only when the model provably still holds the base version.
  2. Context metering (context_stats) — where this session's file-read tokens actually went: totals, per-repo breakdown, heaviest files, and a USD estimate. Counted with a real tokenizer on exactly what was sent. Works identically in any MCP client — Claude Code, Cursor, Cline, custom SDK agents.
  3. Signed context receipts (context_receipt / verify_context_receipt) — an auditable, verifiable answer to "what did the AI see when it did this?": every file/view shown to the model, the SHA-256 of every content version it saw, how each was delivered, token totals — HMAC-SHA256 signed. By default it signs with the same key file as trust-mcp receipts, so one key verifies a complete evidence chain: what the agent saw + what it did.

Why the read path doesn't hurt quality

Every other "send less" trick (slicing, summarizing, compressing) risks eliding something the model needed. This one is different: it only ever withholds or diffs content it can prove the model still has. The proof is bounded by context epochs.

  • First read of a file/view → full content.
  • Re-read of an unchanged view (same epoch) → a tiny "reuse what you have" marker.
  • Re-read of a changed view (same epoch) → a unified diff to apply to the copy you already have — never the whole thing again.
  • When Claude Code compacts (the lossy event that drops file bodies from context), a bundled PreCompact hook bumps the epoch → the server forgets what it "sent" and returns full content again.

So anything it elides is reconstructable from what it already sent this epoch. If it can't prove that, it sends the whole thing. There's also a force_full flag for when you want the full body regardless.

The test suite encodes this as a hard invariant: a 400-op randomized sequence (read / edit / re-read / compact) asserts the model's reconstructed view equals truth after every operation.

Install

npm i -g lossless-context-mcp        # or: npx lossless-context-mcp
claude mcp add lossless-context --scope user -- lossless-context-mcp

Wire the epoch hook so dedup stays lossless across compactions. In ~/.claude/settings.json:

{
  "hooks": {
    "SessionStart": [{ "hooks": [{ "type": "command", "command": "node <path>/hooks/reset-epoch.mjs" }] }],
    "PreCompact":   [{ "hooks": [{ "type": "command", "command": "node <path>/hooks/reset-epoch.mjs" }] }]
  }
}

Then tell Claude to prefer it (in CLAUDE.md): "Prefer the read_file tool from lossless-context for reading files; when it returns a diff, apply it to your prior copy; when it says unchanged, reuse what you have."

Tools

Tool What it does
read_file(path, symbol?, lines?, force_full?) Lossless read: full / unchanged-marker / diff. Optionally read just one symbol (function/class by name) or a lines:"40-90" range — each view dedups independently. Refuses binary files.
read_files(paths[], force_full?) Read a working set in one call — each file through the same ledger; per-file errors don't fail the batch.
outline(path) Cheap structural map (declaration lines, bodies elided) to navigate a big file before reading parts of it.
context_stats() Where this session's file-read tokens went: totals, per-repo breakdown, heaviest files, dedup savings, USD estimate (LOSSLESS_PRICE_PER_MTOK, default $3/MTok).
context_receipt(artifact) Issue a signed context receipt: every file/view shown, every content hash seen, delivery kinds, token totals. Key: LOSSLESS_RECEIPT_KEY or the shared trust key file.
verify_context_receipt(receipt, signature) Timing-safe verification; canonicalized, so JSON field order doesn't matter.

How it's measured

See BENCHMARK.md — the full methodology, the honest range (0% floor, 72.1% synthetic ceiling, −1.4% on 1,839 real sessions), the losslessness invariant, and how to run the same harness against your own transcripts or any other context tool.

Honest limits

  • Dedup savings depend on workload: real Claude Code sessions measure ~0% (see reality check); other clients without native file-state caching may see more. Measure yours: npx tsx bench/real-session.ts.
  • Diffs rely on the model applying a unified diff to its prior copy. That's bounded (only when the base is provably present this epoch) and escapable (force_full), but it is a behavioral dependency.
  • Receipts attest what this server sent the model — reads that bypass it (native Read) are not in the receipt. For complete coverage, route file reads through read_file/read_files.
  • Symbol extraction is a heuristic brace/indent pass — robust and dependency-free, but not a full parser. Precise tree-sitter extraction was evaluated and deferred: web-tree-sitter had ABI/API mismatches with prebuilt WASM grammars across minor versions — too fragile for a production dependency.
  • lossless_stats was renamed context_stats in v1.1.

Status

v1.2.0 — the never-lose engine: measured on 3,363 real transcripts, the old always-diff/always-marker behavior cost 2.6% MORE than native reads, so the engine now emits whichever is smaller (marker vs content, diff vs content) and is structurally unable to cost more than baseline. Plus the v1.1.0 ledger: per-repo token metering with USD estimates (context_stats), signed context receipts (context_receipt/verify_context_receipt, HMAC-SHA256, timing-safe verify), batch working-set reads (read_files), SHA-256 change detection (provably, not probabilistically, lossless), bounded-memory ledger. npm run build clean, npm test green (32 tests incl. the 400-op losslessness invariant and two never-lose regressions), over-the-wire stdio smoke passes. MIT.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选