PrefectHQ/fastmcp · error · ValueError

Unsupported content type for Anthropic: {type(item).__name__

Error message

Unsupported content type for Anthropic: {type(item).__name__}

What it means

ValueError from `_convert_to_anthropic_messages` (list-content branch): a content block of a type the Anthropic converter doesn't recognize was encountered, and it names the concrete class so you can see what leaked in.

Source

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

                                TextBlockParam(type="text", text=sub_item.text)
                                for sub_item in item.content
                                if isinstance(sub_item, TextContent)
                            ]
                            if len(text_blocks) == 1:
                                result_content = text_blocks[0]["text"]
                            elif text_blocks:
                                result_content = text_blocks

                        content_blocks.append(
                            ToolResultBlockParam(
                                type="tool_result",
                                tool_use_id=item.tool_use_id,
                                content=result_content,
                                is_error=item.is_error if item.is_error else False,
                            )
                        )
                    else:
                        raise ValueError(
                            f"Unsupported content type for Anthropic: {type(item).__name__}"
                        )

                if content_blocks:
                    anthropic_messages.append(
                        MessageParam(
                            role=message.role,
                            content=content_blocks,
                        )
                    )
                continue

            # Handle ToolUseContent (assistant's tool calls)
            if isinstance(content, ToolUseContent):
                anthropic_messages.append(
                    MessageParam(
                        role="assistant",
                        content=[

View on GitHub (pinned to 1f02114297)

Solutions

  1. Inspect `type(item).__name__` from the message to identify the offending block
  2. Pre-process messages to convert/strip unsupported blocks (e.g. extract text from EmbeddedResource)
  3. Update fastmcp-slim — newer versions may support the type
  4. Report or subclass the handler to add conversion for the custom type

Example fix

// before
messages = [SamplingMessage(role="user", content=[EmbeddedResource(...)])]  # raises

// after
text = resource_to_text(EmbeddedResource(...))
messages = [SamplingMessage(role="user", content=[TextContent(type="text", text=text)])]
Defensive patterns

Strategy: type-guard

Validate before calling

ALLOWED = (TextContent, ImageContent, AudioContent, ToolResultContent)
bad = [b for m in messages for b in as_list(m.content) if not isinstance(b, ALLOWED)]
assert not bad, f"unsupported blocks: {[type(b).__name__ for b in bad]}"

Type guard

def is_anthropic_convertible(block) -> bool:
    from mcp.types import TextContent, ImageContent, AudioContent, ToolResultContent
    return isinstance(block, (TextContent, ImageContent, AudioContent, ToolResultContent))

Try / catch

try:
    return await handler(messages, params, ctx)
except ValueError as e:
    if e.args and e.args[0].startswith("Unsupported content type"):
        return await handler(convert_blocks(messages), params, ctx)
    raise

Prevention

When it happens

Trigger: A SamplingMessage's list content contains a block that is not TextContent, ImageContent, AudioContent, or ToolResultContent (e.g. EmbeddedResource, AudioContent subclasses, or a custom content type) passed to AnthropicSamplingHandler.

Common situations: Servers returning EmbeddedResource or other MCP content types in sampling contexts; custom middleware injecting non-standard blocks; version drift where newer MCP content types predate the converter.

Related errors


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