forked from cline/cline
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: enhance MCP tool response handling to support image responses #5185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shivangag
wants to merge
6
commits into
RooCodeInc:main
Choose a base branch
from
IdenWorks:feat/support-mcp-image
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,208
−51
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a3eb18
feat: enhance MCP tool response handling to support images
shivangag c9965b2
feat: enhance image handling in MCP tool processing
shivangag 316dbbd
feat: add MCP image handling configuration options
shivangag e2634cd
refactor: centralize supported image types in MCP tool processing
shivangag a416090
test: add image validation tests for unsupported and malformed MIME t…
shivangag 595ae62
Add validation and i18n for MCP image settings
shivangag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,24 +89,39 @@ async function sendExecutionStatus(cline: Task, status: McpExecutionStatus): Pro | |
}) | ||
} | ||
|
||
function processToolContent(toolResult: any): string { | ||
function processToolContent(toolResult: any): { text: string; images: string[] } { | ||
if (!toolResult?.content || toolResult.content.length === 0) { | ||
return "" | ||
return { text: "", images: [] } | ||
} | ||
|
||
return toolResult.content | ||
.map((item: any) => { | ||
if (item.type === "text") { | ||
return item.text | ||
const textParts: string[] = [] | ||
const images: string[] = [] | ||
shivangag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
toolResult.content.forEach((item: any) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Images are validated sequentially in the forEach loop. For responses with many images, this could be slow. Consider batching validation or using Promise.all for parallel processing: const validatedImages = await Promise.all(
toolResult.content
.filter((item: any) => item.type === "image")
.map(async (item: any) => validateAndProcessImage(item))
);
images.push(...validatedImages.filter(Boolean)); This would improve performance for MCP tools that return multiple images. |
||
if (item.type === "text") { | ||
textParts.push(item.text) | ||
} else if (item.type === "image") { | ||
if (item.data && item.mimeType) { | ||
const validImageTypes = ["image/png", "image/jpeg", "image/gif", "image/webp"] | ||
daniel-lxs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (validImageTypes.includes(item.mimeType)) { | ||
const dataUrl = `data:${item.mimeType};base64,${item.data}` | ||
daniel-lxs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
images.push(dataUrl) | ||
daniel-lxs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
console.warn(`Unsupported image MIME type: ${item.mimeType}`) | ||
} | ||
} else { | ||
console.warn("Invalid MCP ImageContent: missing data or mimeType") | ||
} | ||
if (item.type === "resource") { | ||
const { blob: _, ...rest } = item.resource | ||
return JSON.stringify(rest, null, 2) | ||
} | ||
return "" | ||
}) | ||
.filter(Boolean) | ||
.join("\n\n") | ||
} else if (item.type === "resource") { | ||
const { blob: _, ...rest } = item.resource | ||
textParts.push(JSON.stringify(rest, null, 2)) | ||
} | ||
}) | ||
|
||
return { | ||
text: textParts.filter(Boolean).join("\n\n"), | ||
images, | ||
} | ||
} | ||
|
||
async function executeToolAndProcessResult( | ||
|
@@ -130,11 +145,13 @@ async function executeToolAndProcessResult( | |
const toolResult = await cline.providerRef.deref()?.getMcpHub()?.callTool(serverName, toolName, parsedArguments) | ||
|
||
let toolResultPretty = "(No response)" | ||
let images: string[] = [] | ||
|
||
if (toolResult) { | ||
const outputText = processToolContent(toolResult) | ||
const { text: outputText, images: outputImages } = processToolContent(toolResult) | ||
images = outputImages | ||
|
||
if (outputText) { | ||
if (outputText || images.length > 0) { | ||
await sendExecutionStatus(cline, { | ||
executionId, | ||
status: "output", | ||
|
@@ -160,8 +177,8 @@ async function executeToolAndProcessResult( | |
}) | ||
} | ||
|
||
await cline.say("mcp_server_response", toolResultPretty) | ||
pushToolResult(formatResponse.toolResult(toolResultPretty)) | ||
await cline.say("mcp_server_response", toolResultPretty, images) | ||
pushToolResult(formatResponse.toolResult(toolResultPretty, images)) | ||
} | ||
|
||
export async function useMcpToolTool( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.