OpenCode Integration
The MemoryLayer OpenCode plugin (@scitrera/memorylayer-opencode-plugin) gives OpenCode sessions long-term memory that survives context compaction and persists across runs. It pairs the MemoryLayer MCP server (28 tools in the default cc profile, 40 in the full profile) with proactive hooks tied to OpenCode’s session lifecycle.
What It Does
- MCP Tools — The same MCP server used by Claude Code, exposing remember/recall/reflect, session management and context packs, the context sandbox / RLM, chat threads, and read-only skills tools (the MCP-server registry tools are in the
fullprofile). - Proactive Hooks — Inject a bounded context pack at session start, recall memories on preference/convention questions, capture tool observations (file edits, searches, commands) as working memory, durably capture the raw transcript and commit working memory before context compaction, and clean up on exit.
- Slash Commands —
/memorylayer-remember,/memorylayer-recall,/memorylayer-status,/memorylayer-setup.
Prerequisites
The plugin requires a running MemoryLayer server:
pip install memorylayer-servermemorylayer serveThe default hash embedder works with no API keys. For real semantic recall, configure an embedding provider (openai, google, or a self-hosted memorylayer-embed-server). See Installation and Configuration.
Installation
npm install @scitrera/memorylayer-opencode-pluginThe plugin depends on @scitrera/memorylayer-mcp-server, and the configuration below also launches the MCP server on demand with npx, so you don’t need a separate install. Requires Node.js 18 or later.
Configuration
Add the MCP server and the plugin to your opencode.json:
{ "mcp": { "memorylayer": { "type": "local", "command": ["npx", "@scitrera/memorylayer-mcp-server"], "environment": { "MEMORYLAYER_URL": "{env:MEMORYLAYER_URL}", "MEMORYLAYER_API_KEY": "{env:MEMORYLAYER_API_KEY}" }, "enabled": true } }, "plugin": ["@scitrera/memorylayer-opencode-plugin"]}Optional environment overrides:
export MEMORYLAYER_URL=http://localhost:61001export MEMORYLAYER_API_KEY=your-keyexport MEMORYLAYER_WORKSPACE_ID=my-project # otherwise auto-detectedThe workspace ID is auto-detected from:
MEMORYLAYER_WORKSPACE_ID(explicit override)- Git remote origin repo name
- Git root directory name
- Current working directory name
Verifying
Start OpenCode and run:
/memorylayer-statusYou should see the configured URL, detected workspace ID, and the MCP tool count.
Slash Commands
| Command | Description |
|---|---|
/memorylayer-remember <content> | Store a memory with auto-detected type and importance |
/memorylayer-recall <query> | Search memories by semantic query |
/memorylayer-status | Check connection status and workspace info |
/memorylayer-setup | Guided setup and verification |
How the Hooks Work
| OpenCode hook | What the plugin does |
|---|---|
experimental.chat.system.transform | On first interaction, starts a server session and injects one bounded, deterministic context pack into the system prompt. Servers without context packs get the older briefing/directive/sandbox-state path instead. |
chat.message | Detects preference, recall, analysis, implementation, and error patterns in user messages and runs targeted recall before the LLM responds. |
tool.execute.before | Injects recalled context for edit/write and task/delegation tools. |
tool.execute.after | Silently captures observations (files, facts, concepts, intent) as working memory. |
experimental.session.compacting | When OpenCode supplies transcript or message data, uploads only the bytes after the last acknowledged boundary as idempotent raw checkpoints. It then commits working memory and checkpoints server-side sandbox state. Retries are bounded and failures are only logged, so MemoryLayer never blocks compaction. |
shell.env | Propagates MEMORYLAYER_URL and MEMORYLAYER_API_KEY to shell commands the agent runs. |
command.execute.before | Expands the memorylayer-* slash commands into instructions for the MCP tools. |
Architecture
OpenCode├── MCP Server (@scitrera/memorylayer-mcp-server)│ └── 28 memory tools by default (40 in `full` profile)│├── Plugin Hooks (@scitrera/memorylayer-opencode-plugin)│ ├── system.transform → context pack injection│ ├── chat.message → pattern-based recall│ ├── tool.before → pre-tool context injection│ ├── tool.after → observation capture│ ├── session.compacting → raw transcript checkpoint + working memory commit│ └── shell.env → env var propagation│└── MemoryLayer Server (memorylayer-server) ├── Memory storage (SQLite + sqlite-vec) ├── Knowledge graph (associations) ├── Working memory (sessions) └── Context sandbox (Python execution / RLM)Troubleshooting
Server not reachable
curl http://localhost:61001/healthmemorylayer serve # or run the Docker containerMCP tools not available
Verify opencode.json has the mcp.memorylayer block and restart OpenCode after configuration changes.
Plugin not loading
Ensure the plugin is listed in opencode.json:
{ "plugin": ["@scitrera/memorylayer-opencode-plugin"]}Related Pages
- MCP Server — the shared MCP server used by OpenCode, Claude Code, and Codex
- Claude Code — equivalent Claude Code plugin with the same memory model
- Sessions & Working Memory — how working memory is captured and committed
- Context Environment — the server-side sandbox that survives compaction