Title: Image content blocks >99 bytes fail with "illegal base64 at byte 0" · Issue #3123 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Image content blocks >99 bytes fail with "illegal base64 at byte 0" · Issue #3123 · modelcontextprotocol/python-sdk
X Title: Image content blocks >99 bytes fail with "illegal base64 at byte 0" · Issue #3123 · modelcontextprotocol/python-sdk
Description: Image Content Blocks >99 Bytes Fail Client-Side Decode in stdio Mode Summary Image content blocks returned via _mcp_content_blocks fail client-side base64 decoding when the decoded image bytes exceed 99 bytes, despite the server emitting...
Open Graph Description: Image Content Blocks >99 Bytes Fail Client-Side Decode in stdio Mode Summary Image content blocks returned via _mcp_content_blocks fail client-side base64 decoding when the decoded image bytes exce...
X Description: Image Content Blocks >99 Bytes Fail Client-Side Decode in stdio Mode Summary Image content blocks returned via _mcp_content_blocks fail client-side base64 decoding when the decoded image bytes e...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3123
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Image content blocks >99 bytes fail with \"illegal base64 at byte 0\"","articleBody":"# Image Content Blocks >99 Bytes Fail Client-Side Decode in stdio Mode\n\n## Summary\n\nImage content blocks returned via `_mcp_content_blocks` fail client-side base64 decoding when the decoded image bytes exceed **99 bytes**, despite the server emitting spec-compliant MCP protocol messages. The issue manifests as `\"illegal base64 data at input byte 0\"` errors in the client, while server wire logs confirm correct base64 encoding with proper padding.\n\n## Environment\n\n**Server:**\n- Python SDK: `mcp==1.28.1`\n- Python: 3.12\n- Transport: stdio (JSON-RPC over stdin/stdout)\n- OS: Linux (Ubuntu-based)\n\n**Client:**\n- Claude Code (Anthropic's MCP client)\n- Error source: Go `encoding/base64` (based on error message format)\n\n**Dependencies:**\n```\nmcp==1.28.1\nstarlette==1.3.1\npydantic==2.13.4\nfastapi==0.139.2\n```\n\n## Issue Description\n\n### Observed Behavior\n\nImages returned in MCP content blocks exhibit a **binary size threshold at exactly 100 bytes** (decoded image data):\n\n| Decoded Bytes | Base64 Chars | Result | Error |\n|---------------|--------------|--------|-------|\n| ≤99 | ≤132 | ✅ Renders | None |\n| ≥100 | ≥136 | ❌ Fails | \"illegal base64 data at input byte 0\" |\n\n### Test Results\n\n**Pass Class (renders successfully):**\n- 48 bytes (64 base64 chars): ✅ Visual confirmation\n- 99 bytes (132 base64 chars): ✅ Visual confirmation\n\n**Fail Class (decode error):**\n- 102 bytes (136 base64 chars): ❌ \"illegal base64 data at input byte 0\"\n- 4,412 bytes (5,884 base64 chars): ❌ \"illegal base64 data at input byte 0\"\n- 5,108 bytes (6,812 base64 chars): ❌ \"illegal base64 data at input byte 0\"\n\n### Additional Finding (SDK Version Dependent)\n\nWith SDK 1.26.0, payloads >4KB exhibited a second error class:\n- **\"Incorrect padding\"** (Python `binascii` error)\n\nThis was **fixed by upgrading to SDK 1.28.1**, leaving only the 100-byte threshold issue.\n\n## Wire-Level Evidence\n\nServer logs confirm **spec-compliant** image blocks are emitted:\n\n```\n[ISS-011-WIRE] Image block: data_len=64, first_char='U', last_char='B', \n has_mimeType=True, block_keys=['type', 'data', 'mimeType']\nResult: ✅ RENDERED\n\n[ISS-011-WIRE] Image block: data_len=6812, first_char='U', last_char='=', \n has_mimeType=True, block_keys=['type', 'data', 'mimeType']\nResult: ❌ illegal base64 data at input byte 0\n\n[ISS-011-WIRE] Image block: data_len=5884, first_char='U', last_char='=', \n has_mimeType=True, block_keys=['type', 'data', 'mimeType']\nResult: ❌ illegal base64 data at input byte 0\n```\n\n**Analysis:**\n- ✅ Correct keys: `['type', 'data', 'mimeType']`\n- ✅ Valid base64 start: `'U'` (valid character)\n- ✅ Proper padding: `'='` where expected\n- ✅ No wrapping, prefixing, or unexpected keys\n- ❌ **Client receives \"illegal at byte 0\"** despite clean server output\n\n## Reproduction\n\n### Minimal Server Code\n\n```python\nimport base64\nfrom PIL import Image\nimport io\n\ndef img_test_base64_transport(test_variant: int = 0):\n \"\"\"\n Test image content blocks at different sizes.\n Variant 0: 48 bytes (passes)\n Variant 1: 102 bytes (fails)\n \"\"\"\n if test_variant == 0:\n # Small image: 4x4 solid color\n img = Image.new('RGB', (4, 4), color=(128, 128, 128))\n else:\n # Larger image: ~102 bytes\n img = Image.new('RGB', (6, 6), color=(128, 128, 128))\n \n buf = io.BytesIO()\n img.save(buf, format='WEBP', quality=100, method=6)\n webp_bytes = buf.getvalue()\n \n # Encode to base64\n b64_data = base64.b64encode(webp_bytes).decode(\"utf-8\")\n \n # Ensure padding (though b64encode already does this)\n missing_padding = len(b64_data) % 4\n if missing_padding:\n b64_data += '=' * (4 - missing_padding)\n \n # Return as MCP image content block\n return {\n \"success\": True,\n \"test_variant\": test_variant,\n \"actual_b64_length\": len(b64_data),\n \"bytes_emitted\": len(webp_bytes),\n \"b64_padding_check\": len(b64_data) % 4,\n \"mime_type\": \"image/webp\",\n \"_mcp_content_blocks\": [\n {\n \"type\": \"image\",\n \"data\": b64_data,\n \"mimeType\": \"image/webp\"\n }\n ]\n }\n```\n\n### MCP Tool Registration\n\n```python\n@server.call_tool()\nasync def test_image_size(test_variant: int = 0):\n \"\"\"Test image at different sizes to reproduce 100-byte threshold bug.\"\"\"\n result = img_test_base64_transport(test_variant)\n \n # Extract content blocks\n content_blocks = result.pop(\"_mcp_content_blocks\", [])\n \n # Return content blocks + metadata\n return content_blocks + [{\"type\": \"text\", \"text\": json.dumps(result)}]\n```\n\n### Steps to Reproduce\n\n1. Run MCP server with test tool above\n2. Connect with Claude Code client\n3. Call `test_image_size(test_variant=0)` → ✅ Image renders\n4. Call `test_image_size(test_variant=1)` → ❌ \"illegal base64 data at input byte 0\"\n\n## Expected Behavior\n\nImage content blocks should render regardless of size, as long as:\n1. Base64 encoding is valid (length % 4 == 0)\n2. Decoded bytes are valid WebP/image data\n3. Block structure follows MCP spec: `{\"type\": \"image\", \"data\": \"\u003cbase64>\", \"mimeType\": \"image/webp\"}`\n\n## Actual Behavior\n\nImages >99 decoded bytes fail with:\n```\nImage '\u003cuuid>.webp' could not be processed: illegal base64 data at input byte 0\n```\n\nThis error indicates the **first character received by the client decoder is not valid base64**, despite server logs showing `first_char='U'` (valid base64).\n\n## Root Cause Analysis\n\nThe evidence suggests:\n\n1. **Server-side is correct:**\n - Wire logs show proper base64 encoding\n - First/last characters are valid\n - Padding is correct (len % 4 == 0)\n - No unexpected keys or wrapping\n\n2. **Client-side has size threshold:**\n - Exact boundary at 100 decoded bytes\n - Error message from Go `encoding/base64` (not Python)\n - Suggests client code path branches on size:\n ```go\n if len(imageBytes) > 100 {\n // Wrapper/attachment path (breaks base64)\n } else {\n // Inline path (works)\n }\n ```\n\n3. **Hypothesis:**\n - Client wraps/prefixes large payloads before decoding\n - Wrapper invalidates base64 (illegal first byte)\n - 100 bytes is a human-round-number threshold (not power-of-2)\n\n## Impact\n\n- **Thumbnails fail:** 128x128 WebP thumbnails are ~3-5KB (always fail)\n- **Image preview broken:** Cannot display images in MCP responses\n- **Workaround required:** External file references instead of inline images\n\n## Diagnostic Data Available\n\nWe have comprehensive test results including:\n- 10 test fixtures spanning 48-6,812 bytes\n- Server-side wire logs for all sizes\n- Exact boundary detection (99 vs 100 bytes)\n- SDK version comparison (1.26.0 vs 1.28.1)\n\nHappy to provide additional test cases or logs if needed.\n\n## Related Issues\n\n- #2376 - Image/ImageContent serialization fails in stateless HTTP mode (possibly related)\n\n## Proposed Fix\n\nSince server-side emits correct data, the fix likely needs to be in:\n\n1. **Client SDK** (Claude Code's MCP harness) - remove size-based branching for image content blocks\n2. **Or MCP spec clarification** - if >100-byte images require different encoding, document it\n\n---\n\n**CC:** @anthropics/mcp-team\n\n**Test server available:** We have a fully instrumented test server with wire-level logging if needed for debugging.","author":{"url":"https://github.com/PeeperFrog","@type":"Person","name":"PeeperFrog"},"datePublished":"2026-07-18T00:19:29Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3123"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0e4c535b-1ef3-9615-8b4b-0b57bd4572cc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C2F4:29AEE8:9841FC:CCD8A5:6B00E898 |
| html-safe-nonce | 0b1da374ec627d127613733361325d772bfdb133606ccdd023c3af4ab915e73b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMkY0OjI5QUVFODo5ODQxRkM6Q0NEOEE1OjZCMDBFODk4IiwidmlzaXRvcl9pZCI6Ijc3NDY1MzU3OTU3NzQ3Nzc0OTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 24a837678a9b856ae255a55c42ce234594663779b801c5c6d618a18bbed3fc48 |
| hovercard-subject-tag | issue:4915765224 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3123/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ba05fc784b91f46646c48f26493bde984023cd86398861fd7f91357c2a46f044/modelcontextprotocol/python-sdk/issues/3123 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ba05fc784b91f46646c48f26493bde984023cd86398861fd7f91357c2a46f044/modelcontextprotocol/python-sdk/issues/3123 |
| og:image:alt | Image Content Blocks >99 Bytes Fail Client-Side Decode in stdio Mode Summary Image content blocks returned via _mcp_content_blocks fail client-side base64 decoding when the decoded image bytes exce... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | PeeperFrog |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width