Python · MCP · SQLite · LLM

Memory Agent (historical)

An earlier Python MCP server for persistent agent memory, backed by SQLite and local or cloud model summarization. Historical work; no longer part of my current setup.


An MCP server that gives Claude Code (or any MCP client) persistent, structured memory backed by SQLite plus a local or cloud LLM. No vector databases, no embeddings — categorization, dedup/merge, and relevance ranking are delegated to the LLM, while storage and full-text search live in SQLite + FTS5.

What it does

Exposes fourteen tools to the MCP client:

ToolPurpose
memory_storePersist a memory; LLM picks category, tags, importance; merges with an existing memory when content overlaps.
memory_queryFTS5 search over content + tags, supplemented by a LIKE fallback, then re-ranked by the LLM. Defaults to active memories.
memory_indexSame candidate gathering as memory_query but no LLM rerank — one compact line per hit, cheap to scan. Defaults to active memories.
memory_getFetch full content for one or more IDs (full UUID or 8-char prefix). Use after memory_index.
memory_pinMark a memory as startup-safe so inject-context can include it.
memory_unpinRemove startup-injection privilege without deleting the memory.
memory_listPure DB listing, filterable by scope, category, and status. No LLM call. Defaults to active memories.
memory_timelineDB-only chronological view, optionally bounded by before_iso / after_iso. Defaults to active memories.
memory_forgetDelete a memory by full UUID or 8-char prefix.
memory_session_searchSearch archived session transcripts in cold storage.
memory_session_getFetch the full archived transcript for one session.
memory_hot_readRead the bounded per-scope hot memory file.
memory_hot_editAdd, replace, or remove text in hot memory with size and duplicate guards.
memory_consolidateAsk the LLM to propose merge/delete/update actions over a scope’s memories; dry-run by default.

Memories carry a category (session_summary, code_decision, user_preference, project_knowledge, open_action), a free-text scope (e.g. a project name or global), lifecycle status, pinned startup-injection privilege, an importance (1–5), and tags.

The database defaults to ~/.claude/memory/memory.db (WAL mode). Set MEMORY_AGENT_HOME to move client-neutral state elsewhere.

LLM backends

