Add support for image-based tool responses

This commit is contained in:
Dominik Jain 2025-09-27 15:37:08 +02:00 committed by Dominik Jain
parent 13cb1aee0f
commit 77f4a978be

View File

@ -2,6 +2,7 @@ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
type CallToolContent = CallToolResult["content"][number]; type CallToolContent = CallToolResult["content"][number];
type TextItem = Extract<CallToolContent, { type: "text" }>; type TextItem = Extract<CallToolContent, { type: "text" }>;
type ImageItem = Extract<CallToolContent, { type: "image" }>;
class TextContent implements TextItem { class TextContent implements TextItem {
[x: string]: unknown; [x: string]: unknown;
@ -9,12 +10,36 @@ class TextContent implements TextItem {
constructor(public text: string) {} constructor(public text: string) {}
} }
export class TextResponse implements CallToolResult { class ImageContent implements ImageItem {
[x: string]: unknown; [x: string]: unknown;
content: CallToolContent[]; // <- IMPORTANT: protocols union readonly type = "image" as const;
constructor(text: string) {
this.content = [new TextContent(text)]; /**
* @param data - Base64-encoded image data
* @param mimeType - MIME type of the image (e.g., "image/png")
*/
constructor(
public data: string,
public mimeType: string
) {}
}
class PNGImageContent extends ImageContent {
constructor(data: Uint8Array) {
super(Buffer.from(data).toString("base64"), "image/png");
} }
} }
export type ToolResponse = TextResponse; export class ToolResponse implements CallToolResult {
[x: string]: unknown;
content: CallToolContent[]; // <- IMPORTANT: protocols union
constructor(content: CallToolContent[]) {
this.content = content;
}
}
export class TextResponse extends ToolResponse {
constructor(text: string) {
super([new TextContent(text)]);
}
}