This commit is contained in:
2026-07-22 22:53:15 -07:00
parent d87fcb85be
commit 48132aa21c
6 changed files with 149 additions and 135 deletions
+12 -11
View File
@@ -1,8 +1,9 @@
import os
import base64
import mimetypes
import logging
from pathlib import Path
from typing import Any, Dict
from tools.utils import ToolError
logger = logging.getLogger("MattCP")
@@ -11,19 +12,19 @@ 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"}
path_str = args.get("path")
if not path_str:
raise ToolError("Missing path argument")
path = Path(path_str)
# Validate that the file has an image-like MIME type
mime_type, _ = mimetypes.guess_type(path)
mime_type, _ = mimetypes.guess_type(path_str)
if not mime_type or not mime_type.startswith("image/"):
return {"text": f"Error: File {path} is not a supported image format."}
raise ToolError(f"File {path_str} 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}
data = base64.b64encode(path.read_bytes()).decode("utf-8")
return {"type": "image", "data": data, "mimeType": mime_type}
except Exception as e:
return {"text": f"Error reading image: {str(e)}"}
raise ToolError(f"Error reading image: {str(e)}")