Skip to main content

Install

npm install @maximem/synap-mastra @mastra/core zod

What’s included

ExportPurpose
SynapMemoryExtends MastraMemory to persist agent memory in Synap
synapSearchToolFactory that returns a Mastra-compatible search tool
synapStoreToolFactory that returns a Mastra-compatible store tool

Quick start

import { Agent } from "@mastra/core";
import { SynapMemory, synapSearchTool, synapStoreTool } from "@maximem/synap-mastra";

const agent = new Agent({
  name: "MemoryAgent",
  instructions: "You are an agent with persistent memory. Use synapSearch to recall context and synapStore to remember new information.",
  model: openai("gpt-4o"),

  memory: new SynapMemory({
    sdk,
    userId: "alice",
    customerId: "acme",   // optional
  }),

  tools: {
    synapSearch: synapSearchTool({ sdk, userId: "alice", customerId: "acme" }),
    synapStore: synapStoreTool({ sdk, userId: "alice", customerId: "acme" }),
  },
});

const result = await agent.generate("What do you remember about my project deadlines?");
console.log(result.text);

SynapMemory

SynapMemory extends MastraMemory and overrides the storage layer:
  • remember(message) — ingests a message into Synap
  • recall(query, options) — semantic search over the user’s memories, returns MemoryMessage[]
  • getMessages(threadId) — retrieves the message thread from Synap
const memory = new SynapMemory({
  sdk,
  userId: "alice",
  customerId: "acme",      // optional — scopes to org memories
  conversationId: "t-001", // optional — scopes to session
  maxResults: 8,
  mode: "fast",            // "fast" | "accurate"
});

Tools

Use tools as an alternative or complement to SynapMemory. Tools give the model explicit control over when to search or store:
const agent = new Agent({
  tools: {
    synapSearch: synapSearchTool({
      sdk,
      userId: "alice",
      maxResults: 5,
      mode: "accurate",
    }),
    synapStore: synapStoreTool({
      sdk,
      userId: "alice",
    }),
  },
});
synapSearchTool — schema:
z.object({
  query: z.string().describe("What to search for in memory"),
  maxResults: z.number().optional().default(5),
})
synapStoreTool — schema:
z.object({
  content: z.string().describe("The information to remember"),
  memoryType: z.string().optional().default("fact"),
})

Memory vs. tools

SynapMemoryTools
Context injectionAutomatic on every generate callOn-demand when model calls the tool
Memory storageAutomatic after every responseOn-demand when model calls the tool
Best forAlways-on memory for every interactionAgents that decide when memory is relevant
Use both for maximum flexibility: SynapMemory handles automatic recall and synapStore lets the model bookmark new information explicitly.

Next steps

Vercel AI SDK

Model middleware for the Vercel AI SDK.

Claude Agent SDK

Hooks and MCP server for the Claude Agent SDK.