Leanwall
A local-first firewall and token-compression proxy for MCP. Put it in front of every MCP server you use, and your agent sees three small meta-tools instead of dozens of tool schemas. Oversized results get paged instead of dumped, and secrets get redacted before they leave your machine. Poisoned or silently changed tools are quarantined, and every write waits for a human click.
pip install "leanwall[dense]"
leanwall demo # 6-second scripted session against bundled mock servers
leanwall wrap --from claude # move your Claude Desktop MCP servers behind Leanwall (backed up, reversible)
Why
Connecting several MCP servers costs you on every turn:
- Schema bloat. Every tool schema is injected into every prompt, used or not.
- Output bloat. A single
get_logsorquery_dbcall can dump 15k+ tokens of raw JSON, sometimes with API keys or customer PII inside. - Tool poisoning and rug pulls. A third-party server can hide instructions in a tool description, or change it after you installed it.
- Unchecked writes. Read and write tools share one connection, so a confused or prompt-injected agent can merge PRs, drop tables or post to Slack. A network retry can do it twice.
Measured results
From leanwall benchmark (49 tools across 4 bundled mock servers, run as real stdio subprocesses; token counts are
tiktoken estimates). The full report is in BENCHMARK.md.
| Metric | Direct MCP | Leanwall |
|---|---|---|
| Schema tokens per turn | 4,644 | 787 (−83.0%) |
| Tokens for 4 oversized results | 42,942 | 4,394 (−89.8%) |
| Tool-routing Recall@3 (BM25 / dense / hybrid) | – | 84% / 86% / 90% |
| Adversarial suite (20 attacks) | – | 0 bypasses |
| Duplicate writes on 4 identical retries | – | 0 |
discover_tools p95, cold / cached |
– | 17 ms / 1.4 ms |
The mock schemas are terse (~95 tokens per tool). Real servers are often several times larger, so schema savings on a real setup are usually higher.
How it works
Claude / Cursor ──stdio──▶ Leanwall ──stdio/HTTP──▶ your MCP servers
│
├─ discover_tools(intent) hybrid BM25 + bge-small embeddings, RRF fusion
├─ execute_tool(server, tool, args)
│ ├─ quarantine check leanwall.lock (SHA-256 per schema) + poisoning scanner
│ ├─ policy gate policies.yaml: read_only | write_restricted | blocked
│ ├─ human approval dashboard on 127.0.0.1:8787 or `leanwall approve`
│ ├─ idempotency identical approved writes run once
│ └─ response guard secret/PII redaction, then slicing over 1,500 tokens
└─ slice_result(handle_id, json_path, offset, limit)
Every security decision is deterministic Python outside the model's context. The model holds no credentials and has no tool that can approve anything.
Write approvals
agent → execute_tool(db, drop_table, {table: orders})
Leanwall→ APPROVAL_REQUIRED request_id=req_ac25… approve at http://127.0.0.1:8787/approvals/req_ac25…
human → clicks Approve (or: leanwall approve req_ac25…)
agent → execute_tool(db, drop_table, {table: orders}) → runs once
retry → same call again → original result replayed, nothing re-executed
Each approval is bound by HMAC to the exact tool and arguments. It expires, and it can be spent only once. The dashboard accepts approvals only from itself: it uses a CSRF token, Origin and Host checks, and binds to localhost only.
Commands
| Command | What it does |
|---|---|
leanwall wrap --from claude|cursor|claude-code|<path> |
Back up the client config, move its servers into leanwall.yaml, pin leanwall.lock, generate policies.yaml, print the savings |
leanwall unwrap --from … |
Restore the original client config |
leanwall serve [--transport stdio|http] |
Run the proxy (and the dashboard, if port 8787 is free) |
leanwall lock [--accept server/tool] |
Re-pin schemas; changed tools stay quarantined until you --accept them |
leanwall scan |
Check live tools against the lockfile and the poisoning scanner |
leanwall pending / approve <id> / deny <id> |
Handle write approvals from the terminal |
leanwall dashboard |
Savings and approvals UI on 127.0.0.1:8787 |
leanwall stats |
Token savings, redactions and gate decisions |
leanwall benchmark |
Regenerate BENCHMARK.md |
Configuration
wrap writes these to ~/.leanwall/ (or the folder you pass to --config):
# leanwall.yaml
servers:
github: {command: npx, args: ["-y", "@modelcontextprotocol/server-github"], env: {GITHUB_TOKEN: "…"}}
internal: {url: "https://mcp.internal.example.com/mcp", headers: {Authorization: "Bearer …"}}
settings:
top_k: 3
slice_threshold_tokens: 1500
dense: auto # auto | on | off
cache_url: null # redis://localhost:6379 to share the discovery cache
redaction:
pii: true
allowlist: ["ops@mycompany\\.com"]
# policies.yaml — generated from tool names and MCP annotations; review the "# review" lines
default: blocked
tools:
github/list_issues: read_only
github/github_merge_pr: write_restricted
db/query_db: read_only # review: may write
Honest trade-offs
- An extra round-trip. The agent calls
discover_toolsbefore acting. With 3 tools and one turn, direct MCP is faster. With 30+ tools over a multi-turn session, the saved prefill outweighs it. - Redaction false positives. Regex and entropy scanning can redact something harmless. Use
redaction.allowlist, or setpii: false. - Routing misses. Hybrid search gets 90% Recall@3 on our 50-intent eval. The agent can raise
top_kor rephrase, and purely semantic phrasings ("what's paging on-call?") are the hardest. - Stored results are short-lived. Oversized results live in Leanwall's memory for 1 hour and are lost when the
client restarts Leanwall. A later
slice_resultgets a clear error telling the agent to re-run the original call. (They are already redacted, but are kept off disk on purpose.) - Startup takes a few seconds. Leanwall connects every downstream server before it answers, which takes about 6-10s, longer on a cold start. A tool call sent during that window can fail.
- Approximate token counts. Numbers come from OpenAI's
cl100k_basetokenizer as a stand-in for Claude's. - Legacy SSE servers are not proxied yet.
wrapleaves them in your client config untouched.
Development
uv sync --extra dense
uv run pytest # 76 tests, including end-to-end over real stdio subprocesses
uv run ruff check src tests
MIT licensed. See CONTRIBUTING.md.
Metadata
Release files for leanwall 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| leanwall-0.1.0.tar.gz | 214.9 kB | With provenance | Details for leanwall-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| leanwall-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for leanwall-0.1.0-py3-none-any.whl |
Total release size: 282.8 kB
Release files / leanwall-0.1.0.tar.gz
| Download URL | leanwall-0.1.0.tar.gz |
|---|---|
| Size | 214.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c5ff1c1f72423730a7dfaecfaeb1eb771079828dd73367b80488935730a7c1a6
|
|
BLAKE2b-256 checksum How to use checksums |
347ed74cdac5ff73d087ed54541034fc3937c813316deeaaf73826d93d940178
|
| 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 27, 2026.
Transparency logRelease files / leanwall-0.1.0-py3-none-any.whl
| Download URL | leanwall-0.1.0-py3-none-any.whl |
|---|---|
| Size | 67.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
af6f2c5d35ae96c8cecc82ec300ba8553d134d8d55cf4f862d13977d62bf4c5b
|
|
BLAKE2b-256 checksum How to use checksums |
d6fac8ade09c547b12f5919a261aa1330cfea24161633538b7f787234bbd8163
|
| 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 27, 2026.
Transparency logSecurity for leanwall 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.
Attestation for leanwall-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 27, 2026.
Transparency logA 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.