Skip to content

About

A universal memory server for AI assistants. Add it as a custom connector in Claude, ChatGPT, Gemini or any other MCP client, and any of them can save and recall memories. Something you discuss in one app can then be picked up in another.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

exchangeMCP

A universal memory server for AI assistants. Add it as a custom connector in Claude, ChatGPT, Gemini or any other MCP client, and any of them can save and recall memories. Something you discuss in one app can then be picked up in another.

  • Runtime: TypeScript on Cloudflare Workers (free tier)
  • Protocol: MCP Streamable HTTP, stateless, at POST /mcp
  • Storage: Cloudflare D1 (SQLite) with an FTS5 full-text index, behind a swappable MemoryStore interface
  • Auth: a shared secret, sent either as Authorization: Bearer <token> or in the URL path as /mcp/<token>

Memory format

Each memory is one markdown "file", addressed by a path such as /writing/style.md. The format mirrors Claude's memory files:

---
name: style
description: "How I like my writing"
---

- [stated] Prefers British spelling
- [stated] Short sentences, no em dashes

Columns: path (unique), description (one line), body (markdown), tags (JSON array), source (the AI that last wrote it, e.g. claude, chatgpt, gemini), created_at, updated_at.

Paths are normalised to lowercase, get a leading /, and get .md appended if it's missing. Each segment may only contain a-z 0-9 . _ -. If write_memory receives a body without frontmatter, the frontmatter is added automatically.

Tools

Tool Purpose
recall(query, limit?) Best first call. Searches and returns the top matches (default 3) with their content in one step. Output is capped.
search_memory(query, limit?) Ranked FTS5 (bm25) results: path, description and a short preview
read_memory(path) Full markdown of one file. read_memory("/index.md") returns an index of every memory
list_memories(prefix?) Paths with descriptions, optionally under a prefix (capped at 100)
write_memory(path, description, body, tags?, source) Create or overwrite
append_memory(path, content, source) Append lines to an existing memory (atomic, size-checked)
delete_memory(path) Delete a memory
health_check() Server version, database status, memory count and latency (needs auth)
search(query) ChatGPT connector alias. Returns one text item: {"results":[{"id","title","url"}]}
fetch(id) ChatGPT connector alias. Returns one text item: {"id","title","text","url","metadata"}

/index.md is a reserved, auto-generated index (one - path — description line per memory, up to 200). It is computed on read, so it is never stale, and it can't be written or deleted. A model can read it once to find the right file instead of calling list and search repeatedly.

Token and latency design: the tool descriptions and server instructions are short, because they are sent in every chat. Results leave out timestamps, scores and metadata. recall strips frontmatter and truncates each hit (1500 characters, 6000 in total). Most lookups take one recall call.

For search and fetch, the id is the memory path. The server also sends MCP instructions telling clients to check memory at the start of a chat, and to store only durable facts the user stated, never secrets or passwords.

HTTP routes

Route Auth Description
POST /mcp, POST /mcp/<token> yes MCP endpoint. GET returns 405 because the server is stateless.
GET /export?format=json|zip yes Backup. JSON, or a zip of .md files plus a MEMORY.md index. /export/<token> also works.
GET /memory/<path> Bearer header only Raw markdown of one memory. This is the url used in search/fetch results.
GET /health no Returns ok

Limits: each memory body is capped at 64 KB (MAX_BODY_BYTES in wrangler.toml), each request at 512 KB, and every tool input is validated with zod.

Setup

Prerequisites: Node 20+ and a free Cloudflare account. Everything runs through npx wrangler, so you don't need a global install.

npm install
npx wrangler login

1. Create the D1 database

npx wrangler d1 create exchange

You don't need to copy the database_id anywhere. wrangler.toml binds the database by name (database_name = "exchange"), and Wrangler looks up the ID in your account when it deploys or runs remote migrations, so the ID never ends up in the repo. If you use a different name, change database_name to match. If Wrangler offers to write a database_id into wrangler.toml, discard that change.

2. Apply migrations

npm run db:migrate:local    # local dev database (.wrangler/)
npm run db:migrate:remote   # the real D1 database

