This project demonstrates how to integrate Gmail using Klavis MCP and Mastra agents. It provides a dynamic agent setup for Gmail automation and management, with user-specific server instances managed in-memory (for demo purposes).
- Prerequisites
- Installation
- Environment Setup
- Project Structure
- Running the Agent
- Gmail Authorization
- Troubleshooting
- Notes
- Node.js (v16 or later recommended)
- npm (comes with Node.js) or yarn
- Access to Klavis and a valid KLAVIS_API_KEY
- Access to OpenAI and a valid OPENAI_API_KEY
- Clone the repository:
git clone https://github.com/aniket-provue/Gmail-Integration.git cd Gmail-Integration/integrations - Install dependencies:
npm install # or yarn install
- Create a
.envfile in theintegrationsdirectory (same level aspackage.json). - Add the following variables:
KLAVIS_API_KEY=your_klavis_api_key_here OPENAI_API_KEY=your_openai_api_key_here
KLAVIS_API_KEY: Your Klavis API key (required for MCP server and agent creation).OPENAI_API_KEY: Your OpenAI API key (required for the agent's language model).
src/mastra/agents/gmail-mcp-user.ts: Manages user-specific Gmail MCP server instances (in-memory).src/mastra/agents/gmail-mcp-agent.ts: Dynamically creates a Gmail MCP Agent using Klavis.src/mastra/index.ts: Main entry point for handling user Gmail requests and agent orchestration.
- Start the project:
npm run dev # or npm run start - Follow the console output. You should see a message like:
[Gmail MCP Agent] Authorize at: https://klavis.ai/oauth/...
- Open the provided OAuth URL in your browser.
- Complete the Gmail OAuth flow to authorize the agent.
- After authorization, the agent will be able to access Gmail features via Klavis MCP.
- Dependencies not found: Run
npm installagain. - TypeScript errors: Ensure TypeScript is installed (
npm install -g typescript). - Environment variables not loaded: Double-check your
.envfile and variable names. - In-memory storage: User state is lost on server restart; re-authorization will be required.
- Security: Never commit your
.envfile or API keys to GitHub. - Persistence: For production, replace the in-memory store in
gmail-mcp-user.tswith a database. - Customization: Extend the agent or add new features as needed.
For questions or issues, please open an issue on the repository or contact the maintainer.