Skip to content

feat(den-api): binary-safe capability content + MCP image delivery lanes - #3441

Merged
benjaminshafii merged 1 commit into
devfrom
feat/capability-binary-lane
Aug 3, 2026
Merged

benjaminshafii merged 1 commit into
devfrom
feat/capability-binary-lane

Conversation

@benjaminshafii

Copy link
Copy Markdown
Member

Problem

execute_capability runs on Den; results ride back to agents as JSON text. The Drive read capability fetches raw bytes correctly (alt=media) and then destroys them:

// google-workspace.ts (before)
const content = truncateText(await contentResponse.text(), 200_000)

.text() UTF-8-decodes binary, replacing invalid sequences with U+FFFD — measured live this session: a 52,223-byte PNG came back as 49,126 characters of unrecoverable mush. OneDrive reads refused binary outright (unsupported_content_type). And even clean base64 could never reach an agent as an image: the MCP wrapper hard-codes text-only parts and stringifies any downstream result containing image content.

What changed

L1 — shared dual-lane decoder (capability-sources/binary-content.ts)
Strict UTF-8 sniffing (TextDecoder(..., { fatal: true })) → text lane (existing truncation); otherwise base64 lane capped at 10 MiB. Content sniffing beats MIME checking: DXF (image/vnd.dxf, actually ASCII) stays text.

  • Google Drive drive-file/{fileId}: response gains contentBase64, encoding: "text"|"base64"|"none", contentUnavailableReason: "file_too_large"|null; metadata size pre-gate skips the content fetch entirely for oversized files. content unchanged for text files.
  • OneDrive drive-file/{itemId}: same contract; binary now returned instead of refused; byte-limited streaming kept; cap raised 5 MB → 10 MiB for parity.

