kamalmosadegh

TokenSlim MCP

Community kamalmosadegh
Updated

MCP server for TokenSlim — audit, compress, trim and compact LLM context with verifiable token savings

TokenSlim MCP

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

TokenSlim MCP wraps the TokenSlimlibrary as an MCP server. Any MCP client (Claude Code, Cursor, Codex, …)can ask it to audit a context, compress a prompt, trim a bulky tooloutput, or compact a history — and every tool returns the before/aftertoken counts, measured with TokenSlim's own counter, so the saving isverifiable 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 onlycounts 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 itsGitHub release for now; a PyPI distribution named tokenslim-py isplanned (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 themiddle, budget 200 tokens — actual output, measured with the chars/4estimator (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 aresult are always produced by the same counter for before and after, sothe ratio is meaningful either way. Set TOKENSLIM_NO_TIKTOKEN=1 toforce the estimator (offline / air-gapped).

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

Honest limits (v0.1.0)

  • Stateless: no caching across calls in this version — the library'sTokenCache is not exposed as a tool yet.
  • Compression is heuristic (LLMLingua-style), not model-grade; itprotects code-looking lines, numbers and caller-named terms, but it isstill a heuristic — the fact checks in the library's test suite arethe 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

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

MCP Server · Populars

MCP Server · New

    blen-labs

    Capture MCP

    AI-native Model Context Protocol (MCP) server that integrates SAM.gov, USASpending.gov, and Tango APIs to capture and analyze federal procurement and spending data through natural language queries. Responses include both human-readable text and structured JSON so MCP-compatible clients can consume the data programmatically.

    Community blen-labs
    PanStudio

    ZulipChat MCP Server

    ZulipChat MCP Server - A Model Context Protocol server that bridges AI agents with Zulip Chat. Built with FastMCP for lightning-fast performance, featuring comprehensive chat management and intelligent summarization. Transform your Zulip workspace into an AI-powered collaboration hub!

    Community PanStudio
    United-Compute

    GPU Price Tracker: open data

    This project tracks the market prices and performance metrics of various GPUs.

    Community United-Compute
    Graphify-Labs

    graphify

    Turn any codebase, with its docs, SQL schemas, configs, and PDFs, into a queryable knowledge graph. A /graphify skill for Claude Code, Cursor, Codex, and Gemini CLI: local deterministic AST parsing, every edge explained, no vector store.

    Community Graphify-Labs