Commit Graph
3 Commits
Author SHA1 Message Date
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
DottaandClaude Opus 5.5 1d3b4201e3 feat(google-workspace): remaining Gmail read tools in the REST tool set (#22014)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Agents use Gmail through the built-in Google Workspace connector
> - Google's hosted Gmail MCP server is a Developer Preview, so
Paperclip launches on its own REST-backed tool set in
`server/src/google-workspace-rest-tools/` (#22000)
> - That folder has 3 of the 9 Gmail tools on the reviewed allowlist
(`toolScopes.gmail`)
> - Agents cannot read a whole thread, list drafts, list labels or
search single messages
> - This pull request adds the 6 missing read tools, with the same
names, inputs and results as Google's MCP tools where Google documents
them
> - The benefit is that Gmail is complete on the REST transport, and a
message looks the same from every tool

## Linked Issues or Issue Description

Refs #22000

**Problem**
The REST-backed Gmail tool set implements only `get_message`,
`search_threads` and `create_draft`. The allowlist also names
`get_thread`, `get_draft`, `list_drafts`, `list_labels`, `list_threads`
and `search_messages`.

**Proposed solution**
Implement the 6 read tools in the folder. Follow the existing patterns:
one file per tool, pure request and result functions, synthetic unit
tests.

**Why it matters**
Google's reviewers compare an email in Paperclip with the same email in
Gmail. Full threads and drafts must show complete bodies.

## What Changed

- Add `get_thread`, `list_drafts` and `list_labels`. Their schemas come
from Google's Gmail MCP reference.
- Add `get_draft`, `list_threads` and `search_messages`. Google's
reference does not list them, so their schemas copy the sibling tools.
The README lists them under "Not verified against Google's servers".
- Move separately stored body reads from `get_message` into
`readMessageBodies` in `gmail/message.ts`. All tools that return bodies
use it. A body that cannot be read fails the call with "Could not read
the full body of this message".
- Add `gmail/parallel.ts`: a 10-at-a-time read pool that stops after the
first failure, and a helper that skips items deleted between list and
get.
- Cap page sizes (50 for threads, messages and drafts; 500 for labels,
which Gmail returns in one request). The README has a table of Google
requests per call at the maximum page size.
- Update descriptions: `search_threads` now points to `get_thread` and
`list_labels`. The `label:` search operator takes label names (spaces as
hyphens), not IDs. This corrects the wording in the original tools.
Google's MCP reference says IDs; the README records the difference, and
a test prevents drift.
- Cap separately stored body reads at 20 per call. The README lists
requests and quota units per call at the maximum page size.
- `allowlist.test.ts` now asserts that Gmail implements all 9
allowlisted tools and that only `create_draft` writes.

## Verification

- `cd server && npx vitest run src/google-workspace-rest-tools`: 15
files, 156 tests pass. Tests use synthetic fixtures and a fake `fetch`
only.
- `cd server && npx tsc --noEmit -p .`: passes.
- `pnpm verify`: exit 2 (the README is a broad input). Required CI
supplies the broad coverage.
- Tests prove: each tool maps messages exactly as `get_message` does;
separately stored bodies are fetched; a failed body read is an explicit
error; request counts at the maximum page size (51); invalid IDs send no
request; caller cancellation stops the call.

## Risks

- Low risk. Nothing outside the folder imports it yet
(`boundary.test.ts`).
- `get_draft`, `list_threads` and `search_messages` have no Google
schema. A switch back to Google's MCP servers would drop them unless
Google adds them.
- `list_labels` leaves out `threadsTotal` and `threadsUnread`: REST
returns them only with one request per label.
- One call reads at most 20 separately stored bodies. Beyond that it
returns an explicit "Could not read the full body of every message"
error and fetches none. The worst single call is 71 requests and 3,405
Gmail quota units (per-user limit: 6,000 per minute). A second maximal
call in the same minute can get a `rate_limited` tool error.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, via Claude Code with
tool use (file edits, shell, web fetch of Google's public reference).

## 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 09:51:17 -06:00
DottaandClaude Opus 5.5 a29658b5b7 feat(google-workspace): REST-backed Gmail tools in server/src/google-workspace-rest-tools (#22000)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Agents use Google Workspace through the built-in Google Workspace
connector
> - Today the connector sends each tool call to Google's hosted
Workspace MCP servers
> - Those servers are a Developer Preview, so we cannot offer them to
the public
> - The owner decided to launch on our own tools over Google's GA REST
APIs, with the same tool names, inputs and results, in one folder that
we can delete later
> - This pull request adds that folder inside the server, with its first
three Gmail tools, and a written trace of where it will plug in
> - The benefit is a public-ready Gmail path that we can switch back to
Google's MCP servers with one setting, with no new package to publish

## Linked Issues or Issue Description

Refs #21996 (plan and working memory for the Google Workspace connector,
section 9)
Refs #15412 (an earlier community Gmail REST MCP server; this code
follows Google's MCP tool contract instead)

**Subsystem affected**
Connections: the Google Workspace connector and the tool gateway.

**Problem or motivation**
Google's Workspace MCP servers are a Developer Preview. We cannot launch
the Google Workspace connector to the public on them.

**Proposed solution**
Add `server/src/google-workspace-rest-tools/`. It exposes the same tools
as Google's MCP servers over the GA REST APIs. It knows nothing about
Paperclip. A later pull request selects it per service with one switch.

**Alternatives considered**
A separate npm package (it would have to be published, because the
server is published) and a loopback or hosted HTTP MCP endpoint. The
README explains why an in-process call is better: speed, token handling,
and the egress guard, which refuses loopback addresses on public
deployments.

## What Changed

- New folder `server/src/google-workspace-rest-tools/` inside the
existing server package. No new npm package, no new dependency, no
workspace, Docker, lockfile or release change. Every changed file is in
this folder.
- `index.ts` is the only entry point. `boundary.test.ts` enforces that
the folder imports only its own files, Node built-ins,
`@modelcontextprotocol/sdk` and `zod` (both existing server
dependencies; tests may also import `vitest` and `@paperclipai/shared`),
and that no other server code imports the folder yet.
- `google-http.ts` is the only code that calls `googleapis.com`. It sets
the bearer header, allows only listed hosts, has a 20 s deadline and a
20 MB response limit, and retries a read once. It never retries a write.
Every write that fails after it is sent says that the change may or may
not have been made.
- Token errors are thrown as `GoogleWorkspaceAuthError` (an `McpError`).
401 gives `unauthenticated`. 403 insufficient scope gives
`insufficient_scope`, the code the gateway already recognizes. Other
Google errors become tool error results.
- Gmail tools `get_message`, `search_threads` and `create_draft`. Names,
fields, types and enums follow Google's Gmail MCP reference.
`create_draft` calls only `drafts.create`. There is no send tool.
- `get_message` reads bodies that Gmail stores as attachments, and
returns an explicit error, never a body-less `FULL_CONTENT` result.
Address lists are parsed by a single-pass RFC 5322 tokenizer
(`gmail/address.ts`, table-driven tests including IPv6 domain literals
and malformed input); quoted search text and long draft headers are
handled.
- `server.ts`: a thin MCP server (`tools/list`, `tools/call`).
`callGoogleWorkspaceRestTool()` is the in-process equivalent.
- `allowlist.test.ts` fails if a tool is not in `toolScopes` in
`packages/shared/src/google-workspace-connectors.ts`, if its read-only
hint disagrees with the allowlist, or if a description names a tool the
folder does not list.
- `README.md`: purpose, boundary rules, the dispatch trace, the
recommended in-process seam and the files the switch PR touches, known
differences from Google's tools, and the removal procedure (delete the
folder, set the switch to `google_mcp`).
- No code comments. Explanations are in the README.

## Verification

- `cd server && npx vitest run src/google-workspace-rest-tools`: 8
files, 98 tests pass. Tests use synthetic REST fixtures and a fake
fetch. No network.
- Each review fix has a test that fails on the revision before it (10
tests red on `d65b51d245`, 3 more red on `5d7d52ec86`). The boundary
test fails when a probe file in the folder imports `../errors.js`.
- `tsc --noEmit` with the server's `tsconfig.json` settings over the
folder passes.
- `pnpm check:module-boundaries` passes.
- `pnpm verify` exits 2 because a new file under `server/src` is a broad
input. Required CI supplies the broad coverage.
- Local timing probe with a stubbed fetch: about 13 µs overhead per
call.

## Risks

- Low risk. Nothing imports the folder yet, so runtime behavior does not
change.
- The tool descriptions are our own wording. A contract test against a
recorded `tools/list` from Google's servers comes in a later pull
request.
- The allowlist names three Gmail tools (`get_draft`, `list_threads`,
`search_messages`) that Google's published Gmail MCP reference does not
list. The README flags this.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use
(shell, file edits, web fetch of Google's public reference pages).

## 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
- [x] All Paperclip CI gates are green
- [x] 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 08:13:01 -06:00