mnemex

mnemex

Provides persistent, anchored memory for AI coding agents, binding every decision to specific lines of code and delivering relevant context just-in-time via the Model Context Protocol.

Category
访问服务器

README

mnemex — Anchored Memory for AI Coding Agents

The MCP server that gives coding agents persistent, anchored memory — every decision bound to the exact line it's about, delivered just-in-time.

Python 3.10+ License: MIT MCP Compatible

What is mnemex?

mnemex is a local-first Model Context Protocol (MCP) server that solves the #1 pain point of AI coding agents: they forget everything between sessions.

Unlike simple memory stores, mnemex anchors every decision to a specific file and symbol in your codebase, stamps it with a content hash, and delivers relevant context just-in-time — the moment your agent touches that file — not as a wasteful session-start dump.

The Problem

AI coding agents (Claude Code, Codex CLI, Cursor, Windsurf, Gemini CLI) suffer from two types of forgetting:

  • Fact forgetting — "We decided to use JWT for auth" is lost between sessions
  • Shape forgetting — "The auth module uses this specific pattern because..." vanishes every time

This leads to agents re-asking the same questions, contradicting past decisions, and wasting thousands of tokens re-reading files they already understood.

The Solution

mnemex provides:

Feature What it does
Decision anchors Every memory is bound to a file + symbol + content hash
Just-in-time delivery Context injected via PreToolUse hook at edit time (≤400 tokens)
Automatic staleness When anchored code changes, memories are flagged stale
Hybrid retrieval BM25 + vector (sqlite-vec) fused via RRF — no ML model required
Token governor Hard caps (800 session / 400 JIT) — never exceeds budget
Secret stripping PII and credentials stripped deterministically at write time
Self-updating AGENTS.md Auto-generates and maintains the cross-agent config file

Quick Start

Installation

pip install mnemex

No ML model download required. Works immediately in BM25-only mode.

Run the MCP Server

python -m mnemex serve --db project.sqlite3

Configure in Claude Code

Add to your MCP settings:

{
  "mcpServers": {
    "mnemex": {
      "command": "python",
      "args": ["-m", "mnemex", "serve", "--db", "project.sqlite3"]
    }
  }
}

Basic Usage

from mnemex.storage import Storage
from mnemex.anchors import remember, check_freshness
from mnemex.retrieval import recall

# Open the brain
storage = Storage("project.sqlite3")

# Remember a decision, anchored to code
remember(storage, "Use signed cookies for auth sessions",
         anchor=Anchor(file="src/auth.py", symbol="authenticate"),
         rationale="Keeps request handling stateless")

# Recall relevant context (BM25, no ML needed)
result = recall(storage, "authentication sessions", max_tokens=400)

# Check what's gone stale
reports = check_freshness(storage)
for r in reports:
    if r.status == "stale":
        print(f"⚠️  {r.memory_id} — code changed since decision was made")

Architecture

┌──────────────────────────────────────────────────────┐
│                    mnemex (core)                       │
│                                                       │
│   ┌───────────────────────────────────────────────┐   │
│   │              ONE SQLite file                   │   │
│   │  ┌──────────────┐   ┌─────────────────────┐  │   │
│   │  │  structural  │◀──│  episodic memories   │  │   │
│   │  │  nodes/edges │ANCHOR decisions/conventions│  │   │
│   │  │  (hash)      │   │  + vec + fts5        │  │   │
│   │  └──────────────┘   └─────────────────────┘  │   │
│   └───────────────────────────────────────────────┘   │
│           │                                           │
│   Fusion engine (RRF) · Token governor · Staleness    │
│           │                                           │
│   ┌───────▼───────────────────────────────────────┐   │
│   │      MCP server (FastMCP) + HOOKS             │   │
│   │  SessionStart · PreToolUse · Stop             │   │
│   └───────────────────────────────────────────────┘   │
└───────────────────────┬───────────────────────────────┘
                        │ MCP (stdio)
        ┌───────────────┼───────────────┐
   Claude Code      Codex CLI       Cursor/Windsurf

One SQLite file. No cloud. No ML required. Local-first.

MCP Tools (10 tools)

Tool Description
remember_decision Store a decision, optionally anchored to code
recall_memories Hybrid BM25+vector retrieval with token governor
forget_memory Remove a memory by ID
check_memory_freshness Report fresh/stale/orphaned status
context_for JIT context for a file (≤400 tokens)
get_context_brief Session-start brief (≤800 tokens)
why Explain why a symbol is designed this way (decision + callers)
trace_callers_tool Who calls/references this symbol
index_path Index files into the structural graph
generate_agents_md Auto-generate AGENTS.md

