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 (25 tools by default, 38 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, the context sandbox / RLM, chat threads, and the workspace skills + MCP-server registry helpers.
- Proactive Hooks — Inject workspace briefings and user directives at session start, recall memories on preference/convention questions, capture tool observations (file edits, searches, commands) as working memory, 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. See Installation for embedding-provider options (embed_server peer, openai, google, or mock):
pip install "memorylayer-server[openai]"export MEMORYLAYER_EMBEDDING_PROVIDER=openaiexport MEMORYLAYER_EMBEDDING_OPENAI_API_KEY=sk-...memorylayer serveInstallation
npm install @scitrera/memorylayer-opencode-pluginThe plugin pulls in @scitrera/memorylayer-mcp-server as a dependency, so you do not need to install it separately.
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, loads workspace briefing, user directives, and any existing sandbox state into the system prompt. |
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 | Commits working memory to long-term storage and checkpoints server-side sandbox state before the context window is trimmed. |
shell.env | Propagates MEMORYLAYER_URL and MEMORYLAYER_API_KEY to shell commands the agent runs. |
Architecture
OpenCode├── MCP Server (@scitrera/memorylayer-mcp-server)│ └── 25 memory tools by default (38 in `full` profile)│├── Plugin Hooks (@scitrera/memorylayer-opencode-plugin)│ ├── system.transform → session briefing injection│ ├── chat.message → pattern-based recall│ ├── tool.before → pre-tool context injection│ ├── tool.after → observation capture│ ├── session.compacting → 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 both OpenCode and Claude Code
- 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