analyze_image_with_openai
Upload an image file and provide a prompt to analyze its contents using OpenAI's vision model.
Instructions
Analyze an image file with an OpenAI vision-capable model.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| prompt | Yes | ||
| model | No | gpt-4.1-mini |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- server.py:233-234 (registration)Tool registration via @mcp.tool() decorator on the analyze_image_with_openai function
@mcp.tool() def analyze_image_with_openai(path: str, prompt: str, model: str = "gpt-4.1-mini") -> str: - server.py:234-265 (handler)Main handler: reads an image file, encodes it as base64, sends it to OpenAI's vision model via responses API, and returns the model's text output
def analyze_image_with_openai(path: str, prompt: str, model: str = "gpt-4.1-mini") -> str: """Analyze an image file with an OpenAI vision-capable model.""" api_key = os.getenv("OPENAI_API_KEY") if not api_key: raise ValueError("OPENAI_API_KEY is not configured.") target = _resolve_file_ops_path(path) if not target.is_file(): raise ValueError(f"File does not exist: {target}") mime_type, _ = mimetypes.guess_type(str(target)) if not mime_type or not mime_type.startswith("image/"): raise ValueError(f"File is not an image: {target}") image_bytes = target.read_bytes() image_b64 = base64.b64encode(image_bytes).decode("ascii") data_url = f"data:{mime_type};base64,{image_b64}" client = OpenAI(api_key=api_key) response = client.responses.create( model=model, input=[ { "role": "user", "content": [ {"type": "input_text", "text": prompt}, {"type": "input_image", "image_url": data_url}, ], } ], ) return response.output_text - server.py:234-234 (schema)Function signature defines the input schema: path (str), prompt (str), model (str, default 'gpt-4.1-mini')
def analyze_image_with_openai(path: str, prompt: str, model: str = "gpt-4.1-mini") -> str: - server.py:66-76 (helper)_resolve_file_ops_path helper is used to resolve and validate the image path within the allowed root directory
def _resolve_file_ops_path(path: str | None = None) -> Path: if not FILE_OPS_ROOT: raise ValueError("MCP_FILE_OPS_ROOT is not configured in .env.") root = Path(FILE_OPS_ROOT).expanduser().resolve() root.mkdir(parents=True, exist_ok=True) target = root if path is None else (root / path).resolve() if target != root and root not in target.parents: raise ValueError("Path escapes the configured MCP_FILE_OPS_ROOT.") return target - server.py:224-225 (helper)Reference to the tool in the inspect_file handler, telling users to use analyze_image_with_openai for image analysis
elif mime_type.startswith("image/"): result["image_note"] = "Use analyze_image_with_openai for model vision interpretation."