Skip to main content
Glama
champierre

Image Analysis MCP Server

by champierre

analyze_image_from_path

Analyze image content from local file paths using AI to generate descriptive text and insights about visual elements.

Instructions

Loads an image from a local file path and analyzes its content using GPT-4o-mini. AI assistants need to provide a valid path for the server execution environment (e.g., Linux path if the server is running on WSL).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
imagePathYesLocal file path of the image to analyze (must be accessible from the server execution environment)

Implementation Reference

  • Main execution logic for the 'analyze_image_from_path' tool. Validates input, performs path security check, reads and encodes the image file to base64, verifies MIME type, and calls the GPT analysis helper.
    } else if (toolName === 'analyze_image_from_path') {
      if (!isValidAnalyzeImagePathArgs(args)) {
        throw new McpError(
          ErrorCode.InvalidParams,
          'Invalid arguments for analyze_image_from_path: imagePath (string) is required'
        );
      }
      const imagePath = args.imagePath;
      // Basic security check: prevent absolute paths trying to escape common roots (adjust as needed)
      // This is a VERY basic check and might need refinement based on security requirements.
      if (path.isAbsolute(imagePath) && !imagePath.startsWith(process.cwd()) && !imagePath.startsWith(os.homedir()) && !imagePath.startsWith('/mnt/')) {
         // Allow relative paths, paths within cwd, home, or WSL mounts. Adjust if needed.
         console.warn(`Potential unsafe path access attempt blocked: ${imagePath}`);
         throw new McpError(ErrorCode.InvalidParams, 'Invalid or potentially unsafe imagePath provided.');
      }
    
      const resolvedPath = path.resolve(imagePath); // Resolve relative paths
      if (!fs.existsSync(resolvedPath)) {
        throw new McpError(ErrorCode.InvalidParams, `File not found at path: ${resolvedPath}`);
      }
      const imageDataBuffer = fs.readFileSync(resolvedPath);
      const base64String = imageDataBuffer.toString('base64');
      const mimeType = mime.lookup(resolvedPath) || 'application/octet-stream'; // Detect MIME type or default
    
      if (!mimeType.startsWith('image/')) {
         throw new McpError(ErrorCode.InvalidParams, `File is not an image: ${mimeType}`);
      }
    
      analysis = await this.analyzeImageWithGpt4({ type: 'base64', data: base64String, mimeType: mimeType });
  • Input schema definition for the 'analyze_image_from_path' tool, specifying the required 'imagePath' parameter.
    inputSchema: {
      type: 'object',
      properties: {
        imagePath: {
          type: 'string',
          description: 'Local file path of the image to analyze (must be accessible from the server execution environment)',
        },
      },
      required: ['imagePath'],
    },
  • src/index.ts:93-106 (registration)
    Tool registration in the ListToolsRequestSchema handler, including name, description, and schema.
    {
      name: 'analyze_image_from_path',
      description: 'Loads an image from a local file path and analyzes its content using GPT-4o-mini. AI assistants need to provide a valid path for the server execution environment (e.g., Linux path if the server is running on WSL).',
      inputSchema: {
        type: 'object',
        properties: {
          imagePath: {
            type: 'string',
            description: 'Local file path of the image to analyze (must be accessible from the server execution environment)',
          },
        },
        required: ['imagePath'],
      },
    },
  • Type guard helper for validating the arguments passed to the 'analyze_image_from_path' tool.
    const isValidAnalyzeImagePathArgs = (
      args: any
    ): args is { imagePath: string } => // New type guard for path tool
      typeof args === 'object' &&
      args !== null &&
      typeof args.imagePath === 'string';
  • Shared helper method for analyzing images (URL or base64 data URI) using GPT-4o-mini OpenAI API.
    private async analyzeImageWithGpt4(
       imageData: { type: 'url', data: string } | { type: 'base64', data: string, mimeType: string }
     ): Promise<string> {
      try {
        let imageInput: any;
        if (imageData.type === 'url') {
          imageInput = { type: 'image_url', image_url: { url: imageData.data } };
        } else {
          // Construct data URI for OpenAI API
          imageInput = { type: 'image_url', image_url: { url: `data:${imageData.mimeType};base64,${imageData.data}` } };
        }
    
        const response = await openai.chat.completions.create({
          model: 'gpt-4o-mini',
          messages: [
            {
              role: 'system',
              content: 'Analyze the image content in detail and provide an explanation in English.',
            },
            {
              role: 'user',
              content: [
                { type: 'text', text: 'Please analyze the following image and explain its content in detail.' },
                imageInput, // Use the constructed image input
              ],
            },
          ],
          max_tokens: 1000,
        });
    
        return response.choices[0]?.message?.content || 'Could not retrieve analysis results.';
      } catch (error) {
        console.error('OpenAI API error:', error);
        throw new Error(`OpenAI API error: ${error instanceof Error ? error.message : String(error)}`);
      }
    }
Behavior3/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions that the tool 'analyzes its content using GPT-4o-mini,' which adds useful context about the analysis method. However, it doesn't disclose important behavioral traits like whether this is a read-only operation, potential rate limits, authentication needs, or what kind of analysis results to expect. The description doesn't contradict annotations (since none exist), but it leaves significant gaps.

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

Conciseness4/5

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

The description is appropriately sized at two sentences. The first sentence states the core purpose, and the second adds important usage guidance about path requirements. There's no wasted text, and information is front-loaded. It could be slightly more concise by combining ideas, but it's efficient overall.

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

Completeness3/5

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

Given the tool has no annotations, no output schema, and a simple single parameter, the description provides adequate basic information about what the tool does and path requirements. However, for an analysis tool, it should ideally mention what kind of analysis is performed (e.g., object detection, captioning, etc.) or what format the results take. The description is complete enough for basic understanding but lacks depth about the analysis output.

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

Parameters3/5

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

Schema description coverage is 100%, so the schema already documents the single parameter 'imagePath' with its description. The description adds some context about path requirements ('valid path for the server execution environment') but doesn't provide additional semantic meaning beyond what's in the schema. With high schema coverage, the baseline is 3 even without extra parameter information in the description.

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

Purpose4/5

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

The description clearly states the tool's purpose: 'Loads an image from a local file path and analyzes its content using GPT-4o-mini.' This specifies both the action (loads and analyzes) and the resource (image from local file path). However, it doesn't explicitly differentiate from its sibling 'analyze_image' tool, which likely handles images differently (e.g., from URLs or base64).

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

Usage Guidelines4/5

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

The description provides clear context about when to use this tool: when you have a local file path accessible from the server execution environment. It mentions the need for 'valid path for the server execution environment (e.g., Linux path if the server is running on WSL),' which helps guide usage. However, it doesn't explicitly state when NOT to use it or mention the sibling tool as an alternative for different image sources.

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

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/champierre/image-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server