Mastra
Attach a FishMem-backed memory to a Mastra Agent so it recalls and persists facts automatically across calls.
The @fishmem/mastra adapter exposes
FishMem as a Mastra memory provider. Pass it to a Mastra Agent and the agent
recalls relevant facts before each generation and persists the exchange after —
you don't manage the search/add loop yourself.
Install
npm install @mastra/core @fishmem/mastraThe adapter reads FISHMEM_API_KEY from the environment by default.
export FISHMEM_API_KEY="fm_..."Wire memory onto an Agent
createFishMemMemory returns a memory object that satisfies Mastra's memory
interface. Bind it to the agent's memory, and Mastra calls it on every
generate / stream.
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { createFishMemMemory } from "@fishmem/mastra";
export const supportAgent = new Agent({
name: "support-agent",
instructions:
"You are a support agent. Use the user's remembered context to give precise, personalized answers.",
model: openai("gpt-4o"),
memory: createFishMemMemory({ userId: "alex" }),
});Now each call recalls and persists automatically. Across separate calls the agent remembers what it learned earlier:
import { supportAgent } from "./agent";
// First call — FishMem extracts and stores the durable fact.
await supportAgent.generate("I'm on the Enterprise plan and my region is eu-west.");
// Later call, new process — the agent recalls the stored facts before answering.
const res = await supportAgent.generate("Which data region am I in?");
console.log(res.text); // references eu-west from memoryScoping
createFishMemMemory({ userId }) binds every recall and write to that
user_id. For multi-tenant apps, create the memory per request with the current
user so memories never cross between people. You can also pass agentId and
runId to narrow recall to a specific agent or session:
const memory = createFishMemMemory({
userId: currentUser.id,
agentId: "support-agent",
runId: conversationId, // optional: isolate to one session
});See scoping for how user_id, agent_id, and
run_id combine.