Skip to main content

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.

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.

M8ven Score

kamalmosadegh/tokenslim-mcp mcp server

mcp-name: io.github.kamalmosadegh/tokenslim-mcp

Metadata

Release files for tokenslim-mcp 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokenslim-mcp 0.1.0
File Size Uploaded Provenance Actions
tokenslim_mcp-0.1.0.tar.gz 12.0 kB With provenance Details for tokenslim_mcp-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tokenslim-mcp 0.1.0
File Interpreter ABI Platform Actions
tokenslim_mcp-0.1.0-py3-none-any.whl Python 3 none any Details for tokenslim_mcp-0.1.0-py3-none-any.whl

Total release size: 20.7 kB

Release files / tokenslim_mcp-0.1.0.tar.gz

Download URL tokenslim_mcp-0.1.0.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5103295c43f6ee07a47425ff416fd988bb14c1b1f6d873b94bb3fe065ed5720d
BLAKE2b-256 checksum
How to use checksums
55d0f82a739a557add7c2250695918d9131db98d2210bfb742eabc7d5c7de31f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / tokenslim_mcp-0.1.0-py3-none-any.whl

Download URL tokenslim_mcp-0.1.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
691182918a6e28623e7755c465f1a8be92de8fcc650ae6a70fdb2bb51ae0e63e
BLAKE2b-256 checksum
How to use checksums
5e587e21213f2cfb3746bdc218a010a8cff7e83ee38b517388dccc1afc4e05b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for tokenslim-mcp 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tokenslim_mcp-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page