PrefectHQ/fastmcp · error · ValueError

Unsupported image MIME type for Anthropic: {content.mime_typ

Error message

Unsupported image MIME type for Anthropic: {content.mime_type!r}. Supported types: {', '.join(sorted(_ANTHROPIC_IMAGE_MEDIA_TYPES))}

What it means

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).

Source

Thrown at fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py:58

    from anthropic.types.tool_choice_param import ToolChoiceParam
except ImportError as e:
    raise ImportError(
        "The `anthropic` package is not installed. "
        "Install it with `pip install 'fastmcp-slim[anthropic]'` or add `anthropic` to your dependencies."
    ) from e

__all__ = ["AnthropicSamplingHandler"]

# Anthropic supports these image MIME types
_ANTHROPIC_IMAGE_MEDIA_TYPES = frozenset(
    {"image/jpeg", "image/png", "image/gif", "image/webp"}
)


def _image_content_to_anthropic_block(content: ImageContent) -> ImageBlockParam:
    """Convert MCP ImageContent to Anthropic ImageBlockParam."""
    if content.mime_type not in _ANTHROPIC_IMAGE_MEDIA_TYPES:
        raise ValueError(
            f"Unsupported image MIME type for Anthropic: {content.mime_type!r}. "
            f"Supported types: {', '.join(sorted(_ANTHROPIC_IMAGE_MEDIA_TYPES))}"
        )
    return ImageBlockParam(
        type="image",
        source=Base64ImageSourceParam(
            type="base64",
            media_type=content.mime_type,  # type: ignore[arg-type]  # ty:ignore[invalid-argument-type]
            data=content.data,
        ),
    )


class AnthropicSamplingHandler:
    """Sampling handler that uses the Anthropic API.

    Example:
        ```python

View on GitHub (pinned to 1f02114297)

Solutions

  1. Convert the image to a supported type (e.g. PNG/JPEG) before returning it as ImageContent from your tool
  2. Check the exact supported set in `_ANTHROPIC_IMAGE_MEDIA_TYPES` in anthropic.py and align your producer
  3. Ensure mime_type is accurate — a wrong/missing value fails even for supported images
  4. Use a different sampling handler (e.g. OpenAI) or skip images in your sampling context

Example fix

// before
return ImageContent(type="image", data=svg_bytes, mimeType="image/svg+xml")  # raises

// after
png_bytes = convert_svg_to_png(svg_bytes)
return ImageContent(type="image", data=base64.b64encode(png_bytes).decode(), mimeType="image/png")
Defensive patterns

Strategy: validation

Validate before calling

SUPPORTED = {"image/jpeg", "image/png", "image/gif", "image/webp"}
if getattr(img, "mime_type", None) not in SUPPORTED:
    img = reencode(img, target="image/png")

Type guard

def anthropic_safe_image(c) -> bool:
    from fastmcp.client.sampling.handlers.anthropic import _ANTHROPIC_IMAGE_MEDIA_TYPES
    return getattr(c, "mime_type", None) in _ANTHROPIC_IMAGE_MEDIA_TYPES

Try / catch

try:
    result = await llm_sampling(messages)
except ValueError as e:
    if "Unsupported image MIME type" in str(e):
        messages = sanitize_images(messages)
        result = await llm_sampling(messages)
    else:
        raise

Prevention

When it happens

Trigger: 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).

Common situations: Proxies forwarding arbitrary image MIME types from other MCP servers; tools producing SVG or HEIC images; ImageContent built without setting mime_type correctly.

Related errors


AI-assisted analysis of PrefectHQ/fastmcp@1f02114297 (2026-08-29). Data as JSON: /api/errors/6d78ea63c5ea14d4. Report an issue: GitHub.