logoCaywork Knowledge

Google Gmail Tool

Agent tool for performing authenticated Gmail API actions using per-user stored Gmail access tokens.

Google Gmail Tool image

This node exposes a single tool for routing multiple Gmail-specific actions. It retrieves per-user Gmail credentials (access token) from stored API keys, constructs and sends authenticated requests to the Gmail REST API, encodes RFC‑2822 messages for sending/drafts, and returns parsed JSON or raw text responses. Network and API errors are logged and surfaced as exceptions.

Actions

  • Retrieves per-user Gmail config (access_token) from stored API keys; raises if missing.
  • Encodes messages to URL-safe base64 without padding for Gmail API send/draft endpoints.
  • Builds RFC‑2822 formatted email bodies with optional HTML content.
  • Supports the following actions (action-specific params described briefly below):
    • send_message — send an email. Required: from, to. Optional: subject, body, is_html. Returns send response JSON.
    • list_messages — list messages. Optional params: q (query), labelIds (string or list), pageToken, maxResults (default 100). Returns messages list JSON.
    • get_message — get a message by ID. Required: message_id. Optional: format (full|metadata|raw|minimal; default "full").
    • modify_message_labels — add/remove labels on a message. Required: message_id. One of addLabelIds or removeLabelIds required. Returns modified message JSON.
    • create_draft — create a draft message. Required: from, to. Optional: subject, body, is_html. Returns draft JSON.
    • send_draft — send an existing draft. Required: draft_id. Returns send response JSON.
    • list_labels — list user's labels. No params required. Returns labels JSON.
    • get_label — get a label by ID. Required: label_id. Returns label JSON.

Outputs

  • Returns the parsed Gmail API response (JSON-decoded) or raw text if JSON decoding fails.
  • Errors surface as raised exceptions with logged context (network errors, validation errors, unexpected HTTP status codes).

Behavior and Constraints

  • Uses per-user stored API keys; requires a per-user token provided in the user Secret key store.
  • Reuses an httpx.AsyncClient per asyncio event loop to limit connection overhead (timeout 20s, max_connections 20).
  • RFC‑2822 messages are constructed with From/To/Subject headers and Content-Type text/plain or text/html.

Error Handling

  • Missing Gmail config raises a RuntimeError indicating which key is missing.
  • Network/request errors raise RuntimeError with logged exception details.
  • Non-success HTTP responses raise RuntimeError including status and response body.
  • Action-specific parameter validation errors raise ValueError or Pydantic ValidationError as appropriate.