From fa3e327cac08b8a821b3d42af3a689f8774bacb0 Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Thu, 24 Apr 2025 12:13:28 -0400 Subject: [PATCH] Update README.md --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cad1d02e6..d0b83eb75 100644 --- a/README.md +++ b/README.md @@ -340,6 +340,7 @@ The below code requires the `pillow` library to be installed. ```python from mcp.server.fastmcp import FastMCP, Image +from io import BytesIO try: from PIL import Image as PILImage except ImportError: @@ -347,13 +348,14 @@ except ImportError: mcp = FastMCP("My App") - @mcp.tool() 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") + img.thumbnail((100, 100)) + buffer = BytesIO() + img.save(buffer, format="PNG") + return Image(data=buffer.getvalue(), format="png") ``` Return the `Image` helper class from your tool to send an image to the client. The `Image` helper class handles the conversion to/from the base64-encoded format required by the MCP protocol. It works with either a path to an image file, or a bytes object.