Skip to main content
Glama

TokenSlim MCP

Cut your agent's token usage from inside the agent — and prove it on every call.

TokenSlim MCP wraps the TokenSlim library as an MCP server. Any MCP client (Claude Code, Cursor, Codex, …) can ask it to audit a context, compress a prompt, trim a bulky tool output, or compact a history — and every tool returns the before/after token counts, measured with TokenSlim's own counter, so the saving is verifiable from the result itself, not a claim you have to trust.

The library underneath is zero-dependency, deterministic and model-free: no compressor model, no API keys, no network calls. Compression only counts if the facts survive — that is the whole point of the project.

The four tools

Tool

What it does

Returns

tokenslim_audit

Profile an OpenAI-style message list: where the tokens go

Total + by-role counts, tool-result / tool-schema share, largest messages, whitespace waste, duplicate tool outputs, and concrete cut suggestions

tokenslim_compress

Compress a prompt to a target rate (LLMLingua-style heuristics), protecting caller-named terms

Compressed text, before/after counts, per-term kept check

tokenslim_trim

Shrink one tool output / log to a token budget: head + tail + highest-signal lines

Trimmed text, before/after counts, ERROR-line count before vs after (a buried ERROR line survives the cut)

tokenslim_compact

Compact a message history to a budget: old tool results elided, old turns summarized, recent tail verbatim

Compacted messages, before/after counts, tokens saved, within-budget flag

All four tools are read-only and deterministic: same input, same output, same counts.

Related MCP server: contextlens

Install

pip install tokenslim-mcp        # once published; see below for source install

From source (today):

git clone https://github.com/kamalmosadegh/tokenslim-mcp
cd tokenslim-mcp
pip install .

The server depends on the TokenSlim library, which pip pulls from its GitHub release for now; a PyPI distribution named tokenslim-py is planned (the name "tokenslim" on PyPI belongs to an unrelated project). The import name is tokenslim either way.

Run

tokenslim-mcp                    # the installed command (stdio)
python -m tokenslim_mcp.server   # same thing, module form
python -m tokenslim_mcp          # same thing, package form

MCP client config

Claude Code:

claude mcp add tokenslim -- tokenslim-mcp

…or, with uvx (no prior install):

{ "mcpServers": { "tokenslim": {
  "command": "uvx",
  "args": ["tokenslim-mcp"]
} } }

Cursor / Claude Desktop / other clients (installed command):

{ "mcpServers": { "tokenslim": {
  "command": "tokenslim-mcp",
  "args": []
} } }

From a source checkout (no install):

{ "mcpServers": { "tokenslim": {
  "command": "python",
  "args": ["-m", "tokenslim_mcp.server"],
  "cwd": "/path/to/tokenslim-mcp"
} } }

Example result

tokenslim_trim on a 402-line log with one ERROR line buried in the middle, budget 200 tokens — actual output, measured with the chars/4 estimator (this exact case is asserted in the test suite):

{
  "ok": true,
  "tokens_before": 3297, "tokens_after": 196, "tokens_saved": 3101,
  "saved_pct": 94.1,
  "max_tokens": 200,
  "error_lines_before": 1, "error_lines_after": 1,
  "trimmed_text": "INFO boot sequence start\n…\nERROR payment failed trace_id=tx-9911 amount=4250\n…"
}

Honest numbers

Token counting uses TokenSlim's counter: exact tiktoken (cl100k_base) when it is installed, otherwise a chars/4 estimator — the counts in a result are always produced by the same counter for before and after, so the ratio is meaningful either way. Set TOKENSLIM_NO_TIKTOKEN=1 to force the estimator (offline / air-gapped).

Savings depend on what your context is made of. In the library's own realistic coding-agent benchmark, the full pipeline (cache + trim + compress + compact) cut a 2,150-token transcript to 592 (−72.5%, exact tiktoken); the ~98% figures on pathological inputs are mostly history compaction — deletion, not compression — and are labelled as such in the library README. These four tools expose the individual steps; your mileage depends on how much of your context is tool output.

Honest limits (v0.1.0)

  • Stateless: no caching across calls in this version — the library's TokenCache is not exposed as a tool yet.

  • Compression is heuristic (LLMLingua-style), not model-grade; it protects code-looking lines, numbers and caller-named terms, but it is still a heuristic — the fact checks in the library's test suite are the evidence, not a guarantee for arbitrary text.

  • Compaction summarizes old turns extractively by default (no LLM call); quality is bounded by that choice, and the budget is always honoured.

Tests

pip install -e .[dev]   # or: pip install pytest
python -m pytest -q     # direct tool tests + a real MCP protocol test
                        # (stdio client: initialize, list_tools, live call)

License

MIT — see LICENSE. The TokenSlim library is MIT as well.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      D
      maintenance
      Local MCP server for token optimization, providing tools to compress code/JSON, optimize prompts, and manage placeholder-based content redaction and hydration to reduce LLM token usage.
      5
      ISC
    • A
      license
      Not graded
      quality
      C
      maintenance
      MCP server that minimizes LLM token usage by compressing, summarizing, filtering, chunk-referencing, and pruning large context before it reaches the model, with heuristic or local-SLM smart actions, caching, and token counting.
      26 npm
      MIT