Second Brain
Persistent, semantically-searchable memory for AI agents using local PostgreSQL, pgvector, and Ollama embeddings, exposed via MCP with hybrid retrieval, knowledge graph, and auto-recall hook.
README
Second Brain (Public)
A persistent, semantically-searchable memory for AI agents (Claude, Codex, or any MCP client). Memories live in local PostgreSQL + pgvector, are embedded locally with Ollama, and are exposed over the Model Context Protocol plus an optional auto-recall hook. A web UI is included for search, browsing, and a knowledge graph.
Everything runs locally and offline — no API keys, nothing leaves your machine.
Architecture
One store, three loops: Recall reads (hybrid search injects memories into prompts), Capture → Promote grows the store (sessions/imports → review queue → approved memories), and Maintenance curates it (scheduled consolidation, forgetting, and snapshots). Ollama provides local embeddings and the chat model.
Features
- Hybrid retrieval — vector similarity (pgvector HNSW) fused with full-text keyword search via Reciprocal Rank Fusion, then signal-boosted by importance, recency, and access frequency; optional 1-hop graph boost and local-LLM rerank.
- Knowledge graph — typed links between memories, with an interactive 3D view in the UI.
- Capture → promote — optionally log agent sessions, mine them into a review queue, and approve durable facts into long-term memory (nothing is promoted automatically).
- Self-maintenance — scheduled consolidation ("dreaming"), reversible decay + contradiction archival ("forgetting"), and point-in-time database snapshots.
- Import — seed the brain from a ChatGPT data export.
Requirements
- Docker — for PostgreSQL + pgvector
- Node.js ≥ 20
- Ollama running locally, with the models pulled:
ollama pull nomic-embed-text # embeddings (768-dim) ollama pull qwen3:8b # distillation / reflect / dream / forget / rerank
Deploy
git clone <this-repo-url> second-brain && cd second-brain
cp .env.example .env # defaults work out of the box; adjust if needed
npm install
npm run db:up # start PostgreSQL + pgvector (schema auto-applied)
npm run db:migrate # idempotently (re)apply the schema
npm run build # compile TypeScript -> dist/
npm run api # web UI + REST API at http://localhost:8088
Open http://localhost:8088 to search, browse, add, and visualize memories.
Scheduled jobs (dream / forget / reflect / snapshots) run from an in-app scheduler inside the API process, so keep
npm run apirunning if you want them to fire.
Connect an MCP client
Register the compiled MCP server with your agent (use an absolute path to dist/mcp/server.js):
# Claude Code
claude mcp add --scope user second-brain -- node /absolute/path/to/second-brain/dist/mcp/server.js
# Codex
codex mcp add second-brain -- node /absolute/path/to/second-brain/dist/mcp/server.js
Tools exposed: save_memory, search_memory, list_memories, get_memory, update_memory,
delete_memory, find_duplicates, distill_text, memory_stats, plus the graph and maintenance
tools (link_memories, related_memories, dream, freeze, reflect, forget,
review_candidates, promote_candidate, reject_candidate).
Optional: auto-recall hook
Automatically inject the most relevant memories into every prompt. Add a UserPromptSubmit hook that
runs the compiled recall CLI (it fails open — any error or timeout simply injects nothing):
{
"hooks": {
"UserPromptSubmit": [
{ "hooks": [ { "type": "command", "command": "node /absolute/path/to/second-brain/dist/cli/recall.js", "timeout": 15 } ] }
]
}
}
Claude Code reads ~/.claude/settings.json; Codex reads ~/.codex/hooks.json. Remove the entry to disable.
Configuration
Sensible defaults live in .env.example (PostgreSQL on localhost:5432, Ollama on localhost:11434,
API on :8088). Key variables: DATABASE_URL, OLLAMA_URL, EMBED_MODEL / EMBED_DIM,
DISTILL_MODEL, API_PORT, and the RANK_* ranking weights. Schedules and ranking weights are also
editable live in the web UI's Settings tab.
Data & privacy
Memories live only in your local PostgreSQL volume (pgdata). Database snapshots are written to a
git-ignored snapshots/ folder, and ChatGPT imports stay in a git-ignored ingest/ folder. Nothing
is sent off-machine.
Operations
npm run db:down # stop the database (data persists in the pgdata volume)
docker compose down -v # stop AND delete all memories (drops the volume)
License
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。