Blog

Building a Personal Assistant with Mastra and MCP

Personal Assistant Telegram Tweet demo

At Mastra, we are all-in on MCP. So we built a personal assistant that can help with various tasks like managing emails, monitoring GitHub activity, scheduling social media posts, and providing weather information. What made this project particularly interesting was how little code it took to build a powerful assistant with access to multiple tools and services.

The Power of MCP

The key to building this assistant quickly was leveraging Mastra agents with Model Context Protocol (MCP). MCP made it incredibly easy to integrate with various services:

  • Zapier MCP: For Gmail and Typefully integration
  • Composio MCP: For GitHub activity monitoring
  • HackerNews MCP: A community-built server from npm
  • TextEditor MCP: For local filesystem access

You can find more MCP servers using our MCP Registry Registry.

You can easily connect all your MCP servers using Mastra's MCP package. Here's how we set up the MCP configuration:

const mcp = new MCPConfiguration({
  servers: {
    zapier: {
      url: new URL(process.env.ZAPIER_MCP_URL || ""),
    },
    github: {
      url: new URL(process.env.COMPOSIO_MCP_GITHUB || ""),
    },
    hackernews: {
      command: "npx",
      args: ["-y", "@devabdultech/hn-mcp-server"],
    },
    textEditor: {
      command: "pnpx",
      args: [
        `@modelcontextprotocol/server-filesystem`,
        path.join(process.cwd(), "../", "../", "notes"),
      ],
    },
  },
});

This configuration gave our assistant access to all the tools it needed with minimal setup code.

The Agent's Instructions

The heart of our assistant is its system prompt, which clearly defines its capabilities and how to use each tool:

export const personalAssistantAgent = new Agent({
  name: "Personal Assistant",
  instructions: `
      You are a helpful personal assistant that can help with various tasks such as email, 
      monitoring github activity, scheduling social media posts and providing weather information.
      
      You have access to the following tools:
      
      1. Gmail:
         - Use these tools for reading and categorizing emails from Gmail
         - You can categorize emails by priority, identify action items, and summarize content
         - You can also use this tool to send emails
      
      2. GitHub:
         - Use these tools for monitoring and summarizing GitHub activity
         - You can summarize recent commits, pull requests, issues, and development patterns
      
      3. Typefully:
         - Use these tools for 
         - It can also create and manage tweet drafts with Typefully
         - It focuses on AI, Javascript, Typescript, and Science topics
      
      4. Weather:
         - Use this tool for getting weather information for specific locations
         - It can provide details like temperature, humidity, wind conditions, and weather conditions
         - Always ask for the location or if it's not provided try to use your working memory 
           to get the user's last requested location

      5. Hackernews:
         - Use this tool to search for stories on Hackernews
         - You can use it to get the top stories or specific stories
         - You can use it to retrieve comments for stories

      6. Filesystem:
         - You also have filesystem read/write access to a notes directory. 
         - You can use that to store information such as reminders for later use or organize info for the user.
         - You can use this notes directory to keep track of to do list items for the user.
  `,
  model: openai("gpt-4o"),
  tools: { ...mcpTools, weatherTool },
  memory,
});

Agent Memory Management

We leveraged Mastra's memory system to make the assistant more context-aware and personalized:

const memory = new Memory({
  options: {
    // Keep last 20 messages in context
    lastMessages: 20,
    // Enable semantic search to find relevant past conversations
    semanticRecall: {
      topK: 3,
      messageRange: {
        before: 2,
        after: 1,
      },
    },
    // Enable working memory to remember user information
    workingMemory: {
      enabled: true,
      template: `<user>
          <first_name></first_name>
          <username></username>
          <preferences></preferences>
          <interests></interests>
          <conversation_style></conversation_style>
        </user>`,
      use: "tool-call",
    },
  },
});

This configuration allows the assistant to:

  • Maintain conversation history
  • Find relevant past interactions using semantic search
  • Remember user preferences and information
  • Provide more personalized responses

Telegram Integration

To make the assistant accessible from anywhere, we built a Telegram bot interface. The implementation handles message streaming, markdown formatting, and proper error handling. You can find the full Telegram integration code here.

Key Learnings

  1. MCP Simplifies Integration: Using MCP servers dramatically reduced the amount of code needed to integrate with external services. Each service required just a few lines of configuration.

  2. Clear Instructions Matter: The system prompt played a crucial role in ensuring the assistant used tools appropriately. By clearly defining each tool's purpose and usage guidelines, we got more reliable results.

  3. Memory Enhances Experience: The memory system made the assistant feel more personalized and context-aware. It could reference past conversations and maintain user preferences.

  4. Telegram as an Interface: Using Telegram as the interface made the assistant accessible from anywhere while keeping the core logic running locally. This approach provided a great balance of accessibility and privacy.

Try It Yourself

The complete code for this personal assistant is available on GitHub. You can use it as a starting point to build your own assistant with the tools and services you need.

We're excited to see what people build using Mastra Agents + MCP.

Share

Stay up to date