Key Concepts

Decision Anchors

Every memory can be anchored to a structural location (file + symbol). When that code changes, the memory is automatically flagged stale:

  • Fresh — the anchored code hasn't changed
  • Stale — the code changed; the decision may need revisiting
  • Orphaned — the anchored symbol was deleted
  • Unanchored — a global fact not tied to specific code

Token Governor

mnemex guarantees it never exceeds your token budget:

  • Session start brief: ≤800 tokens
  • JIT (PreToolUse) injection: ≤400 tokens
  • Memories that don't fit are dropped (not truncated), and reported

No-ML Mode

Works out of the box with zero model download. BM25 (FTS5) handles keyword retrieval. When you add an embedder, it upgrades to hybrid BM25+vector via Reciprocal Rank Fusion automatically.

Security

Secrets and PII are stripped at write time before persistence:

  • AWS keys, GitHub tokens, JWTs, PEM private keys
  • Connection strings with passwords
  • Email addresses, phone numbers, IP addresses
  • <private> tagged blocks are removed entirely
  • Every redaction is recorded in an audit log

Benchmarks

On mnemex's own source (10 Python files, 120 nodes):

Metric Value
Baseline tokens (reading files) 20,944
JIT tokens (session + 5 contexts) 370
Token savings 98.2%
Compression ratio 56.6×
Index time 0.07s
JIT latency 0.9ms

CLI

# Run the MCP server
python -m mnemex serve --db project.sqlite3

# Index a codebase
python -m mnemex index ./src --db project.sqlite3

# Run benchmarks
python -m mnemex benchmark ./src

Works With

mnemex is compatible with any MCP-supporting agent:

  • Claude Code (hooks: SessionStart + PreToolUse)
  • Codex CLI (MCP tools)
  • Cursor (MCP tools)
  • Windsurf (MCP tools)
  • Gemini CLI (MCP tools)
  • Cline / Roo Code (MCP tools)
  • Any MCP client (stdio transport)

Project Structure

src/mnemex/
├── storage.py      # SQLite + sqlite-vec + FTS5 schema
├── anchors.py      # Anchor resolution + hash stamping + staleness
├── retrieval.py    # BM25 + vector RRF fusion + token governor
├── indexer.py      # Structural backend adapter (Python AST)
├── server.py       # FastMCP server (10 tools)
├── hooks.py        # SessionStart / PreToolUse / Stop hooks
├── agents_md.py    # why() fusion + AGENTS.md generator + staleness watcher
├── security.py     # Secret stripping + audit log
└── __main__.py     # CLI entry point

Development

# Install with dev dependencies
pip install -e ".[dev]"

# Run linter
python -m ruff check .

# Run tests (145 tests)
python -m pytest -v

Comparison with Alternatives

Feature mnemex agentmemory codebase-memory engram
Anchored to code symbols ✅ ❌ ❌ ❌
Automatic staleness detection ✅ ❌ ❌ ❌
JIT hook injection ✅ ❌ ❌ ❌
Token budget governor ✅ ❌ ❌ ❌
No ML required ✅ ❌ ✅ ✅
One SQLite file ✅ ❌ ❌ ✅
Secret stripping at write ✅ ❌ ❌ ❌
Self-updating AGENTS.md ✅ ❌ ❌ ❌
MCP server ✅ ✅ ✅ ✅

FAQ

How is mnemex different from just using CLAUDE.md?

CLAUDE.md is static and manually maintained. mnemex:

  • Automatically detects when decisions go stale (code changed)
  • Delivers only relevant context per-file (not everything at session start)
  • Respects token budgets (CLAUDE.md grows without bound)
  • Works across all MCP-supporting agents (not just Claude)

Does it need a GPU or embedding model?

No. mnemex works in no-ML mode (BM25 keyword search) by default. You can optionally provide an embedder for hybrid retrieval, but it's not required.

How does the token governor work?

Every injection path takes a hard max_tokens budget. The governor ranks memories by relevance, includes them in order while the running sum fits, and drops (never truncates) memories that exceed the cap. It reports what was dropped so the agent can request more if needed.

What happens when I refactor code?

Anchored memories are automatically flagged stale when their symbol's content hash changes, and orphaned when the symbol is deleted. The agent sees these flags and can reconcile (update or remove the decision).

License

MIT

Contributing

Contributions welcome. Please ensure:

  • python -m ruff check . passes
  • python -m pytest passes (145+ tests)
  • New features include tests
  • Security-sensitive changes require Phase 6 gate validation

mnemex — Not another memory list. A brain that anchors every decision to the exact line it's about — and hands it back the instant your agent touches that line.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选