docs/integrations/codex.mdx
Add persistent memory to OpenAI Codex with the Mem0 plugin. Codex forgets everything between tasks — this plugin fixes that by connecting to Mem0's cloud memory layer via MCP and using a skill-based memory protocol to automatically retrieve context and store learnings.
Before setting up Mem0 with Codex, ensure you have:
A Mem0 Platform account and API key:
m0-)OpenAI Codex access
Your API key exported in your shell:
export MEM0_API_KEY="m0-your-api-key"
The fastest way to connect Codex to Mem0 — no downloads, no marketplace. Codex reads MCP servers from ~/.codex/config.toml as TOML. Add:
[mcp_servers.mem0]
url = "https://mcp.mem0.ai/mcp"
bearer_token_env_var = "MEM0_API_KEY"
Make sure MEM0_API_KEY is exported in the shell you launch Codex from, then restart Codex.
For the full plugin experience — MCP server plus the Mem0 SDK skill, memory protocol skill, and opt-in lifecycle hooks — sideload the plugin from a local clone. The Mem0 repo already ships a marketplace manifest at .agents/plugins/marketplace.json, so there's no JSON to author by hand. This follows the Codex build-plugins local-testing workflow.
Step 1. Clone the Mem0 repository anywhere on disk:
git clone https://github.com/mem0ai/mem0.git ~/codex-plugins/mem0-source
Step 2. Register the bundled marketplace with Codex's CLI:
codex plugin marketplace add ~/codex-plugins/mem0-source
This points Codex at the repo's .agents/plugins/marketplace.json. The bundled file uses path: "./mem0-plugin", which Codex resolves relative to the clone root.
Step 3. Restart Codex, run /plugins, browse the Mem0 Plugins marketplace, and install Mem0.
Step 4 (optional) — enable lifecycle hooks. Codex doesn't auto-wire hooks from plugin manifests; it only reads them from ~/.codex/hooks.json (or <repo>/.codex/hooks.json). Run the bundled installer once to merge the Mem0 entries into your global hooks file:
python3 ~/codex-plugins/mem0-source/mem0-plugin/scripts/install_codex_hooks.py
Then enable the hooks feature flag in ~/.codex/config.toml:
[features]
codex_hooks = true
Restart Codex. The installer registers three hooks pointing at scripts inside your clone:
| Event | Behavior |
|---|---|
SessionStart | Loads prior memories as bootstrap context |
UserPromptSubmit | Injects relevant memories before each prompt |
Stop | Reminds the agent to persist learnings at turn end |
Re-running the installer is idempotent. To remove the hooks: python3 ~/codex-plugins/mem0-source/mem0-plugin/scripts/install_codex_hooks.py --uninstall.
Codex provides CLI commands for managing marketplaces after install:
codex plugin marketplace upgrade # pull latest plugin versions
codex plugin marketplace remove mem0-plugins # unregister the marketplace
To pull updates to the plugin source itself, git pull inside your clone (~/codex-plugins/mem0-source) and then run codex plugin marketplace upgrade to refresh Codex's plugin cache. Plugins are cached at ~/.codex/plugins/cache/<marketplace>/<plugin>/<version>/.
| Component | Sideloaded Plugin | Direct MCP |
|---|---|---|
| MCP Server (9 memory tools) | Yes | Yes |
| Memory Protocol Skill | Yes | No |
| Mem0 SDK Skill | Yes | No |
| Lifecycle Hooks (opt-in) | Yes | No |
Once installed, the following tools are available in every Codex session:
| Tool | Description |
|---|---|
add_memory | Save text or conversation history for a user/agent |
search_memories | Semantic search across memories with filters |
get_memories | List memories with filters and pagination |
get_memory | Retrieve a specific memory by ID |
update_memory | Overwrite a memory's text by ID |
delete_memory | Delete a single memory by ID |
delete_all_memories | Bulk delete all memories in scope |
delete_entities | Delete a user/agent/app/run entity and its memories |
list_entities | List users/agents/apps/runs stored in Mem0 |
When the plugin is sideloaded, the memory protocol skill instructs the agent to:
search_memories with a query related to the current task to load relevant contextget_memories to browse all stored memoriesStore key learnings using add_memory with structured metadata:
| What to store | Metadata type |
|---|---|
| Architectural decisions | {"type": "decision"} |
| Strategies that worked | {"type": "task_learning"} |
| Failed approaches | {"type": "anti_pattern"} |
| User preferences observed | {"type": "user_preference"} |
| Environment discoveries | {"type": "environmental"} |
| Conventions established | {"type": "convention"} |
Store a comprehensive session summary including goals, accomplishments, decisions, files modified, and current state with metadata {"type": "session_state"}.
The Codex plugin manifest (.codex-plugin/plugin.json) follows the Codex plugin specification:
{
"name": "mem0",
"version": "0.1.0",
"description": "Mem0 memory layer for AI applications.",
"skills": "./skills/",
"mcpServers": "./.codex-mcp.json",
"interface": {
"displayName": "Mem0",
"shortDescription": "Persistent memory layer for AI coding workflows",
"category": "Productivity",
"capabilities": ["Read", "Write"]
}
}
# Task 1: Setting up a new service
You: Create a REST API for the notifications service using Express and TypeScript.
# Codex searches memories, finds user preferences from prior tasks.
# After completing the task, Mem0 stores:
# - Decision: "Notifications service uses Express + TypeScript + Zod validation"
# - Convention: "All API routes follow /api/v1/{resource} pattern"
# - Preference: "User prefers explicit error types over generic catch-all"
# Task 2 (days later): Extending the service
You: Add WebSocket support for real-time notification delivery.
# Codex searches memories, retrieves the architecture decisions and conventions.
# Follows the same patterns established in the first task.
MEM0_API_KEY is set in your shell: echo $MEM0_API_KEYmem0 MCP server / "tool collision" errors — You combined Option A (Direct MCP) with Option B (sideload). The sideloaded plugin auto-registers mem0 from .codex-mcp.json, so remove the [mcp_servers.mem0] block from ~/.codex/config.toml.plugin/read failed in TUI — Codex can't find the plugin directory the marketplace points at. If you used codex plugin marketplace add <path>, confirm the path is your clone root and that <clone>/.agents/plugins/marketplace.json exists. If you hand-authored ~/.agents/plugins/marketplace.json, source.path must be relative (start with ./), inside the marketplace root (~/ for personal installs), and end in mem0-plugin — e.g. "./codex-plugins/mem0-source/mem0-plugin"./plugins — Run codex plugin marketplace add ~/path/to/clone again, or confirm the marketplace was registered with codex plugin marketplace remove mem0-plugins then re-add.skills field in plugin.json points to a valid directory containing SKILL.md files.codex_hooks = true is in ~/.codex/config.toml under [features], and that ~/.codex/hooks.json contains the Mem0 entries (re-run the installer if not). Restart Codex after enabling the flag.~/.codex/hooks.json pointing at scripts inside your clone. If you moved or renamed the clone directory, run python3 <new-clone>/mem0-plugin/scripts/install_codex_hooks.py from the new location — the installer is idempotent and replaces the old entries.