File size: 8,589 Bytes
c110c32 1136fac c110c32 1136fac c110c32 1136fac c110c32 99ae7fe c110c32 1136fac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
---
title: Gmail AI Agent with MCP Integration
emoji: π§
colorFrom: blue
colorTo: indigo
sdk: gradio
sdk_version: "4.19.2"
app_file: app.py
pinned: false
tags:
- agent-demo-track
- Agents-MCP-Hackathon
---
# π§ Gmail AI Agent with MCP Integration
> AI-powered email management using Qwen and Model Context Protocol (MCP)
**Track 3: Agentic Demo**
#agent-demo-track
*Note: A video overview of this application demonstrating its usage and purpose is available at: https://youtu.be/1K3YiV30DMU
## π Overview
This project combines Qwen AI with the Model Context Protocol (MCP) to create an intelligent email management system. It allows you to interact with your Gmail inbox using natural language, get AI-powered email summaries, and manage your emails through a beautiful web interface.
**Developed for the Gradio Agents & MCP Hackathon 2025**
## β¨ Features
### π¬ Email Management
- **Smart Filtering:** Find emails using Gmail search syntax or natural language
- **Advanced Search:** Filter by sender, subject, attachments, read status
- **Batch Processing:** Handle multiple emails at once
- **Intelligent Triage:** Automatically categorize and prioritize emails
### π€ AI Capabilities
- **Email Summarization:** Get concise overviews of your inbox
- **Natural Language Queries:** Ask questions about your emails in plain English
- **Pattern Detection:** Identify important trends and insights
- **Smart Responses:** Generate context-aware email replies
- **Actionable Insights:** Get recommendations based on email content
### π MCP Integration
- **Tool Integration:** Standard MCP tools for email operations
- **Resource Access:** Standard resources for inbox, unread, and recent emails
- **Protocol Support:** Full MCP protocol implementation
- **Client-Server Architecture:** Clean separation of concerns
## ποΈ Architecture
```
βββββββββββββββββββββββ MCP Protocol βββββββββββββββββββββββ
β βββββββββββββββββββββΊβ β
β Gradio Frontend β β Gmail MCP Server β
β (Port 7861) β β (stdio) β
β β β β
β + Qwen AI β β + Gmail API β
β + MCP Client β β + Email Processing β
β β β β
βββββββββββββββββββββββ βββββββββββββββββββββββ
β β
β β
βΌ βΌ
User Interface Gmail Account
+ AI Chat + Authentication
+ Email Management + Email Data
+ Tool Integration
```
## π Quick Start
### Prerequisites
- Python 3.8+ installed
- Gmail API credentials (`credentials.json`)
- Modal account for Qwen AI deployment
### Step 1: Install Dependencies
```bash
# Clone the repository
git clone <repository-url>
cd <repository-directory>
# Install requirements
pip install -r requirements.txt
```
### Step 2: Set Up Environment Variables
Create a `.env` file in the project directory:
```
# Gmail API Configuration
GMAIL_CLIENT_ID=your_gmail_client_id_here
GMAIL_CLIENT_SECRET=your_gmail_client_secret_here
GMAIL_TOKEN_PATH=token.json
# AI Configuration
MODAL_API_URL=your_modal_deployment_url_here
```
### Step 3: Run the Application
**Option 1: All-in-One (Recommended)**
```bash
# This automatically starts both MCP server and frontend
python project/enhanced.py
```
Open http://localhost:7861 in your browser
**Option 2: Run Separately**
Terminal 1 - Start the MCP server:
```bash
python project/gmail_mcp_server.py
```
Terminal 2 - Start the frontend:
```bash
python project/enhanced.py
```
## π§ Detailed Setup
### 1. Gmail API Setup
1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Create a project or select existing one
3. Enable the Gmail API
4. Create OAuth 2.0 credentials (Desktop application)
5. Download as `credentials.json` into project directory
### 2. Modal Setup for Qwen AI
1. Sign up for a [Modal](https://modal.com/) account
2. Deploy the LLM service using the provided `llm.py` file:
```bash
modal deploy project/llm.py
```
3. Copy the deployment URL to your `.env` file as `MODAL_API_URL`
### 3. First Run Authentication
When running the app for the first time:
1. The app will open a browser window for Gmail authentication
2. Allow the requested permissions
3. The app will create a `token.json` file for future sessions
## π User Guide
### π¬ AI Chat Examples
Try these in the AI Assistant tab:
- "Show me my unread emails"
- "Summarize emails from this week"
- "Find emails with attachments"
- "What are the main topics in my inbox?"
- "Any important emails I missed?"
### π Gmail Search Syntax
Use these in the Email Management tab:
- `is:unread` - Unread emails
- `from:sender@example.com` - From specific sender
- `subject:meeting` - Subject contains "meeting"
- `has:attachment` - Has attachments
- `newer_than:7d` - Newer than 7 days
- `older_than:1m` - Older than 1 month
### π Email Triage
The Email Triage feature provides AI-powered analysis:
- Automatic categorization of emails
- Priority scoring for each email
- Action recommendations
- Visual representation of email importance
- Workflow suggestions based on email content
## π οΈ MCP Tools Reference
### Available Tools
#### `fetch_emails`
Fetch emails with optional search query.
**Parameters:**
- `query` (string, optional): Gmail search query
- `max_results` (integer): Maximum number of emails (1-50)
#### `summarize_emails`
Generate email summary with statistics.
**Parameters:**
- `days` (integer): Number of days to analyze (1-30)
- `include_body` (boolean): Include email body previews
#### `search_emails`
Advanced email search with multiple filters.
**Parameters:**
- `from_address` (string): Filter by sender
- `subject_contains` (string): Filter by subject keywords
- `has_attachment` (boolean): Filter emails with attachments
- `is_unread` (boolean): Filter unread emails only
- `max_results` (integer): Maximum results
### Available Resources
- `gmail://inbox` - Access to Gmail inbox messages
- `gmail://unread` - Access to unread Gmail messages
- `gmail://recent` - Access to recent Gmail messages
## π Troubleshooting
### Common Issues
**"MCP Connection Failed"**
- Check if MCP server is running
- Verify network connectivity
- Ensure proper file paths
**"Gmail Authentication Failed"**
- Check that `credentials.json` exists and is valid
- Try removing `token.json` and re-authenticating
- Verify your Gmail account has API access enabled
**"Modal API Error"**
- Verify Modal deployment URL is correct in `.env` file
- Check for API rate limits or quotas
- Ensure connectivity to Modal's API servers
**"Import Errors"**
- Install all dependencies: `pip install -r requirements.txt`
- Check for compatible Python version (3.8+)
- Verify all required packages are installed
## π Project Information
### Technology Stack
- **Frontend:** Gradio (Modern UI Framework)
- **AI:** Qwen 3-4B (hosted on Modal)
- **Email API:** Gmail API
- **Protocol:** Model Context Protocol (MCP)
- **Language:** Python 3.8+
### Why MCP Matters
The Model Context Protocol (MCP) integration provides:
- Standardized tool calling for AI assistants
- Clean separation between client and server
- Reusable components across applications
- Future compatibility with other MCP clients
### Security Features
- OAuth 2.0 for secure Gmail access
- No password storage
- Token-based authentication
- Local credential storage
## π Additional Resources
- [MCP Documentation](https://github.com/modelcontextprotocol/mcp)
- [Modal Documentation](https://modal.com/docs)
- [Gmail API Reference](https://developers.google.com/gmail/api/reference/rest)
- [Gradio Documentation](https://www.gradio.app/docs)
## π Acknowledgements
- Model Context Protocol (MCP) for the protocol specification
- Modal for AI model hosting
- Google for Gmail API
- Gradio for the user interface framework
## π License
This project is licensed under the MIT License. |