Skip to main content
Add persistent, per-user memory to a Mastra agent in TypeScript. The integration ships a MastraMemory subclass for always-on memory and a pair of tools the model can call when it wants explicit control.
@maximem/synap-mastra wraps the JavaScript SDK, which in turn wraps the Python SDK as a subprocess. It requires a Python 3.11+ runtime on the host. Edge Runtime, Cloudflare Workers, Bun, Deno Deploy, and AWS Lambda Node-only runtimes are not supported. See Installation → JavaScript / TypeScript SDK.
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 generate call
  • 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
The Synap Mastra integration ships three exports: one memory class and two tool factories.

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.
Configure your API key. Generate one from the Synap Dashboard.
.env
Initialize the SDK once at application startup:
See SDK Initialization for the full lifecycle and configuration options.

Basic integration

The smallest useful integration plugs SynapMemory into an Agent. Every generate call automatically pulls relevant memories into the prompt and writes the new turn back out:
The scoping triple is bound when 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:
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. 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 via SynapMemory AND can call the search/store tools when it decides to:
Three things to notice in this pattern:
  1. Memory and tools are complementary, not redundant. SynapMemory handles the always-on baseline; tools handle the explicit “I should look this up” path.
  2. Scope is per-agent. buildAgent(...) constructs a fresh agent per user, so each invocation has its scope baked in.
  3. The instructions are the policy. Telling the model when to use synapSearch and synapStore is 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.
For multi-tenant services, build agents per request rather than caching them; each agent should have its scope baked in.

Choosing between memory, tools, or both

  • SynapMemory only: 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.recall degrades gracefully: returns [] and logs on failure.
  • SynapMemory.remember surfaces failures: raises SynapIntegrationError.
  • synapSearchTool degrades gracefully: returns [] and logs on failure.
  • synapStoreTool surfaces failures: raises SynapIntegrationError.
This is by design: read failures shouldn’t break a user-facing turn, but silent write failures would let the memory drift away from reality.

Going further


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.