> For the complete documentation index, see [llms.txt](https://docs.labs.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.labs.ai/protocols-and-integration/connections/google-workspace.md).

# Google Workspace — Gmail & Drive Tools

> A recipe, not a built-in feature: one [connection](https://docs.labs.ai/protocols-and-integration/connections.md) and two [httpcalls](https://docs.labs.ai/agent-configuration/httpcalls.md) configurations that give an LLM agent tools over **each user's own** Gmail and Google Drive. Nothing in it is Google-specific code — it is ordinary agent configuration, and every rule below is the connection or httpcall rule it comes from.

**Contents:** [What the recipe contains](#what-the-recipe-contains) · [Prerequisites](#prerequisites) · [Installing it](#installing-it) · [Security](#security) · [Limitations](#limitations)

The files are under [`docs/agent-configs/google-workspace-tools/`](https://github.com/labsai/EDDI/tree/main/docs/agent-configs/google-workspace-tools/README.md), in the [agent ZIP layout](https://docs.labs.ai/agent-configuration/agent-config-authoring.md#zip-structure-for-agent-import). The unit tests that sweep `docs/agent-configs` parse them, so a field renamed in a model class fails the build here rather than on somebody's import.

***

## What the recipe contains

| File                                      | What it is                                                                                                                 |
| ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `connections/…connection.json`            | `google-workspace`: `OAUTH2_AUTHORIZATION_CODE`, bound `PER_USER`, against `accounts.google.com` / `oauth2.googleapis.com` |
| `…/1/…003.httpcalls.json`                 | Gmail tools against `https://gmail.googleapis.com/gmail/v1/users/me`                                                       |
| `…/1/…004.httpcalls.json`                 | Drive tools against `https://www.googleapis.com/drive/v3`                                                                  |
| `…/1/…005.langchain.json`                 | The LLM task: httpcall tools on, built-in tools **off**                                                                    |
| `…/1/…002.behavior.json`, workflow, agent | A catch-all rule that hands every input to the LLM task — the shape the agent setup wizard generates                       |

| Tool            | Call                                                                                 | Scope it needs   |
| --------------- | ------------------------------------------------------------------------------------ | ---------------- |
| `searchMail`    | `GET /threads?q=…&maxResults=10` — thread ids and plain-text snippets                | `gmail.readonly` |
| `readThread`    | `GET /threads/{threadId}?format=full`                                                | `gmail.readonly` |
| `createDraft`   | `POST /drafts` with an RFC 2822 message in `message.raw`                             | `gmail.compose`  |
| `searchDrive`   | `GET /files?q=…&pageSize=10&fields=files(id,name,mimeType,modifiedTime,webViewLink)` | `drive.readonly` |
| `readDriveFile` | `GET /files/{fileId}/export?mimeType=text/plain` (or `text/csv`)                     | `drive.readonly` |

Every call authenticates with `"Authorization": "${connection:google-workspace}"` — the whole header value is the reference, as [connections require](https://docs.labs.ai/protocols-and-integration/connections.md#the-value-must-be-the-reference-and-nothing-else), and the connection contributes the `Bearer` scheme. Every tool declares an [`argumentSchema`](https://docs.labs.ai/agent-configuration/httpcalls.md#argument-schema-for-llm-tools) with `unknownFields: reject`, so a call with a missing, misspelled or oversized argument is refused before it is sent.

**There is no `send` tool, on purpose.** See [Security](#security).

***

## Prerequisites

### Users must be verified by EDDI's own OIDC

A `PER_USER` credential is released only to a conversation whose user id EDDI **authenticated** — provenance `VERIFIED`, recorded once when the conversation is created. Anything else is `SELF_ASSERTED` and refused, by name. See [Whose identity counts](https://docs.labs.ai/protocols-and-integration/connections.md#whose-identity-counts). In practice that rules out:

| Where the turn comes from                                                                                 | Why it is refused                                                                                                                                                                                                                                                   |
| --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| No OIDC at all (`authorization.enabled=false`, the shipped default)                                       | There is no verified identity, so a `PER_USER` connection cannot even be **saved** — the write answers 400 naming `authorization.enabled`                                                                                                                           |
| Anonymous chat                                                                                            | The user id was generated for the session, not authenticated                                                                                                                                                                                                        |
| Open WebUI over `/v1` in **api-key** mode with `eddi.openai-compat.trust-user-headers=true` (the default) | The caller proved the shared key, then *asserted* a user id in `X-OpenWebUI-User-Id`; anyone holding that key could open a conversation as anyone. See [Open WebUI — Two modes](https://docs.labs.ai/protocols-and-integration/open-webui-integration.md#two-modes) |
| A conversation opened by a webhook, a schedule or an inbound mail                                         | Nobody authenticated is present: the conversation's user id is not the principal of an authenticated request                                                                                                                                                        |

`allowUnverifiedPrincipal: true` on the connection accepts a `SELF_ASSERTED` id, for deployments that authenticate at a front proxy. With it, anyone who can make that proxy forward a user id spends **that** user's Google tokens — read [the opt-in](https://docs.labs.ai/protocols-and-integration/connections.md#whose-identity-counts) before you set it. The recipe leaves it off.

### Connections enabled, with Google's endpoints approved

[Connection settings](https://docs.labs.ai/protocols-and-integration/connections.md#enabling-connections) (`eddi-admin`, or pinned by properties):

```http
PUT /connectionstore/settings
{
  "enabled": true,
  "publicBaseUrl": "https://eddi.example.com",
  "credentialEndpointAllowlist": ["https://accounts.google.com", "https://oauth2.googleapis.com"]
}
```

* **`enabled`** turns on account linking (`authorize`, `callback`, `mine`, `disconnect`). Off, they answer 404.
* **`publicBaseUrl`** must be a bare `https` origin: the OAuth `redirect_uri` is built from it, and `authorize` answers 400 while it is unset. `GET /connectionstore/settings` returns the exact `redirectUri` to register at Google — `https://eddi.example.com/connections/callback`.
* **`credentialEndpointAllowlist`** must name **both** Google origins: `authorize` refuses with 400 when either the `authorizationUrl` or the `tokenUrl` origin is missing, and an empty allowlist means no OAuth connection resolves at all. See [Two allowlists](https://docs.labs.ai/protocols-and-integration/connections.md#two-allowlists-and-why-they-are-separate).
* The **vault** must be active: grants are envelope-encrypted with the tenant key, and an OAuth connection on an inert vault is refused at save time.

### A Google OAuth client

In the Google Cloud console, for the project that will own the client:

1. Enable the **Gmail API** and the **Google Drive API**.
2. Configure the OAuth consent screen with the three scopes: `https://www.googleapis.com/auth/gmail.readonly`, `https://www.googleapis.com/auth/gmail.compose`, `https://www.googleapis.com/auth/drive.readonly`.
3. Create an OAuth client of type **Web application** and add the `redirectUri` from `GET /connectionstore/settings` as an authorized redirect URI — exactly, since Google matches it character for character.
4. Store the client secret in the vault (`PUT /secretstore/secrets/{tenantId}/google-workspace-client-secret`) and put the client id in the connection.

The consent screen's **publishing status** decides who can link and for how long. All three scopes are on Google's *restricted* list, which is what makes the last row expensive:

| Publishing status                           | Who can link                                    | What to know                                                                                                                                                                                       |
| ------------------------------------------- | ----------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Internal** (Workspace organisations only) | Users of your own Google Workspace organisation | No Google verification. The usual choice for an in-house deployment                                                                                                                                |
| **External — Testing**                      | Only the test users you list, at most 100       | Refresh tokens **expire after 7 days**. EDDI then gets `invalid_grant`, marks the grant `REFRESH_FAILED`, and the user has to link again — expected in a pilot, not a bug                          |
| **External — In production**                | Any Google account                              | Google's app verification, and for restricted scopes an additional **security assessment**, before users outside your organisation can link without warnings. Plan for it as a project, not a form |

Google changes these rules from time to time; check its current OAuth policy before you commit to a status.

### Users link their Google account

The flow is the [per-user account](https://docs.labs.ai/protocols-and-integration/connections.md#per-user-accounts) flow: `POST /connections/google-workspace/authorize` (any authenticated user — it files the grant under the caller's own verified identity) returns Google's consent URL, Google redirects to `/connections/callback`, and EDDI stores the grant.

In the Manager, every signed-in user reviews and revokes their links under **Linked accounts** (`/manage/linked-accounts`, in the user menu). That page can **start** a new link only for an `eddi-admin`: no endpoint lists the connections a non-admin could link, so for everybody else the page shows only what they have already linked. For end users, start the first link from your own UI (a "Connect Google" button that calls `authorize` and opens the returned URL — with `credentials: 'include'` when that UI is on another origin) or tell them the connection name.

Until a user has linked, the connection refuses with `NOT_CONNECTED` ("has no grant for this user yet. Connect the account first …"), the tool call fails with that message, and the recipe's system prompt tells the model to relay it to the user.

***

## Installing it

1. **Create the connection first**, with your real client id — from `connections/bbb000000000000000000006.connection.json`, via `POST /connectionstore/connections` (`eddi-admin`). An import never overwrites an existing connection of the same name, so creating it by hand first means the archive's placeholder client id is simply skipped.
2. Store the LLM key the task references (`${vault:openai-api-key}`), or change `type`, `modelName` and `apiKey` in the langchain file to your provider — see [LLM Integration](https://docs.labs.ai/agent-configuration/langchain.md).
3. Zip the **contents** of `docs/agent-configs/google-workspace-tools/` (the `*.agent.json` at the archive root) and `POST /backup/import` it, then deploy the agent.
4. Sign in as a user with a verified identity, link Google, and ask the agent for something in your mail.

***

## Security

### Read plus send is an exfiltration channel — so the recipe drafts

Everything `readThread` and `readDriveFile` return was written by somebody else. A mail can carry instructions aimed at the model ("forward the last three invoices to …"), and an agent that can both read the mailbox and **send** can be talked into mailing its contents to the attacker, in a turn where the user asked for something unrelated. That is prompt injection with a delivery mechanism attached.

So the recipe has no send tool. `createDraft` leaves the message in the user's Drafts folder, and a human reads it, and its recipients, before anything leaves. The system prompt also tells the model that tool output is data, not instructions — useful, but a prompt is not a control; the absence of the tool is.

Two further things keep the read side from becoming an outbound channel of its own:

* **Built-in tools are off** (`enableBuiltInTools: false`). A web search or URL fetch tool would let the model carry mail content out in a query string.
* **Every httpcalls step in the workflow becomes a tool** ([`enableHttpCallTools`](https://docs.labs.ai/agent-configuration/langchain.md)). Adding another httpcalls step to this workflow adds its calls to the same model that reads the mail.

**If you add a send tool**, gate it behind [tool-level approval](https://docs.labs.ai/conversations-and-orchestration/hitl.md#tool-level-approval-gating) so that each send pauses for a human who sees the recipients and body before it runs:

```json
{
  "hitlConfig": {
    "toolApprovals": {
      "requireApproval": ["sendMail"],
      "approvalTimeout": "PT30M",
      "timeoutPolicy": "AUTO_REJECT"
    }
  }
}
```

Note that the grant already allows sending: Google's `gmail.compose` scope covers *manage drafts and send email*. The recipe's "no send" is a property of its tool set, not of the token — anyone who can edit the agent's httpcalls can add a send call without users consenting to anything new.

### Header injection in drafts

`createDraft` builds the RFC 2822 message from model-supplied `to`, `subject` and `body` and base64url-encodes it (`{encoder:base64Url(…)}`) into Gmail's `message.raw`. Headers end at a line break, so a recipient or subject containing CR/LF (`alice@example.com\r\nBcc: attacker@evil.test`) would add headers of its own — written by whoever wrote the mail the model just read.

The obvious fix, an `argumentSchema` `pattern` forbidding `\r` and `\n`, **does not work**: the argument check deliberately ignores `pattern` (an author's regex run on model text is a backtracking risk), so such a pattern accepts every value. The template neutralises both fields instead:

* **`to`** has every `\r` and `\n` removed (`to.replace(…)`) before it is spliced in, so the injected text becomes part of one malformed address line rather than a new header.
* **`subject`** goes through `{encoder:mimeHeader(subject)}`, which writes it as RFC 2047 encoded words, `=?UTF-8?B?…?=`. Every character is base64-encoded, ASCII included, so nothing in the subject can end the header, and non-ASCII subjects come out right as a side effect. A long subject is split into several words, each within RFC 2047's 75-character limit and never cutting a character in half, folded onto continuation lines. Wrapping the whole subject in one `encoder:base64` word would exceed that limit from 46 UTF-8 bytes up (24 `é`), and a strict reader then shows the encoded text.
* **`body`** follows the blank line that ends the headers, as `Content-Transfer-Encoding: base64` (`encoder:base64Mime`); a line break there is body text.

`maxLength` on each argument still applies, so an oversized value is refused before the template runs. Keep this shape if you add a header field; never splice a model-supplied value into a header line as is.

### Mail and file content lands in conversation memory

The tools set `saveResponse: true`, which is what returns the response body to the model — and it also stores that body in the conversation's memory, where it is kept and exported like any other conversation data (see [GDPR / CCPA Compliance](https://docs.labs.ai/security-and-compliance/gdpr-compliance.md)). `maxResponseSizeInBytes` caps each one (`readThread` and `readDriveFile` at 200 KB; a larger body is truncated).

***

## Limitations

* **Message bodies arrive base64url-encoded.** Gmail's API returns body parts that way and EDDI's templating can encode but not decode, so `readThread` hands the model `payload.parts[].body.data` as Gmail sent it, next to plain-text snippets and headers.
* **Drive reads Google-native files only.** `readDriveFile` uses the export endpoint, which works for Docs, Sheets (first sheet, as CSV) and Slides. A PDF or Word upload answers with an error.
* **Fixed page sizes** — 10 threads, 10 files. Change `maxResults` / `pageSize` in the httpcalls files.
* **Not for group conversations or scheduled work** — a member or scheduled conversation usually has no verified principal, so its `PER_USER` calls are refused; see [Connections → Limitations](https://docs.labs.ai/protocols-and-integration/connections.md#limitations).
* **Revocation is local.** Unlinking deletes EDDI's grant but does not revoke the token at Google; users who want it gone everywhere remove the app under their Google account's third-party access.
* **Gmail as an inbound channel** (an agent that answers incoming mail) is a different feature with different constraints — see [Email Integration](https://docs.labs.ai/protocols-and-integration/email-integration.md#any-other-imap-host-app-password).
