openrouter-image-mcp
Version:
MCP server for image analysis using OpenRouter's vision models
84 lines (83 loc) • 3.38 kB
JavaScript
import { ImageProcessor } from '../utils/image-processor.js';
export async function handleAnalyzeImage(args, config, openRouterClient, logger) {
const imageProcessor = ImageProcessor.getInstance();
try {
const imageInput = {
type: args.type,
data: args.data,
mimeType: args.mimeType,
};
const options = {
prompt: args.prompt,
format: args.format,
maxTokens: args.maxTokens,
temperature: args.temperature,
};
logger.info(`Starting image analysis for type: ${imageInput.type}`);
// Add timeout for image processing (30 seconds)
const processTimeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Image processing timed out after 30 seconds')), 30000);
});
// Process the image with timeout
const processedImage = await Promise.race([
imageProcessor.processImage(imageInput),
processTimeoutPromise
]);
// Validate image type
if (!imageProcessor.isValidImageType(processedImage.mimeType)) {
throw new Error(`Unsupported image type: ${processedImage.mimeType}`);
}
// Check file size
const serverConfig = config.getServerConfig();
const maxImageSize = serverConfig.maxImageSize || 10485760;
if (processedImage.size > maxImageSize) {
throw new Error(`Image size ${processedImage.size} exceeds maximum allowed size ${maxImageSize}`);
}
// Add timeout for the API call (120 seconds)
const apiTimeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Image analysis timed out after 2 minutes')), 120000);
});
// Analyze the image with timeout
const analysisPromise = openRouterClient.analyzeImage(processedImage.data, processedImage.mimeType, options.prompt || 'Analyze this image in detail. Describe what you see, including objects, people, text, and any notable features.', options);
const result = await Promise.race([analysisPromise, apiTimeoutPromise]);
if (!result.success) {
throw new Error(result.error || 'Failed to analyze image');
}
logger.info(`Image analysis completed successfully`, {
model: result.model,
usage: result.usage,
});
return {
content: [
{
type: 'text',
text: result.analysis || 'No analysis available',
},
],
};
}
catch (error) {
logger.error('Image analysis failed', error);
// Check if it's a timeout error
if (error instanceof Error && error.message.includes('timed out')) {
return {
content: [
{
type: 'text',
text: `Error: ${error.message}. The image may be too large or the server is experiencing delays.`,
},
],
isError: true,
};
}
return {
content: [
{
type: 'text',
text: `Error: ${error.message}`,
},
],
isError: true,
};
}
}