Personal Memory MCP Server
# Personal Memory Engine
[简体中文](README_CN.md)
[](https://github.com/gaoziyang111/personal-memory-engine/actions/workflows/ci.yml)
**A model-agnostic, local-first long-term memory engine for AI agents, powered by MCP.**
**Give Your AI Agents a Persistent Memory.**
Personal Memory Engine stores durable user context in local SQLite, exposes it through an MCP stdio server, maintains a human-readable Markdown projection, and uses an optional OpenAI-compatible model only for language-understanding tasks such as memory extraction.
The project is designed around four principles:
- **Local ownership** — SQLite and Markdown remain on the user's machine.
- **Evidence first** — extracted memories require explicit user evidence.
- **Candidate review** — uncertain or inferred information never writes directly to confirmed memory.
- **Provider independence** — DeepSeek, OpenAI, or another compatible endpoint can be configured; storage and retrieval are provider-agnostic.
## Contents
- [What problem it solves](#what-problem-it-solves)
- [Core scenario](#core-scenario)
- [Core features](#core-features)
- [Demo](#demo)
- [Architecture](#architecture)
- [Installation](#installation)
- [Configuration](#configuration)
- [Codex MCP integration](#codex-mcp-integration)
- [Memory model](#memory-model)
- [MCP tools](#mcp-tools)
- [Testing](#testing)
- [Privacy and security](#privacy-and-security)
- [Current limitations](#current-limitations)
- [Roadmap](#roadmap)
- [License](#license)
## What problem it solves
AI agents lose useful context between sessions. Repeating background, preferences, goals, project decisions and learning progress wastes time and creates inconsistent answers. Personal Memory Engine provides a local, auditable memory layer that retrieves only task-relevant context and keeps uncertain extracted information under explicit review.
## Current integrations
- **Codex** — MCP stdio integration is implemented and tested end to end.
- **DeepSeek API** — supported through the generic OpenAI-compatible chat adapter; no real external API key is included in the repository.
- **MCP-compatible agents** — the server uses standard MCP stdio tools, but clients other than Codex have not yet been fully validated.
## Core scenario
The core cross-session workflow is:
1. In session A, Codex calls `save_memory` with durable user information.
2. The MCP server validates the write and stores it in local SQLite.
3. In a new session B, Codex calls `build_memory_context` or `search_memories` with the current task.
4. The retriever returns only relevant top-K memories, including provenance and confidence.
5. Uncertain extracted information remains a pending candidate until the user confirms it.
## Core features
Current version: **0.1.0**
Implemented and tested:
- SQLite with WAL, transactions, FTS5 keyword search, and history/audit records.
- Create, read, update, soft delete, protected hard delete, tags, categories, and provenance.
- Automatic memory candidate extraction with deterministic quality gating.
- User fact / preference / plan / state / event / model-inference separation.
- Long-term, current, transient, and unknown stability classes.
- Same-fact paraphrase detection and local canonicalization.
- Goal replacement with supersession and historical snapshots.
- Current learning-progress handling without polluting profile memory.
- Optional OpenAI-compatible semantic embeddings and hybrid retrieval.
- MCP stdio server with 15 tools.
- Backup, restore, JSON export, candidate review, and audit history.
The checked-in test suite currently contains **36 passing tests** on Windows and WSL Ubuntu with Python 3.12. Real DeepSeek inference has not been exercised in this repository because no API key is included.
## Demo
Actual terminal demonstration:
```powershell
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\smoke-mcp.ps1
```
The run saves a synthetic memory through one MCP server process, restarts the server, retrieves the memory from a new session, and verifies pending extraction state.
- [Terminal demo transcript](docs/demo/terminal-demo.txt)
- [Demo and GIF recording guide](docs/demo/README.md)
No fabricated screenshot or GIF is included. A `docs/demo/demo.gif` asset can be added after recording the verified terminal flow.
## Architecture
```text
Codex / MCP client
|
| stdio
v
Personal Memory MCP Server
|
+-- Memory Manager
| +-- CRUD, consolidation, supersession, history
|
+-- Extraction Service
| +-- OpenAI-compatible adapter
| +-- quality gate and evidence checks
| +-- pending candidate review
|
+-- Hybrid Retriever
| +-- FTS5 keyword search
| +-- optional embeddings
| +-- importance, recency, context budget
|
+-- Storage
+-- SQLite as source of truth
+-- Markdown as generated projection
+-- backups and JSON export
```
More detail:
- [Architecture](docs/architecture.md)
- [Data model](docs/data-model.md)
- [Automatic memory quality](docs/memory-quality.md)
- [Security and backups](docs/security-backup.md)
- [FAQ](docs/faq.md)
## Installation
Requirements:
- Python 3.12 or newer
- Windows 10/11, Linux, or WSL2
- SQLite built with FTS5 (the standard Python SQLite build normally includes it)
### Windows PowerShell
```powershell
cd <PROJECT_ROOT>
python -m venv .venv
.\.venv\Scripts\python.exe -m pip install --upgrade pip
.\.venv\Scripts\python.exe -m pip install -e ".[dev]"
$env:PME_DATA_DIR="$env:LOCALAPPDATA\personal-memory-engine"
.\.venv\Scripts\personal-memory.exe init
.\.venv\Scripts\python.exe -m pytest
```
### Linux / WSL2
```bash
cd <PROJECT_ROOT>
python3.12 -m venv .venv
.venv/bin/python -m pip install --upgrade pip
.venv/bin/python -m pip install -e '.[dev]'
export PME_DATA_DIR="$HOME/.local/share/personal-memory-engine"
.venv/bin/personal-memory init
.venv/bin/python -m pytest
```
### Setup script
```powershell
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\setup.ps1
```
The default data directory is outside the repository. Source code and user memory remain separate.
## Configuration
Copy the example values into your shell, IDE, or Codex environment:
```text
.env.example
```
Never commit a real `.env` file.
### DeepSeek
DeepSeek exposes an OpenAI-compatible Chat Completions API. Example:
```powershell
$env:DEEPSEEK_API_KEY="<YOUR_LOCAL_KEY>"
$env:PME_LLM_BASE_URL="https://api.deepseek.com"
$env:PME_LLM_MODEL="deepseek-flash"
$env:PME_LLM_JSON_MODE="true"
```
The endpoint is configurable; no provider is hard-coded into the storage or retrieval layers.
### Embeddings
DeepSeek's current chat API does not expose the embedding endpoint used by this project. To enable semantic retrieval, configure any OpenAI-compatible embedding service:
```powershell
$env:PME_EMBEDDING_ENABLED="true"
$env:PME_EMBEDDING_BASE_URL="<OPENAI_COMPATIBLE_EMBEDDING_BASE_URL>"
$env:PME_EMBEDDING_API_KEY="<YOUR_LOCAL_KEY>"
$env:PME_EMBEDDING_MODEL="<EMBEDDING_MODEL>"
```
Without an embedding provider, the hybrid retriever automatically uses local keyword retrieval.
See:
- [Installation and configuration](docs/installation.md)
- [DeepSeek configuration](docs/deepseek.md)
## Codex MCP Integration
The project exposes an MCP stdio server through:
```text
.venv/Scripts/personal-memory-mcp.exe # Windows
.venv/bin/personal-memory-mcp # Linux/WSL
```
Use the registration script:
```powershell
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\install-codex-mcp.ps1
codex mcp list
codex mcp get personal_memory
```
The script:
- backs up the existing Codex configuration before modifying it;
- refuses to overwrite an existing server unless `-Force` is supplied;
- registers the server with a server-scoped approval setting;
- sets UTF-8 environment variables for Windows reliability.
A manual configuration example is available at [codex/config.toml.example](codex/config.toml.example). Replace both `PROJECT_ROOT` and `DATA_DIR` placeholders.
After registration, start a new Codex session to load the MCP tools. See [Codex integration](docs/codex-integration.md).
## Memory Model
| Category | Purpose |
|---|---|
| `profile` | Stable background, capabilities, and preferences |
| `goal` | Long-term goals and plans |
| `state` | Current learning, project, and environment state |
| `episodic` | Decisions, events, conclusions, and reusable lessons |
Each memory or candidate can additionally carry:
- `assertion_type`: `user_fact`, `user_preference`, `user_plan`, `user_state`, `episodic_event`, or `model_inference`
- `stability`: `long_term`, `current`, `transient`, or `unknown`
- `quality_score`
- `observed_at`
- `source_ref`
- evidence, conversation hash, and extractor metadata
Automatic extraction produces **pending candidates only**. Confirmation is required before a candidate becomes confirmed memory.
## MCP Tools
| Tool | Purpose |
|---|---|
| `search_memories` | Search relevant active memories |
| `build_memory_context` | Return a bounded context block |
| `get_user_profile` | Retrieve stable profile memories |
| `get_project_context` | Retrieve project context |
| `get_learning_progress` | Retrieve learning state |
| `save_memory` | Explicitly save confirmed information |
| `update_memory` | Update content with optional optimistic version check |
| `delete_memory` | Soft delete by default; hard delete requires confirmation |
| `get_memory_history` | Retrieve immutable history |
| `list_memories` | Filter by category, status, and tags |
| `extract_memories` | Extract pending candidates with quality gating |
| `review_pending_memories` | Review candidates |
| `accept_candidate` | Accept only after explicit user confirmation |
| `reject_candidate` | Reject with audit trail |
| `get_memory_stats` | Inspect storage and integrity statistics |
## CLI
```powershell
.\.venv\Scripts\personal-memory.exe init
.\.venv\Scripts\personal-memory.exe search "project architecture" --json
.\.venv\Scripts\personal-memory.exe list --category goal
.\.venv\Scripts\personal-memory.exe backup
.\.venv\Scripts\personal-memory.exe export
```
Backups are stored outside the source tree by default. Restore requires the literal confirmation value `RESTORE`, and a pre-restore backup is created automatically.
## Testing
```powershell
.\.venv\Scripts\python.exe -m pytest
.\.venv\Scripts\python.exe scripts\evaluate.py
.\.venv\Scripts\python.exe scripts\evaluate_memory_quality.py
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\smoke-mcp.ps1
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\test-codex-e2e.ps1
```
The tests cover CRUD, persistence, FTS synchronization, conflict handling, backups, automatic memory quality, paraphrase detection, short-term learning progress, credential rejection, and Codex-native MCP calls. See [Testing](docs/testing.md) and [Test report](docs/test-report.md).
## Privacy and Security
- API keys and tokens are never included in this repository.
- Runtime databases, WAL/SHM files, Markdown memory mirrors, logs, backups, and local Codex configuration are excluded by `.gitignore`.
- Credential-like content is rejected before memory storage.
- Hard delete requires the exact memory ID.
- Restore requires explicit confirmation and checksum/integrity validation.
- Historical snapshots and audit records remain local.
- Markdown is a generated projection, not a retrieval source.
Read [PRIVACY.md](PRIVACY.md) and [SECURITY.md](SECURITY.md) before deploying.
## Current limitations
- The current release is single-user and local-first.
- SQLite is not encrypted at rest.
- Codex is the only fully validated MCP client.
- DeepSeek support is implemented but has not been tested with a real external API key in this repository.
- Embedding retrieval is optional and disabled by default.
- Automatic extraction remains model-assisted; the deterministic quality gate reduces risk but does not eliminate extraction errors.
- The Markdown mirror is generated output and must not be edited as a source of truth.
See [Roadmap](docs/roadmap.md) for planned work.
## Community
- [Contributing](CONTRIBUTING.md)
- [Code of Conduct](CODE_OF_CONDUCT.md)
- [Changelog](CHANGELOG.md)
## Repository Layout
```text
src/memory_engine/ application source
tests/ automated tests
docs/ architecture, security, operations, and evaluation
examples/ fictional example memories
scripts/ setup, smoke, evaluation, and backup scripts
codex/ Codex MCP and agent configuration examples
```
## License
Personal Memory Engine is licensed under the **Apache License 2.0**.
- [LICENSE](LICENSE)
- [NOTICE](NOTICE)
- [Third-party notices](THIRD_PARTY_NOTICES.md)
Copyright 2026 Personal Memory Engine contributors.
TDQS
Scored across 15 tools
Most tools have clearly distinct purposes: CRUD on memories, a pending-candidate workflow, and read-only retrieval helpers. A few boundaries blur — search_memories vs list_memories vs build_memory_context all retrieve memories, and extract_memories vs review_pending_memories require reading descriptions to tell apart. Descriptions do resolve the ambiguity, so misselection is unlikely but possible.
Every tool follows a strict verb_noun snake_case pattern (search_memories, get_user_profile, save_memory, accept_candidate, list_memories). Verbs are used meaningfully and consistently across the whole surface.
15 tools sits at the top of the well-scoped range but each maps to a real operation: memory CRUD, candidate lifecycle, history/stats, and scoped context retrieval. Nothing appears redundant or speculative for a personal memory server.
Strong lifecycle coverage: create/read/update/soft-delete, versioning, history, candidate extraction-review-accept-reject, plus stats and bounded context assembly. Gaps exist — there is no direct get_memory_by_id (only search/list), and soft-deleted memories have no restore/undelete operation.