Prerequisites
Install dependencies
Setup Google Project and OAuth
Reference: https://developers.google.com/gmail/api/quickstart/python-
Enable Gmail API
- Go to Google Cloud Console.
- Select Project and Enable.
- Go To API & Service -> OAuth Consent Screen
-
Select User Type
- If you are a Google Workspace user, select Internal.
- Otherwise, select External.
- Fill in the app details (App name, logo, support email, etc).
-
Select Scope
- Click on Add or Remove Scope.
- Search for Gmail API (Make sure you’ve enabled Gmail API otherwise scopes won’t be visible).
- Select scopes accordingly
- For read-only access: Select
/auth/gmail.readonly - For full access: Select
/auth/gmail.modifyand/auth/gmail.compose
- For read-only access: Select
- Save and continue.
-
Adding Test User
- Click Add Users and enter the email addresses of the users you want to allow during testing.
- NOTE: Only these users can access the app’s OAuth functionality when the app is in “Testing” mode. Any other users will receive access denied errors.
- To make the app available to all users, you’ll need to move the app’s status to “In Production”. Before doing so, ensure the app is fully verified by Google if it uses sensitive or restricted scopes.
- Click on Go back to Dashboard.
-
Generate OAuth 2.0 Client ID
- Go to Credentials.
- Click on Create Credentials -> OAuth Client ID
- Select Application Type as Desktop app.
- Download JSON.
-
Using Gmail Tool
- Pass the path of downloaded credentials as
credentials_pathto GmailTools. - Optional: Set the
token_pathparameter to specify where the tool should create thetoken.jsonfile. - The
token.jsonfile is used to store the user’s access and refresh tokens and is automatically created during the authorization flow if it doesn’t already exist. - If
token_pathis not explicitly provided, the file will be created astoken.jsonin your current working directory. - If you choose to specify
token_path, please ensure that the directory you provide has write access, as the application needs to create or update this file during the authentication process.
- Pass the path of downloaded credentials as
Service Account Authentication (Alternative)
For server/bot deployments without browser access:- Create a service account at IAM & Admin > Service Accounts in Google Cloud Console
- Download the JSON key file
- Configure domain-wide delegation in Google Workspace Admin Console (required for Gmail)
- Set environment variables:
Example
cookbook/91_tools/google/gmail_tools.py
Quick Start Examples
Toolkit Params
Toolkit Functions
Reading Emails
Managing Emails
Composing & Sending
Label Management
You can use
include_tools or exclude_tools to modify the list of tools the agent has access to. Learn more about selecting tools.
Cookbook Examples
The agno cookbook includes several Gmail examples demonstrating different use cases:Tips for Using Gmail Tools
-
Authentication:
- Use OAuth for user-facing applications (requires browser)
- Use service accounts for server/bot deployments (requires domain-wide delegation)
-
Search Queries: The
search_emailsfunction supports natural language queries that get converted to Gmail’s search syntax. - Attachments: When sending emails with attachments, provide file paths as strings or a list of strings.
-
Thread Management: Use thread operations (
get_thread,trash_thread, etc.) to manage entire conversations. - Label Operations: Gmail’s label system is hierarchical - use forward slashes for nested labels (e.g., “Work/Projects”).
- Rate Limits: Be mindful of Gmail API quotas when performing bulk operations.