From 570646fda36d1d32fbe6cfd4163fe717495c26cf Mon Sep 17 00:00:00 2001 From: Andrei Edell Date: Fri, 18 Apr 2025 13:33:40 -1000 Subject: [PATCH] MCP Image: Support image type messages / base64 text (#2962) * feat: MCP ImageContent support * feat: MCP ImageContent support * feat: MCP ImageContent support changeset * Update src/core/prompts/responses.ts Co-authored-by: Dennis Bartlett * Update src/core/Cline.ts Co-authored-by: Dennis Bartlett * feat: MCP ImageContent support run format:fix * Remove mcpToolResult * Fix: Display original data:image URLs in rich display mode to maintain transparency --------- Co-authored-by: rikaaa0928 Co-authored-by: rikaaa0928 <8528731+rikaaa0928@users.noreply.github.com> Co-authored-by: Dennis Bartlett Co-authored-by: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Co-authored-by: Andrei Edell --- .changeset/slimy-jeans-dance.md | 5 ++++ src/core/prompts/responses.ts | 1 + src/core/task/index.ts | 26 ++++++++++++++++--- .../mcp/chat-display/McpResponseDisplay.tsx | 2 +- 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 .changeset/slimy-jeans-dance.md diff --git a/.changeset/slimy-jeans-dance.md b/.changeset/slimy-jeans-dance.md new file mode 100644 index 00000000..f1387f99 --- /dev/null +++ b/.changeset/slimy-jeans-dance.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +MCP ImageContent as tool result diff --git a/src/core/prompts/responses.ts b/src/core/prompts/responses.ts index 6a4a9c21..520f52a8 100644 --- a/src/core/prompts/responses.ts +++ b/src/core/prompts/responses.ts @@ -2,6 +2,7 @@ import { Anthropic } from "@anthropic-ai/sdk" import * as diff from "diff" import * as path from "path" import { ClineIgnoreController, LOCK_TEXT_SYMBOL } from "../ignore/ClineIgnoreController" +import { McpToolCallResponse } from "../../shared/mcp" export const formatResponse = { duplicateFileReadNotice: () => diff --git a/src/core/task/index.ts b/src/core/task/index.ts index be3e40df..7de06703 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -2690,8 +2690,13 @@ export class Task { await this.say("mcp_server_request_started") // same as browser_action_result const toolResult = await this.mcpHub.callTool(server_name, tool_name, parsedArguments) - // TODO: add progress indicator and ability to parse images and non-text responses - const toolResultPretty = + // TODO: add progress indicator + + const toolResultImages = + toolResult?.content + .filter((item) => item.type === "image") + .map((item) => `data:${item.mimeType};base64,${item.data}`) || [] + let toolResultText = (toolResult?.isError ? "Error:\n" : "") + toolResult?.content .map((item) => { @@ -2706,8 +2711,21 @@ export class Task { }) .filter(Boolean) .join("\n\n") || "(No response)" - await this.say("mcp_server_response", toolResultPretty) - pushToolResult(formatResponse.toolResult(toolResultPretty)) + // webview extracts images from the text response to display in the UI + const toolResultToDisplay = + toolResultText + toolResultImages?.map((image) => `\n\n${image}`).join("") + await this.say("mcp_server_response", toolResultToDisplay) + + // MCP's might return images to display to the user, but the model may not support them + const supportsImages = this.api.getModel().info.supportsImages ?? false + if (toolResultImages.length > 0 && !supportsImages) { + toolResultText += `\n\n[${toolResultImages.length} images were provided in the response, and while they are displayed to the user, you do not have the ability to view them.]` + } + + // only passes in images if model supports them + pushToolResult( + formatResponse.toolResult(toolResultText, supportsImages ? toolResultImages : undefined), + ) await this.saveCheckpoint() diff --git a/webview-ui/src/components/mcp/chat-display/McpResponseDisplay.tsx b/webview-ui/src/components/mcp/chat-display/McpResponseDisplay.tsx index 71da16cc..195eedf1 100644 --- a/webview-ui/src/components/mcp/chat-display/McpResponseDisplay.tsx +++ b/webview-ui/src/components/mcp/chat-display/McpResponseDisplay.tsx @@ -162,7 +162,7 @@ const McpResponseDisplay: React.FC = ({ responseText }) try { const text = responseText || "" const matches: UrlMatch[] = [] - const urlRegex = /https?:\/\/[^\s<>"']+/g + const urlRegex = /(?:https?:\/\/|data:image)[^\s<>"']+/g let urlMatch: RegExpExecArray | null let urlCount = 0