Skip to content

About

A self-hosted MCP server that gives Claude, ChatGPT, Claude Code and any other MCP client one shared long-term memory, kept separate for each project.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

shared-brain logo: three little blobs under one cloud

shared-brain

Stop re-explaining your project to every AI you use.

CI Python 3.12 MCP server Postgres with pgvector and pg_trgm Supabase free tier

A self-hosted MCP server that gives Claude, ChatGPT, Claude Code and any other MCP client one shared long-term memory, kept separate for each project.

shared-brain architecture: MCP clients → MCP server on Render (with OpenAI embeddings) → Supabase Postgres

Features

  • Any MCP client: Claude, ChatGPT, Claude Code and more. Adding one is a URL, no code change.
  • Hybrid search: pgvector semantic search plus pg_trgm keyword matching, merged with RRF.
  • Typed memories with history: new states supersede old ones instead of overwriting them.
  • Duplicate-aware: near-duplicates go back to the assistant to decide.
  • Knows when it may be out of date: a state or blocker untouched for two weeks is flagged, so the assistant asks instead of presenting it as current.
  • Session summaries without asking: a Claude Code Stop hook keeps each session's summary up to date.
  • A page for people: a read-only web viewer at /viewer, phone-friendly.

Example

Monday, in ChatGPT
  you      Let's go with Astro for the site, the content is static.
  ChatGPT  save_memory(project="my-site", kind="decision",
                       content="my-site: decided on Astro because content is static")

Wednesday, in Claude Code
  you      Set up the site skeleton.
  Claude   search_memory(project="my-site", query="site framework")
           → "my-site: decided on Astro because content is static"
           Scaffolding an Astro project...

Nobody had to explain the decision twice.

Quick start

You need a Supabase project (free), an OpenAI key for embeddings, and Python 3.12.

  1. In the Supabase SQL Editor, run schema.sql, then the files in migrations/ in order.
  2. Copy .env.example to .env and fill it in.
  3. Run the server:
pip install -r requirements.txt
python server.py

Or with Docker:

docker build -t shared-brain .
docker run --env-file .env -p 8000:8000 shared-brain

Check it with the MCP Inspector (npx @modelcontextprotocol/inspector) at http://localhost:8000/<MCP_SECRET_PATH>.

Full setup: deploy and connect your assistants
  1. Database. Row Level Security is on with no public policies, so only your server can touch the tables.

  2. Environment. MCP_SECRET_PATH is the output of openssl rand -hex 24. It becomes the URL path and is the only thing standing between the internet and your memories, so treat it like a password. Projects live in the projects table: migration 005 registers every project that already has memories, and new ones are added with the add_project tool, no redeploy needed. (ALLOWED_PROJECTS is only a fallback for databases that have not run migration 005.) Migration 006 adds project aliases: every tool that takes a project accepts an alias too, so "心力球" resolves to ai-pendant for every assistant, not just the one that happened to know the nickname. Optionally set VIEWER_TOKEN to turn on the web viewer.

  3. Deploy. Push to GitHub and create a Render Web Service from the repo: start command python server.py, the same env vars, plus PYTHON_VERSION=3.12.0. Free instances sleep after 15 minutes of inactivity and take a few seconds to wake up.

  4. Connect the assistants. The connector URL is https://<your-service>.onrender.com/<MCP_SECRET_PATH>.

    Client Where to add it
    Claude web Settings > Connectors > Add custom connector
    ChatGPT desktop Settings > Plugins > Add MCP server (Streamable HTTP, no auth)
    Claude Code claude mcp add --transport http --scope user shared-brain <URL>, on every machine it runs on
  5. Tell them when to use it. Tools alone do nothing; the assistant has to know when to reach for them. Put standing instructions in Claude (Profile or Project instructions), ChatGPT (Custom instructions) and Claude Code (~/.claude/CLAUDE.md). Mine says roughly:

    When I mention a project or ask what we decided, search shared memory first. When I make a decision, change a plan or hit a milestone, save one short self-contained entry. When updating, keep the details and change only what changed. Never store secrets, credentials, personal data, or details of security or compliance incidents.

    Then add one line per project in its Claude Project, ChatGPT Project and repo CLAUDE.md: "the shared-memory project name is X." After that, "save that" is enough.

Tools

