## Mastra Integration Overview

The [**Mastra**](https://mastra.ai/) integration demonstrates how to use Mastra’s agent system with Mem0 as the memory backend through custom tools. This enables agents to remember and recall information across conversations.

## Overview

In this guide, we’ll create a Mastra agent that:

1. Uses Mem0 to store information using a memory tool
2. Retrieves relevant memories using a search tool
3. Provides personalized responses based on past interactions
4. Maintains context across conversations and sessions

## Setup and Configuration

Install the required libraries:

```
npm install @mastra/core @mastra/mem0 @ai-sdk/openai zod
```

Set up your environment variables:

Remember to get the Mem0 API key from [Mem0 Platform](https://app.mem0.ai/?utm_source=oss&utm_medium=integration-mastra).

```
MEM0_API_KEY=your-mem0-api-key
OPENAI_API_KEY=your-openai-api-key
```

## Initialize Mem0 Integration

Import required modules and set up the Mem0 integration:

```
import { Mem0Integration } from '@mastra/mem0';
import { createTool } from '@mastra/core/tools';
import { Agent } from '@mastra/core/agent';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

// Initialize Mem0 integration
const mem0 = new Mem0Integration({
  config: {
    apiKey: process.env.MEM0_API_KEY || '',
    user_id: 'alice', // Unique user identifier
  },
});
```

## Create Memory Tools

Set up tools for memorizing and remembering information:

```javascript
// Tool for remembering saved memories
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 \
