Context-Optimizer-MCP

Context-Optimizer-MCP

An MCP server suite that optimizes prompt context by reducing tokens up to 98.8%, acting as persistent long-term memory and codebase scanner to save API costs.

Category
访问服务器

README

Context-Optimizer-MCP

Python FastAPI React MCP Tests License

Cuts AI prompt context by 90–99% and reduces API costs by $70–$140 per 1,000 queries.

A local-first Model Context Protocol (MCP) server suite that gives AI coding assistants persistent long-term memory and high-speed codebase discovery — eliminating context amnesia and token waste across every session.


The Problem

Modern AI coding assistants (Claude, Cursor, Copilot) suffer from two compounding problems:

  • Context amnesia — Every new session starts from zero. Architectural decisions, past mistakes, and established patterns must be re-explained each time.
  • Token waste — To answer a simple question, the AI blindly reads thousands of lines of source code, burning tokens on irrelevant logic before finding anything useful.

Context-Optimizer-MCP solves both.


Benchmark Results

Tested against this codebase (21 source files, 58,808 raw tokens) across 15 diverse query types — from narrow configuration lookups to broad architectural questions.

Metric Value
Minimum context reduction 97.4%
Median context reduction 99.81%
Maximum context reduction 99.9%

Run it yourself:

python benchmark.py

Architecture

┌─────────────────────────────────────────────┐
│         AI Agent (Claude / Cursor)          │
└───────────────┬─────────────────────────────┘
                │ MCP Protocol
    ┌───────────┴────────────┐
    │                        │
┌───▼──────────┐    ┌────────▼────────┐
│ Memory       │    │ Discovery       │
│ MCP Server   │    │ MCP Server      │
│              │    │                 │
│ Stores and   │    │ AST + Regex     │
│ retrieves    │    │ codebase scan   │
│ decisions,   │    │ → endpoints,    │
│ mistakes,    │    │   queries,      │
│ observations │    │   tech debt     │
└──────┬───────┘    └────────┬────────┘
       │                     │
       └──────────┬──────────┘
                  │
          ┌───────▼────────┐          ┌──────────────────┐
          │  SQLite DB     │◄────────►│  ai-memory.yaml  │
          │  mcp_memory.db │  cli.py  │  (Git-tracked)   │
          └───────┬────────┘          └──────────────────┘
                  │
          ┌───────▼────────┐
          │ FastAPI +       │
          │ React Dashboard │
          └────────────────┘

Core Components

Memory MCP Server

Persistent SQLite-backed memory for AI agents. Stores decisions, mistakes, and observations with full lifecycle management.

  • Semantic deduplication — Embeds incoming memories (Gemini text-embedding-004 / OpenAI text-embedding-3-small) and runs cosine similarity in RAM. Similarity ≥ 0.85 triggers a merge instead of a new insert, incrementing the existing memory's confidence score. Falls back to exact-string matching when no API key is present.
  • Staleness tracking — Classifies memories as fresh (<30 days), warming (30–90 days), or stale (>90 days) based on last validation timestamp. Auto-migrates older databases on startup.
  • Memory pruningmem_prune deletes unreinforced one-off entries (confidence == 1.0) older than N days, with dry-run mode on by default.

Discovery MCP Server

Scans codebases structurally using AST parsing and regex — extracts API endpoints, database queries, class/function maps, and # TODO debt markers without reading implementation logic line-by-line.

Produces a lightweight "blueprint" of the project that the AI can query in ~150 tokens instead of reading the full source.

Context Engine (FastAPI)

The search backend bridging agents and storage.

  • Dual-mode semantic search — Uses vector embeddings when API keys are present; falls back to TF-IDF + cosine similarity for fully offline, zero-setup retrieval.
  • Context compression — Retrieves, ranks, and compresses relevant memories and code structures before passing them to the LLM.

React Dashboard

Local UI for auditing the AI's memory state.

  • Trigger codebase scans manually
  • Search memories with Google-style queries
  • Verify/refresh warming and stale memory cards with a one-click checkmark (✓)
  • View AST blueprints of the current project structure

Key Design Decisions

Cross-agent portability. Memory is stored in open SQLite — no vendor lock-in. Switch from Claude to Gemini tomorrow; the new agent inherits the full project history instantly.

Git-friendly memory sync. The binary .db file is not committed directly. cli.py export converts it to a human-readable ai-memory.yaml. Teams commit the YAML, and cli.py import --merge rebuilds the database on each machine using the semantic dedup engine to resolve conflicts rather than overwriting.

Zero mandatory dependencies. No API key required to run. Semantic search degrades gracefully to TF-IDF offline mode. The whole system works on an air-gapped machine.


Installation

Requirements: Python 3.10+, Node.js (only needed if rebuilding the dashboard; precompiled build included)

# 1. Clone and install
git clone https://github.com/your-username/Context-Optimizer-MCP.git
cd Context-Optimizer-MCP
pip install -r requirements.txt

# 2. Configure environment (API keys optional)
cp .env.template .env
# Add GEMINI_API_KEY or OPENAI_API_KEY to enable semantic search
# Leave blank for offline TF-IDF mode

# 3. Import memory from Git history
python cli.py import

# 4. Start the dashboard
python context_engine/server.py
# Open http://127.0.0.1:8000

Connecting to AI Clients

Cursor

Settings → Cursor Settings → Features → MCP → + Add New MCP Server

Field Value
Name memory-server
Type command
Command python -u "C:/path/to/Context-Optimizer-MCP/mcp_servers/memory_server.py"

Repeat for discovery-server using discovery_server.py.

Claude Desktop

Add to claude_desktop_config.json:

{
  "mcpServers": {
    "codebase-memory": {
      "command": "python",
      "args": ["C:/path/to/Context-Optimizer-MCP/mcp_servers/memory_server.py"]
    },
    "codebase-discovery": {
      "command": "python",
      "args": ["C:/path/to/Context-Optimizer-MCP/mcp_servers/discovery_server.py"]
    }
  }
}

CLI Reference

# Export SQLite → YAML (for Git)
python cli.py export

# Rebuild SQLite from YAML
python cli.py import

# Merge YAML into existing DB (semantic dedup on conflicts)
python cli.py import --merge

# Preview stale memories eligible for pruning (dry run)
python cli.py prune --days 90 --confidence 1.0

# Execute pruning
python cli.py prune --days 90 --confidence 1.0 --execute

Tests

python -m unittest tests/test_memory_discovery.py

10 integration tests covering deduplication logic, YAML sync, staleness scoring, pruning, and benchmark validation. All passing.


Tech Stack

Python · FastAPI · SQLite · React · Vite · Model Context Protocol (MCP) · Google Gemini Embeddings · OpenAI Embeddings · TF-IDF / Cosine Similarity


License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选