memgw
Provides shared long-term memory for AI coding agents via MCP, allowing tools like Claude Code and Codex to store and retrieve distilled facts, notes, and conversation history to persist context across sessions.
README
<div align="center">
🧠 memgw
Shared long-term memory for AI coding agents.
One store that Claude Code, Codex CLI, opencode, and anything that speaks MCP can all read from and write to. Your agents stop forgetting you every session.
English · Tiếng Việt
npm install -g @holetex/memgw && memgw setup
</div>
One command: it creates ~/.memgw, generates keys, asks for your LLM key, wires up
Claude Code (hooks + MCP) and Codex (MCP) when they are on the machine, starts the
gateway, and ends with a health check. What it does NOT fully automate, it says out
loud: reboot supervision is installed automatically on macOS (launchd) and Linux
(systemd --user) from a permanent checkout (npm i -g @holetex/memgw or a clone), while
Windows / npx-cache runs get the exact Task Scheduler / install command printed
instead; opencode gets a one-line copy command. When Codex CLI is found, its
transcript watcher (Codex has no hooks) is installed supervised too on
macOS/Linux — elsewhere the exact memgw watch --agent codex command is printed.
Just want to try it without installing anything? npx @holetex/memgw setup works
too — same wizard, but running from the npx cache means no reboot supervision
(the wizard says so and prints the permanent-install command). Prefer doing
everything by hand? memgw start just starts the gateway and prints the commands.
Prefer delegating? Tell your agent: "install memgw — follow https://github.com/holetexvn/memgw/blob/main/AGENTS.md". AGENTS.md is a playbook written for agents: non-interactive setup, what to ask you (the LLM key), and how to verify the install.
The problem
Every agent keeps its memory in its own silo. Claude Code on your laptop knows nothing about the session you ran on your desktop. Codex knows nothing about either. Close the terminal and the context is gone.
The expensive part of your day is not writing code, it is re-explaining context: what this project is, which conventions apply, what not to touch, and what you already tried last week that did not work.
memgw keeps that in one place. Every agent writes to it, every agent reads from it.
Architecture
<picture> <source media="(prefers-color-scheme: dark)" srcset="docs/assets/architecture-en-dark.svg"> <img alt="memgw architecture: agents, gateway, store" src="docs/assets/architecture-en-light.svg"> </picture>
Three layers, each more distilled than the last:
| Layer | What | Written by | Lifetime |
|---|---|---|---|
| events | raw conversation turns | capture, no LLM involved | 90 days |
| facts | one-sentence atoms, deduplicated | worker, 2 LLM calls per batch | forever |
| notes | Markdown topic files in a git repo | worker, agentic loop | forever |
Capture is cheap and dumb on purpose: it writes raw turns and returns immediately, so it never slows an agent down. Everything expensive happens later, in the background.
A session with memgw
<picture> <source media="(prefers-color-scheme: dark)" srcset="docs/assets/session-en-dark.svg"> <img alt="a session with memgw" src="docs/assets/session-en-light.svg"> </picture>
Retrieval follows one rule that matters: stable context is injected once at session start (profile, topic index) so it stays inside the prompt cache, while specific facts are exposed as tools for the agent to call when it actually needs them. Injecting search results on every turn breaks caching and floods the context with noise.
One fact type earns its keep more than the rest: deadend, meaning something you tried
that failed and why. Agents love repeating the same mistake; this is what stops them.
Why not just… ?
| Chat history | RAG over transcripts | memgw | |
|---|---|---|---|
| Survives closing the terminal | ❌ | ✅ | ✅ |
| Shared across different agents | ❌ | ❌ per-tool | ✅ one store |
| Distilled (facts, not walls of text) | ❌ | ❌ raw chunks | ✅ one-sentence atoms |
| Remembers what failed and why | ❌ | ⚠️ buried | ✅ deadend type |
| Prompt-cache friendly | ❌ | ❌ injects every turn | ✅ inject once + tools |
| Human-auditable | ❌ | ❌ opaque index | ✅ git log -p on notes |
| Infrastructure needed | — | vector DB + pipeline | one SQLite file |
Features
- 🔌 Every MCP agent — Claude Code, Codex CLI, opencode, claude.ai, Hermes, or any CLI via the generic transcript watcher. Five MCP tools, one endpoint.
- 🏠 Local-first — one process, one SQLite file, binds
127.0.0.1. Deploy to a VPS with one installer when you want several machines sharing memory. - 🔍 Hybrid search — FTS5/BM25 always works (no API, no cost);
memgw embed onadds semantic vectors in the same SQLite file, RRF-fused, falling back to BM25 if the embeddings API is down. - 🪦 Dead-end memory —
type=deadendfacts stop agents from retrying what already failed. The most valuable fact type, and the easiest for humans to forget to pass on. - 📓 Git-audited notes — everything the model writes to Markdown is one
git log -paway from inspection and onegit revertaway from undo. - 🛡️ Auth is never optional — the server refuses to start without a key, and refuses weak keys off-loopback. There is no configuration in which memgw is an open store.
- 💸 $1–3 / month — cost tracks how much you talk to agents, not store size; every background worker runs off a cursor. Works with any OpenAI-compatible endpoint.
- 🌏 English + Vietnamese prompts — facts come out in the language you work in
(
MEMGW_PROMPT_LANG=vi). Adding a language is copying one block.
Install
Local
npx @holetex/memgw start # zero config, binds 127.0.0.1
No LLM key yet? That is the default path, not a degraded one: capture keeps working, events queue locally, and when you add a key later the backlog gets distilled — nothing is lost. Add it whenever you are ready:
memgw key # masked prompt; provider auto-detected from the key
OpenAI, Anthropic, Groq, and OpenRouter keys are recognised by their prefix and
configure the endpoint and a sensible cheap model automatically. Any other
OpenAI-compatible endpoint (DeepSeek, local Ollama or vLLM) works too — set
MEMGW_LLM_BASE_URL in ~/.memgw/env. The key lives in that file on your machine
and is sent only to the provider you chose.
No key at all? npx @holetex/memgw start --mock exercises the entire pipeline without one.
Server
For several machines sharing one store, or for web clients that need a public HTTPS URL:
bash scripts/build-installer.sh # builds memgw-installer.run from this checkout
scp memgw-installer.run user@vps:/tmp/
ssh user@vps "sudo MEMGW_DOMAIN=memgw.example.com MEMGW_LLM_API_KEY=sk-xxx \
bash /tmp/memgw-installer.run"
The installer handles Node, a dedicated user, systemd, Caddy with TLS, the firewall, and an optional Litestream backup to S3-compatible storage. See docs/02-OPERATIONS.md.
Connect your agents
| Agent | Read | Write |
|---|---|---|
| Claude Code | MCP + SessionStart hook |
Stop hook |
| Codex CLI | MCP | transcript watcher |
| opencode | MCP | native plugin |
| claude.ai / web | MCP connector | the agent calls memory_save |
| Hermes | prefetch() |
sync_turn() |
| anything else | MCP, if it speaks it | generic watcher |
npx @holetex/memgw hooks # Claude Code capture + bootstrap
claude mcp add --transport http memgw http://127.0.0.1:8931/mcp \
--header "Authorization: Bearer $MEMGW_KEY"
codex mcp add memgw --url http://127.0.0.1:8931/mcp/<MEMGW_MCP_SECRET>
npx @holetex/memgw watch --agent codex # Codex has no hooks, so watch its transcripts
Full details for every client: docs/03-INTEGRATION.md and docs/05-MULTI-AGENT.md.
Adding an unsupported CLI usually needs no code at all:
npx @holetex/memgw watch --agent generic --dir ~/.some-cli/sessions --once --dry-run
MCP tools
Five tools, available to every connected agent:
| Tool | Purpose |
|---|---|
memory_search |
search distilled facts, filterable by type and topic |
conversation_search |
search raw transcripts across all agents |
memory_read_note |
read a topic note or the profile |
memory_save |
save a fact |
memory_bootstrap |
profile plus the topic index |
CLI
memgw start Start the gateway
memgw init Create config without starting
memgw status Store statistics
memgw doctor Diagnose configuration and connectivity
memgw search Search facts from the terminal
memgw save Save a fact from the terminal
memgw forget Retire facts (dry-run by default)
memgw key Set the LLM key later (provider auto-detected, gateway restarted)
memgw embed Toggle semantic search on or off
memgw watch Follow agent transcripts
memgw hooks Install Claude Code hooks
Design choices
SQLite plus FTS5, no vector database. Full-text search is the layer that always
works: no API dependency, no cost. Semantic search is one command away (memgw embed on)
and lives in the same SQLite file — vectors as BLOBs, fused with BM25, falling back to
BM25 whenever the embeddings API is down. Off by default.
Notes are Markdown in a git repo. Memory the model writes is memory you must be able
to inspect and correct. git log -p shows every change the worker made, and a bad edit is
one git revert away.
Auth is never optional. The server refuses to start without a key. Bind to anything other than loopback and it also demands a strong one. There is no configuration in which memgw is an open memory store.
Best-effort by design. Every call has a timeout, every failure is swallowed and logged. A dead memory store must never take your agent down with it.
The reasoning behind each of these, including what was rejected and why, is in docs/01-ARCHITECTURE.md.
Measured recall
End-to-end on LoCoMo (1,986 questions through the real capture → extraction → dedup → search pipeline, LLM-judged):
| Retrieval | Overall | Refuses correctly when memory has no answer |
|---|---|---|
| BM25 (default) | 58.6% | 84.5% |
+ embeddings (memgw embed on) |
66.4% | 80.3% |
On PersonaMem (COLM 2025, 32k-token tier, 589 multiple-choice questions on user personalization): 59.6% against a 25% random baseline, end-to-end through the same pipeline.
Methodology, per-category numbers, and how to reproduce it for ~$4: docs/06-BENCHMARKS.md.
Cost
Roughly 15 sessions a day across all agents, with a cheap model, comes to about 60 LLM calls and 0.5M tokens per day: $1-3 a month. Cost tracks how much you talk to your agents, not how large the store grows, because every worker runs off a cursor.
Documentation
| Document | Read it when |
|---|---|
| 01-ARCHITECTURE | you want to know how it works and why |
| 02-OPERATIONS | deploying, configuring, troubleshooting, backups |
| 03-INTEGRATION | connecting a specific client |
| 04-API | endpoint and MCP tool reference |
| 05-MULTI-AGENT | adding Codex, opencode, or your own CLI |
| 06-BENCHMARKS | measured recall on LoCoMo, and how to rerun it |
Prompt language
Extraction prompts ship in English and Vietnamese. The facts a model writes come out in the prompt language, so pick the one you actually work in:
echo 'MEMGW_PROMPT_LANG=vi' >> ~/.memgw/env
Adding a language means copying one block in src/prompts.js. Contributions welcome.
Development
git clone https://github.com/holetexvn/memgw.git
cd memgw && npm install
bash test/run-all.sh # 11 suites, no API key needed; the runner prints totals
The suite includes verify-docs.mjs, which cross-checks this documentation against the
code: endpoints, tool names, and the constants quoted in prose. Change a default without
updating the docs and the suite goes red.
See CONTRIBUTING.md.
Prior art
The layered-distillation approach and several implementation details were informed by
reading TencentDB Agent Memory
(MIT). memgw is a much smaller, single-user take on the same idea: no team management, no
ACL layer, no external infrastructure. docs/01-ARCHITECTURE.md has a section comparing
the two.
License
MIT
<div align="center">
If memgw saves you from re-explaining your project one more time, ⭐ the repo — it helps other people's agents find their memory too.
</div>
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。