模型上下文引擎
令牌感知透明代理,消除了AI代理的上下文窗口膨胀。
  ](https://github.com/DexopT/MCE/releases)
______________________________________________________________________
问题
AI代理(Claude Code、Cursor、Windsurf)浪费 40-80%的上下文窗口 关于臃肿的工具响应——原始HTML、base64 blob、空字段、截断数组。每一个被浪费的代币都会让你付出代价,减缓推理速度,并将重要的上下文推到窗外。
解决方案
MCE作为一个 透明反向代理 在您的AI代理和MCP工具服务器之间。它拦截每个工具响应,评估其令牌成本,并应用一个3层压缩管道——在标准硬件上几乎没有延迟。
┌──────────┐ JSON-RPC ┌──────────┐ JSON-RPC ┌──────────────┐
│ AI Agent │ ───────────────→ │ MCE │ ───────────────→ │ MCP Server │
│ │ ←─── minified ── │ Proxy │ ←─── raw ─────── │ (Tool) │
└──────────┘ └──────────┘ └──────────────┘
│
┌───────┴───────┐
│ Squeeze Engine │
│ L1: Pruner │
│ L2: Semantic │
│ L3: Synth. │
└───────────────┘MCE做什么
- 🧹 李废弃物 --strips HTML、base64 blob、空值、过多空白
- 🧠 语义过滤 --通过CPU友好的RAG仅提取相关块
- 📝 可选LLM摘要 --到达局部模型(Ollama)进行最终压缩的路线
- 💾 语义缓存 --重复请求的零令牌即时响应
- 🔒 策略引擎 --阻止破坏性命令(
rm -rf,DROP TABLE) - 🔄 断路器 --检测无限的工具调用循环
- 📊 实时仪表板 --实时TUI显示令牌节省和缓存统计信息
______________________________________________________________________
快速开始
1.安装
git clone https://github.com/DexopT/MCE.git
cd MCE/mce-core
pip install -r requirements.txt2.配置
编辑 config.yaml --将MCE指向您的实际MCP服务器:
upstream_servers:
- name: "filesystem"
url: "http://localhost:3001"
token_limits:
safe_limit: 1000 # pass through if under
squeeze_trigger: 2000 # compress if over
absolute_max: 8000 # hard cap3.跑步
python main.py # start the proxy
python main.py --dashboard # start with live TUI dashboard4.连接您的代理
将AI代理的MCP配置指向 http://127.0.0.1:3025 而不是直接的工具服务器URL。MCE透明地代理所有内容。
______________________________________________________________________
建筑
| 组件 | 文件 | 目的 |
|---|---|---|
| 代理服务器 | core/proxy_server.py | FastAPI JSON-RPC反向代理 |
| MCP客户端 | core/mcp_client.py | 将调用转发到真实的工具服务器 |
| 代币经济学家 | engine/token_economist.py | 通过tiktoken设置预算护栏 |
| 策略引擎 | engine/policy_engine.py | 破坏性命令拦截器+HitL |
| 断路器 | engine/circuit_breaker.py | 无限环路检测器 |
| 懒惰的注册员 | engine/lazy_registrar.py | 即时模式注入 |
| L1修剪机 | engine/squeeze/layer1_pruner.py | HTML→MD、空条带、base64删除 |
| L2语义 | engine/squeeze/layer2_semantic.py | CPU友好的RAG过滤 |
| L3合成器 | engine/squeeze/layer3_synthesizer.py | 可选本地LLM摘要(Ollama) |
| 语义缓存 | models/semantic_cache.py | LRU+TTL响应缓存 |
| 上下文管理器 | core/context_manager.py | 会话令牌跟踪 |
| TUI仪表板 | tui/dashboard.py | 实时富终端仪表板 |
挤压发动机管路
Raw Response (e.g., 12,000 tokens)
│
▼
┌─── Layer 1: Pruner ────────────────┐
│ HTML → Markdown │
│ Strip base64 blobs │
│ Remove null values │
│ Truncate arrays (50 items max) │
│ Normalize whitespace │
└────────────────────────────────────┘
│ ~4,000 tokens
▼
┌─── Layer 2: Semantic Router ───────┐
│ Chunk text (500 tokens each) │
│ Embed chunks + agent query │
│ Cosine similarity search │
│ Return top-5 relevant chunks │
└────────────────────────────────────┘
│ ~1,500 tokens
▼
┌─── Layer 3: Synthesizer (opt.) ────┐
│ Send to Ollama (Qwen 2.5 3B) │
│ Generate 300-token summary │
│ Graceful fallback if unavailable │
└────────────────────────────────────┘
│ ~300 tokens (97.5% reduction)
▼
Minified Response → Agent______________________________________________________________________
配置参考
Full config.yaml reference
proxy:
host: "127.0.0.1"
port: 3025
token_limits:
safe_limit: 1000 # pass through if under
squeeze_trigger: 2000 # route to squeeze engine
absolute_max: 8000 # hard cap after squeeze
squeeze:
layer1_pruner: true # deterministic pruning
layer2_semantic: true # semantic RAG filtering
layer3_synthesizer: false # requires Ollama
cache:
enabled: true
max_entries: 512
ttl_seconds: 600
upstream_servers:
- name: "filesystem"
url: "http://localhost:3001"
policy:
blocked_commands:
- "rm -rf"
- "mkfs"
- "FORMAT"
blocked_network:
- "0.0.0.0"
- "169.254."
hitl_commands:
- "DROP"
- "TRUNCATE"
- "git push --force"
circuit_breaker:
window_size: 5
failure_threshold: 3
synthesizer:
model: "qwen2.5:3b"
ollama_url: "http://localhost:11434"
max_summary_tokens: 300
embeddings:
model_name: "all-MiniLM-L6-v2"
logging:
level: "INFO"
show_tokens: true______________________________________________________________________
运行测试
cd mce-core
python -m pytest tests/ -v______________________________________________________________________
技术栈
- Python 3.11+ --异步运行时
- FastAPI+Uvicorn --高性能异步代理
- 令牌。 --OpenAI令牌化器,用于精确的令牌计数
- 句子变换器 --CPU友好型嵌入(全MiniLM-L6-v2)
- 数值Python --内存向量存储(无FAISS依赖性)
- httpx --用于上游通信的异步HTTP客户端
- 富有的 --漂亮的终端日志和TUI仪表板
- Pydantic v2 --类型安全配置和模式验证
______________________________________________________________________
贡献
看 贡献.md 作为指导方针。
安全
看 安全.md 用于报告漏洞。
许可证
此项目根据MIT许可证获得许可——请参阅 许可证 了解详情。
支持和资金
如果MCE为您节省了代币并提高了代理的性能,请考虑支持开发:
- 索拉纳 :
0x03034e1d4ec0e8bf830bdb576b36aece22bcf3a7
______________________________________________________________________
建造于 蛇 黑树
*如果MCE保存了您的代币,请给它一个⭐*