Tool What it does
save_memory Save one typed entry. Rejects near-duplicates (cosine ≥ 0.9); supersedes replaces an outdated entry.
update_memory Fix wording or kind/tags. The old text is archived by a trigger.
retire_memory Mark an entry as no longer valid. Hidden, not deleted.
search_memory Hybrid search within one project.
recent_memories Latest changes, newest first.
project_brief One call for the whole picture: current state, open blockers, latest decisions, last session summary. States and blockers not updated for STALE_DAYS (default 14) are flagged possibly_stale, with a plain-language note.
delete_memory Remove something that was wrong from the start.
list_projects Registered projects with a one-line description and aliases each.
add_project Register a new project, optionally with aliases. Rejects badly formed names and asks first when a similar name exists (scai-lab vs scai).
add_alias Record another name the user uses for a project. From then on it works as project everywhere; an alias cannot point to two projects.

Superseded and retired entries are hidden unless you pass include_history.

The tables, how each migration changed them, and how hybrid search ranks results are described in docs/schema.md.

Web viewer

The same server also serves a read-only page at /viewer for reading the memories yourself, on a phone or a laptop:

  • per project: where it stands, open blockers, latest decisions and the last session summary on top, then every memory as a timeline
  • filter by kind or by text, or run the same hybrid search the assistants use (with its vector and keyword scores)
  • with history on: superseded and retired memories, links between a state and the one that replaced it, and earlier wordings of edited memories

It is off unless you set VIEWER_TOKEN (at least 24 characters, e.g. openssl rand -hex 24) in the server's environment. Open https://<your-service>.onrender.com/viewer, enter the token once, and the browser keeps a signed, HttpOnly cookie for a year; the token itself is never stored in the browser. Changing the token signs every browser out. No route in viewer.py writes to the database, and the tests check that.

Automatic session summaries (Claude Code)

hooks/session_summary.py is a Claude Code Stop hook. Every 10 replies, or after 45 minutes with at least 3 replies, it asks Claude to continue for one more step and save or update this session's session-summary through the MCP tools it already has. The model that did the work writes the summary, so the hook needs no API key, and each session keeps one summary that is superseded as it grows. It only runs in repos whose CLAUDE.md names a project ("The shared-memory project name is my-site." or "project 名是 my-site"), and it never blocks twice in a row.

Install it on every machine that runs Claude Code, with one command:

curl -fsSL https://raw.githubusercontent.com/anqiXu33/shared-brain/main/hooks/install.py | python3 -

(or python3 hooks/install.py from a clone). It copies the hook to ~/.claude/hooks/ and adds it to ~/.claude/settings.json without touching anything else in that file, after saving a backup. Run it again to update; --uninstall removes it.

Intervals and an off switch are environment variables, listed at the top of the script.

Design choices

Choice Why
The server never runs an LLM The assistant already has the context, so judgement calls (like duplicates) go back to it.
Typed memories (kind) Decisions, states and blockers age differently.
Staleness flags on states and blockers only They describe the present, which changes in conversations that never save anything. A decision or a log stays true as a record of what happened.
Supersede, don't overwrite Search shows what is true now; how you got there stays queryable.
Vector + keyword search Embeddings do find exact names and short Chinese queries, but often rank them lower; keyword matching pulls them to the top (see Evaluation).
Project registry A table with a foreign key from every memory, so assistants can't split one project into three spellings. New projects are added by a tool call, not by editing env vars.

Evaluation

52 hand-labeled queries over my own memories (about 220 across three projects), comparing the three ways the server could rank results. Top 5, the same as the default k.

Method Hit@5 Recall@5 MRR
Vector only 0.904 0.728 0.801
Keyword only 0.635 0.560 0.598
Hybrid (what the server uses) 0.885 0.743 0.854
  • Hybrid ranks the right memory higher than vector alone: MRR +0.053, paired bootstrap 95% CI [+0.002, +0.110]. Whether it is found at all (Hit@5, Recall@5) is the same.
  • The gain comes from exact names (MRR 0.85 → 0.96) and short Chinese queries (0.89 → 0.96). Vector search already found them; it just ranked them lower.
  • Reworded questions still depend on vectors alone: keyword matching finds the answer for 2 of 16.

Caveats: a small set, one user's data, and the queries were drafted while looking at the memories, which favours wording that overlaps with the text.

Building the evaluation also found a bug. On short queries, many memories get exactly the same keyword score, and the search broke those ties arbitrarily, so which of them got the keyword boost changed from run to run. Migration 004 breaks ties by vector distance; afterwards the live search matched the offline reproduction on 52 of 52 queries. Code and how to run it: eval/.

Does the assistant actually use it?

Good retrieval does not help if the assistant never calls it, or saves a second copy instead of superseding. eval/behavior/ runs a model with the same instructions and tool descriptions as my clients against a private in-memory copy of the store, on 26 scripted scenarios: looking things up before answering (and not for unrelated questions), asking which project is meant when two fit, saving decisions with their reasons, superseding and retiring instead of duplicating or deleting, keeping keys out of memory, recording a new nickname, finding in a new conversation what was saved in an earlier one, and flagging a month-old status. Checks are rules on the tool calls and the final memory, plus a judge model for answer content and for whether saved memories contain only what the user said. Each scenario runs several times; rates come with Wilson intervals.

