@fpytloun/opencode-mnemory

March 26, 2026 · View on GitHub

OpenCode plugin for mnemory — persistent AI memory with automatic recall, automatic capture, and 16 explicit memory tools.

No MCP server configuration needed. All memory tools are built into the plugin.

How It Works

PhaseHookAction
Session startsession.createdPre-fetches core memories and instructions from mnemory (non-blocking)
Each user messagechat.messageStarts a semantic search with the user's query (non-blocking)
Before each LLM callexperimental.chat.system.transformInjects instructions + core memories + search results into system prompt
After each exchangesession.idleExtracts the last user+assistant exchange and sends to mnemory for memory extraction (fire-and-forget)
On compactionexperimental.session.compactingPreserves core memories across context window compaction
After compactionsession.compactedResets state and re-fetches memories
Session cleanupsession.deletedCleans up session state

The LLM also has access to 16 memory tools for explicit operations (search, add, update, delete, artifacts).

Installation

Add to your opencode.json (project) or ~/.config/opencode/opencode.json (global):

{
  "$schema": "https://opencode.ai/config.json",
  "plugin": ["@fpytloun/opencode-mnemory"]
}

Set environment variables:

export MNEMORY_URL=http://localhost:8050
export MNEMORY_API_KEY=your-api-key  # if auth is enabled

From local files (for development)

# Global
cp integrations/opencode/*.ts ~/.config/opencode/plugins/

# Or project-level
cp integrations/opencode/*.ts .opencode/plugins/

Configuration

All configuration is via environment variables:

VariableDefaultDescription
MNEMORY_URLhttp://localhost:8050Mnemory server URL
MNEMORY_API_KEY(empty)Bearer token for authentication
MNEMORY_AGENT_IDopencodeAgent ID sent to mnemory
MNEMORY_USER_ID(empty)User ID (optional if API key maps to user)
MNEMORY_SCORE_THRESHOLD0.5Minimum relevance score for recalled memories (0.0-1.0)
MNEMORY_INCLUDE_ASSISTANTfalseInclude assistant messages in remember calls
MNEMORY_SEARCH_MODEsearchDefault search mode for subsequent turns: find (AI-powered) or search (fast vector)
MNEMORY_FIND_FIRSTtrueUse AI-powered search on the first turn of each session
MNEMORY_MANAGEDtrueInclude mnemory behavioral instructions in the system prompt
MNEMORY_TIMEOUT30000HTTP request timeout in milliseconds

Tools

The plugin registers 16 tools that the LLM can call for explicit memory operations:

ToolDescription
memory_searchSemantic search across memories
memory_findAI-powered multi-query search with LLM reranking
memory_askAsk a question and get a synthesized answer from memories
memory_addStore a new memory (auto-extracts facts, deduplicates)
memory_add_batchStore multiple memories in one call
memory_updateUpdate existing memory content or metadata
memory_deleteDelete a memory by ID
memory_delete_batchDelete multiple memories
memory_listList memories with optional filters
memory_categoriesList available predefined categories
memory_recentGet recent memories from last N days
memory_save_artifactAttach artifact (report, code, data) to a memory
memory_get_artifactRetrieve artifact content
memory_get_artifact_urlGenerate signed download URL for large/binary artifacts
memory_list_artifactsList artifacts attached to a memory
memory_delete_artifactDelete an artifact

Architecture

index.ts        Plugin entry point — wires hooks + tools
hooks.ts        Lifecycle hooks — auto-recall, auto-remember, compaction
tools.ts        16 custom tool definitions
client.ts       HTTP client for mnemory REST API
helpers.ts      Config, session store, escaping, text extraction

Two-Phase Recall

  1. Init recall (session.created): Pre-fetches instructions + core memories (no query). Cached for session lifetime.
  2. Per-turn search (chat.messagesystem.transform): On each user message, starts a search with the user's query. Results are awaited and injected before the LLM call.

First turn uses find mode (AI-powered multi-query search, higher quality). Subsequent turns use search mode (fast vector search, no LLM overhead). Configurable via MNEMORY_SEARCH_MODE and MNEMORY_FIND_FIRST.

Graceful Degradation

  • If the mnemory server is offline, the plugin logs a warning and the LLM works normally without memory context.
  • All API calls have timeouts and never throw — errors are logged via OpenCode's structured logging.
  • Per-turn search has an 8-second timeout in system.transform to avoid blocking the LLM call.

Troubleshooting

Memories not appearing?

  • Check that MNEMORY_URL is correct and the server is running
  • Look for mnemory: messages in OpenCode logs
  • Verify the API key is valid (if auth is enabled)

Search results not relevant?

  • Try lowering MNEMORY_SCORE_THRESHOLD (e.g., 0.3)
  • Use MNEMORY_FIND_FIRST=true for AI-powered search on the first turn

Too much latency on first turn?

  • Set MNEMORY_FIND_FIRST=false to use fast vector search on all turns
  • The init recall runs in the background and shouldn't add latency

Development

# Run tests
cd integrations/opencode
bun test

License

Apache 2.0