> Correct it once. It remembers forever.4-layer persistent memory for AI agents via MCP.
Every AI conversation starts from scratch — until now. Memory Engine gives your agent persistent memory across sessions, organized in four specialized layers:
| Traditional AI | With Memory Engine |
|---|---|
| Resets every conversation | Remembers across sessions |
| Makes the same mistakes repeatedly | Learns from corrections automatically |
| You re-explain context every time | Auto-retrieves relevant memories |
| Zero domain knowledge accumulation | Continuously learns your rules and preferences |
graph TD
subgraph Agent["🤖 AI Agent"]
A1[Hermes / Claude / Custom] --> MCP["🔌 MCP Server<br/>22 Tools"]
end
subgraph Layers["Memory Engine"]
L1["📚 Layer 1: Memory Tree<br/>Vector search (FAISS)<br/>Hierarchical summaries"]
L2["⚙️ Layer 2: Preferences<br/>Field mappings, date rules<br/>Learned from corrections"]
L3["🔧 Layer 3: Error Memory<br/>Remember past mistakes<br/>Auto-upgrade after 3x"]
L4["🗺️ Layer 4: Knowledge Graph<br/>Entity relationships<br/>3-tier permissions"]
end
MCP --> L1 & L2 & L3 & L4
L1 & L2 & L3 & L4 --> DB[("🗄️ SQLite")]
L1 --> FAISS[("📊 FAISS Index<br/>384-dim vectors")]
| Layer | Name | Function | Inspired By |
|---|---|---|---|
| L1 | Memory Tree | External data ingestion + vector search + hierarchical summaries | OpenHuman |
| L2 | Preferences | Learns rules and habits from user corrections | Mem0 |
| L3 | Error Memory | Remembers mistakes; auto-upgrades to rules after 3 occurrences | 原创 (Original) |
| L4 | Knowledge Graph | Entity/relationship management with 3-tier permissions | Zep |
# 1. Clone & install
git clone https://github.com/qq1009128320-dotcom/memory-engine.git
cd memory-engine
python3 -m venv venv && source venv/bin/activate
pip install -r requirements.txt
# 2. Initialize database
python3 -c "from memory_server import _init_db; _init_db()"
# 3. Start MCP Server (Ctrl+C to exit)
python3 memory_server.py- 4 specialized memory layers — Tree, Preferences, Error Memory, Knowledge Graph
- 22 MCP tools — Full surface accessible via MCP protocol
- Hybrid search — FAISS vector search (384-dim, <3ms hot) + keyword fallback
- Auto-learning — Errors logged; ≥3 occurrences auto-upgrade to hard rules
- Auto-fact extraction — Extracts entities, preferences, and corrections from conversations
- Hierarchical summaries — L0 (global) → L1 (grouped topics) → L2 (raw blocks)
- Health check & audit — 30-point comprehensive audit built-in
- Deployment options — Lightweight (FAISS+SQLite) / Heavy (Milvus+PostgreSQL+Redis)
- Third-party sync — Feishu/Lark, local files, database tables
- Docker + systemd — Production-ready with OOM protection
| Operation | Cold Start | Hot Query |
|---|---|---|
| Vector Search (FAISS) | ~500ms (model load) | ~3ms |
| Keyword Search | — | ~0.1ms |
| Cross-layer Search | — | ~0.1ms |
memory_tree_ingest— Ingest data (auto-embeds via FAISS)memory_tree_vector_search— Semantic vector search (recommended)memory_tree_search— Keyword search (fallback)memory_tree_fetch— Get full content by IDmemory_tree_score— Adjust memory relevance scorememory_tree_reindex— Rebuild FAISS indexmemory_tree_summary— L0/L1/L2 hierarchical summaries
preference_add/preference_search/preference_list/preference_disable
error_check— Check before execution: has this task failed before?error_log— Log an error + user correctionerror_list— List unresolved errors
entity_add/entity_search/entity_link/graph_query
memory_search— Search all 4 layers at oncememory_stats— Memory statistics overviewmemory_health— Health check + operational metrics
python3 run_extraction.py --text "User: Check Tencent's R&D expenses
Agent: Found it. R&D spending: 2.83 billion yuan.
User: Use the amt_jpy field, not base_amt.
Agent: Got it, I've noted that."Automatically extracts: field aliases → preference rules | corrections → error memory | entities → knowledge graph.
| Variable | Description | Default |
|---|---|---|
DEEPSEEK_API_KEY |
DeepSeek API key (for extraction/summary) | (optional) |
MEMORY_DB_PATH |
SQLite database path | ./memory.db |
FAISS_INDEX_PATH |
FAISS index path | ./faiss.index |
EMBEDDING_MODEL |
Embedding model name | all-MiniLM-L6-v2 |
LLM_MODEL |
LLM model for extraction | deepseek-chat |
LLM_TIMEOUT |
LLM request timeout (seconds) | 30 |
| Option | Specs | Best For |
|---|---|---|
| Lightweight (FAISS+SQLite) | 2 vCPU, 2GB RAM | Personal / small team |
| Heavy (Milvus+PG+Redis) | 8 vCPU, 16GB RAM | Enterprise / 10M+ vectors |
chmod +x deploy.sh
./deploy.shAutomates: Python check → venv creation → deps install → DB init → FAISS rebuild → verification.
- ✅ FAISS concurrent write lock (thread-safe)
- ✅ Request rate limiting (BoundedSemaphore 50)
- ✅ Log rotation + redaction (API keys auto-filtered)
- ✅ Config validation on startup
- ✅ Input sanitization (NULL bytes, control chars)
- ✅ PID file lock (prevents duplicate process spawn)
- ✅ WAL auto-checkpoint (every 5 min)
- ✅ Daily backup script (integrity_check → gzip → 30-day retention)
- ✅ OOM protection (systemd MemoryMax)
- ✅ Docker multi-stage build (non-root user)
Add to config.yaml:
mcp_servers:
enterprise-memory:
command: /path/to/venv/bin/python3
args: ["/path/to/memory_server.py"]Or connect to existing HTTP server (port 8765):
mcp_servers:
enterprise-memory:
url: "http://127.0.0.1:8765/mcp"Memory Engine speaks standard MCP protocol. Connect any MCP-compatible agent.
├── memory_server.py # MCP Server (22 tools)
├── schema.sql # Database schema (6 tables)
├── run_extraction.py # End-to-end fact extraction
├── extract_facts.py # LLM prompt templates
├── summary_tree.py # Hierarchical summary generator
├── auto_fetch.py # Feishu auto-sync
├── observability.py # Metrics + observability
├── validators.py # Input validation
├── config.py # Unified configuration
├── log_utils.py # Logging utilities
├── audit.py # 30-point comprehensive audit
├── deploy.sh # One-click deployment
├── Dockerfile # Docker image
├── docker-compose.yml # Docker Compose
└── memory-engine.service # systemd unit
| Feature | Memory Engine | agentmemory | Mem0 | Zep | Letta |
|---|---|---|---|---|---|
| Memory layers | 4 | 1 | 2 | 2 | 1 |
| Error auto-learning | ✅ | ❌ | ❌ | ❌ | ❌ |
| FAISS vector search | ✅ | ❌ (custom) | ✅ | ✅ | ❌ |
| 0 external DBs | ✅ | ✅ | ❌ (cloud) | ❌ (cloud) | ❌ (cloud) |
| MCP protocol | ✅ | ✅ | ❌ | ❌ | ❌ |
| Self-hosted | ✅ | ✅ | ❌ | ✅ (limited) | ❌ |
| Heavy deployment | ✅ | ❌ | ❌ | ❌ | ❌ |
| Production audit | ✅ | ❌ | ❌ | ❌ | ❌ |
| Feishu/Lark sync | ✅ | ❌ | ❌ | ❌ | ❌ |
| Open Source | ✅ MIT | ✅ Apache 2.0 | ✅ | ✅ | ✅ |
- v2.0 — FAISS migration, 22 MCP tools, production hardening
- v2.1 — CI/CD, Docker, comprehensive audit, embedding timeout protection
- v2.2 — Cross-layer search, error auto-upgrade, 84 tests passing
- v2.3 — English documentation, GitHub Pages site, benchmark suite
- v2.4 — Multi-agent memory coordination (ShadowClone-X integration)
- v3.0 — Milvus production deploy, horizontal scaling, enterprise SSO
MIT
四层 Agent 记忆系统。让 Agent 越用越聪明,越用越懂你。
传统 AI:每次从头开始,能力不变
记忆引擎:每次被纠正就更聪明一点
git clone https://github.com/qq1009128320-dotcom/memory-engine.git
cd memory-engine
python3 -m venv venv && source venv/bin/activate
pip install -r requirements.txt
python3 -c "from memory_server import _init_db; _init_db()"
python3 memory_server.py| 层 | 名称 | 功能 | 借鉴 |
|---|---|---|---|
| L1 | Memory Tree | 外部数据感知,向量检索 + 层级摘要 | OpenHuman |
| L2 | 偏好记忆 | 从纠正中自动学习规则和习惯 | Mem0 |
| L3 | 纠错记忆 | 记住错误,≥3 次自动升级为永久规则 | 独创 |
| L4 | 知识图谱 | 实体关系 + 三级权限 | Zep |
详见中文版 README 的完整说明(本文件上半部分为英文、下半部分为中文)。
