DEV Community

Rishi
Rishi

Posted on

Creating my first agent using Mastra

Building a Spiritual AI Agent with Mastra and Telex.im

In this blog post, I'll walk you through how I built a Bhagavad Gita AI agent using Mastra and integrated it with Telex.im - an AI agent platform similar to Zapier, but specifically designed as a Slack alternative for educational communities and bootcamps. Telex.im provides a powerful environment for deploying and managing AI agents while fostering community engagement and learning.

You can interact with my Gita Agent here: Gita Agent on Telex.im - it serves as your spiritual guide by providing verses and wisdom from the Bhagavad Gita, helping users explore ancient wisdom through modern AI technology.

The Vision & Implementation with Mastra

The idea was simple yet profound: create an AI agent that could make the timeless wisdom of the Bhagavad Gita accessible through modern technology. Users can request specific verses, get random daily inspiration, or ask for guidance on particular topics.

Key Mastra Features Utilized

  1. Tool System: Leveraged Mastra's tool architecture to create a custom Gita verse retrieval system
  2. Memory Management: Used Mastra's LibSQLStore for persistent conversation context
  3. Agent Framework: Utilized the built-in agent architecture for handling complex conversations
  4. A2A Protocol Integration: Implemented Mastra's agent-to-agent communication protocol
  5. Observability: Enabled Mastra's default observability for monitoring and debugging
  6. API Route Management: Used Mastra's route registration system for handling HTTP endpoints
  7. Type Safety: Leveraged Mastra's TypeScript integration for robust type checking

Leveraging Mastra's Features

Mastra's comprehensive feature set made it the perfect choice for this project. Here's how I utilized its key capabilities:

  1. Intelligent Agent System

    • Used Mastra's agent architecture for natural language understanding
    • Implemented custom instructions for Gita-specific knowledge
    • Leveraged the Google Gemini model integration
  2. Advanced Tool System

    • Created custom tools with Mastra's createTool utility
    • Implemented input/output schema validation using Zod
    • Utilized tool error handling and retry mechanisms
  3. Robust Memory Management

    • Implemented conversation persistence with LibSQLStore
    • Maintained context across user interactions
    • Enabled efficient conversation history retrieval
  4. API Integration Features

    • Used built-in JSON-RPC 2.0 support
    • Implemented A2A protocol handlers
    • Leveraged automatic OpenAPI documentation
  5. Development Features

    • Hot reload during development
    • Built-in TypeScript support
    • Automatic code bundling

Implementation Journey

1. Setting Up the Agent with Mastra

The core agent setup was straightforward with Mastra:

export const gitaAgent = new Agent({
  name: 'Gita Agent',
  instructions: `
    - You are a profound and knowledgeable assistant...
  `,
  model: 'google/gemini-2.0-flash',
  tools: { gitaTool },
  memory: new Memory({
    storage: new LibSQLStore({
      url: 'file:../mastra.db',
    }),
  }),
});
Enter fullscreen mode Exit fullscreen mode

2. Creating the Gita Tool

The most challenging part was implementing the verse retrieval tool. I needed to:

  • Handle both specific and random verse requests
  • Integrate with RapidAPI's Bhagavad Gita service
  • Manage verse boundaries for each chapter
  • Format responses consistently

3. A2A Protocol Integration

Telex.im integration required implementing the A2A protocol:

  • JSON-RPC 2.0 compliance
  • Proper message formatting
  • Error handling
  • Response validation

4. Challenges Faced

  1. Verse Randomization: Implementing proper randomization while respecting chapter verse limits.
  2. Error Handling: Gracefully handling API failures and invalid requests.
  3. Memory Management: Setting up persistent storage for conversation context.
  4. Response Formatting: Ensuring consistent output structure.

5. Key Learnings

  1. Mastra's tool system is incredibly flexible
  2. A2A protocol provides a solid foundation for agent communication
  3. TypeScript's type system helps catch errors early
  4. Proper error handling is crucial for production systems

Results and Impact

The agent successfully:

  • Handles various types of verse requests
  • Maintains conversation context
  • Provides meaningful spiritual guidance
  • Scales well under load

Code Structure

src/
  mastra/
    agents/
      gita-agent.ts    # Core agent definition
    tools/
      gita-tool.ts     # Verse retrieval implementation
    routes/
      a2a-agent-route.ts  # Telex.im integration
Enter fullscreen mode Exit fullscreen mode

Future Improvements

  1. Add verse commentary support
  2. Implement verse categorization by topic
  3. Add multi-language support
  4. Enhance conversation memory
  5. Include verse audio recitations

Conclusion

Building this agent demonstrated the power of combining modern AI tools with ancient wisdom. Mastra made it possible to focus on the unique aspects of the agent while handling the infrastructure complexities.

The full source code is available on GitHub, and you can try the agent on Telex.im. I'd love to hear your feedback and suggestions for improvements!

Mastra #AI #BhagavadGita #TelexIM #TypeScript

Top comments (0)