Skip to main content
OpenCode gets two layers: MCP wiring through 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

The adapter detects ~/.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:
~/.config/opencode/opencode.json
Optionally add the slash commands:
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 standard mcpServers 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[]:
  1. Memory context from POST /context, fetched once per session on the first turn, together with agentmemory usage instructions.
  2. File enrichment from POST /enrich on every file-touching turn, batching the files stashed by tool.execute.before, file.edited, and file parts.
There is no MEMORY.md intermediary, so context is never stale. The plugin calls the consolidation pipeline on 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

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.
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.
Adapter source: src/cli/connect/opencode.ts. Plugin: plugin/opencode/agentmemory-capture.ts, full hook table in plugin/opencode/README.md.
Last modified on August 15, 2026