MemoryRouter for OpenAI Codex
Native Codex integration for durable MemoryRouter memory. It uses Codex's current MCP, lifecycle hooks, and AGENTS.md surfaces rather than pretending that model-selected MCP calls alone are automatic memory.
Release status: memoryrouter-codex is the published npm release. Its automatic capture is message-only: one user request plus the final assistant response, with no tool payloads.
What this integration does
On ordinary turns, automatically: recall before the prompt, save after the turn. What is saved on an ordinary turn is exactly two things, the user's message and the assistant's final response. Tool calls, tool output, reasoning, plans, intermediate assistant messages and subagent output are never saved.
| Layer |
Behavior |
UserPromptSubmit hook |
Searches your whole MemoryRouter vault with the prompt and injects what it finds as developer context for that turn. The vault is shared with Claude Code and every other MemoryRouter surface. Records the prompt for this turn. |
Stop hook |
Saves the user's message and the final assistant response for an ordinary turn, not an inbox review. Nothing else. |
SessionStart hook |
Restores context after Codex compacts a long chat (key mode also on new/resumed chats). |
SessionEnd (key mode) |
Only if the last turn never finished (interrupted), saves that turn's user message. A completed turn is never saved twice. |
| Remote MCP |
OAuth-based explicit search/store through https://mcp.memoryrouter.ai/mcp (search_memories, store_memory, ...). |
| Local MCP fallback |
API-key-authenticated stdio tools over the same whole vault. |
AGENTS.md |
MemoryRouter's universal "when to use memory" instructions (search before each new message, save useful details after it), then how they map to Codex's tool names and hooks. |
Two ways the hooks reach MemoryRouter (1.8.0):
- Over your MemoryRouter sign-in (no API key). Default when you signed in with OAuth (Codex app Settings > Plugins > Authenticate, or
codex mcp login memoryrouter) and no key is configured. The hooks are Codex mcp_tool hooks that call hidden lifecycle tools on the already-connected memoryrouter server (codex_turn_start, codex_turn_end, codex_session_start) with your OAuth token and the vault you picked when you signed in. Nothing secret is written to disk.
- Local runtime with a Memory Key. Used when a key is configured (
MEMORYROUTER_API_KEY, --api-key-stdin, or memoryrouter-claude's ~/.memoryrouter/config.json). Same per-turn behavior.
Force either with --hooks-via mcp or --hooks-via command.
1.8.0 removed the SubagentStop and PreCompact capture hooks: subagent output is intermediate work, and the PreCompact checkpoint re-saved a prompt the Stop hook already captured.
MemoryRouter is the cross-machine, cross-tool, team-shareable, and auditable layer: one memory across every session, repo and tool, shared by Codex, Claude Code, ChatGPT, and Claude. Codex's native Memories remain useful as an automated local personal recall layer.
Connections and Inbox
Ask to send the text you choose to a public vault ID. No contact or handshake is required. With a configured Memory Key, memoryrouter-codex send --to <vault-ID> --text-file note.txt sends the exact UTF-8 file to the destination inbox, not to its memory. Retrying the same file and destination reuses the same intent key; to send it again deliberately, supply a fresh --idempotency-key (16 to 128 characters).
A requested inbox check returns full sender content whether the sender is known or unknown. For an unknown authenticated sender the server supplies their ID, a warning and a prompt to choose a name private to this vault. A known sender's next message does not prompt for a name. The recipient independently chooses whether to save that specific message, with server-attributed sender and date. Neither naming nor reviewing writes the message to memory. Sender text is data, not instructions. No inbox polling hook or scope escalation is installed.
In local API-key mode MemoryRouter suppresses automatic capture and generic memory writes for inbox reviews. When a host omits turn identifiers, this can also suppress an already-running turn whose safety cannot be established. A new ordinary turn identified by its start hook can capture normally. Original inbox messages can still be explicitly saved with save_inbox_item.
Requirements and supported surfaces
- Node.js 18 or newer.
- Codex 0.145.0 or newer for the complete lifecycle-hook integration. Version 0.146.0 was tested directly.
- Local Codex CLI, ChatGPT desktop app's local Codex host, and Codex IDE extension share
config.toml MCP configuration.
- Hooks must be trusted once: in the Codex app open a New chat, click the hooks icon next to Ask for approval in the message box, then Allow all; in the CLI,
/hooks. OAuth (mcp_tool) hooks need a Codex build with MCP tool hooks (the current app and CLI 0.166 have them).
- Project
.codex config is loaded only for trusted projects.
- ChatGPT web does not read a laptop's
~/.codex/config.toml or local hooks. Use MemoryRouter's hosted plugin/connector surface there.
- Codex cloud environments do not automatically inherit host-local user config or secrets. A project-scope install can travel with a repository, but the environment still needs Node, project trust, package/network availability for the stdio fallback, and
MEMORYROUTER_API_KEY for hooks.
There is no dependency on the obsolete experimental_use_rmcp_client setting in current Codex. That flag was required by old Codex releases; it is absent from the current 0.146 configuration reference and is not written by this installer.
Install
Codex app (Mac): two steps, no key
-
Add the server in the app: Settings > Plugins > Add > Add MCP server, Type Streamable HTTP, Name memoryrouter, URL https://mcp.memoryrouter.ai/mcp, Save, then Authenticate and sign in (or create an account) in the browser tab that opens.
-
In a Codex chat, paste:
Run npx -y memoryrouter-codex@latest init with network access, then tell me to trust the MemoryRouter hooks.
Approve the internet access Codex asks for. Then open a New chat, click the hooks icon next to Ask for approval in the message box, and choose Allow all for the three MemoryRouter hooks (Recalling MemoryRouter context, Searching MemoryRouter, Saving to MemoryRouter).
init keeps the server the app added (and its sign-in), adds the three mcp_tool hooks to ~/.codex/hooks.json and the MemoryRouter guidance to ~/.codex/AGENTS.md. The Codex CLI and IDE extension on the same Mac share all of it.
Codex CLI, OAuth (no key)
npx -y memoryrouter-codex init --scope user
codex mcp login memoryrouter
npx -y memoryrouter-codex doctor --scope user
Then start Codex, trust the MemoryRouter hooks in /hooks, and check /mcp verbose.
With a Memory Key (local hook runtime)
Pass the key on stdin so it does not appear in shell history:
printf '%s' "$MEMORYROUTER_API_KEY" | \
npx -y memoryrouter-codex init --scope user --auth oauth --api-key-stdin
codex mcp login memoryrouter
memoryrouter-codex doctor --scope user
Then start Codex and inspect (trust the MemoryRouter hook definitions in /hooks):
/mcp verbose
/hooks
The API key is written to ~/.memoryrouter/codex/config.json with mode 0600; the directory is 0700. It is never written to config.toml, hooks.json, AGENTS.md, or a repository.
Already using MemoryRouter in Claude Code? When no Codex key is configured, the hooks reuse the memoryrouter-claude key in ~/.memoryrouter/config.json, so both agents use the same vault (one memory for every session, repo and tool) with no second paste. Key precedence: MEMORYROUTER_API_KEY, then the Codex private config, then the Claude Code config.
If you prefer not to persist the key, omit --api-key-stdin and ensure MEMORYROUTER_API_KEY is in the environment of the Codex host. Without any key, init installs the OAuth (mcp_tool) hooks instead, which work as soon as you are signed in.
Project scope
cd /path/to/repository
printf '%s' "$MEMORYROUTER_API_KEY" | \
npx -y memoryrouter-codex init --scope project --auth oauth --api-key-stdin
codex mcp login memoryrouter
This writes managed blocks/files under the repository's AGENTS.md and .codex/. The runtime contains no secret. Review which files your team wants to commit. Every machine or cloud environment must supply its own authentication.
Secure API-key fallback when OAuth is blocked
printf '%s' "$MEMORYROUTER_API_KEY" | \
npx -y memoryrouter-codex init --scope user --auth api-key --api-key-stdin
This uses a local stdio MCP server and the MemoryRouter API. It does not put the memory key in Codex config. The fallback tools are:
memory_search
memory_date_search
memory_inspect
memory_store
memory_status
memory_forget
For project scope, the stdio command is package-version-pinned through npx; secrets still come from the environment or private home config.
Exact configuration written
The remote mode adds a versioned managed block equivalent to:
[mcp_servers.memoryrouter]
url = "https://mcp.memoryrouter.ai/mcp"
auth = "oauth"
enabled = true
required = false
startup_timeout_sec = 20
tool_timeout_sec = 45
default_tools_approval_mode = "writes"
scopes = ["memories:read", "memories:write"]
[mcp_servers.memoryrouter.tools.store_memory]
approval_mode = "prompt"
oauth_resource is intentionally omitted: MemoryRouter's RFC 9728 metadata already supplies the exact resource. Codex 0.146 was observed duplicating the resource authorization parameter when the same value was also pinned in config.
The installer preserves unrelated TOML byte-for-byte outside its managed block. If an unmanaged [mcp_servers.memoryrouter] table already exists, it refuses to create invalid duplicate TOML. --force creates a timestamped backup, removes only that table and its nested MemoryRouter tool tables, and installs the managed block.
Hooks are merged into hooks.json; foreign hooks remain. Re-running init upgrades/replaces only MemoryRouter-owned handlers. uninstall removes only owned handlers and managed blocks.
One vault per user (shared with Claude Code and every MemoryRouter surface)
Since 1.7.0, every hook call (recall before each prompt, capture after each turn, compaction checkpoints, post-compaction recall) and every local MCP tool call reads and writes your whole vault. No session_id is sent, so there is no per-session or per-repository partition. That gives you:
- A new Codex session recalls earlier sessions, in this repository and every other one.
- Codex and Claude Code share one memory. A decision captured in Claude Code in repo A is recalled by Codex in repo B, and the other way round (requires
memoryrouter-claude 2.7.0 or newer on the same vault).
- Everything else on the vault is recalled too: saves from the hosted MCP (
store_memory from ChatGPT, Claude, or codex mcp login), consolidated reflections, the server's pending buffer, and Codex envelopes from any release. Only handles you suppressed locally with memory_forget are hidden.
Repository tag. Codex still computes the same ccp_ id as Claude Code:
ccp_<first 24 hex chars of SHA-256(canonical project identity)>
from, in order, an explicit id (MEMORYROUTER_PROJECT_ID or projectId in .memoryrouter.json), the normalized git remote, the git common directory, then the directory path. It is metadata only: Codex stamps it on the turns it saves and labels recalled entries from the current repository as this repo. It never limits what is recalled. Only the opaque hash leaves the machine.
One user, one vault. When you also connect MemoryRouter over OAuth (codex mcp login memoryrouter, ChatGPT, Claude), pick the same vault your hook key belongs to. init asks https://auth.memoryrouter.ai/v1/vault-binding whether the hook key is the vault your OAuth connections selected: on a mismatch it stops before writing anything and names the selected vault; with no connection yet it tells you which vault to pick at login. doctor reports the same one-vault check. The response never contains a key.
.memoryrouterignore (or enabled: false in .memoryrouter.json) still disables hooks for a repository. The pre-1.7 settings retrievalScope, includeGlobal, and memoryScope are ignored and removed from the private config.
Upgrading from 1.6.x. Releases before 1.7.0 used a per-Codex-session partition (mrcodex:<project>:<session>), so every new session started empty and Codex never saw Claude Code's memory. When 1.7.0 shipped, those partitions (and memoryrouter-claude's per-repository ccp_ partitions) were moved into each vault's main memory server-side, with stored embeddings kept, exact repeats skipped, and unflushed buffers saved, so old memories are recalled again with nothing lost or duplicated. Re-run init to upgrade the installed runtime.
Historical import (agent-driven)
Prior Codex conversations on this machine can be imported with a strict division of labor: the server owns the extraction prompt (versioned, fetched at import time), Codex itself owns discovery/parsing of its own session files, and this package owns transport. The agent never uploads; this CLI never parses history.
memoryrouter-codex import --instructions # print the server's extraction prompt
memoryrouter-codex import --instructions --run # or run it headless via codex exec
memoryrouter-codex import --file extract.jsonl # validate, preview, quote, approve, upload
memoryrouter-codex import --file extract.jsonl --yes # non-interactive approval
memoryrouter-codex import --dismiss # permanently silence the import nudge
Guarantees: the extract is validated locally (four fields per record, codex: prefixed ids, epoch-ms timestamps); the CLI shows record count, date range, estimated tokens, and the server price quote and requires explicit approval before any write; the import id is content-derived and every batch carries an Idempotency-Key, so re-running after any interruption replays committed batches for free and resumes. Duplicates are never re-billed. The API key never appears in the prompt or the extract file. When a vault is new, injected context includes a one-line import suggestion until an import completes or you --dismiss.
ChatGPT archive imports remain a separate flow (the memoryrouter ChatGPT CLI). Remote MCP OAuth connection by itself never imports history.
Handles, deduplication, retries, and forgetting