Token Optimizer

io.github.omkar9854由 GitHub 账号 omkar9854 发布v0.1.0更新于 Oct 24, 2026

Reversible context compression for AI agents: cut token usage up to 94%, originals retrievable

已验证STDIO仅桌面开发者工具AI 与机器学习

概览

AI 生成的概览

一个本地 MCP 服务器,在上下文(工具输出、日志、JSON、代码、文本)送入大模型前进行压缩,并可取回逐字节一致的原文。

可以做什么
在上下文送入模型前压缩,按 JSON、日志、代码和文本分别采用无损或可逆的转换以减少 token 用量。通过引用标记按需取回任意压缩内容的逐字节原文。错误和警告内容原样保留,系统提示和最近消息不被修改。原文可存于内存、SQLite 或自带的加密存储。
适用场景
适合智能体读取大量工具输出、日志、JSON 数据或源码,且 token 成本或上下文长度成为问题时使用,尤其适用于禁止外网访问或需交付客户环境的场景。
运行要求
通过 uvx 从 PyPI 包 slimctx 以本地进程运行(Python,仅用标准库)。README 中的 MCP 示例需指定一个 SQLite 存储路径。未声明账号、API 密钥、环境变量或请求头。
安装前请注意
部分内容类型的压缩是有损的,回答质量取决于模型是否按需取回原文。原文保存在本地存储中,除非提供加密器,否则为明文 SQLite。README 说明其功能少于所对比的 Headroom 项目。

安装

在 SourceWeft 中

  1. 打开 控制台中的 Token Optimizer,将其添加到工作区。
  2. 为需要使用其工具的对话启用该服务。

Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。

其他 MCP 客户端

参照 仓库 中的启动说明。

README

slimctx — the token optimizer for AI agents

[CI] [PyPI] [MCP Registry] [License: Apache 2.0] [Python 3.9+] [Dependencies: zero]

Zero-dependency, fully-reversible context compression for AI agents.

slimctx compresses what your agent reads — tool outputs, logs, JSON, source files, prose — before it reaches the LLM. Same answers, fraction of the tokens. Pure Python stdlib: no ML models, no downloads, no network calls, ever. Auditable end to end in ~1,600 lines.

[slimctx demo: 61,700 tokens compressed to 298 in 29ms, FATAL lines preserved, byte-exact retrieval]
Live output of python3 benchmarks/demo.py — run it yourself, nothing is staged.

python
from slimctx import Pipeline, Config
pipe = Pipeline(Config(target_tokens=32_000))result = pipe.compress(messages)        # OpenAI/Anthropic-style dictsprint(result.savings_ratio)             # e.g. 0.82
original = pipe.retrieve("a1b2c3d4...")  # byte-exact original, any time

Results (synthetic workloads modeled on real agent traffic)

WorkloadBeforeAfterSavingsKey facts kept
Code search (100 results)5,55791684%✓
SRE incident debugging61,699298100%✓
GitHub issue triage12,83697592%✓
Codebase exploration5,7342,76052%✓

Every run also verifies that each planted "needle" (the FIXME, the OOMKill, the outlier) survives compression, and that every lossy transform is byte-exact reversible. Reproduce with python3 benchmarks/bench.py.

How it works

messages ──► ContentRouter ──► one of:                ├─ JSON  : lossless tabularization (repeated keys → header,                │          constant columns → legend), then relevance-ranked                │          row selection only if still over budget                ├─ LOG   : Drain-style template mining — repeated lines                │          collapse to `pattern [x1432]`; errors verbatim                ├─ CODE  : AST skeleton — signatures + docstrings kept,                │          bodies elided EXCEPT those relevant to the query                └─ TEXT  : extractive sentence selection (BM25 + salience                           + position), verbatim, never paraphrased

The four guarantees

  1. Universal reversibility. Before any lossy transform, the original goes into a content-addressed store (memory / SQLite / bring-your-own cipher) and the output carries a [slimctx-ref <hash> ...] marker. The model — or you — can always get the byte-exact original back.
  2. Errors are never dropped. Every compressor pins error/warning content: log errors pass verbatim, salient JSON rows are kept, salient sentences outrank filler.
  3. Deterministic output. Same input → byte-identical output, across runs and processes. Compressed prefixes stay stable, so provider prompt-caches (Anthropic/OpenAI) keep hitting.
  4. Net gain or no-op. If a transform doesn't save enough tokens to pay for its marker, the original is kept untouched. The live zone (system prompt + last N messages) is never modified at all.

Why not just use Headroom?

Headroom is the established project in this space and is more featureful today (provider proxy with SSE streaming, agent wrappers, cross-agent memory, an ML compression model). slimctx makes a different set of trade-offs, aimed at locked-down / client-site deployments:

Headroomslimctx
ReversibilityJSON only (CCR); dropped text is goneevery lossy transform
Log handlinggeneric text scoringtemplate mining ([x1432] collapse)
Code handlingAST skeletonAST skeleton + query-relevant bodies kept
DependenciesRust core, ONNX runtime, 261MB HF modelstdlib only
Network egressHuggingFace pull on first runnone, ever
Store encryptionnone (plaintext SQLite)cipher hook (bring your own)
Determinismcache-aligner componentby construction (pure functions + memo)
Audit surface~10s of KLOC across 3 languages~1,200 lines of Python

If you need the proxy/wrap ecosystem, use Headroom. If you need something you can read in an afternoon, run air-gapped, and certify for a client environment, use slimctx.

Install / test

bash
pip install slimctx           # from PyPI — or vendor the slimctx/ directorypython -m pytest tests/ -q    # 26 tests: invariants, not examplespython3 benchmarks/bench.py   # reproduce the numbers above

Integration sketches

As a library (any framework): call pipe.compress(messages) right before your provider SDK call; expose pipe.retrieve as a tool named retrieve so the model can pull originals.

As an MCP server (GitHub Copilot, Claude Code, Cursor, ...): ships built in, stdlib-only:

bash
python3 -m slimctx.mcp_server --db ~/.slimctx/store.db

See USAGE.md for the GitHub Copilot (.vscode/mcp.json) setup and a security deployment checklist.

Encrypted store:

python
from cryptography.fernet import Fernet          # optional, your choicef = Fernet(key)store = SqliteStore("ccr.db", cipher=(f.encrypt, f.decrypt))pipe = Pipeline(store=store)

License

Apache-2.0. Original implementation — no code derived from Headroom.

来源:README.md,提交 f2cc938

工具

0
SourceWeft 尚未在沙箱中启动过该软件包,因此其工具未知。

版本历史

1
  1. v0.1.0最新Sep 16, 2026

评分

已满足适用检查项的 50%

该评分衡量此收录条目的完整程度,依据注册中心条目、端点与公开来源计算得出。它不是对服务器本身的评价,也不表示服务器安全或当前可用:可连通性是带日期的检查结果,评分也不包含安装量或用户评价。

  • 近 30 天内可连通25 分

    请保持端点能够响应 MCP 握手;SourceWeft 每 30 天探测一次。

  • 已声明认证方式10 分

    请在注册中心条目中声明凭据请求头,或保持端点能够响应探测,以便判定其认证方式。

  • 已列出工具15 分

    请让服务器在无凭据时响应 tools/list,或发布可在沙箱中无凭据启动的软件包。

  • 描述必需15 分
  • 图标10 分
  • 文档15 分
  • 许可证10 分