Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/snippets/servers/images.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Example showing image handling with MCPServer."""

import io

from PIL import Image as PILImage

from mcp.server.mcpserver import Image, MCPServer
Expand All @@ -12,4 +14,6 @@ def create_thumbnail(image_path: str) -> Image:
"""Create a thumbnail from an image"""
img = PILImage.open(image_path)
img.thumbnail((100, 100))
return Image(data=img.tobytes(), format="png")
buffer = io.BytesIO()
img.save(buffer, format="PNG")
return Image(data=buffer.getvalue(), format="png")
27 changes: 27 additions & 0 deletions tests/server/mcpserver/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# pyright: reportUnknownArgumentType=false

import json
from pathlib import Path

import pytest
from inline_snapshot import snapshot
Expand All @@ -19,6 +20,7 @@
ElicitRequestParams,
ElicitResult,
GetPromptResult,
ImageContent,
LoggingMessageNotification,
LoggingMessageNotificationParams,
NotificationParams,
Expand All @@ -40,6 +42,7 @@
basic_tool,
completion,
elicitation,
images,
mcpserver_quickstart,
notifications,
sampling,
Expand Down Expand Up @@ -342,3 +345,27 @@ async def test_structured_output() -> None:
assert "sunny" in result_text # condition
assert "45" in result_text # humidity
assert "5.2" in result_text # wind_speed


async def test_images_thumbnail(tmp_path: Path) -> None:
"""Test image thumbnail creation with valid PNG encoding."""
import base64
import io

from PIL import Image as PILImage

img_file = tmp_path / "test.png"
PILImage.new("RGB", (200, 200), (255, 0, 0)).save(img_file, format="PNG")

async with Client(images.mcp) as client:
result = await client.call_tool("create_thumbnail", {"image_path": str(img_file)})
assert len(result.content) == 1
content = result.content[0]
assert isinstance(content, ImageContent)
assert content.mime_type == "image/png"

# Verify decoded bytes form a valid PIL image
decoded = base64.b64decode(content.data)
with PILImage.open(io.BytesIO(decoded)) as decoded_img:
assert decoded_img.format == "PNG"
assert decoded_img.size == (100, 100)
Loading