repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | --- |
| #2 | title: Mastra |
| #3 | --- |
| #4 | |
| #5 | 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. |
| #6 | |
| #7 | ## Overview |
| #8 | |
| #9 | In this guide, we'll create a Mastra agent that: |
| #10 | 1. Uses Mem0 to store information using a memory tool |
| #11 | 2. Retrieves relevant memories using a search tool |
| #12 | 3. Provides personalized responses based on past interactions |
| #13 | 4. Maintains context across conversations and sessions |
| #14 | |
| #15 | ## Setup and Configuration |
| #16 | |
| #17 | Install the required libraries: |
| #18 | |
| #19 | ```bash |
| #20 | npm install @mastra/core @mastra/mem0 @ai-sdk/openai zod |
| #21 | ``` |
| #22 | |
| #23 | Set up your environment variables: |
| #24 | |
| #25 | <Note>Remember to get the Mem0 API key from [Mem0 Platform](https://app.mem0.ai).</Note> |
| #26 | |
| #27 | ```bash |
| #28 | MEM0_API_KEY=your-mem0-api-key |
| #29 | OPENAI_API_KEY=your-openai-api-key |
| #30 | ``` |
| #31 | |
| #32 | ## Initialize Mem0 Integration |
| #33 | |
| #34 | Import required modules and set up the Mem0 integration: |
| #35 | |
| #36 | ```typescript |
| #37 | import { Mem0Integration } from '@mastra/mem0'; |
| #38 | import { createTool } from '@mastra/core/tools'; |
| #39 | import { Agent } from '@mastra/core/agent'; |
| #40 | import { openai } from '@ai-sdk/openai'; |
| #41 | import { z } from 'zod'; |
| #42 | |
| #43 | // Initialize Mem0 integration |
| #44 | const mem0 = new Mem0Integration({ |
| #45 | config: { |
| #46 | apiKey: process.env.MEM0_API_KEY || '', |
| #47 | user_id: 'alice', // Unique user identifier |
| #48 | }, |
| #49 | }); |
| #50 | ``` |
| #51 | |
| #52 | ## Create Memory Tools |
| #53 | |
| #54 | Set up tools for memorizing and remembering information: |
| #55 | |
| #56 | ```typescript |
| #57 | // Tool for remembering saved memories |
| #58 | const mem0RememberTool = createTool({ |
| #59 | id: 'Mem0-remember', |
| #60 | description: "Remember your agent memories that you've previously saved using the Mem0-memorize tool.", |
| #61 | inputSchema: z.object({ |
| #62 | question: z.string().describe('Question used to look up the answer in saved memories.'), |
| #63 | }), |
| #64 | outputSchema: z.object({ |
| #65 | answer: z.string().describe('Remembered answer'), |
| #66 | }), |
| #67 | execute: async ({ context }) => { |
| #68 | console.log(`Searching memory "${context.question}"`); |
| #69 | const memory = await mem0.searchMemory(context.question); |
| #70 | console.log(`\nFound memory "${memory}"\n`); |
| #71 | |
| #72 | return { |
| #73 | answer: memory, |
| #74 | }; |
| #75 | }, |
| #76 | }); |
| #77 | |
| #78 | // Tool for saving new memories |
| #79 | const mem0MemorizeTool = createTool({ |
| #80 | id: 'Mem0-memorize', |
| #81 | description: 'Save information to mem0 so you can remember it later using the Mem0-remember tool.', |
| #82 | inputSchema: z.object({ |
| #83 | statement: z.string().describe('A statement to save into memory'), |
| #84 | }), |
| #85 | execute: async ({ context }) => { |
| #86 | console.log(`\nCreating memory "${context.statement}"\n`); |
| #87 | // To reduce latency, memories can be saved async without blocking tool execution |
| #88 | void mem0.createMemory(context.statement).then(() => { |
| #89 | console.log(`\nMemory "${context.statement}" saved.\n`); |
| #90 | }); |
| #91 | return { success: true }; |
| #92 | }, |
| #93 | }); |
| #94 | ``` |
| #95 | |
| #96 | ## Create Mastra Agent |
| #97 | |
| #98 | Initialize an agent with memory tools and clear instructions: |
| #99 | |
| #100 | ```typescript |
| #101 | // Create an agent with memory tools |
| #102 | const mem0Agent = new Agent({ |
| #103 | name: 'Mem0 Agent', |
| #104 | instructions: ` |
| #105 | You are a helpful assistant that has the ability to memorize and remember facts using Mem0. |
| #106 | Use the Mem0-memorize tool to save important information that might be useful later. |
| #107 | Use the Mem0-remember tool to recall previously saved information when answering questions. |
| #108 | `, |
| #109 | model: openai('gpt-4.1-nano'), |
| #110 | tools: { mem0RememberTool, mem0MemorizeTool }, |
| #111 | }); |
| #112 | ``` |
| #113 | |
| #114 | |
| #115 | ## Key Features |
| #116 | |
| #117 | 1. **Tool-based Memory Control**: The agent decides when to save and retrieve information using specific tools |
| #118 | 2. **Semantic Search**: Mem0 finds relevant memories based on semantic similarity, not just exact matches |
| #119 | 3. **User-specific Memory Spaces**: Each user_id maintains separate memory contexts |
| #120 | 4. **Asynchronous Saving**: Memories are saved in the background to reduce response latency |
| #121 | 5. **Cross-conversation Persistence**: Memories persist across different conversation threads |
| #122 | 6. **Transparent Operations**: Memory operations are visible through tool usage |
| #123 | |
| #124 | ## Conclusion |
| #125 | |
| #126 | By integrating Mastra with Mem0, you can build intelligent agents that learn and remember information across conversations. The tool-based approach provides transparency and control over memory operations, making it easy to create personalized and context-aware AI experiences. |
| #127 | |
| #128 | <CardGroup cols={2}> |
| #129 | <Card title="Mastra Agent Cookbook" icon="star" href="/cookbooks/integrations/mastra-agent"> |
| #130 | Build a complete Mastra agent with persistent memory |
| #131 | </Card> |
| #132 | <Card title="Vercel AI SDK Integration" icon="triangle" href="/integrations/vercel-ai-sdk"> |
| #133 | Create web applications with Vercel AI SDK |
| #134 | </Card> |
| #135 | </CardGroup> |
| #136 | |
| #137 |