Skip to content

Return images and files from MCP results as media blocks - #331

Merged
tnkuehne merged 4 commits into
mainfrom
feat/mcp-media-blocks
Nov 23, 2026
Merged

tnkuehne merged 4 commits into
mainfrom
feat/mcp-media-blocks

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The MCP server sends every call and execute result as one JSON text block. An image a script returns reaches the model as base64 text it cannot see, and it counts against the 1 MiB result limit.

Change

Media blocks in a result become MCP content blocks.

  • call and execute find MCP image, audio and blob resource blocks in the result, at any depth.
  • Each one is sent as its own content block. The JSON keeps the block without its bytes, plus contentIndex, the block's index in content.
  • A block that the result repeats is sent once.
  • base64url, unpadded and line-wrapped data become standard base64.
  • Text resources are readable as JSON and stay in it. Values that are not valid blocks stay JSON unchanged.

Limits.

  • A result carries up to 1 MiB of JSON plus 5 MiB of media.
  • Service results inside execute may be up to 6 MiB, so a script can return a file it reads.
  • Service input stays limited to 1 MiB.

The execute description tells agents how to return images and files.

Tests

  • Unit tests cover lifting at any depth, deduplication, base64 normalization, text resources and invalid blocks staying JSON, and both limits.
  • The service integration test runs a script that returns a 2 MiB image in workerd and celld, and checks the text block and the image block.
  • Ran pnpm check and RUN_SERVICE_TESTS=1 for app-services-runtime.test.ts.

The docs follow in #332.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no new actionable issues were found.

Summary

Returns images, audio, and files as MCP content blocks while keeping JSON references to them.

  • MCP results send images, audio, and files as media blocks.

All four supplied previous threads are addressed: extra fields remain in JSON, large service responses have coverage, different metadata stays separate, and matching metadata shares one block. None of those threads supplied a Comment number.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Service call or script result] --> B[Find media blocks]
  B --> C[Compare blocks including metadata]
  C --> D[JSON with contentIndex references]
  C --> E[Unique media blocks]
  D --> F[Check JSON limit]
  E --> G[Check media limit]
  F --> H[MCP content array]
  G --> H
Loading

Reviews (4) · Last reviewed commit: "Compare media blocks independent of key ..." · Reviewed by Greptile

The MCP server sent every result as one JSON text block. An image a script
returned reached the model as base64 text it cannot see, and counted against
the 1 MiB result limit.

call and execute now move each MCP image, audio or blob resource block in a
result, at any depth, into its own content block. The JSON keeps the block
without its bytes and with contentIndex, the block's index in content. A block
the result repeats is sent once. base64url, unpadded and line-wrapped data
become standard base64. Text resources are readable as JSON and stay in it.

A result carries up to 1 MiB of JSON plus 5 MiB of media. Service results in
execute may be up to 6 MiB, so a script can return a file it reads. The execute
description tells agents how to return images and files.
Comment thread packages/app-runtime/src/mcp-media.ts Resolved
The JSON keeps every field of a lifted block except its bytes, and the
emitted block keeps MCP annotations and _meta. The service integration test
reads a 2 MiB file from a service and returns it as an image.
Comment thread packages/app-runtime/src/mcp-media.ts Resolved
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 61834a7 Nov 23 2026, 12:46 PM

Comment thread packages/app-runtime/src/mcp-media.ts Resolved
@jonathanlaib
jonathanlaib added this pull request to stack #334 November 23, 2026 12:51
@jonathanlaib
jonathanlaib removed this pull request from stack #334 November 23, 2026 12:53
@tnkuehne
tnkuehne merged commit 6c23363 into main Nov 23, 2026
16 checks passed
@tnkuehne
tnkuehne deleted the feat/mcp-media-blocks branch November 23, 2026 12:58
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