-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: enhance MCP image handling with robustness and security controls #7925
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
Conversation
- Add configurable size and count limits for images (mcpMaxImagesPerResponse, mcpMaxImageSizeMB) - Implement image validation for corrupted/invalid base64 data - Add constants for supported image types (PNG, JPEG, GIF, WebP, SVG+XML, BMP) - Update McpExecutionStatus type to include optional images array - Process and validate images in useMcpToolTool with error handling - Update UI components (McpExecution) to display images from MCP responses - Preserve images when combining MCP server responses - Add comprehensive test coverage for edge cases Fixes #5233
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewing my own code is like debugging in a mirror - everything looks backward but the bugs are still mine.
|
|
||
| try { | ||
| // Try to decode to verify it's valid base64 | ||
| atob(base64Only) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the atob function guaranteed to be available in all environments where this code runs? I see it's mocked in tests, but we might want to add a fallback or check for its existence to prevent runtime errors in edge cases:
| atob(base64Only) | |
| try { | |
| // Try to decode to verify it's valid base64 | |
| if (typeof atob !== 'undefined') { | |
| atob(base64Only) | |
| } else { | |
| // Fallback for environments without atob | |
| Buffer.from(base64Only, 'base64').toString('binary') | |
| } | |
| return true | |
| } catch { | |
| return false | |
| } |
| return JSON.stringify(rest, null, 2) | ||
|
|
||
| // Check if we've reached the image limit | ||
| if (result.images.length >= maxImages) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the default limits (5 images × 2MB each), we could potentially hold ~10MB of base64 data in memory. Should we consider adding a warning in the settings description or documentation about memory implications when increasing these limits?
| } | ||
|
|
||
| function processToolContent(toolResult: any): string { | ||
| interface ProcessedContent { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ProcessedContent interface would benefit from JSDoc comments explaining what each field represents:
| interface ProcessedContent { | |
| /** | |
| * Result of processing MCP tool content with separated text and images | |
| */ | |
| interface ProcessedContent { | |
| /** Extracted text content from the tool response */ | |
| text: string | |
| /** Valid base64-encoded images that passed validation */ | |
| images: string[] | |
| /** Validation errors encountered during processing */ | |
| errors: string[] | |
| } |
| ) : ( | ||
| <Markdown markdown={response} partial={isPartial} /> | ||
| )} | ||
| {images.length > 0 && ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When image validation fails, users see the error message but have no way to retry or recover. Should we consider adding a retry mechanism or at least clearer guidance on how to resolve image loading issues?
| * Extract MIME type from a data URL | ||
| */ | ||
| export function extractMimeType(dataUrl: string): string | null { | ||
| const match = dataUrl.match(/^data:([a-z]+\/[a-z+-]+);base64,/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider extracting this regex pattern to a named constant for better maintainability:
| const match = dataUrl.match(/^data:([a-z]+\/[a-z+-]+);base64,/) | |
| const DATA_URL_MIME_PATTERN = /^data:([a-z]+\/[a-z+-]+);base64,/ | |
| export function extractMimeType(dataUrl: string): string | null { | |
| const match = dataUrl.match(DATA_URL_MIME_PATTERN) | |
| return match ? match[1] : null | |
| } |
|
@roomote-agent compare this implementation with PR 5185 |
|
@hannesrudolph I see your request to compare this PR with #5185. Let me analyze both implementations and provide you with a detailed comparison. Working on it now... |
|
@hannesrudolph Here is a detailed comparison between PR #7925 and PR #5185: 📊 Comparison SummaryBoth PRs aim to enhance MCP image handling, but PR #7925 takes a more robust, security-focused approach compared to PR #5185. 🔍 Key Differences1. Architecture & OrganizationPR #7925 ✅
PR #5185
2. Security & Validation 🔒PR #7925 ✅
PR #5185
3. Image Size Handling 📏PR #7925 ✅
PR #5185
4. Error Handling
|
Summary
This PR enhances the MCP (Model Context Protocol) image handling system with robust validation, configurable limits, and comprehensive error handling to address security and stability concerns raised in #5233.
Changes
🔒 Security & Validation
⚙️ Configuration
mcpMaxImagesPerResponse: Configurable limit for maximum images per MCP response (default: 5)mcpMaxImageSizeMB: Configurable limit for maximum image size in MB (default: 2MB)🏗️ Architecture
mcpImageConstants.tswith centralized validation utilities and constantsMcpExecutionStatustype to include optional images arrayuseMcpToolTool.tsto process and validate images with comprehensive error handlingcombineCommandSequences.tsto preserve images when combining MCP responses🎨 UI Improvements
McpExecution.tsxcomponent to display images from MCP responses🧪 Testing
Related Issues
Fixes #5233
Testing
Screenshots
N/A - Backend changes with UI updates for image display
Checklist
Important
Enhances MCP image handling with validation, configuration, and error handling, including UI updates and comprehensive testing.
useMcpToolTool.ts.global-settings.ts.useMcpToolTool.ts.mcpMaxImagesPerResponseandmcpMaxImageSizeMBtoglobalSettingsSchemainglobal-settings.ts.EVALS_SETTINGS.mcpImageConstants.tsfor image handling utilities.McpExecutionStatusinmcp.tsto include images.combineCommandSequences.tsto handle image data.McpExecution.tsxto display images in responses.mcpImageHandling.test.tsfor image validation tests.useMcpToolTool.spec.tsfor tool execution tests with images.This description was created by
for 6cb60ad. You can customize this summary. It will automatically update as commits are pushed.