Add persistent, per-user memory to a Mastra agent in TypeScript. The integration ships aDocumentation Index
Fetch the complete documentation index at: https://docs.maximem.ai/llms.txt
Use this file to discover all available pages before exploring further.
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 and persist memory automatically on every
generatecall - 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
| Export | Mastra interface | Purpose |
|---|---|---|
SynapMemory | MastraMemory subclass | Always-on memory: auto-recall + auto-store on every generate |
synapSearchTool | Tool factory | Returns a Mastra-compatible tool for explicit memory search |
synapStoreTool | Tool factory | Returns a Mastra-compatible tool for explicit memory storage |
Setup
Install the package alongside Mastra:.env
Basic integration
The smallest useful integration plugsSynapMemory into an Agent. Every generate call automatically pulls relevant memories into the prompt and writes the new turn back out:
SynapMemory is constructed — the model never sees the user identity. Memory reads degrade gracefully (empty context on failure); writes raise so silent data loss is impossible.
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 and overrides the storage layer to route through Synap:
fast | accurate | |
|---|---|---|
| Latency | 50-100ms | 200-500ms |
| Search | Vector similarity | Vector + graph + re-ranking |
| Best for | Real-time chat | Multi-entity queries |
| Method | Behavior |
|---|---|
remember(message) | Ingests a message into Synap |
recall(query, options) | Semantic search; returns MemoryMessage[] |
getMessages(threadId) | Retrieves the message thread from Synap |
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:synapSearchTool schema:
synapStoreTool schema:
Memory vs. tools
SynapMemory | Tools | |
|---|---|---|
| Context injection | Automatic on every generate | On-demand when model calls the tool |
| Memory storage | Automatic after every response | On-demand when model calls the tool |
| Best for | Always-on memory for every interaction | Agents that decide when memory matters |
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
All three exports accept the standard scoping triple —userId (required), optional customerId, optional conversationId. customerId is required on B2B Synap instances and ignored on single-tenant ones. 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:SynapMemory.recalldegrades gracefully — returns[]and logs on failure.SynapMemory.remembersurfaces failures — raisesSynapIntegrationError.synapSearchTooldegrades gracefully — returns[]and logs on failure.synapStoreToolsurfaces failures — raisesSynapIntegrationError.
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.