christmas-mcp-image-describe
Version:
MCP server that analyzes images and provides structured metadata for website placement decisions using OpenAI GPT-4o Vision API
196 lines (175 loc) • 6.72 kB
JavaScript
// Christmas MCP Image Describe - Dedicated MCP Server Entry Point
// This file is specifically for MCP server functionality and VS Code integration
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import { analyzeImage, analyzeImages } from '../src/imageAnalyzer.js';
// Enhanced error handling and debugging
process.on('uncaughtException', (error) => {
console.error('[MCP Server] Uncaught Exception:', error);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('[MCP Server] Unhandled Rejection at:', promise, 'reason:', reason);
process.exit(1);
});
class ImageAnalysisMCPServer {
constructor() {
try {
this.server = new Server(
{
name: 'christmas-mcp-image-describe',
version: '1.0.4',
},
{
capabilities: {
tools: {},
},
}
);
this.setupHandlers();
console.error('[MCP Server] Server initialized successfully');
} catch (error) {
console.error('[MCP Server] Failed to initialize server:', error);
process.exit(1);
}
}
setupHandlers() {
try {
// List available tools
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
console.error('[MCP Server] Listing tools');
return {
tools: [
{
name: 'analyze_image',
description: 'Analyze a single image and return structured metadata for website placement decisions',
inputSchema: {
type: 'object',
properties: {
imagePath: {
type: 'string',
description: 'Path to the image file to analyze'
},
context: {
type: 'string',
description: 'Optional context about where the image will be used (e.g., "homepage hero", "about section")'
},
apiKey: {
type: 'string',
description: 'OpenAI API key'
},
projectId: {
type: 'string',
description: 'OpenAI project ID (optional)'
}
},
required: ['imagePath', 'apiKey']
}
},
{
name: 'analyze_images_batch',
description: 'Analyze multiple images in batch and return structured metadata for each',
inputSchema: {
type: 'object',
properties: {
imagePaths: {
type: 'array',
items: { type: 'string' },
description: 'Array of paths to image files to analyze'
},
context: {
type: 'string',
description: 'Optional context about where the images will be used'
},
apiKey: {
type: 'string',
description: 'OpenAI API key'
},
projectId: {
type: 'string',
description: 'OpenAI project ID (optional)'
}
},
required: ['imagePaths', 'apiKey']
}
}
]
};
});
// Handle tool calls
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
console.error(`[MCP Server] Tool called: ${request.params.name}`);
try {
if (request.params.name === 'analyze_image') {
const { imagePath, context, apiKey, projectId } = request.params.arguments;
if (!imagePath || !apiKey) {
throw new Error('Missing required parameters: imagePath and apiKey are required');
}
console.error(`[MCP Server] Analyzing image: ${imagePath}`);
const result = await analyzeImage(imagePath, context, apiKey, projectId);
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2)
}
]
};
}
if (request.params.name === 'analyze_images_batch') {
const { imagePaths, context, apiKey, projectId } = request.params.arguments;
if (!imagePaths || !Array.isArray(imagePaths) || !apiKey) {
throw new Error('Missing required parameters: imagePaths (array) and apiKey are required');
}
console.error(`[MCP Server] Analyzing ${imagePaths.length} images in batch`);
const results = await analyzeImages(imagePaths, context, apiKey, projectId);
return {
content: [
{
type: 'text',
text: JSON.stringify(results, null, 2)
}
]
};
}
throw new Error(`Unknown tool: ${request.params.name}`);
} catch (error) {
console.error(`[MCP Server] Tool execution error:`, error);
return {
content: [
{
type: 'text',
text: `Error: ${error.message}`
}
],
isError: true
};
}
});
console.error('[MCP Server] Handlers set up successfully');
} catch (error) {
console.error('[MCP Server] Failed to setup handlers:', error);
process.exit(1);
}
}
async run() {
try {
console.error('[MCP Server] Starting server transport...');
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error('[MCP Server] Server connected and running');
} catch (error) {
console.error('[MCP Server] Failed to start server:', error);
process.exit(1);
}
}
}
// Start the server
console.error('[MCP Server] Initializing Christmas MCP Image Describe server...');
const server = new ImageAnalysisMCPServer();
server.run().catch((error) => {
console.error('[MCP Server] Server failed to start:', error);
process.exit(1);
});