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 builds on the JavaScript SDK and needs Node.js 20+ and @mastra/core 1.0 or later. Use 0.1.4 or later: it accepts a SynapClient directly under strict TypeScript, and its memory lookups no longer send an empty query that the SDK refuses. Context and memory operations also run on Edge Runtime and Cloudflare Workers; the gRPC stream opens only on Node.js, and elsewhere turns go over REST. See Installation → JavaScript and TypeScript SDK.

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 generate or stream call 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
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 (SynapClient) comes from the separate @maximem/synap-js-sdk package.
Configure your API key. Generate one from the Synap Dashboard.
.env
The examples also use @ai-sdk/openai for the model. 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 and passes a thread on each call. Every call then pulls relevant memories into the prompt and writes the new turn back out:
SynapMemory does nothing on a call that has no memory: { thread, resource } option. Mastra gives a memory its thread only through that option, and the thread id is the Synap conversation id. Reuse the same thread id for every message of one conversation, and it must be a valid UUID.
The scoping is bound when 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 call listen(). 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:
An open stream does not keep a Node.js script alive: the process exits when its work is done. This applies only to the connection to Synap, at the address in 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:
Both factories take 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 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

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.
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 reads degrade gracefully: a failed context fetch or compaction lookup adds nothing and logs; Mastra’s own history stands.
  • SynapMemory turn writes never throw: a failed write is logged and the run goes on.
  • synapSearchTool degrades gracefully: returns { formattedContext: "", available: false } and logs on failure.
  • synapStoreTool surfaces failures: throws an Error that begins synap_store:, so the model sees that the write failed.
Read failures shouldn’t break a user-facing turn. An explicit store is different: the model asked for it, so a failure is reported back to it.

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.