This document explains how to set up and use the Gmail integration in your multi-agent supervisor system.
The Gmail integration provides the following capabilities:
- Send emails with subject, body, CC, and BCC
- Read emails by ID with full content extraction
- Search emails using Gmail search syntax
- Get recent emails with filtering options
- Reply to emails with automatic threading
- Forward emails to other recipients
- Delete emails by ID
- Mark emails as read/unread
- Get all Gmail labels for organization
- Filter emails by labels
- Gmail Agent with natural language processing
- Conversational interface for email management
- Context-aware responses with memory
- Google Cloud Project with Gmail API enabled
- OAuth 2.0 credentials for Gmail API access
- Access token with Gmail API scopes
https://www.googleapis.com/auth/gmail.send
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/gmail.labels
Add the following to your .env file:
# Gmail API
GOOGLE_GMAIL_TOKEN=your_gmail_access_token_here- Go to Google Cloud Console
- Create a new project or select existing one
- Enable Gmail API
- Create OAuth 2.0 credentials
- Download the credentials JSON file
- Use Google's OAuth 2.0 playground or a script to get access token
- Visit Google OAuth 2.0 Playground
- Click the settings icon (⚙️) in the top right
- Check "Use your own OAuth credentials"
- Enter your OAuth 2.0 client ID and client secret
- Close settings
- Select Gmail API v1 from the list
- Select the required scopes:
https://www.googleapis.com/auth/gmail.sendhttps://www.googleapis.com/auth/gmail.readonlyhttps://www.googleapis.com/auth/gmail.modifyhttps://www.googleapis.com/auth/gmail.labels
- Click "Authorize APIs"
- Click "Exchange authorization code for tokens"
- Copy the access token to your
.envfile
Run the test script to verify everything is working:
python test_gmail.pyfrom app.agents.gmail_agent import run_gmail_agent
# Send an email
response = run_gmail_agent("Send an email to john@example.com with subject 'Meeting' and body 'Hi John, let's meet tomorrow.'")
# Get recent emails
response = run_gmail_agent("Get my recent emails")
# Search for emails
response = run_gmail_agent("Search for emails from alice@company.com")
# Reply to an email
response = run_gmail_agent("Reply to email ID 12345 with message 'Thanks for the update!'")from app.tools.gmail_tool import send_email_tool, get_emails_tool
# Send email
result = send_email_tool.invoke({
"to": "recipient@example.com",
"subject": "Test Email",
"body": "This is a test email"
})
# Get emails
result = get_emails_tool.invoke({
"max_results": 10,
"query": "is:unread"
})# Send email
curl -X POST "http://localhost:8000/api/gmail/send" \
-H "Content-Type: application/json" \
-d '{
"to": "recipient@example.com",
"subject": "Test Email",
"body": "This is a test email"
}'
# Get emails
curl -X POST "http://localhost:8000/api/gmail/get" \
-H "Content-Type: application/json" \
-d '{
"max_results": 10
}'
# Search emails
curl -X POST "http://localhost:8000/api/gmail/search" \
-H "Content-Type: application/json" \
-d '{
"query": "from:john@example.com",
"max_results": 5
}'Send an email
{
"to": "recipient@example.com",
"subject": "Email Subject",
"body": "Email body content",
"cc": "cc@example.com",
"bcc": "bcc@example.com"
}Get emails with optional filtering
{
"query": "is:unread",
"max_results": 10,
"label": "INBOX"
}Read a specific email by ID
{
"email_id": "email_id_here"
}Search emails using Gmail search syntax
{
"query": "from:john@example.com subject:meeting",
"max_results": 10
}Delete an email by ID
{
"email_id": "email_id_here"
}Reply to an email
{
"email_id": "email_id_here",
"reply_body": "Your reply message"
}Forward an email
{
"email_id": "email_id_here",
"forward_to": "newrecipient@example.com",
"additional_message": "Optional additional message"
}Get all Gmail labels (no body required)
Mark an email as read
{
"email_id": "email_id_here"
}Mark an email as unread
{
"email_id": "email_id_here"
}The Gmail integration supports Gmail's powerful search syntax:
from:john@example.com- Emails from specific senderto:alice@company.com- Emails sent to specific recipientsubject:meeting- Emails with "meeting" in subjectis:unread- Unread emailsis:read- Read emailshas:attachment- Emails with attachmentslabel:important- Emails with specific labelafter:2024/01/01- Emails after specific datebefore:2024/12/31- Emails before specific date
- Token Security: Keep your Gmail access token secure and never commit it to version control
- Token Expiration: Gmail access tokens expire. You'll need to refresh them periodically
- Scope Limitation: Only request the scopes you actually need
- Rate Limiting: Be mindful of Gmail API rate limits
-
"Invalid Credentials" Error
- Check that your
GOOGLE_GMAIL_TOKENis correct - Verify the token hasn't expired
- Ensure the token has the required scopes
- Check that your
-
"Permission Denied" Error
- Verify your OAuth 2.0 credentials are correct
- Check that Gmail API is enabled in your Google Cloud project
- Ensure the user has granted the necessary permissions
-
"Quota Exceeded" Error
- Gmail API has rate limits
- Implement exponential backoff for retries
- Consider caching results when appropriate
Enable verbose logging by setting the agent's verbose=True:
gmail_agent_executor = AgentExecutor(
agent=agent,
tools=tools,
memory=memory,
verbose=True, # Enable debug output
handle_parsing_errors=True,
max_iterations=10
)- Batch Operations: When possible, batch multiple operations
- Caching: Cache frequently accessed data like labels
- Pagination: Use
max_resultsparameter to limit response size - Error Handling: Implement proper error handling and retries
Gmail access tokens expire. To handle token refresh:
- Store both access token and refresh token
- Implement token refresh logic
- Update the stored token when it expires
- Consider using Google's client libraries for automatic refresh
This Gmail integration is part of the multi-agent supervisor system and follows the same license terms.