fix(snippets): encode thumbnail as PNG in images example (#3805)

Closed
TE
21d ago
+32-1

Fixes #3805

In examples/snippets/servers/images.py, the create_thumbnail tool previously returned Image(data=img.tobytes(), format="png"). Pillow's tobytes() produces raw uncompressed RGB pixel bytes lacking PNG file headers, chunks, and compression, causing downstream clients that attempt to decode the data URL (data:image/png;base64,...) to fail with UnidentifiedImageError or invalid image decode errors.

This PR encodes the resized image into a valid in-memory PNG stream via io.BytesIO() and img.save(buffer, format="PNG"), returning true PNG bytes. An integration test has also been added in tests/server/mcpserver/test_integration.py to verify valid PNG container headers and correct thumbnail dimensions.

Motivation and Context

Fixes #3805. Clients calling create_thumbnail expect valid PNG encoded bytes as indicated by format="png".

How Has This Been Tested?

  • Added test_images_thumbnail to tests/server/mcpserver/test_integration.py.
  • Tested locally with uv run pytest tests/server/mcpserver/test_integration.py -v (11 passed).
  • Formatted and linted with uv run ruff check and uv run ruff format.

Breaking Changes

No breaking changes. Only fixes erroneous encoding in the example snippet.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I am assigned to the linked issue (or it is labeled help wanted, or I'm a maintainer)
  • I have disclosed any AI assistance and can explain the change in my own words
  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Awaiting issue assignment on #3805 to pass intake gate verification.