Gmail

Search, read, and send emails via Gmail.

Search, read, and send emails. By default, exposes query_gmail for searching and reading. Enable write=True to also expose update_gmail for drafting and sending.

Create a virtual environment, then install the Google Workspace clients and the model SDK:

uv pip install -U agno openai google-api-python-client google-auth-httplib2 google-auth-oauthlib
export OPENAI_API_KEY="your-openai-api-key"

For the personal OAuth example, enable the relevant Google API in your Cloud project, configure its consent screen, and create a Desktop OAuth client. Set GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET and GOOGLE_PROJECT_ID using that client. Authorize access in the browser on first use. The authentication section below also covers service accounts.

For a headless service, provision credentials first and set GOOGLE_OAUTH_NONINTERACTIVE=1. Gmail and Calendar also accept auth=AuthConfig(interactive=False) from agno.tools.google.auth; the current Drive provider does not accept auth=. If credentials cannot be loaded or refreshed, authentication reports an error instead of opening a browser. Provider configuration selects the credentials; forwarding a run’s user ID does not switch Google accounts.

Save as gmail_context.py and run python gmail_context.py after configuring authentication:

gmail_context.py
import asyncio

from agno.agent import Agent
from agno.context.gmail import GmailContextProvider
from agno.models.openai import OpenAIResponses

async def main():
    gmail = GmailContextProvider(model=OpenAIResponses(id="gpt-5.4-mini"))
    agent = Agent(
        model=OpenAIResponses(id="gpt-5.4"),
        tools=gmail.get_tools(),
        instructions=gmail.instructions(),
    )
    await agent.aprint_response("Do I have any unread emails from the engineering team?")

if __name__ == "__main__":
    asyncio.run(main())

Authentication

Gmail requires Google OAuth or a service account with domain-wide delegation.

Set these environment variables:

export GOOGLE_CLIENT_ID=...
export GOOGLE_CLIENT_SECRET=...
export GOOGLE_PROJECT_ID=...

Opens browser on first use. Token cached to gmail_token.json.

Configuration

ParameterTypeDefaultDescription
idstr"gmail"Tools become query_<id> and update_<id>.
modelModelNoneModel for sub-agents.
readboolTrueExpose query_gmail.
writeboolFalseExpose update_gmail. Disabled by default for safety.
modeContextModedefaultSee Mode.

The read/write flags below describe the default tool surface. See read/write control for mode and authorization boundaries.

Tools Exposed

ToolDescription
query_gmailSearch emails, get messages, get threads, list labels. Requires read=True (default).
update_gmailCreate drafts, send emails, send replies, manage labels. Requires write=True.

Example queries

For draft/send queries, enable writes: GmailContextProvider(write=True)

QueryWhat happens
"Find emails from Alice about the Q4 report"Searches with from:alice subject:Q4 report
"Summarize the thread about the API outage"Gets thread and synthesizes
"Draft a reply saying I'll review it tomorrow"Creates draft in thread
"Send a quick update to the team about the fix"Composes and sends

Resources