Backend is selected by LLM_BACKEND:

  • ollama (default) — POSTs to OLLAMA_URL (default http://localhost:11434) with OLLAMA_MODEL (default qwen2.5:14b).
  • lm-studio / lmstudio — POSTs to the OpenAI-compatible LM_STUDIO_URL (default http://localhost:1234) with LM_STUDIO_MODEL (default qwen3-4b-instruct-2507-mlx) and LM_STUDIO_MAX_TOKENS (default 4096).
  • bedrock — uses boto3 against BEDROCK_MODEL (default us.anthropic.claude-3-5-haiku-20241022-v1:0) in AWS_REGION (default us-east-1), honoring AWS_PROFILE if set.
  • codex — shells out to non-interactive codex exec for cloud summarization. Configure with CODEX_BIN (optional explicit binary path), CODEX_MODEL (empty uses the Codex CLI default), CODEX_REASONING (default low), and CODEX_TIMEOUT seconds (default 180).

LLM failures never raise out of llm.py; the server falls back to safe defaults. FTS5 is treated as optional too — every FTS read/write is wrapped so the server still runs on SQLite builds without it.

Tested on an M4 Max / 128 GB Mac:

TagResult on full integration suite
qwen2.5:14bSolid baseline — small pull, strong JSON adherence. The code-level default.
qwen3:30b-a3bFast (MoE, 3B active), but weaker on structured-output tasks: tag extraction, merge proposals, and consolidation actions sometimes empty.
gemma4:26b17/17 integration tests pass. Built on Gemini 3 research, MoE with 3.8B active params. Currently the best local fit for this workload.

Install

# via nathanmauro.io/r/memory-agent-mcp
git clone https://github.com/nathanmauro/memory-agent
cd memory-agent

# Python 3.10+ required (FastMCP)
uv venv --python 3.13 .venv
uv pip install --python .venv/bin/python "mcp[cli]" pydantic
# add boto3 only if using the Bedrock backend

For Ollama: install Ollama and pull a model, e.g.:

ollama pull gemma4:26b

Install

One command registers the MCP server and writes hook entries:

# via nathanmauro.io/r/memory-agent-mcp
# Claude Code (default)
.venv/bin/python -m mcp_memory_agent.install --client claude

# Codex (project-local .codex/hooks.json + user MCP registration)
.venv/bin/python -m mcp_memory_agent.install --client codex

# Both
.venv/bin/python -m mcp_memory_agent.install --client both

For Claude Code, hooks land in ~/.claude/settings.json (backed up to settings.json.bak). For Codex, enable hooks in ~/.codex/config.toml with [features] codex_hooks = true. SessionStart inject-context is intentionally strict: it injects bounded hot memory plus explicitly pinned active warm memories only. Broad warm memories and cold session pointers stay pull-based through query/search tools.

If you’d rather register the MCP server by hand, point the client at the installed console script:

# via nathanmauro.io/r/memory-agent-mcp
claude mcp add -s user memory /absolute/path/to/.venv/bin/mcp-memory-agent-server
codex mcp add memory -- /absolute/path/to/.venv/bin/mcp-memory-agent-server

Auto-capture

install.py writes client hook entries that call mcp-memory-agent-hook:

Hook eventWhat it does
SessionStartInjects bounded hot memory and explicitly pinned active warm memories. Broad warm memories and cold session pointers are retrieved on demand.
UserPromptSubmitClaude only: appends the user prompt to a per-session JSONL buffer.
PostToolUseAppends tool details to the same buffer.
SessionEnd / StopArchives any non-empty buffer, summarizes sessions with at least 3 records into warm memories, and stores extracted open_action rows.

Scope is derived from the session’s cwd: basename of the nearest .git toplevel, else basename of cwd, else global. Every hook is wrapped to exit 0 on failure — a memory-agent fault cannot break a Claude Code session.

Run directly (debugging)

.venv/bin/python -m mcp_memory_agent

Tests

# via nathanmauro.io/r/memory-agent-mcp
# Pure-local validation, mocks the LLM, no network needed
.venv/bin/python tests/test_validation.py

# Live integration tests; defaults to Ollama, set LLM_BACKEND to switch
LLM_BACKEND=ollama OLLAMA_MODEL=gemma4:26b .venv/bin/python tests/test_integration.py

Architecture

Small and procedural; models/ is the only subpackage.

File / DirRole
src/mcp_memory_agent/server.pyEntry point: db.init_db() then mcp.run().
src/mcp_memory_agent/tools.pyFastMCP app and the fourteen @mcp.tool() handlers.
src/mcp_memory_agent/db.pySQLite path/init, FTS upsert/delete, query-term extraction, session-buffer and archive helpers.
src/mcp_memory_agent/llm.pyLLM dispatch (Ollama / LM Studio / Bedrock / Codex), JSON extraction, ranking.
src/mcp_memory_agent/hook_handler.pyCLI entry for hooks: inject-context, record, summarize-session, finalize-session, sweep, curator.
src/mcp_memory_agent/integrations/Claude and Codex installer adapters.
src/mcp_memory_agent/install.pyOne-shot client installer for Claude, Codex, or both.
src/mcp_memory_agent/models/Pydantic models and reusable Annotated validators.
tests/test_integration.pyLive-LLM tests, isolated to memory_test.db + __test__ scope.
tests/test_validation.pyLocal tests; mocks llm.llm_call, uses a tempdir DB + sessions/archive dirs.

See CLAUDE.md for deeper architecture notes and AGENTS.md for code-style conventions.

License

MIT — see LICENSE if present, otherwise treat as MIT.

Status

Personal project. Used daily as the persistent-memory layer for Claude Code on the author’s machine. No CI, no release process, no support guarantees.

type to search...

↑↓ move ↵ open

keyboard shortcuts

/ or ⌘ then K or Ctrl then K
search the site
?
show these shortcuts
t
switch between dark and light
g then h
go to home
g then s
go to stories
g then f
go to field manual
g then p
go to projects
g then w
go to writing
g then a
go to about
esc
close search or this sheet

Shortcuts pause while you type in a field.