MastraMemory subclass for always-on memory and a pair of tools the model can call when it wants explicit control.
Overview
This guide shows how to add Synap to a Mastra application to build agents that:- Recall long-term memory and persist each turn automatically on every
generateorstreamcall that carries a thread - Show the model Synap’s compaction in place of earlier turns once a conversation has been compacted
- Search and store memories on demand when the model decides it’s relevant
- Mix and match: use the memory class alone, the tools alone, or both together
Setup
Install the package alongside Mastra:Import the integration from its package name directly:
import { SynapMemory } from "@maximem/synap-mastra". The core SDK (SynapClient) comes from the separate @maximem/synap-js-sdk package..env
@ai-sdk/openai for the model.
Initialize the SDK once at application startup:
Basic integration
The smallest useful integration plugsSynapMemory into an Agent and passes a thread on each call. Every call then pulls relevant memories into the prompt and writes the new turn back out:
SynapMemory is constructed: the model never sees the user identity. A failed read degrades gracefully (no Synap context is added, Mastra’s own history stands). A failed turn write is logged, not thrown, so Synap being unreachable cannot end a run.
To let the model decide when to recall or store (rather than running on every turn), use the tools below.
Core concepts
SynapMemory
SynapMemory extends Mastra’s MastraMemory. Mastra’s Agent does not call a memory’s recall or saveMessages directly; it asks the memory for input and output processors and runs those around the model. SynapMemory supplies Synap’s:
The two retrieval modes trade latency against comprehensiveness:
fast is lower-latency and suited to the hot path; accurate adds LLM-driven query decomposition and reranking for relationship-aware queries at a higher latency cost.
What runs on a call that carries a thread:
Thread management, working memory, message deletion and thread cloning are not backed by Synap. Thread metadata is kept in process memory, working memory is a no-op that warns once, and
deleteMessages does nothing.
Short-term history
Once Synap has compacted a conversation, the model sees Synap’s compaction block in place of the earlier turns Mastra loaded, followed by the turns after the compaction. The current turn (the last user message onward) is untouched.- With a Mastra storage on the agent, Mastra’s own memory processors run first and load their history; Synap’s processor then replaces the turns before the current one with the compaction. Before the first compaction, or if Synap cannot answer, Mastra’s messages are left alone.
- Without a Mastra storage, Synap’s turns are the history: before the first compaction the model sees the recent turns Synap holds, and after it, the compaction plus the turns after it.
The gRPC stream
The first call opens the gRPC stream for you; you do not calllisten(). Once it is open, turns, tool calls, tool results and reasoning go on it, and anticipated context comes back into the SDK’s cache. The open waits up to 10 seconds. If it fails, turns are written over REST with conversation.record_message (tool calls and reasoning are dropped, and tool results stay in the SDK’s local archive), and the stream is tried once more a few seconds later, then no more than once a minute. It never throws into your agent loop. A stream your own code opened with sdk.instance.listen() is used as is.
To keep the integration on REST, set SYNAP_AUTO_LISTEN=false, or turn it off for one SDK:
SYNAP_GRPC_HOST and SYNAP_GRPC_PORT or the default; other HTTP/2 connections in your process behave as before.
synapSearchTool and synapStoreTool
The tool factories return Mastra-compatible tool objects with Zod schemas. They’re for agents that should decide when to query memory rather than running on every turn:sdk, userId, and optionally customerId, conversationId and mode. With a conversationId, the search is biased to that conversation.
synapSearchTool (tool id synap_search) input and output:
synapStoreTool (tool id synap_store) input and output:
Memory vs. tools
Use both for maximum coverage:
SynapMemory handles the always-on path and synapStoreTool lets the model bookmark new information explicitly when it sees something worth remembering.
Complete example: agent with memory + tools
The pattern below assembles all three exports. The agent has always-on memory viaSynapMemory AND can call the search/store tools when it decides to:
- Memory and tools are complementary, not redundant.
SynapMemoryhandles the always-on baseline; tools handle the explicit “I should look this up” path. - Scope is per-agent.
buildAgent(...)constructs a fresh agent per user, so each invocation has its scope baked in. - The instructions are the policy. Telling the model when to use
synapSearchandsynapStoreis what produces the explicit-memory behavior.
Advanced patterns
Multi-tenant scoping
SynapMemory and both tools take userId (required) and customerId (B2B instances only); the tools also take an optional conversationId, and for SynapMemory the conversation is the Mastra thread you pass on each call. customerId is required on B2B Synap instances and not accepted on B2C (single-tenant) ones, where a call carrying it is rejected with HTTP 400. See Memory Scopes.
Choosing between memory, tools, or both
SynapMemoryonly: always-on agents where every turn benefits from recall and ingestion.- Tools only: agents that should be selective about memory (e.g. research agents that should only remember important findings).
- Both: production agents where automatic recall sets the baseline and tools let the model dig deeper or bookmark explicitly.
Failure semantics
The integration follows the Synap-wide contract:SynapMemoryreads degrade gracefully: a failed context fetch or compaction lookup adds nothing and logs; Mastra’s own history stands.SynapMemoryturn writes never throw: a failed write is logged and the run goes on.synapSearchTooldegrades gracefully: returns{ formattedContext: "", available: false }and logs on failure.synapStoreToolsurfaces failures: throws anErrorthat beginssynap_store:, so the model sees that the write failed.
Going further
- Patterns overview: reusable memory patterns across frameworks.
- Cookbook overview: end-to-end worked examples.
Next steps
Vercel AI SDK
Model middleware for the Vercel AI SDK.
Claude Agent SDK
Hooks and MCP server for the Claude Agent SDK.
Context Fetch
The retrieval API behind
SynapMemory and synapSearchTool.Memory Scopes
How
userId, customerId, and conversationId interact across reads.