module

Anthropic::MCP

Convert Model Context Protocol payloads into Anthropic content blocks.

This is a dependency-free port of the content converters from the TypeScript SDK's helpers/beta/mcp module: inputs are raw JSON::Any values shaped like MCP SDK responses, so no MCP client library is needed.

content = JSON.parse(%({"type":"text","text":"hello"}))
block = Anthropic::MCP.content(content)

Constants

SUPPORTED_IMAGE_TYPES = ["image/jpeg", "image/png", "image/gif", "image/webp"]

Class methods

content(block : JSON::Any) : ContentBlock

Convert one MCP prompt content item to an Anthropic content block.

Handles text, image, and embedded resource items. audio and resource_link items are rejected, as is any unknown type. Malformed items raise UnsupportedMCPValueError.

Source
message(message : JSON::Any) : MessageParam

Convert one MCP prompt message to an Anthropic message param.

Source
messages(messages : Array(JSON::Any)) : Array(MessageParam)

Convert MCP prompt messages (e.g. from getPrompt) to Anthropic message params.

Source
resource_to_content(result : JSON::Any) : ContentBlock

Convert MCP resource contents (e.g. from readResource) to a single Anthropic content block.

Uses the first entry with a supported MIME type: images become image blocks, PDFs and text/* entries become documents.

Source

Nested types