Added to_data_uri method for Image class. (#2227)

* Added to_data_uri method and path_to_data_uri classmethod for Image class.

* Removed path_to_data_uri classmethod and modified _get_mime_type to use mimetypes.guess_type function instead of hardcoded dictionary.

* Register image/webp with mimetypes before guess_type to support WEBP mimetype detection on Python 3.10.

* Improved branch coverage for Image.to_data_uri.

* Added Image._to_data_uri example in the docs.
This commit is contained in:
Harshith Thota 2025-11-01 21:10:08 +05:30 committed by GitHub
commit 321f404046
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 75 additions and 30 deletions

View file

@ -115,6 +115,7 @@ For small icons or when you want to embed the icon directly, use data URIs:
```python
from mcp.types import Icon
from fastmcp.utilities.types import Image
# SVG icon as data URI
svg_icon = Icon(
@ -126,4 +127,13 @@ svg_icon = Icon(
def my_tool() -> str:
"""A tool with an embedded SVG icon."""
return "result"
# Generating a data URI from a local image file.
img = Image(path="./assets/brand/favicon.png")
icon = Icon(src=img.to_data_uri())
@mcp.tool(icons=[icon])
def file_icon_tool() -> str:
"""A tool with an icon generated from a local file."""
return "result"
```