Persistent Mastra Agents - Mem0

Overview

This guide will show you how to integrate Mem0 with Mastra to add long-term memory capabilities to your agents. We’ll create tools that allow agents to save and retrieve memories using Mem0’s API.

Installation

Install the Integration Package
To install the Mem0 integration, run:

npm install @mastra/mem0

Add the Integration to Your Project
Create a new file for your integrations and import the integration:

integrations/index.ts

import { Mem0Integration } from "@mastra/mem0";

export const mem0 = new Mem0Integration({
  config: {
    apiKey: process.env.MEM0_API_KEY!,
    userId: "alice",
  },
});

Use the Integration in Tools or Workflows
You can now use the integration when defining tools for your agents or in workflows.

tools/index.ts

import { createTool } from "@mastra/core";
import { z } from "zod";
import { mem0 } from "../integrations";

export const mem0RememberTool = createTool({
  id: "Mem0-remember",
  description:
    "Remember your agent memories that you've previously saved using the Mem0-memorize tool.",
  inputSchema: z.object({
    question: z
      .string()
      .describe("Question used to look up the answer in saved memories."),
  }),
  outputSchema: z.object({
    answer: z.string().describe("Remembered answer"),
  }),
  execute: async ({ context }) => {
    console.log(`Searching memory \