Skip to main content
Glama

read-metadata

Extract and analyze metadata from images including EXIF, GPS, XMP, ICC, IPTC, JFIF, and IHDR segments for offline image analysis and data extraction.

Instructions

Read all or specified metadata segments from an image

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
imageYes
segmentsNo

Implementation Reference

  • The core handler function for the 'read-metadata' tool. Loads image buffer using loadImage, builds exifr options from segments using buildOptions, parses metadata with exifr.parse, returns success response with metadata JSON or error.
    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)}`);
      }
    }
  • Input schema definition: ImageSourceSchema (zod object for image source kinds) and segments (optional array of specific metadata segments enum). Used in server.tool call.
    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()
    });
    
    // Tool 1: read-metadata - reads all or specified metadata segments from an image
    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()
      },
  • Registration of the 'read-metadata' tool via server.tool() with name, description, input schema, and handler function. Reference stored in 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;
  • loadImage helper: asynchronously loads image data from path, URL (http/file), base64 data URI or raw, or base64 buffer into Buffer/Uint8Array. Handles errors and size limits.
    export async function loadImage(src: ImageSourceType): Promise<Buffer | Uint8Array> {
      try {
        switch (src.kind) {
          case 'path':
            if (!src.path) {
              throw new Error('Path is required for kind="path"');
            }
            return await fs.promises.readFile(src.path);
          
          case 'url':
            if (!src.url) {
              throw new Error('URL is required for kind="url"');
            }
            
            if (src.url.startsWith('file://')) {
              // Handle file:// URLs by converting to filesystem path
              const filePath = fileURLToPath(src.url);
              return await fs.promises.readFile(filePath);
            } else {
              // Handle HTTP/HTTPS URLs
              const response = await fetch(src.url);
              if (!response.ok) {
                throw new Error(`Failed to fetch URL: ${response.status} ${response.statusText}`);
              }
              return new Uint8Array(await response.arrayBuffer());
            }
          
          case 'base64':
            if (!src.data) {
              throw new Error('Data is required for kind="base64"');
            }
            
            // Check for potential oversized base64 string (>30MB)
            if (src.data.length > 40000000) { // ~30MB in base64
              throw new Error('PayloadTooLarge: Base64 data exceeds 30MB limit');
            }
            
            // Handle data URIs or raw base64
            if (src.data.startsWith('data:')) {
              const base64Data = src.data.split(',')[1];
              return Buffer.from(base64Data, 'base64');
            } else {
              return Buffer.from(src.data, 'base64');
            }
          
          case 'buffer':
            if (!src.buffer) {
              throw new Error('Buffer is required for kind="buffer"');
            }
            return Buffer.from(src.buffer, 'base64');
          
          default:
            // This should never happen due to type constraints, but TypeScript needs it
            throw new Error(`Unsupported image source kind: ${(src as any).kind}`);
        }
      } catch (error) {
        if (error instanceof Error) {
          throw new Error(`Failed to load image: ${error.message}`);
        }
        throw error;
      }
    }
  • buildOptions helper: creates exifr.parse options enabling all segments by default or only specified ones (EXIF/GPS via 'tiff', XMP, ICC, IPTC, JFIF, IHDR).
    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;
    }

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observed

TDQS

C2.8/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It discloses a read-only operation but does not mention return format, error behavior when a segment is missing, or any limits on image formats. The behavior beyond 'read' is completely unspecified.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness3/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single sentence with no filler, but it is under-specified. While concise, it does not enough cover the tool's complexity (2 parameters, nested image object). The structure is fine, but the brevity comes at the cost of clarity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool has 2 parameters, one complex nested object, no annotations, and no output schema, the description is grossly incomplete. It provides no information about how to pass the image, what segments are supported, or what the return value looks like. The tool is not usable from this description alone.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%. The description only hints at the 'segments' parameter through 'all or specified metadata segments'. It says nothing about the required 'image' parameter, which has a complex nested object structure (kind, url, data, path, buffer). The description fails to add any meaning to the schema beyond a vague hint.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb 'read', the resource 'metadata segments', and the scope 'all or specified'. This distinguishes it from sibling tools that target specific segment readers (read-exif, read-xmp), as it can read all at once or a specified subset.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies when to use this tool: when you need to read all or a selection of metadata segments. However, it does not explicitly name alternatives or state when to prefer the segment-specific sibling tools, so the guidance is implicit rather than explicit.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.