30 lines
980 B
Python
30 lines
980 B
Python
import os
|
|
import base64
|
|
import mimetypes
|
|
import logging
|
|
from typing import Any, Dict
|
|
|
|
logger = logging.getLogger("MattCP")
|
|
|
|
async def handle(args: Dict[str, Any]):
|
|
"""
|
|
Reads an image file from disk and returns it as a base64 encoded string
|
|
within an MCP ImageContent object.
|
|
"""
|
|
path = args.get("path")
|
|
if not path:
|
|
return {"text": "Error: Missing path argument"}
|
|
|
|
# Validate that the file has an image-like MIME type
|
|
mime_type, _ = mimetypes.guess_type(path)
|
|
if not mime_type or not mime_type.startswith("image/"):
|
|
return {"text": f"Error: File {path} is not a supported image format."}
|
|
|
|
try:
|
|
with open(path, "rb") as f:
|
|
# Read raw bytes and encode to base64 for transport
|
|
data = base64.b64encode(f.read()).decode("utf-8")
|
|
return {"type": "image", "data": data, "mimeType": mime_type}
|
|
except Exception as e:
|
|
return {"text": f"Error reading image: {str(e)}"}
|