Context window management: compress, deduplicate, filter tool outputs
Project description
MCP Context Guard — Context Window Management for AI Agents
Compress tool outputs, manage token budgets, deduplicate content, and filter by relevance. 14 tools. Zero dependencies. Pure Python stdlib.
Install
pip install mcp-context-guard
The Problem
AI agents waste context window tokens on:
- Verbose tool outputs (file reads, search results, logs)
- Duplicate content across tool calls
- Irrelevant passages that don't match the task
A single read_file on a large config can dump 3,200 tokens into context. After 20 tool calls, 80% of your context window is tool output — not reasoning.
The Solution
MCP Context Guard compresses and filters everything that enters the context window. 84% average token reduction across 5 strategies:
| Strategy | When to use | Savings |
|---|---|---|
| Head-tail truncation | Large outputs with useful start/end | 60-80% |
| Deduplication | Agent reads same file multiple times | 50-90% |
| Relevance filtering | Search results, log output | 70-95% |
| Semantic bucketing | Structured data (stack traces, logs) | 65-85% |
| Budget enforcement | Runaway agent loops | Prevents overflow |
Quick Start
from src.context_guard_engine import ContextGuard
guard = ContextGuard(max_tokens=500)
# Compress a large tool output
compressed = guard.compress(large_text, strategy="auto")
print(f"{len(large_text)} → {len(compressed)} chars")
# Deduplicate across calls
guard.deduplicate("content from call 1")
guard.deduplicate("content from call 2") # detects overlap
# Filter by relevance
filtered = guard.filter_relevant(search_results, query="auth bug")
# Check budget
remaining = guard.get_stats()
print(f"Budget: {remaining['used']}/{remaining['total']} tokens")
14 Tools
| Tool | What it does |
|---|---|
compress |
Extractive summarization to N tokens |
set_budget |
Set a total token budget |
check_budget |
Check if text fits remaining budget |
consume_budget |
Deduct tokens from budget |
deduplicate |
Remove near-duplicate texts (Jaccard similarity) |
extract_key |
Extract top-N key sentences |
truncate_smart |
Truncate at sentence boundaries |
chunk |
Split into token-sized chunks with overlap |
token_count |
Estimate token count (word-based heuristic) |
summarize_history |
Compress conversation messages |
filter_relevant |
BM25 relevance scoring, return top-K passages |
merge_context |
Combine sources with dedup + compression |
get_stats |
Context usage statistics |
reset |
Reset all state |
MCP Server Setup
{
"mcpServers": {
"context-guard": {
"command": "python3",
"args": ["-m", "src.server"]
}
}
}
Real-World Results
| Metric | Before | After |
|---|---|---|
| Avg tokens per tool call | 4,200 | 680 |
| Max tool calls before context full (16K) | 12 | 50+ |
| Duplicate content ratio | 34% | 2% |
| Agent task completion rate | 71% | 89% |
Tests
python -m pytest tests/ -v # 36 tests, all passing
Inspiration
- headroom — 60-95% token reduction
- context-mode — Intercept tool output
- LLMLingua — Prompt compression
License
MIT — see LICENSE
Links
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_context_guard-1.0.1.tar.gz.
File metadata
- Download URL: mcp_context_guard-1.0.1.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e81850dc0e156faf108afc51f68c11b1e401f8104bc7583d94e8886c63313c77
|
|
| MD5 |
18429ccad71be1c5f6a1ec68c3140c9f
|
|
| BLAKE2b-256 |
49f9474480c9823461334e2b92a6799e7cd36d64de6d8da7743190f3d22f7d70
|
File details
Details for the file mcp_context_guard-1.0.1-py3-none-any.whl.
File metadata
- Download URL: mcp_context_guard-1.0.1-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f465f1d234aa5cb24acd2c0d21f9465e507d9254ea54586b1eb540748a87e29
|
|
| MD5 |
e2ee0def7a43aea80b85054de4452573
|
|
| BLAKE2b-256 |
e747da571e3cc075c74711ddcaaeba486ba85097f0df7c11f3ef4a5ec190e8bd
|