ExMCP.Content (ex_mcp v0.9.0)
View SourceContent handling utilities for MCP messages.
This module provides functions for creating and validating different content types including text, image, audio, and embedded resources.
Summary
Functions
Creates an audio content object.
Extracts the type of a content object.
Creates an image content object.
Creates a resource content object.
Creates a text content object.
Creates a tool result content object (new in 2025-11-25).
Creates a tool use content object (new in 2025-11-25).
Checks if content is of a specific type.
Validates content object structure.
Functions
@spec audio(String.t(), String.t(), map() | nil) :: ExMCP.Types.audio_content()
Creates an audio content object.
Parameters
- data: Base64-encoded audio data
- mime_type: MIME type of the audio (e.g., "audio/mp3", "audio/wav", "audio/ogg")
- annotations: Optional annotations
Examples
iex> ExMCP.Content.audio(base64_data, "audio/mp3")
%{type: :audio, data: base64_data, mimeType: "audio/mp3"}
@spec get_type(ExMCP.Types.content()) :: ExMCP.Types.content_type()
Extracts the type of a content object.
@spec image(String.t(), String.t(), map() | nil) :: ExMCP.Types.image_content()
Creates an image content object.
Parameters
- data: Base64-encoded image data
- mime_type: MIME type of the image (e.g., "image/png", "image/jpeg")
- annotations: Optional annotations
Examples
iex> ExMCP.Content.image(base64_data, "image/png")
%{type: :image, data: base64_data, mimeType: "image/png"}
@spec resource(map(), map() | nil) :: ExMCP.Types.embedded_resource()
Creates a resource content object.
Parameters
- resource: Resource object with uri, name, description, etc.
- annotations: Optional annotations
Examples
iex> resource = %{uri: "file:///example.txt", name: "Example"}
iex> ExMCP.Content.resource(resource)
%{type: :resource, resource: %{uri: "file:///example.txt", name: "Example"}}
@spec text(String.t(), map() | nil) :: ExMCP.Types.text_content()
Creates a text content object.
Examples
iex> ExMCP.Content.text("Hello, world!")
%{type: :text, text: "Hello, world!"}
@spec tool_result(String.t(), [map()], keyword()) :: ExMCP.Types.tool_result_content()
Creates a tool result content object (new in 2025-11-25).
Used in sampling/createMessage to provide the result of a tool call.
Parameters
- tool_use_id: The ID of the tool_use this is a result for
- content: List of content items (the tool's output)
- opts: Optional keyword list with
:is_errorboolean
@spec tool_use(String.t(), String.t(), map()) :: ExMCP.Types.tool_use_content()
Creates a tool use content object (new in 2025-11-25).
Used in sampling/createMessage responses when the model wants to call a tool.
Parameters
- id: Unique identifier for this tool use
- name: Name of the tool to call
- input: Arguments to pass to the tool
@spec type?(ExMCP.Types.content(), ExMCP.Types.content_type()) :: boolean()
Checks if content is of a specific type.
@spec validate(map()) :: {:ok, ExMCP.Types.content()} | {:error, String.t()}
Validates content object structure.
Returns {:ok, content} if valid, {:error, reason} otherwise.