Skip to main content
Gmail enables an Agent to interact with Gmail, allowing it to read, search, send, manage emails, and organize them with labels. Supports both OAuth and service account authentication.

Prerequisites

Install dependencies

Setup Google Project and OAuth

Reference: https://developers.google.com/gmail/api/quickstart/python
  1. Enable Gmail API
  2. Go To API & Service -> OAuth Consent Screen
  3. Select User Type
    • If you are a Google Workspace user, select Internal.
    • Otherwise, select External.
  4. Fill in the app details (App name, logo, support email, etc).
  5. 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.modify and /auth/gmail.compose
    • Save and continue.
  6. 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.
  7. Generate OAuth 2.0 Client ID
    • Go to Credentials.
    • Click on Create Credentials -> OAuth Client ID
    • Select Application Type as Desktop app.
    • Download JSON.
  8. Using Gmail Tool
    • Pass the path of downloaded credentials as credentials_path to GmailTools.
    • Optional: Set the token_path parameter to specify where the tool should create the token.json file.
    • The token.json file 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_path is not explicitly provided, the file will be created as token.json in 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.
    Alternatively, if you prefer environment variables over a credentials file:

Service Account Authentication (Alternative)

For server/bot deployments without browser access:
  1. Create a service account at IAM & Admin > Service Accounts in Google Cloud Console
  2. Download the JSON key file
  3. Configure domain-wide delegation in Google Workspace Admin Console (required for Gmail)
  4. 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

  1. Authentication:
    • Use OAuth for user-facing applications (requires browser)
    • Use service accounts for server/bot deployments (requires domain-wide delegation)
  2. Search Queries: The search_emails function supports natural language queries that get converted to Gmail’s search syntax.
  3. Attachments: When sending emails with attachments, provide file paths as strings or a list of strings.
  4. Thread Management: Use thread operations (get_thread, trash_thread, etc.) to manage entire conversations.
  5. Label Operations: Gmail’s label system is hierarchical - use forward slashes for nested labels (e.g., “Work/Projects”).
  6. Rate Limits: Be mindful of Gmail API quotas when performing bulk operations.

Developer Resources