PrefectHQ/fastmcp · error · ValueError

Unsupported content type: {type(content)}

Error message

Unsupported content type: {type(content)}

What it means

AnthropicSamplingHandler._convert_to_anthropic_messages raises ValueError when an MCP sampling message contains a content type the Anthropic Messages API cannot represent (anything other than TextContent, ImageContent, or AudioContent, which is explicitly rejected earlier). It is a defensive guard so unsupported MCP content never silently reaches the API.

Source

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

            # Handle ImageContent
            if isinstance(content, ImageContent):
                if message.role != "user":
                    raise ValueError(
                        "ImageContent is only supported in user messages for Anthropic"
                    )
                anthropic_messages.append(
                    MessageParam(
                        role="user",
                        content=[_image_content_to_anthropic_block(content)],
                    )
                )
                continue

            # Handle AudioContent - not supported by Anthropic
            if isinstance(content, AudioContent):
                raise ValueError("AudioContent is not supported by the Anthropic API")

            raise ValueError(f"Unsupported content type: {type(content)}")

        return anthropic_messages

    @staticmethod
    def _message_to_create_message_result(
        message: Message,
    ) -> CreateMessageResult:
        if len(message.content) == 0:
            raise ValueError("No content in response from Anthropic")

        # Join all text blocks to avoid dropping content
        text = "".join(
            block.text for block in message.content if isinstance(block, TextBlock)
        )
        if text:
            return CreateMessageResult(
                content=TextContent(type="text", text=text),
                role="assistant",

View on GitHub (pinned to 1f02114297)

Solutions

  1. Inspect the sampling message's content types before choosing the Anthropic handler and strip/downgrade unsupported blocks to text.
  2. Handle AudioContent separately — Anthropic API does not accept audio; convert or drop it upstream.
  3. Use a different sampling handler (e.g. OpenAI or a fallback handler) that supports the content types your server sends.
  4. Catch ValueError in your sampling callback and return a polite refusal message to the server.

Example fix

// before
client = Client(transport, sampling_handler=AnthropicSamplingHandler(api_key=...))
// after
from fastmcp.client.sampling import AnthropicSamplingHandler
class SafeHandler(AnthropicSamplingHandler):
    async def __call__(self, messages, params, context):
        messages = [m for m in messages if not any(isinstance(c, AudioContent) for c in ([m.content] if not isinstance(m.content, list) else m.content))]
        return await super().__call__(messages, params, context)
Defensive patterns

Strategy: validation

Validate before calling

from mcp.types import TextContent, ImageContent
SUPPORTED = (TextContent, ImageContent)
def validate_sampling_messages(messages):
    for m in messages:
        contents = m.content if isinstance(m.content, list) else [m.content]
        for c in contents:
            if not isinstance(c, SUPPORTED):
                raise ValueError(f"Anthropic handler cannot send {type(c).__name__}")

Type guard

def is_anthropic_supported(c) -> bool:
    return isinstance(c, (TextContent, ImageContent))

Try / catch

try:
    result = await handler(messages, params, context)
except ValueError as e:
    if "Unsupported content type" in str(e):
        result = CreateMessageResult(content=TextContent(type="text", text="Cannot process this content type."), role="assistant", model="unknown")
    else:
        raise

Prevention

When it happens

Trigger: A server sends a sampling request whose message content is an EmbeddedResource (or any non-Text/Image/Audio MCP content block, e.g. resource_link) to a client using AnthropicSamplingHandler.

Common situations: Servers returning tool results or resources inline in sampling prompts; MCP servers forwarding AudioContent; new MCP content types added after the handler was written.

Related errors


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