Why I built this

I use Claude for thinking, ChatGPT for second opinions and Claude Code for the actual work, and none of them talk to each other. Now project state that Claude Code saves late at night is there the next morning when I ask Claude web "what's waiting on my decision?"

Things I learned the hard way

  • The mcp Python package shipped a 2.0 the week I built this and renamed FastMCP. Pin mcp<2.
  • Claude Code sometimes passes list arguments as JSON strings. server.py coerces them.
  • "Don't store sensitive data" is not specific enough. An assistant will avoid the data itself and then store a paragraph about it. Spell out what sensitive means.

Roadmap

  • Memory kinds, supersede/retire, hybrid search (migration 002)
  • Retrieval evaluation and deterministic tie-breaking (migrations 003, 004)
  • project_brief tool: recent progress, open decisions, blockers and last session summary in one call
  • Session summaries written automatically by Claude Code via a Stop hook
  • Project registry in the database instead of an env var (migration 005)
  • Project aliases, so any assistant resolves a nickname to the right project (migration 006)
  • Read-only web viewer
  • Staleness flags in project_brief and the viewer
  • Live round-trip check against the deployed server, and a by-hand checklist per client
  • Behaviour evaluation: does the assistant look up, supersede, retire and keep secrets out
  • Later: OAuth instead of a secret path

Tests and CI

Every push runs GitHub Actions:

  • Database tests on a real Postgres with pgvector: schema.sql and every migration are applied to a fresh database, then the tests check that hybrid search ranks exactly as the evaluation reproduces it (including keyword ties, the bug migration 004 fixed), hides superseded and retired memories, filters by kind and project, archives old wording on update, and enforces the project registry (unregistered names, name format, renames, backfill).
  • Server unit tests with a fake database: duplicate rejection, supersede guards, retire, project registration and its similar-name check, and argument coercion through the real MCP tool layer.
  • Viewer tests: login, cookie flags, CSP, pagination past 1000 rows, and that every route only reads.
  • Hook tests: run the Stop hook as a subprocess the way Claude Code does: when it asks, that it never asks twice in a row, project detection, the off switch, bad input. The installer is tested against existing settings files: other hooks and settings kept, idempotent, invalid JSON left alone, clean uninstall.
  • Docker: builds the image, starts it with dummy credentials, and runs the MCP handshake and tools/list against it.
  • Round trip: tests/roundtrip_live.py saves, finds, supersedes, corrects, retires and deletes memories in a roundtrip-check project. CI runs it against the real server app over HTTP with a fake database; Actions > Live round trip > Run workflow runs it against the deployed server (secret SHARED_BRAIN_MCP_URL). Whether each assistant really looks up, supersedes and retires is checked by hand with docs/roundtrip-checklist.md.
  • Behaviour evaluation code: the scenario file is validated, the sandbox runs the real tools, and a whole run goes through with a scripted model and stub API clients.

Locally:

pip install -r requirements-dev.txt
ruff check . && pytest            # database tests skip without DATABASE_URL
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres pytest

Inspiration

Repository

File Purpose
server.py The MCP server
viewer.py Read-only web viewer at /viewer
hooks/session_summary.py Claude Code Stop hook for automatic session summaries
hooks/install.py One-command installer for that hook
docs/schema.md Tables, migration history and how search ranks
schema.sql Initial database setup
migrations/001_history.sql memory_history table and trigger
migrations/002_kinds_validity_hybrid.sql Memory kinds, supersede/retire, hybrid search
migrations/003_eval.sql Read-only scoring function for the retrieval evaluation
migrations/004_deterministic_ties.sql Deterministic tie-breaking in hybrid search
migrations/005_projects.sql projects table and foreign key from memories
migrations/006_project_aliases.sql aliases column on projects
eval/ Retrieval evaluation: vector vs keyword vs hybrid (how to run)
tests/ Database tests, server, viewer and hook unit tests, HTTP smoke test
Dockerfile Container image for the server
.github/workflows/ci.yml Lint, tests and Docker build on every push
requirements.txt Python dependencies (requirements-dev.txt adds test tools)
.env.example Environment variable template
CLAUDE.md Instructions for Claude Code when working on this repo
docs/architecture.svg The architecture diagram above

About

A self-hosted MCP server that gives Claude, ChatGPT, Claude Code and any other MCP client one shared long-term memory, kept separate for each project.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages