Files
PaperClipAI/doc/plans/2026-11-17-google-workspace-code-fixes.md
T
DottaandClaude Opus 5.5 56023fe80f feat(google-workspace): gateway sets the REST tool result size limit for all six services; update connector docs (#22050)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Agents read Gmail, Calendar, Drive, Docs, Sheets and Slides through
the built-in Google Workspace connection
> - Paperclip now has its own REST-backed Google tools in
`server/src/google-workspace-rest-tools/`. No service uses them yet
> - That folder guessed a result size limit of 250,000 bytes. It applied
the limit only to Drive, Docs, Sheets and Slides. Gmail and Calendar had
no limit
> - The guess was wrong in both directions. It refused documents that
Paperclip can deliver, and the folder should not know Paperclip's limits
> - This pull request makes the limit a number that the gateway gives.
The folder applies it to every tool in all six services through one
check. It also brings three Google planning docs up to date
> - The benefit is that a REST result is refused only when the gateway
would also refuse the same result from Google's own MCP server

## Linked Issues or Issue Description

Refs #22027, #22028, #22029, #22026

**Is your feature request related to a problem?**
The REST tools refused results over a size the folder chose itself
(250,000 bytes). Paperclip can deliver larger results: the runner saves
a large result as an artifact and gives the agent a pointer. Gmail and
Calendar results had no limit at all.

**Describe the solution you'd like**
The gateway gives the limit. The folder applies the given number to the
final serialized result of every tool, with one helper. A result over
the limit is an error that says how to ask for less. It is never cut
short.

**Describe alternatives you've considered**
A larger number chosen from the runner's 1 GiB capture limit. Google's
own transfer limit (20 MB per response) and memory use make that a poor
choice. Also, results would then differ between the REST path and
Google's MCP path.

### Limits a tool result passes on the way to an agent

| Point | Limit | What happens over it |
| --- | --- | --- |
| Google response read by `google-http.ts` | 20 MB
(`MAX_RESPONSE_BYTES`) | Tool error. Nothing returned |
| REST folder result (new) | the number the gateway passes: 999,885
bytes | Tool error with "read less" advice. The gateway returns
`tool_error` (502) |
| Gateway read of a remote MCP response (`MAX_REMOTE_MCP_RESPONSE_BYTES`
in `tool-gateway.ts`) | 1,000,000 bytes per JSON body or event-stream
message | `mcp_remote_response_too_large` (502). This is the "1,000,000
bytes are rejected" statement in `doc/prompt-injection-prevention.md`.
It applies to the Google MCP path only; the REST path has no HTTP read |
| Injection screening model check | 256 KiB model request | Not a
delivery limit. A larger result is checked by the built-in patterns only
|
| Gateway HTTP route (`POST /tool-gateway/.../tools/call`) | none in
Paperclip | `res.json(result)` |
| Runner (`captureRunnerToolResult`) | 500,000 bytes
(`RUNNER_TOOL_RESULT_MAX_BYTES`) | Saved as an artifact. The agent gets
a pointer and reads it with `read_tool_result` |
| Runner capture | 1 GiB (`RUNNER_API_RESPONSE_MAX_BYTES`) |
`SemanticToolOutcomeUnknownError` |

The gateway wraps the result as `{ content, data: { content,
structuredContent, ... } }`, so the payload occurs again. Measured: a
Gmail result of 676,459 bytes from the folder became 1,017,386 bytes
delivered (ratio 1.50). The ratio cannot be more than 2.

### The budget

`GOOGLE_WORKSPACE_REST_MAX_RESULT_BYTES` in `tool-gateway.ts` =
`MAX_REMOTE_MCP_RESPONSE_BYTES` less the JSON-RPC envelope and
event-stream framing (`event: message\ndata:
{"jsonrpc":"2.0","id":"paperclip-tool-<uuid>-version","result":...}`) =
**999,885 bytes**. A result that Google's MCP server could deliver
through the gateway is also delivered over REST, and the reverse. With
the wrapper, the largest delivered result is less than 2 MB. The runner
saves it as an artifact (far below 1 GiB). So the folder never refuses a
result that the runner can give as a pointer.

Practical limits at that budget (plain ASCII; escaped and non-ASCII
characters count more):

| Tool | Largest content |
| --- | --- |
| Gmail `get_message` | about 500 KB plain body, or about 250 KB each
for plain plus HTML |
| Calendar `get_event` | about 500 KB description |
| Drive `read_file_content` | about 500 KB text |
| Drive `download_file_content` | about 375 KB file bytes |
| Docs `read_doc` | about 240 KB document text |
| Slides `read_presentation` | about 250 KB text |
| Sheets `get_values` | about 500 KB cell text, at most 50,000 cells |

## What Changed

- `tool.ts`: `defineTool` takes `whenTooLarge` advice per read tool
(required for reads, refused for writes) and a `ToolLimits` argument. It
measures the final `CallToolResult` once (`withinResultBudget`) and
replaces an oversized result with a `too_large` tool error.
`FALLBACK_MAX_RESULT_BYTES` (250,000) applies only when a direct caller
gives no limit.
- `callGoogleWorkspaceRestTool` and `createGoogleWorkspaceRestServer`
take `maxResultBytes`. A value that is not a positive integer is refused
before any request.
- All 23 read tools state their advice: a smaller `pageSize` for list
tools, `messageFormat` or `view` for Gmail, a narrower range for Sheets,
`fields` for Slides. Write tools are not checked: a completed write is
never reported as an error, so an agent is not led to repeat it.
- Drive, Docs, Slides and Sheets no longer have their own byte checks
(`boundedStructuredResult`, `boundedResult`, `sizedResult`,
`MAX_CONTENT_BYTES` removed). Drive downloads stop reading at the given
limit. The Sheets 50,000-cell limit stays.
- Two Drive tool descriptions no longer state a fixed number.
- Adapter `google-workspace-rest-dispatch.ts`: `maxResultBytes` is a
required input and is passed through.
- `tool-gateway.ts`: new `GOOGLE_WORKSPACE_REST_MAX_RESULT_BYTES`,
passed on the REST branch.
- Folder README: new "Result size" section with the limits and costs;
old 250,000-byte text replaced.
- Docs: `doc/memory/google-connector-memory.md`, the production
checklist and the code-fix plan now list #22026 to #22029, the REST
status (36 of 36, none switched on), the November 17 decisions, the
known differences from Google's tools, open risks and next steps.

## Verification

- `cd server && npx vitest run src/google-workspace-rest-tools` passes
(53 files, 1,361 tests with the adapter test).
- New `result-budget.test.ts`: for Gmail (read, list), Calendar (list,
read), Drive, Docs, Slides and Sheets, a result just under a given
budget is returned and one just over is refused with the tool's advice.
Without a budget, the fallback applies; with a larger budget, the same
result is returned. Drive downloads use the given cap. A Gmail list
tells the caller to lower `pageSize`. A Gmail draft result over the
budget is still returned, not an error. `defineTool` refuses a read tool
without advice and a write tool with it.
- `npx vitest run src/services/google-workspace-rest-dispatch.test.ts`:
the adapter passes the gateway's number, and the folder applies exactly
that number.
- `npx vitest run src/__tests__/google-workspace-rest-transport.test.ts`
(24 tests): with Gmail on REST, a 330 KB email passes the gateway with
screening, is more than 500,000 bytes delivered, and
`captureRunnerToolResult` turns it into a `read_tool_result` pointer. A
520 KB email is refused with `tool_error` and the advice, after one
Google request.
- `npx tsc --noEmit -p server/tsconfig.json`: no errors.
- `pnpm verify --explain`: broader verification required (a doc under
`doc/` is a global CI input). CI supplies that coverage.
- Time per call: the check is one `JSON.stringify` of the result. About
5 µs for a 4 KB result, 18 µs for 42 KB, 0.24 ms for 700 KB. Drive,
Docs, Sheets and Slides had the same pass before, so they add nothing.
Gmail and Calendar add this one pass.

## Risks

- Gmail and Calendar results now have a limit. Direct callers that pass
no limit get the 250,000-byte fallback. The gateway always passes its
number.
- Drive, Docs, Slides and Sheets accept results up to about 4 times
larger than before through the gateway. Large results reach agents as
artifact pointers.
- Error text for oversized results changed to one shared form.
- No service is switched to REST, so users see no change today.

## Model Used

Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used in Claude Code with
tool use (shell, file edits, GitHub CLI) and extended reasoning.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 13:05:13 -06:00

19 KiB

Google Workspace connector: code fixes before submission

Started November 17, 2026. Temporary working plan; delete it once the items land. It covers section 2 of the production checklist.

Based on a read of master at 56654adecd. Items marked confirmed were seen in the code. Items marked to check are suspected gaps that need a look before any change. Fixes 1 to 4 and 6 landed on November 17. Fix 9 has every tool built but no service switched on. Fix 10 is done in the app. Fixes 5, 7 and 8 are not started.

Each fix is its own pull request with a failing test first. For the scope change in fix 2, the deploy order is app first, then broker (see fix 10).

Order

# Fix Size Blocks submission Status
1 Data-use disclosure in the picker Small Yes Done, #21998
2 Lock the managed scope set to 14 Small to medium Yes Done in the app, #22001
3 Take gmail.modify off the Paperclip-managed client Small Yes Done, #22001
4 Confirm injection screening on Google results Check, then small Yes Done, #21999
5 Revoke at Google on last disconnect Medium Likely Not started
6 Retention and deletion of Google content Audit, then unknown Yes Done with limits, #22010
7 Security scan findings Unknown Yes (assessment) Not started
8 Remove the experimental gate Small No, release step Not started
9 REST-backed tool set Large Yes All 36 tools and the switch merged; none switched on
10 Scope cleanup across code, Cloud and Console Medium Yes App done; broker and Console open

1. Data-use disclosure in the picker

Done (Nov 17, #21998). Every Google Workspace sign-in start now goes through the picker, including four sign-in paths on the app page. The Gmail read & write line says Google's screen also mentions sending and that Paperclip does not send. The plan below is kept for reference.

Confirmed gap. ServicePicker in ui/src/features/connections/GoogleWorkspaceServices.tsx shows three service rows and Cancel / Continue. Its only description is screen-reader text. Google requires a disclosure inside the app, immediately before consent, that says what data is accessed and how it is used and shared; a privacy-policy link alone does not count.

  • Add a short visible block above the footer, built from the current selection: what Paperclip will be able to read and change for the chosen services, that agents send this content to the model provider configured for the workspace to do the tasks they are given, that it is not used to train general models, and where results are stored.
  • Link the privacy policy and the Limited Use statement.
  • The copy changes with the selection (read only versus read & write; Gmail write says "create drafts", never "send").
  • Show it for new setup and for reconnect, in both the Apps flow and the task-hosted flow (both use GoogleWorkspaceServicesDialog).
  • Tests in GoogleWorkspaceServices.test.tsx: disclosure present for each selection, absent wording for unselected services, links present.
  • Update the Storybook story under ui/storybook/stories/connection-setup/google-workspace-services.stories.tsx.
  • Follow DESIGN.md (tokens only, form footer rules).

Link to https://paperclip.ing/privacy (section 4.1 holds the Limited Use statement). The disclosure wording must agree with that policy, so the policy edits listed in the working memory land first or together. Dotta signs off the wording.

2. Lock the managed scope set to 14

Done in the app (Nov 17, #22001). GOOGLE_WORKSPACE_SUBMITTED_SCOPES is the hand-written list of 14 and workspace.all uses it. Requests outside the 14 are rejected; scopes Google reports on a saved grant are capped to the 14 instead of failing it. The broker change is in fix 10. The plan below is kept for reference.

Confirmed. The selector derives scopes from GOOGLE_WORKSPACE_SERVICE_GROUPS in packages/shared/src/google-workspace-connectors.ts, which yields 14 scopes across all modes. But the workspace.all profile is still the 21-scope legacy union (Chat, Contacts, Directory, profile and calendar.readonly included), and isGoogleWorkspaceScopeGrant accepts any subset of those 21.

  • Add a test that the union of every selectable combination equals an explicit list of the 14 scopes, so a new scope cannot slip in unnoticed. Today's tests pin 9 (default) and 21 (legacy) only.
  • Add a server test that an authorization request for the unified connection carrying any scope outside the 14 is rejected, for both the managed and the customer-owned path.
  • Narrow isGoogleWorkspaceScopeGrant for the unified connection to the 14. To check first: whether saved grants or the broker's sealed envelopes depend on the 21-scope definition (the code comment says the union is kept stable for the broker contract). If so, add a separate 14-scope constant and leave the legacy definition for old grants.
  • Companion change in the Cloud broker registry so the production client cannot be asked for the other 7. App first, then broker (fix 10).

3. Take gmail.modify off the Paperclip-managed client

Done (Nov 17, #22001). Gmail archive, Chat, People and Workspace Search managed methods are no longer offered for new setup or reconnect; saved connections keep refreshing and running. The broker has no gmail.archive profile. The plan below is kept for reference.

Confirmed. packages/shared/src/app-definitions/gmail.json has a paperclip-archive method ("Connect with Paperclip", profile gmail.archive) that requests gmail.modify, a restricted scope that is not in the submission. If the production broker ever enables that profile, the verified client requests an undeclared restricted scope.

  • Remove the paperclip-archive method, or gate it so it can never use the Paperclip-managed client. Keep customer-archive-oauth (the customer's own Google client is outside our verification).
  • Keep server/src/services/gmail-archive-tool-policy.ts working for saved connections.
  • To check: whether any saved connection uses paperclip-archive, and whether the production or staging broker has gmail.archive enabled.
  • Do the same inventory for every other "Connect with Paperclip" Google method in packages/shared/src/app-definitions/google-*.json and gmail.json (Chat, People, Workspace Search and the per-service cards): each one that can reach the managed client either stays within the 14 scopes or loses its managed method.
  • Test: no app definition with the managed strategy lists a scope outside the 14.

4. Confirm injection screening on Google results

Done (Nov 17, #21999). Every path was traced and tested. The one unscreened path, resource reads through a named gateway, is now screened for every connector. What screening does and does not cover is in doc/prompt-injection-prevention.md. The plan below is kept for reference.

To check. Tool-result screening exists (doc/prompt-injection-prevention.md): built-in detection is always on, and a model check is optional per company. Not yet confirmed that every Google Workspace tool result passes through it, in particular email bodies and file content fetched through the unified connection.

  • Trace a gmail__get_message and a drive__read_file_content call from the gateway to the runner and confirm the guard runs on the result.
  • Add a test with an injected instruction inside an email body and inside a document, expecting prompt_injection_blocked.
  • Fix any path that skips the guard.
  • Write two paragraphs for the submission describing what the screening does and does not protect against.

5. Revoke at Google on last disconnect

Confirmed behavior, likely gap. Removing a managed Google connection is local only, because Google's revoke ends every grant for that user on the client and would break their other Google connections (doc/connections/GOOGLE-WORKSPACE.md). With one unified connection that reasoning mostly goes away, and reviewers expect disconnect to end access.

  • When a user removes their last grant on the Paperclip-managed Google client, call Google's revoke endpoint through the broker.
  • If other grants for the same Google account remain, stay local and say so in the confirmation text.
  • Tests: last grant revokes; one of two does not; a revoke failure still removes the local credential and reports the failure.
  • Needs a broker endpoint; broker first.

6. Retention and deletion of Google content

Done, with limits (Nov 17, #22010). Any connection's page has "Delete data", and removing a connection offers the same. It clears previews, approval cards, results passed to later runs, app views and the whole transcript of each settled run that used the connection. It keeps call records without content. Limits: content agents wrote into tasks stays; deleting a task or a company does not yet run the same cleanup; runs in progress are skipped. The help page is docs/guides/board-operator/deleting-connection-data.md. The inventory is in the working memory under "Where Google content is kept". Revoking at Google is fix 5. The plan below is kept for reference.

To check. Email and file content returned by tools is stored in task comments, run history, tool results and attachments. Disconnecting does not delete any of it. Google requires that users can delete their data and that we document how.

Audit findings (Nov 17, as reported) are in the working memory under "Where Google content is kept". The planned fix is a "delete data read through this connection" action.

  • Inventory where Google tool results are stored and for how long.
  • Confirm that deleting a task or a workspace removes them.
  • Decide with the privacy owner whether an explicit "delete data from this connection" action is needed. If the answer is yes, that becomes its own plan.
  • Write the user help page either way.

7. Security scan findings

  • Run the static and dynamic scans used for the CASA self-assessment against the server, the broker and the reviewer instance.
  • Triage findings into fix now, accepted with reason, and not applicable.
  • One pull request per finding class.

8. Remove the experimental gate (release step, last)

Confirmed. enableGoogleWorkspaceConnector defaults to off (packages/shared/src/validators/instance.ts, set on the app definition in google-workspace.json, enforced in tool-gateway.ts, connection-intents.ts and setup-policy.ts).

  • After Google approves: default it on, or remove the key and its checks.
  • Update doc/connections/GOOGLE-WORKSPACE.md and the release notes.
  • Decide what happens to the aggregator Google apps that appear while the gate is off.

9. REST-backed tool set (chosen November 17)

Decision: launch on our own tools over Google's REST APIs, designed so we can switch to Google's hosted MCP servers later, kept in one folder so it can be deleted cleanly. Part 1 is #22000 (merged Nov 17): the folder and Gmail get_message, search_threads and create_draft. Its README records the dispatch trace and the recommended in-process seam.

Status (Nov 17, night):

Service Implemented Switched on PRs
Gmail 9 of 9 No #22000, #22014
Calendar 9 of 9 No #22017
Drive 8 of 8 No #22028
Docs 2 of 2 No #22029
Sheets 6 of 6 No #22027
Slides 2 of 2 No #22029

The per-service switch and in-process dispatch landed in #22016. Every service is still on Google's MCP. The gateway calls the REST tools in process, as the trace in #22000 recommended. The gateway also sets the size limit for REST results (999,885 bytes, matching what it accepts from Google's MCP servers); see "Result size" in the folder README.

Remaining work:

  • Refuse inserting an image by public web address in Docs and Slides before they are switched on (Google fetches an address the agent chose).
  • Create the staging tester and run live acceptance there.
  • Switch services to REST one at a time, each after its live test.
  • Record a tools/list and sample answers from Google's servers for the contract test, and compare read_doc and read_presentation content.
  • Check copy_file under drive.file on a file the app never opened.
  • Removal test (step 4 below).

The seam

Today the unified connection sends each tool call, with the user's Google access token, to a Google MCP endpoint chosen by a closed registry in packages/shared/src/google-workspace-connectors.ts (serverUrl per service). Everything above that point (picker, scopes, grant checks, allowlist, approval, audit, injection screening) only knows tool names and required scopes.

So the replacement is an MCP server of our own that exposes the same tools as Google's: same names, same input fields, same result shape, backed by REST. The rest of the system keeps speaking MCP to it and cannot tell the difference. Switching to Google's MCP later is changing which endpoint the registry returns.

Layout

One folder inside the existing server, not a new package. No new dependency, workspace or release change.

server/src/google-workspace-rest-tools/
  index.ts            the only entry point: create the server / handle a call
  server.ts           MCP wiring (list tools, call tool); thin
  google-http.ts      the one place that calls googleapis.com: auth header,
                      allowed hosts, errors mapped to MCP errors, retry rules
  tools.ts, tool.ts   the tool list and the shared tool shape
  errors.ts
  README.md           boundary rules, dispatch trace, differences from Google
  gmail/              one file per tool, plus schemas and helpers
  drive/              (later) args in, REST request out,
  docs/               REST response in, MCP result out
  sheets/
  slides/
  calendar/

Boundary rules

  • One switch. The registry gains a transport choice per service (google_mcp or paperclip_rest). That choice, plus registering the folder, is the only Google-REST knowledge outside the folder.
  • No REST types leave the folder. Its public surface is MCP tool definitions and MCP results. A boundary test limits its imports to its own files, Node built-ins, the MCP SDK and zod.
  • Nothing in the folder knows about Paperclip. No company, connection, grant, policy or audit code. It receives a tool name, arguments and an access token, and returns a result. Authorization, scope checks, approval, audit and injection screening stay with their current owners in the gateway.
  • It stores nothing. No tokens, no content, no cache.
  • The allowlist stays where it is. toolScopes in google-workspace-connectors.ts remains the single list of allowed tools; the folder implements exactly that list, and a test fails if the two differ.
  • Removal test. Deleting the folder and setting the switch to google_mcp leaves a build that passes. Add this as a checklist item for the removal PR.

Parity

  • Tool names and schemas come from Google's published MCP tool reference, not from our guess. A contract test compares our definitions with a recorded listing from Google's servers (we can record it on our enrolled internal account).
  • Where REST cannot match an MCP tool's behavior exactly, write the difference in the folder's README rather than hiding it.

Order of work

  1. Trace the dispatch path and confirm the seam. Traced in #22000's README: an in-process call, because the egress guard refuses a loopback address on public deployments.
  2. Folder skeleton, google-http.ts, and Gmail get_message, search_threads and create_draft (#22000, merged). Then the switch, end to end on the staging tester. The demo depends on Gmail.
  3. Rest of Gmail, then Calendar, Drive, Docs, Sheets, Slides (done Nov 17).
  4. Contract and removal tests.

Tools to build (36): Gmail 9, Drive 8, Docs 2, Sheets 6, Slides 2, Calendar 9.

To check

  • Whether a Google client marked for AI agents may call the REST APIs.
  • Each Cloud project needs the six REST APIs enabled.
  • Speed: this sits in the tool-call path. Measured: the adapter adds about 15 µs per call; the result size check adds about 5 µs for a 4 KB result.

Lessons

  • Address parsing: patching a split-on-commas parser case by case kept breaking (comments, groups, IPv6 literals); write one RFC 5322 tokenizer with table-driven tests from the start.
  • No new package: the server is published, so a new package it depends on would also need publishing; a folder inside the server avoids that.

10. Scope cleanup across code, Cloud and Console

Decision (November 17): what we ask for, what the code accepts, what the Cloud broker allows and what the Console declares must be the same 14 scopes. Fixes 2 and 3 cover this repository and are done. Still needed in the Cloud broker (paperclip-cloud, registry at src/connections/providers.ts, as reported by the Nov 17 audit):

  • Limit workspace.all to the 14.
  • On refresh and claim, cap reported scopes to the 14 instead of failing, and keep accepting the old 21-scope refresh binding until older app versions are gone.
  • Stop offering Chat, People and Search for new sessions on the verified client.
  • Do not merge the branch that adds a Gmail archive profile.
  • Deploy order: app first, then broker.

Also needed:

  • Compare the Console declaration for both Google apps with the 14 (needs the project owner's sign-in).
  • Keep the working memory's "Scopes" and "Cloud broker" sections current.

Read & write Calendar now also requests calendar.events.freebusy (Nov 17). It is one of the 14 and the broker accepts any nonempty subset of workspace.all, so neither the broker nor the Console needs a change for it.

Not code, but needed by these fixes

  • Published privacy policy and Limited Use statement URLs (fix 1).
  • Sign-off on disclosure wording (fix 1).
  • Broker changes in the Cloud repository (fixes 5 and 10).
  • Current broker profile settings for staging and production (fixes 2, 3).