8000 fix: return binary HTTP tool responses as MCP content by avelino · Pull Request #128 · avelino/mcp · GitHub
[go: up one dir, main page]

Skip to content

fix: return binary HTTP tool responses as MCP content - #128

Merged
avelino merged 1 commit into
mainfrom
avelino/fix/binary-response-base64
Oct 20, 2026
Merged

avelino merged 1 commit into
mainfrom
avelino/fix/binary-response-base64

Conversation

@avelino
@avelino avelino commented Oct 20, 2026 •
Copy link
Copy Markdown
Owner

A generic HTTP tool such as whatsapp__api-call answers tools/call with the upstream HTTP body. WAHA's GET /api/files/avelino/<id>.jpeg comes back Content-Type: image/jpeg and the file itself. The HTTP transport read every body with Response::text, which decodes as UTF-8 and replaces bytes it cannot represent with U+FFFD. The image that came back was not the image that was fetched.

Text, JSON and SSE still go through Response::text and the JSON-RPC parser, including charset handling. image/* becomes an image block (mimeType + base64 data). Every other non-text type becomes an embedded resource with mimeType and a base64 blob. Those are the tool-result shapes in the 2025-06-18 schema (ImageContent.data / mimeType, BlobResourceContents.blob / mimeType) and the same blocks in the 2026-07-28 tools page. Audio stays an embedded resource: the split here is image versus everything else, not a third content type.

The CLI's typed Content keeps the nested resource, so a PDF is not dropped when a direct call is printed as JSON. The proxy already relays the raw result, so whatsapp__api-call through mcp serve was never going through that struct.

How it was tested

cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test

cargo test: 989 passed, 0 failed.

The fixture is src/transport/fixtures/waha-8x8.jpeg, a real baseline JPEG (JFIF, 8x8, 225 bytes, sha256 4f1093edc44e2aff91909bd7a6cb4b09b9b92b8cbc9158a82db68b06a7c49cb9). It is not valid UTF-8. String::from_utf8_lossy on it contains U+FFFD, which is the old bug.

  • transport::body::tests::jpeg_round_trips_as_image_content decodes the image block's base64 and checks the bytes and the sha256 against the fixture.
  • transport::http::tests::jpeg_tool_response_round_trips serves that file as Content-Type: image/jpeg on tools/call for GET /api/files/avelino/abc.jpeg and checks the same sha256 through HttpTransport.
  • transport::http::tests::pdf_tool_response_is_an_embedded_resource does the same for application/pdf (embedded resource blob, sha256 match, mimeType set).
  • transport::http::tests::json_and_text_tool_responses_are_unchanged checks application/json and text/plain still parse as JSON-RPC text content, not base64.
  • transport::http::tests::non_json_text_is_still_a_parse_error checks a non-JSON text body is still a parse error.
  • protocol::tests::test_content_embedded_resource_blob_round_trips checks the blob survives a typed Content round trip.

Not tested

No WAHA instance and no API key here, so this was not run against a live GET /api/files/avelino/<id>.jpeg. The local stub returns the same content type and a real JPEG body.

The content types follow the published spec: the 2025-06-18 schema (ImageContent, EmbeddedResource, BlobResourceContents) and the 2026-07-28 tools page.

Checklist

  • Tests added/updated
  • Docs updated
  • Follows project conventions
  • No breaking change for text, JSON or SSE responses

A generic HTTP tool such as whatsapp__api-call answers tools/call with
the upstream body. WAHA's GET /api/files/avelino/<id>.jpeg is
image/jpeg. The HTTP transport read every body with Response::text,
which decodes as UTF-8 and replaces bytes it cannot represent with
U+FFFD, so the image that came back was not the image that was fetched.

Text, JSON and SSE still go through Response::text and the JSON-RPC
parser. image/* becomes an image block (mimeType + base64 data). Every
other non-text type becomes an embedded resource with mimeType and a
base64 blob, the tool-result shapes in the 2025-06-18 and 2026-07-28
specs. The CLI's typed Content keeps the nested resource, so a PDF is
not dropped on the way to JSON output.

Signed-off-by: Avelino <31996+avelino@users.noreply.github.com>
@avelino
avelino merged commit b956440 into main Oct 20, 2026
10 checks passed
@avelino
avelino deleted the avelino/fix/binary-response-base64 branch October 20, 2026 11:12
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

0