Skip to content

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 full profile).
  • 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:

Terminal window
pip install memorylayer-server
memorylayer serve

The 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

Terminal window
npm install @scitrera/memorylayer-opencode-plugin

The 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:

Terminal window
export MEMORYLAYER_URL=http://localhost:61001
export MEMORYLAYER_API_KEY=your-key
export MEMORYLAYER_WORKSPACE_ID=my-project # otherwise auto-detected

The workspace ID is auto-detected from:

  1. MEMORYLAYER_WORKSPACE_ID (explicit override)
  2. Git remote origin repo name
  3. Git root directory name
  4. Current working directory name

Verifying

Start OpenCode and run:

/memorylayer-status

You should see the configured URL, detected workspace ID, and the MCP tool count.

Slash Commands

CommandDescription
/memorylayer-remember <content>Store a memory with auto-detected type and importance
/memorylayer-recall <query>Search memories by semantic query
/memorylayer-statusCheck connection status and workspace info
/memorylayer-setupGuided setup and verification

How the Hooks Work

OpenCode hookWhat the plugin does
experimental.chat.system.transformOn 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.messageDetects preference, recall, analysis, implementation, and error patterns in user messages and runs targeted recall before the LLM responds.
tool.execute.beforeInjects recalled context for edit/write and task/delegation tools.
tool.execute.afterSilently captures observations (files, facts, concepts, intent) as working memory.
experimental.session.compactingWhen 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.envPropagates MEMORYLAYER_URL and MEMORYLAYER_API_KEY to shell commands the agent runs.
command.execute.beforeExpands 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

Terminal window
curl http://localhost:61001/health
memorylayer serve # or run the Docker container

MCP 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"]
}