{"record":{"id":"6d78ea63c5ea14d4","repo":"PrefectHQ/fastmcp","slug":"unsupported-image-mime-type-for-anthropic-conten","errorCode":null,"errorMessage":"Unsupported image MIME type for Anthropic: {content.mime_type!r}. Supported types: {', '.join(sorted(_ANTHROPIC_IMAGE_MEDIA_TYPES))}","messagePattern":"Unsupported image MIME type for Anthropic: (.+?)\\. Supported types: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py","lineNumber":58,"sourceCode":"    from anthropic.types.tool_choice_param import ToolChoiceParam\nexcept ImportError as e:\n    raise ImportError(\n        \"The `anthropic` package is not installed. \"\n        \"Install it with `pip install 'fastmcp-slim[anthropic]'` or add `anthropic` to your dependencies.\"\n    ) from e\n\n__all__ = [\"AnthropicSamplingHandler\"]\n\n# Anthropic supports these image MIME types\n_ANTHROPIC_IMAGE_MEDIA_TYPES = frozenset(\n    {\"image/jpeg\", \"image/png\", \"image/gif\", \"image/webp\"}\n)\n\n\ndef _image_content_to_anthropic_block(content: ImageContent) -> ImageBlockParam:\n    \"\"\"Convert MCP ImageContent to Anthropic ImageBlockParam.\"\"\"\n    if content.mime_type not in _ANTHROPIC_IMAGE_MEDIA_TYPES:\n        raise ValueError(\n            f\"Unsupported image MIME type for Anthropic: {content.mime_type!r}. \"\n            f\"Supported types: {', '.join(sorted(_ANTHROPIC_IMAGE_MEDIA_TYPES))}\"\n        )\n    return ImageBlockParam(\n        type=\"image\",\n        source=Base64ImageSourceParam(\n            type=\"base64\",\n            media_type=content.mime_type,  # type: ignore[arg-type]  # ty:ignore[invalid-argument-type]\n            data=content.data,\n        ),\n    )\n\n\nclass AnthropicSamplingHandler:\n    \"\"\"Sampling handler that uses the Anthropic API.\n\n    Example:\n        ```python","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py#L40-L76","documentation":"ValueError from `_image_content_to_anthropic_block` when an MCP ImageContent has a MIME type Anthropic's vision API does not accept (only a fixed set of image media types is supported).","triggerScenarios":"Server sampling via AnthropicSamplingHandler where a sampling message contains ImageContent whose `mime_type` is not in `_ANTHROPIC_IMAGE_MEDIA_TYPES` (e.g. image/webp variants not supported, image/svg+xml, video/*, or a missing/wrong mime type).","commonSituations":"Proxies forwarding arbitrary image MIME types from other MCP servers; tools producing SVG or HEIC images; ImageContent built without setting mime_type correctly.","solutions":["Convert the image to a supported type (e.g. PNG/JPEG) before returning it as ImageContent from your tool","Check the exact supported set in `_ANTHROPIC_IMAGE_MEDIA_TYPES` in anthropic.py and align your producer","Ensure mime_type is accurate — a wrong/missing value fails even for supported images","Use a different sampling handler (e.g. OpenAI) or skip images in your sampling context"],"exampleFix":"// before\nreturn ImageContent(type=\"image\", data=svg_bytes, mimeType=\"image/svg+xml\")  # raises\n\n// after\npng_bytes = convert_svg_to_png(svg_bytes)\nreturn ImageContent(type=\"image\", data=base64.b64encode(png_bytes).decode(), mimeType=\"image/png\")","handlingStrategy":"validation","validationCode":"SUPPORTED = {\"image/jpeg\", \"image/png\", \"image/gif\", \"image/webp\"}\nif getattr(img, \"mime_type\", None) not in SUPPORTED:\n    img = reencode(img, target=\"image/png\")","typeGuard":"def anthropic_safe_image(c) -> bool:\n    from fastmcp.client.sampling.handlers.anthropic import _ANTHROPIC_IMAGE_MEDIA_TYPES\n    return getattr(c, \"mime_type\", None) in _ANTHROPIC_IMAGE_MEDIA_TYPES","tryCatchPattern":"try:\n    result = await llm_sampling(messages)\nexcept ValueError as e:\n    if \"Unsupported image MIME type\" in str(e):\n        messages = sanitize_images(messages)\n        result = await llm_sampling(messages)\n    else:\n        raise","preventionTips":["Emit only PNG/JPEG (and verified supported types) from tools","Always set mimeType accurately on ImageContent","Sanitize/normalize images at MCP-server boundaries","Check _ANTHROPIC_IMAGE_MEDIA_TYPES when adding new image formats"],"tags":["anthropic","sampling","image","mime-type"],"backgroundTag":"unsupported-media-type","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}