connect, and a bundled capture plugin that observes the full session lifecycle and injects memory context directly into the system prompt.
Setup
1
Start agentmemory
2
Wire MCP
~/.config/opencode/, backs up the config, writes the entry below, and verifies it after writing.3
Install the capture plugin
Copy the bundled plugin and register it:Optionally add the slash commands:
~/.config/opencode/opencode.json
4
Restart OpenCode
Restart OpenCode or open a new session. The plugin auto-captures from the first turn.
5
Verify
Confirm the MCP server handshake first:It should show
✓ agentmemory connected. Then run one prompt, open http://localhost:3113 and check the Sessions tab, or:What connect writes
OpenCode does not use the standardmcpServers envelope. Its config is a top-level mcp key whose entries carry type, command as an array, and enabled:
~/.config/opencode/opencode.json
The entry deliberately carries no
environment block. OpenCode does not expand shell-style ${VAR:-default} values, so writing them would override your real shell AGENTMEMORY_URL with an unexpanded literal. The stdio child inherits the shell environment, and the shim defaults unset vars (URL to localhost:3111, no secret, all tools).connect also writes a memory-usage guideline block into ~/.config/opencode/AGENTS.md so the agent proactively calls the memory tools. Skip that with --no-guidelines.
What the capture plugin does
The plugin registers 22 hooks over the OpenCode SDK, covering session lifecycle (session.created, session.idle, session.compacted, session.deleted, session.error), messages (chat.message, message.updated, message.removed), tool parts (message.part.updated for subtasks, tool completions, tool errors, reasoning, patches, retries), permissions, todos, executed commands, and model parameters. Everything posts to the REST API as observations.
Project attribution is per-session: one OpenCode process spanning several repositories files each session under its own project.
Instead of a memory file, OpenCode gets direct injection. experimental.chat.system.transform fires before every LLM call and pushes two layers into output.system[]:
- Memory context from
POST /context, fetched once per session on the first turn, together with agentmemory usage instructions. - File enrichment from
POST /enrichon every file-touching turn, batching the files stashed bytool.execute.before,file.edited, and file parts.
session.deleted, mirroring Claude Code’s CONSOLIDATION_ENABLED=true behavior.
Slash commands: /recall <query> searches past observations and lessons, /remember <text> saves an insight.
Troubleshooting
Sessions appear but no context is injected
Sessions appear but no context is injected
Injection runs through
experimental.chat.system.transform. Confirm the plugin file is listed under the plugin key and the path is relative to the config directory. Restart OpenCode after any plugin edit; plugins load at startup.Connection refused on port 3111
Connection refused on port 3111
The agentmemory server is not running. Start it with
npx @agentmemory/agentmemory and re-run the prompt; the plugin fails open, so the session continues without capture until the server is reachable.src/cli/connect/opencode.ts. Plugin: plugin/opencode/agentmemory-capture.ts, full hook table in plugin/opencode/README.md.