MCP Hub
Back to servers

memory-mcp

A self-organizing, persistent semantic memory layer that enables AI agents to store, categorize, and retrieve information using hybrid vector and keyword search. It features autonomous chunking, deduplication, and hierarchical taxonomy management through a PostgreSQL-backed MCP server.

Updated
Feb 27, 2026

memory-mcp

A self-organizing, persistent semantic memory layer for AI agents, exposed as an MCP (Model Context Protocol) server over HTTP. Memories are stored in PostgreSQL with pgvector, embedded via OpenAI, and retrieved through hybrid vector + keyword search with Reciprocal Rank Fusion (RRF). The system autonomously chunks, deduplicates, and categorizes content—no manual schema management required.


Architecture

AI Agent / Claude / Cursor
        │  HTTP (MCP)
        ▼
  server.py  ──── FastMCP (tools.py)
        │
        ├── config.py       Environment & OpenAI client
        ├── llm.py          Embedding + LLM calls
        ├── db.py           PostgreSQL pool, schema, dedup logic
        └── utils.py        Chunking, retries, ID generation

  PostgreSQL + pgvector
        ├── memories        Chunks, embeddings, category path (ltree), metadata
        ├── memory_edges    Graph edges (sequence_next, relates_to)
        └── reference.system.primer  Auto-synthesized global context

The server starts a background timer that triggers automatic System Primer regeneration whenever enough new memories accumulate (threshold: 10 changes, max age: 1 hour).


MCP Tools

ToolDescription
memorize_contextIngest raw text. Chunks, embeds, categorizes, and deduplicates automatically. Supports optional ttl_days.
search_memoryHybrid vector + BM25 search with RRF. Filter by category_path (e.g. projects.hardpoint).
initialize_contextFast bootstrap: returns all reference.system.* memories. Call this at the start of every session.
traverse_sequenceWalk sequence_next edges forward or backward from a memory ID.
list_categoriesReturn all occupied taxonomy paths with item counts.
delete_memoryHard-delete a memory by ID (cascades edges).
prune_historyBatch-delete superseded (merged) memories older than N days.
recategorize_memoryMove a single memory to a new taxonomy path.
bulk_move_categoryMove an entire taxonomy branch (e.g. old prefix → new prefix).
synthesize_system_primerForce a full scan → LLM summary stored at reference.system.primer. Use sparingly.

Taxonomy

Memories are organized into a hierarchical dot-path taxonomy using PostgreSQL ltree:

user.profile.personal.focus
user.health_profile.medical
projects.<name>.architecture.stack
organizations.<name>.business.subscription
concepts.ai_interaction.behavior.code_execution
reference.system.primer

The system automatically assigns a path during ingestion. You can override with recategorize_memory or bulk_move_category.


Environment Variables

Copy .env.example to .env and fill in your values.

VariableRequiredDefaultDescription
DATABASE_URLPostgreSQL connection string
OPENAI_API_KEYOpenAI API key (for embeddings + LLM)
EMBEDDING_MODELtext-embedding-3-smallOpenAI embedding model
CHAT_MODELgpt-4o-miniLLM for categorization + primer synthesis
EMBED_DIM1536Embedding vector dimension
DEFAULT_SEARCH_LIMIT10Default result count for search_memory
DEFAULT_LIST_LIMIT50Default result count for list_categories
OPENAI_TIMEOUT_S60Per-request OpenAI timeout (seconds)
OPENAI_MAX_RETRIES5Exponential-backoff retry limit
MAX_CONCURRENT_API_CALLS5Semaphore for parallel OpenAI calls
PG_POOL_MIN1asyncpg min pool connections
PG_POOL_MAX10asyncpg max pool connections
LOG_LEVELINFODEBUG / INFO / WARNING
MCP_TRANSPORTstreamable-httpFastMCP transport mode
PRIMER_UPDATE_MAX_AGE_S3600Max seconds before auto primer regeneration
FASTMCP_JSON_RESPONSESet to 1 to force JSON responses

Running with Docker (Recommended)

Prerequisites: Docker + Docker Compose.

# 1. Copy and fill in your environment variables
cp .env.example .env
$EDITOR .env

# 2. Start the stack (PostgreSQL + API + optional backup service)
docker compose up -d

# 3. The MCP server is now available at:
#    http://localhost:8766/mcp

To rebuild after code changes:

docker compose up -d --build memory-api

Running Locally (Development)

Requirements: Python 3.11+, a running PostgreSQL instance with pgvector extension.

# 1. Create and activate virtual environment
python3.11 -m venv .venv
source .venv/bin/activate

# 2. Install dependencies
pip install -r requirements.txt

# 3. Set environment variables
cp .env.example .env
$EDITOR .env
source .env  # or use direnv

# 4. Start the server
python -m server
# Server running on http://0.0.0.0:8766

Backup Service

The backup/ directory contains a containerized PostgreSQL backup job that:

  1. Runs on a configurable interval (default: every 6 hours)
  2. pg_dumps the memory database to memory.sql
  3. Commits and pushes the dump to a private GitHub repository

Additional backup environment variables:

VariableDescription
GITHUB_PATGitHub Personal Access Token with repo scope
GITHUB_BACKUP_REPOTarget repo in owner/repo format (e.g. isaacriehm/memory-backup)
DB_PASSWORDPostgreSQL password (same as in DATABASE_URL)
BACKUP_INTERVAL_SECONDSSeconds between backups (default: 21600 = 6 hours)

The backup service is included in docker-compose.yml and starts automatically with the stack.


Memory Visualization

visualize_memories.py generates an interactive graph of all memories at memory_map.html. Run it locally (requires DATABASE_URL in environment):

python visualize_memories.py
open memory_map.html

Reviews

No reviews yet

Sign in to write a review