feat: image content handling

This commit is contained in:
Timothy Jaeryang Baek 2025-04-02 23:48:06 -07:00
parent 74dd291192
commit 074a8d4a0e

View File

@ -77,13 +77,26 @@ async def create_dynamic_endpoints(app: FastAPI, api_dependency=None):
result = await session.call_tool(endpoint_name, arguments=args) result = await session.call_tool(endpoint_name, arguments=args)
response = [] response = []
for content in result.content: for content in result.content:
text = content.text if isinstance(content, types.TextContent):
if isinstance(text, str): text = content.text
try: if isinstance(text, str):
text = json.loads(text) try:
except json.JSONDecodeError: text = json.loads(text)
pass except json.JSONDecodeError:
response.append(text) pass
response.append(text)
elif isinstance(content, types.ImageContent):
image_data = content.data
if isinstance(image_data, bytes):
image_data = image_data.decode("utf-8")
image_data = f"data:{content.mimeType};base64,{image_data}"
response.append(image_data)
elif isinstance(content, types.EmbeddedResource):
# TODO: Handle embedded resources
response.append("Embedded resource not supported yet.")
return response return response
return tool_endpoint return tool_endpoint