3. Set the secret

Generate a long random token. Tokens shorter than 16 characters are rejected.

openssl rand -hex 32
npx wrangler secret put EXCHANGE_TOKEN      # paste the token when prompted

For local dev:

cp .dev.vars.example .dev.vars              # then put a token in .dev.vars

4. Local dev

npm run dev                                 # wrangler dev → http://localhost:8787
curl localhost:8787/health

Try it with the MCP Inspector (npx @modelcontextprotocol/inspector): choose transport "Streamable HTTP", use URL http://localhost:8787/mcp, and add the header Authorization: Bearer <token>.

5. Deploy

To deploy automatically from GitHub instead, see CI/CD with GitHub Actions.

npm run deploy                              # wrangler deploy

Your endpoint is https://exchange-mcp.<your-subdomain>.workers.dev/mcp.

CI/CD with GitHub Actions

.github/workflows/ci.yml runs the typecheck and tests on every pull request. On a push to the default branch (or a manual Run workflow) it also applies D1 migrations and deploys. The workflow holds no account details: everything comes from GitHub secrets, so it works unchanged in your fork.

1. One-time Cloudflare setup

Create the database once (the pipeline doesn't create it):

npx wrangler d1 create exchange

If you have never deployed a Worker on the account, also choose your workers.dev subdomain in the Cloudflare dashboard (Workers & Pages).

2. Create a Cloudflare API token

In the Cloudflare dashboard go to My Profile → API Tokens → Create Token. Start from the Edit Cloudflare Workers template, add the permission Account → D1 → Edit, and limit it to your account. Your account ID is shown on the Workers & Pages overview (or run npx wrangler whoami).

3. Add GitHub secrets

Repository Settings → Secrets and variables → Actions → New repository secret:

Secret Required Purpose
CLOUDFLARE_API_TOKEN yes Lets the workflow migrate and deploy
CLOUDFLARE_ACCOUNT_ID yes Which Cloudflare account to deploy to
EXCHANGE_TOKEN optional Your MCP access token (16+ characters, openssl rand -hex 32). If set, it is copied to the Worker on each deploy, overwriting the current value. If unset, the Worker's secret is left alone
WORKER_URL optional e.g. https://exchange-mcp.<your-subdomain>.workers.dev, for a /health check after deploy

Or with the GitHub CLI, which prompts for each value so it doesn't end up in your shell history:

gh secret set CLOUDFLARE_API_TOKEN
gh secret set CLOUDFLARE_ACCOUNT_ID
gh secret set EXCHANGE_TOKEN
gh secret set WORKER_URL

Without the two required secrets, the deploy job is skipped with a notice and tests still run.

If you never set EXCHANGE_TOKEN in GitHub, set it once with npx wrangler secret put EXCHANGE_TOKEN. Otherwise every request returns 401.

4. Deploy

Push or merge to the default branch, or use Actions → CI/CD → Run workflow.

Security notes

  • Logs of a public repository are public. Wrangler output is piped through scripts/redact.sh, which masks resource IDs, workers.dev hostnames and email addresses.
  • Pull requests, including those from forks, only run tests. They never deploy or receive your secrets.
  • Anyone who can push to the default branch can deploy and could read the secrets through a modified workflow. Protect the branch if others have write access.

Connecting AI clients

Use one of these two URL forms:

  • Header auth (preferred): https://…workers.dev/mcp, with Authorization: Bearer <token>
  • URL auth (for UIs without a headers field): https://…workers.dev/mcp/<token>

Treat the /mcp/<token> URL like a password. Anyone who has it can read and write your memories. To revoke it, rotate the secret with wrangler secret put EXCHANGE_TOKEN.

Menu names change often, and custom connectors may require a paid plan. The steps below describe the general flow.

Claude (claude.ai, Desktop, mobile)

  1. Go to Settings → Connectors → Add custom connector.
  2. Name: exchange. URL: https://…workers.dev/mcp/<token>.
  3. Leave the OAuth fields empty, then save. In a chat, enable the connector from the tools menu.

Claude Code:

claude mcp add --transport http exchange https://…workers.dev/mcp --header "Authorization: Bearer <token>"

ChatGPT

  1. Go to Settings → Apps & Connectors → Advanced and enable Developer mode.
  2. Create a connector. MCP server URL: https://…workers.dev/mcp/<token>. Authentication: No authentication, because the token is in the URL.
  3. ChatGPT uses search and fetch for deep research and connector search. In developer mode, every tool is available.

Gemini

Gemini CLI (~/.gemini/settings.json):

{
  "mcpServers": {
    "exchange": {
      "httpUrl": "https://…workers.dev/mcp",
      "headers": { "Authorization": "Bearer <token>" }
    }
  }
}

If your Gemini app or Workspace edition lets you add custom MCP connectors, use the same URL. Use /mcp/<token> if it has no headers field.

Any other MCP client

Point a Streamable HTTP client at /mcp with the Bearer header, or at /mcp/<token>.

Custom instruction

To make each assistant use the memory consistently, add this line to its custom instructions (Claude: profile preferences; ChatGPT: Customize ChatGPT; Gemini: saved info):

At the start of a chat, check exchange for relevant memory. Save durable facts there when I ask.

A leaner variant that uses fewer calls and tokens:

Only check exchange when the topic could depend on my preferences or past context: call recall once with 2-3 keywords, and don't list or read unless needed. Save durable facts there only when I ask.

Backup

curl -H "Authorization: Bearer $TOKEN" "https://…workers.dev/export" -o exchange.json
curl -H "Authorization: Bearer $TOKEN" "https://…workers.dev/export?format=zip" -o exchange.zip

Tests

npm test          # Vitest inside workerd (Miniflare) with a real local D1 and migrations applied
npm run typecheck

The tests cover the storage layer (CRUD, prefix listing, atomic append, FTS5 ranking, trigger sync, FTS injection), auth (bearer, path token, constant-time compare, fail-closed), every MCP tool (through an in-memory MCP client, including recall and the generated index), and the HTTP routes (401/404/405/413, stateless initialize, export JSON/zip).

Project layout

migrations/0001_init.sql   memories table, FTS5 index, sync triggers
src/index.ts               Worker router (/health, /mcp, /export, /memory)
src/auth.ts                Bearer / path-token auth, constant-time compare, OAuth TODO
src/mcp.ts                 MCP server factory and tool definitions
src/indexfile.ts           auto-generated /index.md
src/schemas.ts             zod schemas, path normalisation, size limits
src/export.ts              JSON / zip export
src/storage/types.ts       MemoryStore interface
src/storage/d1.ts          D1 implementation
.github/workflows/ci.yml   tests on PRs, deploy from the default branch
scripts/redact.sh          masks IDs and hostnames in CI logs
test/                      Vitest suites

Swapping storage

Everything outside src/storage/ depends only on the MemoryStore interface in src/storage/types.ts. To move to Postgres or Azure SQL:

  1. Implement MemoryStore. Use a tsvector/GIN index (Postgres) or a full-text index (Azure SQL) in place of FTS5.
  2. Construct it in src/index.ts instead of D1MemoryStore. From a Worker, Postgres can be reached through Hyperdrive.

Security notes

  • Tokens are compared in constant time: both values are SHA-256 hashed, then XOR-compared.
  • The server never logs tokens, request bodies or memory content. Unhandled errors log only the error class.
  • A URL-path token can still show up in platform request logs. That includes wrangler dev output and Cloudflare's request logs, if you enable them. Prefer the Bearer header wherever the client supports it.
  • Memories are meant for durable facts, not secrets. The tool descriptions tell every AI client this.
  • TODO (OAuth): add OAuth 2.1 per the MCP authorization spec, probably with @cloudflare/workers-oauth-provider. That would let Claude and ChatGPT run their native connector sign-in and give each client its own revocable token. See the TODO in src/auth.ts.

License

MIT

About

A universal memory server for AI assistants. Add it as a custom connector in Claude, ChatGPT, Gemini or any other MCP client, and any of them can save and recall memories. Something you discuss in one app can then be picked up in another.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages