Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Dec 4, 2025

This PR attempts to address Issue #9805. Feedback and guidance are welcome!

Problem

When invoking an MCP tool with an image parameter, the system was passing file paths instead of base64-encoded image data, preventing MCP servers from properly receiving and processing image inputs.

Solution

Modified UseMcpToolTool to automatically detect and convert image file paths to base64 data URLs before passing arguments to MCP servers.

Changes

  • Added image file path detection in UseMcpToolTool.ts
  • Implemented recursive conversion of image paths to base64 data URLs in tool arguments
  • Added support for nested image paths in complex argument objects
  • Preserved non-image paths and already-encoded base64 data unchanged
  • Added comprehensive test coverage for all image handling scenarios

Implementation Details

  1. Image Detection: Checks file extension against supported image formats and verifies file existence
  2. Base64 Conversion: Uses existing readImageAsDataUrlWithBuffer helper from imageHelpers.ts
  3. Recursive Processing: Handles images in nested objects and arrays within MCP tool arguments
  4. Error Handling: Falls back to original value if image conversion fails

Testing

  • ✅ All existing tests pass
  • ✅ Added 5 new test cases for image handling scenarios
  • ✅ Tests cover: basic conversion, nested paths, non-image files, already-encoded data, and error cases

Fixes #9805

- Add image file path detection in UseMcpToolTool
- Convert image file paths to base64 data URLs automatically
- Support nested image paths in complex argument objects
- Add comprehensive tests for image handling scenarios
- Preserve non-image paths and already-encoded base64 data

Fixes #9805
@roomote
Copy link
Contributor Author

roomote bot commented Dec 4, 2025

Rooviewer Clock   See task on Roo Cloud

Review complete. Found 3 issues that need attention:

  • Relative image paths won't resolve correctly - fs.stat() needs path resolution relative to task.cwd
  • Missing cwd parameter - helper methods need access to resolve paths properly
  • Silent error handling - image conversion failures should notify users via task.say()

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@roomote roomote bot mentioned this pull request Dec 4, 2025
2 tasks
Comment on lines +59 to +62
// Check if the file exists
try {
const stats = await fs.stat(value)
return stats.isFile()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relative image paths won't be resolved correctly. The fs.stat(value) call on line 61 uses the path as-is without resolving it relative to task.cwd. This means relative paths like "./images/photo.png" will fail the file existence check even if the file exists, preventing conversion to base64. Other tools like ReadFileTool resolve paths with path.resolve(task.cwd, relPath) before checking file existence.

Fix it with Roo Code or mention @roomote and request a fix.

Comment on lines +128 to +131
// Convert any image file paths in arguments to base64 data URLs
const processedArguments = parsedArguments
? ((await this.convertImagePathsToBase64(parsedArguments)) as Record<string, unknown>)
: parsedArguments
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isImageFilePath and convertImagePathsToBase64 methods need access to task.cwd to properly resolve relative paths, but they don't receive it as a parameter. To fix the path resolution issue, these methods should accept cwd: string as a parameter so they can resolve paths like path.resolve(cwd, value) before calling fs.stat().

Fix it with Roo Code or mention @roomote and request a fix.

Comment on lines +96 to +100
} catch (error) {
// If we can't read the image, return the original value
console.error(`Failed to convert image file to base64: ${error}`)
return obj
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image conversion errors are only logged to console and silently fall back to the original value. Users won't be notified when image conversion fails. Consider using await task.say("error", ...) to report conversion failures to the user, similar to how ReadFileTool handles image errors on line 382.

Fix it with Roo Code or mention @roomote and request a fix.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

mcp can't upload image base64

4 participants