read-metadata
Extract and analyze specific or all metadata segments (EXIF, GPS, XMP, ICC, etc.) from images using the exifr library, supporting formats like path, URL, base64, or buffer.
Instructions
Read all or specified metadata segments from an image
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image | Yes | ||
| segments | No |
Implementation Reference
- src/tools/index.ts:69-84 (handler)The handler function that implements the core logic of the 'read-metadata' tool. It destructures the input arguments, loads the image buffer, builds parsing options from the segments parameter, uses exifr.parse to extract metadata, and returns a standardized success or error response.async (args, extra) => { try { const { image, segments } = args; const buf = await loadImage(image); const opts = buildOptions(segments as any); const meta = await exifr.parse(buf, opts); if (!meta || Object.keys(meta).length === 0) { return createErrorResponse('No metadata found in image'); } return createSuccessResponse(meta); } catch (error) { return createErrorResponse(`Error reading metadata: ${error instanceof Error ? error.message : String(error)}`); } }
- src/tools/index.ts:66-68 (schema)The Zod input schema for the 'read-metadata' tool, specifying the required 'image' source and optional 'segments' array for filtering metadata types.image: ImageSourceSchema, segments: z.array(z.enum(['EXIF', 'GPS', 'XMP', 'ICC', 'IPTC', 'JFIF', 'IHDR'])).optional() },
- src/tools/index.ts:63-86 (registration)The registration of the 'read-metadata' tool on the MCP server using server.tool(), including name, description, schema, and handler function. Also stores a reference in the tools object.const readMetadataTool = server.tool('read-metadata', "Read all or specified metadata segments from an image", { image: ImageSourceSchema, segments: z.array(z.enum(['EXIF', 'GPS', 'XMP', 'ICC', 'IPTC', 'JFIF', 'IHDR'])).optional() }, async (args, extra) => { try { const { image, segments } = args; const buf = await loadImage(image); const opts = buildOptions(segments as any); const meta = await exifr.parse(buf, opts); if (!meta || Object.keys(meta).length === 0) { return createErrorResponse('No metadata found in image'); } return createSuccessResponse(meta); } catch (error) { return createErrorResponse(`Error reading metadata: ${error instanceof Error ? error.message : String(error)}`); } } ); tools['read-metadata'] = readMetadataTool;
- src/tools/index.ts:54-60 (schema)Zod schema definition for the 'image' input parameter, used in the read-metadata tool schema and shared across other image tools.const ImageSourceSchema = z.object({ kind: z.enum(['path', 'url', 'base64', 'buffer']), path: z.string().optional(), url: z.string().optional(), data: z.string().optional(), buffer: z.string().optional() });
- src/tools/segments.ts:24-73 (helper)Helper function buildOptions that creates exifr parsing options based on the requested segments, used specifically in the read-metadata handler to configure which metadata segments to extract.export function buildOptions(segments?: SegmentType[]): ExifrOptions { // Default options - include everything if segments not specified if (!segments || segments.length === 0) { return { tiff: true, // Includes EXIF and GPS xmp: true, icc: true, iptc: true, jfif: true, ihdr: true, }; } // Start with all segments disabled const options: ExifrOptions = { tiff: false, xmp: false, icc: false, iptc: false, jfif: false, ihdr: false, }; // Enable requested segments segments.forEach(segment => { switch (segment) { case 'EXIF': case 'GPS': options.tiff = true; break; case 'XMP': options.xmp = true; break; case 'ICC': options.icc = true; break; case 'IPTC': options.iptc = true; break; case 'JFIF': options.jfif = true; break; case 'IHDR': options.ihdr = true; break; } }); return options; }