AI Coding Harnesses & IDE Setup · 8 min read

OpenCode MCP Setup: Shared Memory Across Subagents & Worktrees

Set up Memoreal MCP in OpenCode. Learn how automatic worktree path stripping allows parallel subagents in .worktrees/* to share a canonical repository namespace.

Direct Answer (TL;DR) Self-Contained GEO Passage

OpenCode powers high-throughput parallel subagent workflows across isolated git worktrees. By configuring Memoreal MCP, OpenCode subagents automatically strip ephemeral worktree path segments (such as .worktrees/feat-*), mapping all operations back to the canonical repository namespace. This allows up to 50 concurrent specialist workers (architect, reviewer, qa, security, ops) to share high-salience architectural invariants without memory silos.

5 Wings
Specialist agent wings supported (architect, reviewer, qa, security, ops)
OpenCode Multi-Agent Framework
100% Stripping
Worktree path normalization (.worktrees/feat-* mapped to canonical repo)
Memoreal Path Engine
50 Workers
Concurrent subagents executing parallel workstreams without memory silos
Memoreal Cloud Benchmarks
Table of Contents ↓

OpenCode Context Sharing: Standard Environment vs. Memoreal MCP Integration

OpenCode Context Sharing: Standard Environment vs. Memoreal MCP Integration
Architecture AspectStandard OpenCode SetupOpenCode + Memoreal MCPImpact on Parallel Orchestration
Worktree Memory ScopeFragmented per worktree folder pathAutomatically stripped to root repo slugAgents in .worktrees/feat-x access main repo knowledge
Multi-Agent HandoffsEphemeral text summaries in chatDedicated handoff_to_agent MCP toolArchitect passes high-salience invariants directly to QA
Belief ConsistencyProne to hallucination across forksConsolidated graph with Supersedes linksEnsures all subagents operate on identical truth
Token OverheadPasting multi-KB markdown per workerDeterministic token-budgeted build_contextDrastically lowers subagent context token consumption

The Architecture of OpenCode Multi-Agent Subagent Workflows

OpenCode is engineered specifically for multi-agent software engineering, orchestrating complex development pipelines across specialized agent wings including architect, reviewer, qa, security, and ops.

To ensure complete isolation and prevent branch contamination, OpenCode executes autonomous workstreams within temporary Git worktrees located under .worktrees/feat-*. However, in naive setups, this file-level isolation isolates agent memory as well, preventing workers from accessing knowledge discovered by sister agents.

  • OpenCode provisions ephemeral worktrees to allow parallel test execution and code changes.
  • Isolated file leases prevent concurrent workers from colliding on source code edits.
  • Without shared persistent memory, each worker is forced to rediscover project rules from scratch.

Automated Worktree Path Normalization and Canonical Namespaces

Memoreal solves the worktree isolation dilemma through automated path normalization. When an OpenCode subagent executes inside /Users/dev/repo/.worktrees/feat-billing/, Memoreal automatically strips the .worktrees/* segment from the path.

The memory engine resolves the underlying Git root and canonical manifest, binding the subagent directly to the project canonical namespace (e.g., repo-billing-service). Whether an agent is working in the root directory or in worktree number 42, it queries and updates the exact same consolidated cognitive memory graph.

  • Path normalization strips /.worktrees/*, /.claude/worktrees/*, and /.opencode/worktrees/*.
  • Canonical namespace binding eliminates fragmented, disconnected memory silos across branches.
  • Subagents can write memories during feature development that immediately ground QA agents.

Executing Multi-Agent Handoffs with handoff_to_agent

In addition to shared repository memory, Memoreal provides a specialized handoff_to_agent tool designed for multi-agent workflows. When an architect agent designs an implementation specification, it invokes handoff_to_agent to deposit a 0.9 salience memory into the destination wing namespace (e.g., wing:qa).

When the QA worker initializes in its own worktree, it reads its dedicated wing namespace, immediately discovering verified test criteria, modified file paths, and potential risk areas without consuming thousands of chat history tokens.

  • handoff_to_agent attaches file paths and prior memory UUIDs as searchable metadata context tags.
  • Target wings (architect, reviewer, qa, security, ops) maintain isolated, priority handoff queues.
  • High salience guarantees that critical handoff constraints are prioritized in downstream context.

Configuring Memoreal MCP in opencode.json for Parallel Subagents

json
{
  "mcp": {
    "servers": {
      "memoreal": {
        "type": "command",
        "command": "npx -y @kolivri/memoreal-mcp@latest",
        "environment": {
          "MEMOREAL_API_KEY": "{env:MEMOREAL_API_KEY}"
        }
      }
    }
  }
}

Frequently Asked Questions

How does Memoreal recognize that a process is running inside a Git worktree?

Memoreal inspects the current working directory for known worktree patterns like /.worktrees/* and reads the git common directory (.git/worktrees/*) to locate the canonical project root.

Can multiple parallel subagents write to Memoreal simultaneously without data races?

Yes, Memoreal is built with concurrent atomic operations and transactional belief revision, ensuring that parallel writes from multiple workers never corrupt the cognitive graph.

Does OpenCode require any special flags to pass environment variables to MCP servers?

In opencode.json, you can reference system environment variables using the {env:VARIABLE_NAME} interpolation syntax, which securely forwards your credentials to the MCP process.

60-Second Setup

Install Persistent Memory for Your Coding Agent

Sub-15ms deterministic recall, AST symbol navigation, and 70%+ token savings across Claude Code, Cursor, Codex CLI, and Windsurf.

npx -y @kolivri/memoreal-mcp setup
Start Free (1,000 Calls · $0 LLM Tax) Compare All Alternatives →