MastraMemory subclass for always-on memory and a pair of tools the model can call when it wants explicit control.
Runtime: Node.js 18+ only. The JS SDK wraps the Python SDK as a subprocess, so it needs Python 3.11+ on the host and does NOT run on Edge / Cloudflare Workers / Bun / Deno / Lambda-Node-only. On Next.js, pin the route to runtime = “nodejs”.
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
Setup
Install the package alongside Mastra:Import the integration from its package name directly:
import { SynapMemory } from "@maximem/synap-mastra". The core SDK (createClient) comes from the separate @maximem/synap-js-sdk package..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 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.
Method behavior:
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
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
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.
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.