Skip to content

fix(studio): normalize MCP image base64 before llama-server decode - #12080

Closed
Souravrajvi0 wants to merge 4 commits into
unslothai:mainfrom
Souravrajvi0:feat/mcp-image-base64-normalize-6ce1
Closed

Souravrajvi0 wants to merge 4 commits into
unslothai:mainfrom
Souravrajvi0:feat/mcp-image-base64-normalize-6ce1

Conversation

@Souravrajvi0

Copy link
Copy Markdown
Contributor

Summary

Fixes intermittent Invalid base64 value from llama-server when MCP tools return images in Unsloth Studio (reported in #12058).

MCP servers sometimes put a full data:<mime>;base64,<payload> string in the image data field, or wrap base64 across lines. The chat UI could still render those (tool cards concatenate mimeType + data), but promotion and llama-server decoding rejected them—especially on replay deep in context, which matches the “reprocess whole window then succeed” behavior.

Changes

  • Add normalize_mcp_image_b64() to peel data-URI prefixes and transport whitespace before decode.
  • Normalize at MCP flatten time, in _png_data_url, and in GGUF image_url normalization (bare base64 is coerced through the same path instead of being passed through unchanged).
  • Frontend mcpImageDataUrl() so tool cards do not double-wrap data URIs.
  • Backend and frontend unit tests.

Testing

  • Manual checks on CPU VM for data-URI and whitespace payloads (full GGUF vision + llama-server not run here—no GPU).
  • node --test studio/frontend/tests/mcp-images.test.ts — all pass.

Closes #12058

MCP tools sometimes return data URLs or line-wrapped base64 in image payloads.
The UI could still render those while promotion or llama-server rejected them
with Invalid base64 value on replay. Peel data-URI prefixes and whitespace at
ingestion and decode time, and coerce bare base64 through the same normalizer.
@danielhanchen

Copy link
Copy Markdown
Member

The (Python 3.13, l-r) failure here comes from this branch, not from main:

FAILED tests/test_remote_image_url_fetch_behaviour.py::TestRemoteUrlNeverReachesLlamaServer::test_bare_base64_is_payload_and_still_passes_through - AssertionError: assert ['data:image/...RU5ErkJggg==']

test_remote_image_url_fetch_behaviour.py passes on main at d27631e78 (40 passed). This PR's base64 normalisation in routes/inference.py now wraps a bare base64 image into a data:image/... URL, and that test pins bare base64 passing through untouched. Either the normalisation should leave bare base64 on that path alone, or, if wrapping it is the intended new behaviour, the test's expectation should move with it.

…ation

Keep MCP/data-URL base64 normalization on the data: branch only. Bare
base64 image_url values must still pass through unchanged after remote
inline, matching llama-server and test_remote_image_url_fetch_behaviour.
@danielhanchen

Copy link
Copy Markdown
Member

Thank you for the PR and for digging into #12058!

Closing this as superseded by #12236, which found the actual cause and closed #12058. Before each request Studio counts the prompt tokens with llama-server, and for that count it replaced every image with the text [image]. llama-server rejected that with "Invalid base64 value", and the failed count reserved the whole shared cache for that request, so the next turn reprocessed the history. #12236 leaves images out of that count.

The data URI / wrapped base64 path here cannot produce that error: Studio never forwards MCP image bytes to llama-server as received. _png_data_url in mcp_images.py decodes with validate=True and re-encodes to PNG, and the GGUF image_url path rebuilds the payload through _llama_image_data_url, so llama-server only ever sees freshly encoded base64. A payload with a data: prefix is dropped before it reaches llama-server. The report also showed the same image succeeding after the reprocess, which malformed data would not do.

If you have a real MCP server that sends a data URI in the data field, feel free to open a new PR scoped as hardening for that server, with a reproduction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Randomly Getting "Invalid base64 value" When Valid MCP Image Returns

2 participants