L2 — MCP wrapper image lanes (mcp/tool-content.ts, used by invoke.ts + agent.ts)

  • Native lane: REST payloads carrying contentBase64 + mimeType: image/* emit a real MCP {type:"image"} part; >1 MiB images are downscaled with sharp (≤1568px, JPEG q80; sharp already a dep); the base64 is redacted from the text part (never double-shipped).
  • External lane: downstream MCP content arrays of known text/image parts pass through verbatim instead of being stringified — org-connected MCPs (Paper screenshots, future Drive MCPs) can now show agents actual images.

Upload symmetry note: the platform already accepts dataBase64 (Gmail attachments, Drive upload); this PR makes the read side match. Signed upload/download URLs for >10 MiB are a deliberate follow-up.

Tests run (all from ee/apps/den-api, bun test --conditions=development ...)

Suite Result
test/google-workspace-capabilities.test.ts 34 pass / 0 fail (5 new: binary byte-exact roundtrip, DXF text-sniffing, size pre-gate with zero content hits, text lane, Google Apps export unchanged)
test/microsoft-365-routes.test.ts 8 pass / 0 fail (5 new incl. byte-exact roundtrip, MIME-lies-text, folder/too-large)
test/mcp-tool-content.test.ts (new) 5 pass / 0 fail (small image passthrough+redaction, >1 MiB sharp downscale verified by decoding output, non-image untouched, external passthrough/fallback)
test/mcp-invoke-body.test.ts 9 pass / 0 fail
test/mcp-agent-timeouts.test.ts + test/agent-mcp-routes.test.ts 17 pass / 0 fail
pnpm --filter @openwork-ee/den-api exec tsc --noEmit clean

Known pre-existing failures (not this PR — verified via git stash -u baseline on dev tip 06225346f):

  • test/mcp-agent-config-policy.test.ts: 14 pass / 2 fail on clean baseline and identically with this diff (expectations drifted from the import-mcps body schema, e.g. marketplaceId no longer required — the live API confirms).
  • Multi-file combined bun test runs of DB-backed suites intermittently fail with mysql2 pool errors on baseline too (shared-process collision); every suite above passes solo.

Validation scope

Server-API change only — no UI surface, so no video; runtime behavior is proven by the HTTP-level integration tests above (real Hono app, fake Google/Graph upstreams, byte-exact assertions). After deploy to OpenWork Cloud, the end-to-end demo this was built for becomes runnable live: draw on a Daylight tablet → PNG syncs to Drive → agent says "use my latest drawing" → sees it → recreates it in Paper. Happy to record that as follow-up proof once deployed.

Follow-ups (deliberately out of scope)

  • Signed download/upload URLs for >10 MiB (bytes around the model, not through it)
  • admin-capabilities.ts image-part parity
  • Fix the two pre-existing mcp-agent-config-policy expectations

@vercel

vercel Bot commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
openwork-app Ready Ready Preview Aug 3, 2026 9:20am
openwork-den Ready Ready Preview Aug 3, 2026 9:20am
openwork-den-worker-proxy Ready Ready Preview Aug 3, 2026 9:20am
openwork-landing Ready Ready Preview, v0 Aug 3, 2026 9:20am
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
openwork-diagnostics Skipped Skipped Aug 3, 2026 9:20am

Drive/OneDrive file reads decoded fetched bytes as UTF-8 text,
irreversibly corrupting binary files (PNG etc). Native and external
MCP results were also flattened to text-only parts, so agents could
never receive images.

- capability-sources/binary-content.ts: shared strict-UTF-8-or-base64
  decoder (content sniffing, 10 MiB binary cap) + relocated truncateText
- google-workspace drive-file route: dual-lane response (content |
  contentBase64 + encoding + contentUnavailableReason) with metadata
  size pre-gate before the content fetch
- microsoft-365 drive-file route: same contract; binary returned as
  base64 instead of unsupported_content_type; cap raised to 10 MiB
- mcp/tool-content.ts: execute_capability now emits MCP image parts
  for native image payloads (sharp downscale >1 MiB to <=1568px JPEG,
  base64 redacted from the text part) and passes through downstream
  text|image content instead of stringifying it

@diff-warden diff-warden Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warden security clearance: clear. No new security issues found in this diff (ccf38dcc89a205ed399cb47346ab61acbc4ff2cb). Automated clearance satisfies the required-review gate only — a human still reviews and merges. Analysis run

@benjaminshafii
benjaminshafii merged commit 892b12a into dev Aug 3, 2026
19 checks passed
@benjaminshafii
benjaminshafii deleted the feat/capability-binary-lane branch August 3, 2026 09:47
benjaminshafii pushed a commit that referenced this pull request Aug 3, 2026
…ext, storage-free (#3458)

Revives #3354 by @reachjalil onto current dev.

- apps/server: new openwork-cloud-uploads extension (drive_upload_file,
  gmail_create_draft_with_attachments) — authorized workspace paths in,
  multipart FormData out to Den; 4 MiB total enforced before network I/O;
  realpath containment incl. symlink escapes.
- den-api: POST /v1/direct-uploads/google-workspace/{drive-files,gmail-drafts}
  behind new cloudTransportRoute() (MCP bearer + mcp:write + org membership);
  bytes forward straight to Google, never persisted.
- Removes the model-facing base64 lanes: gmail-draft attachments field and the
  drive-files base64 upload capability. Gmail draft logic extracted into
  executeGmailDraft, shared by the JSON capability and the multipart route,
  preserving dev behavior (#3441 binary read lanes and #3449 formatting kept).
- Legacy flow deleted per the frozen corpus; successor proof is
  evals/specs/google-workspace-direct-uploads.slow.test.ts (real Den boot,
  mock-Google byte-exact witness, pre-network 413, no bytes in results).
- evals/tsconfig.json stops typechecking the frozen flow corpus, fixing the
  pre-existing red evals:typecheck (FlowContext.page in den-trim-page-headers).

Co-authored-by: Jalil <[email protected]>

This branch was successfully deployed

4 active and 1 inactive deployments
Preview – openwork-den-worker-proxy — ccf38dcc Deployed Aug 3, 2026 by vercel[bot]
Preview – openwork-den — ccf38dcc Deployed Aug 3, 2026 by vercel[bot]
Preview – openwork-app — ccf38dcc Deployed Aug 3, 2026 by vercel[bot]
Preview – openwork-landing — ccf38dcc Deployed Aug 3, 2026 by vercel[bot]
Preview – openwork-diagnostics — ccf38dcc Deployed Aug 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant