analyze_image
Extract detailed metadata from image files using the analyze_image tool on the image-reader MCP Server. Specify the image path to retrieve essential information for processing or analysis.
Instructions
Get detailed metadata about an image
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the image file |
Implementation Reference
- src/index.ts:336-383 (handler)Executes the analyze_image tool: validates image path, retrieves metadata and thumbnail using helper functions, formats and returns detailed image information as JSON or handles errors.case "analyze_image": { const { path: imagePath } = request.params.arguments as { path: string }; if (!imagePath) { throw new McpError(ErrorCode.InvalidParams, "Image path is required"); } if (!fs.existsSync(imagePath)) { throw new McpError(ErrorCode.InvalidRequest, `Image not found: ${imagePath}`); } try { const metadata = await getImageMetadata(imagePath); const thumbnail = await generateThumbnail(imagePath); return { content: [ { type: "text", text: JSON.stringify({ filename: metadata.filename, format: metadata.format, dimensions: `${metadata.width}x${metadata.height}`, size: { bytes: metadata.size, kilobytes: (metadata.size / 1024).toFixed(2), megabytes: (metadata.size / (1024 * 1024)).toFixed(2) }, created: metadata.created.toISOString(), modified: metadata.modified.toISOString(), path: metadata.path, thumbnail }, null, 2) } ] }; } catch (error) { return { content: [ { type: "text", text: `Error analyzing image: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
- src/index.ts:240-249 (schema)Input schema for the analyze_image tool, defining the required 'path' parameter.inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the image file" } }, required: ["path"] }
- src/index.ts:237-250 (registration)Registration of the analyze_image tool in the ListTools response, including name, description, and schema.{ name: "analyze_image", description: "Get detailed metadata about an image", inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the image file" } }, required: ["path"] } },
- src/index.ts:70-103 (helper)Helper function to retrieve detailed metadata (dimensions, format, size, timestamps) for an image using sharp and fs, with caching.async function getImageMetadata(imagePath: string): Promise<ImageMetadata> { // Check cache first if (imageMetadataCache.has(imagePath)) { return imageMetadataCache.get(imagePath)!; } try { // Use fs.promises.stat instead of fsExtra.stat const stats = await fs.promises.stat(imagePath); const metadata = await sharp(imagePath).metadata(); const imageMetadata: ImageMetadata = { path: imagePath, filename: path.basename(imagePath), format: metadata.format || path.extname(imagePath).replace('.', ''), width: metadata.width, height: metadata.height, size: stats.size, created: stats.birthtime, modified: stats.mtime }; // Cache the metadata imageMetadataCache.set(imagePath, imageMetadata); return imageMetadata; } catch (error) { console.error(`Error getting metadata for ${imagePath}:`, error); throw new McpError( ErrorCode.InternalError, `Failed to process image: ${error instanceof Error ? error.message : String(error)}` ); } }
- src/index.ts:108-122 (helper)Helper function to generate a base64-encoded thumbnail of the image using sharp, resized to max 300px width.async function generateThumbnail(imagePath: string, maxWidth = 300): Promise<string> { try { const buffer = await sharp(imagePath) .resize({ width: maxWidth, withoutEnlargement: true }) .toBuffer(); return `data:image/${path.extname(imagePath).replace('.', '')};base64,${buffer.toString('base64')}`; } catch (error) { console.error(`Error generating thumbnail for ${imagePath}:`, error); throw new McpError( ErrorCode.InternalError, `Failed to generate thumbnail: ${error instanceof Error ? error.message : String(error)}` ); } }