test-mcp-images.jsโข3.14 kB
#!/usr/bin/env node
/**
* Test script demonstrating proper MCP image responses using jimp
*/
const Jimp = require('jimp');
async function generateTestImage() {
console.log('๐จ Testing MCP Image Response Format\n');
try {
// Create a test image with jimp
const image = new Jimp(400, 200, '#3498db'); // Blue background
// Add text to the image
const font = await Jimp.loadFont(Jimp.FONT_SANS_32_WHITE);
image.print(
font,
0,
0,
{
text: 'MCP Fullstack\nImage Response',
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
},
400,
200
);
// Get the PNG buffer
const buffer = await image.getBufferAsync(Jimp.MIME_PNG);
// Create proper MCP response format
const mcpResponse = {
content: [
{
type: 'image',
data: buffer.toString('base64'),
mimeType: 'image/png'
}
],
// Additional metadata
width: image.getWidth(),
height: image.getHeight(),
message: 'Successfully generated test image'
};
console.log('โ
Generated MCP Image Response:');
console.log(' - Image Type: PNG');
console.log(` - Dimensions: ${mcpResponse.width}x${mcpResponse.height}`);
console.log(` - Base64 Size: ${mcpResponse.content[0].data.length} chars`);
console.log(' - Format: MCP content array with type "image"');
// Show the response structure (truncated)
console.log('\n๐ฆ Response Structure:');
console.log(JSON.stringify({
content: [
{
type: mcpResponse.content[0].type,
data: mcpResponse.content[0].data.substring(0, 50) + '...',
mimeType: mcpResponse.content[0].mimeType
}
],
width: mcpResponse.width,
height: mcpResponse.height,
message: mcpResponse.message
}, null, 2));
console.log('\nโจ This is the proper MCP format for returning images!');
console.log(' Tools should return content: [{type: "image", data: base64, mimeType: "..."}]');
// Test cropping functionality
console.log('\n๐ง Testing Image Cropping:');
const croppedImage = image.clone().crop(50, 50, 200, 100);
const croppedBuffer = await croppedImage.getBufferAsync(Jimp.MIME_PNG);
console.log(` โ
Cropped to 200x100 from (50,50)`);
// Test redaction functionality
console.log('\n๐ Testing Redaction (Black Box):');
const redactedImage = image.clone();
const blackBox = new Jimp(100, 50, '#000000');
redactedImage.composite(blackBox, 150, 75);
const redactedBuffer = await redactedImage.getBufferAsync(Jimp.MIME_PNG);
console.log(` โ
Applied black box redaction at (150,75)`);
console.log('\n๐ All image utilities working with jimp!');
console.log(' - No sharp dependency needed');
console.log(' - Works everywhere (pure JavaScript)');
console.log(' - Proper MCP content format');
} catch (error) {
console.error('โ Error:', error.message);
}
}
// Run the test
generateTestImage();