McpServer.Tool.Content (HTTP MCP Server v0.7.0)
View SourceStructs and helper functions for representing tool result content items.
This module provides typed structs and convenience functions for different types of content that can be returned from tool functions, following the MCP protocol specification.
Content Types
Text- Text contentImage- Image content with base64-encoded dataResource- Embedded resource content
Usage
You can alias this module and use the helper functions to build tool results:
alias McpServer.Tool.Content, as: ToolContent
def search_tool(_conn, %{"query" => query}) do
[
ToolContent.text("Found 5 results for: #{query}"),
ToolContent.text("Result 1: ..."),
ToolContent.text("Result 2: ...")
]
end
def generate_chart(_conn, %{"data" => data}) do
chart_image = create_chart(data)
[
ToolContent.text("Chart generated successfully"),
ToolContent.image(chart_image, "image/png")
]
end
def read_file(_conn, %{"path" => path}) do
[ToolContent.resource("file://#{path}", text: File.read!(path), mimeType: "text/plain")]
endLow-level Struct API
iex> alias McpServer.Tool.Content
iex> text = Content.Text.new(text: "Hello, World!")
iex> text.text
"Hello, World!"
iex> image_data = <<137, 80, 78, 71, 13, 10, 26, 10>>
iex> image = Content.Image.new(data: image_data, mime_type: "image/png")
iex> image.mime_type
"image/png"
iex> resource = Content.Resource.new(uri: "file:///path/to/file.txt")
iex> resource.uri
"file:///path/to/file.txt"
Summary
Functions
Creates an image content item for tool responses.
Creates an embedded resource content item for tool responses.
Creates a text content item for tool responses.
Functions
@spec image(binary(), String.t()) :: McpServer.Tool.Content.Image.t()
Creates an image content item for tool responses.
Returns a McpServer.Tool.Content.Image struct.
The image data will be automatically base64-encoded during JSON serialization.
Parameters
data(binary): the raw image datamime_type(string): the MIME type of the image (e.g., "image/png", "image/jpeg")
Examples
iex> image_data = <<137, 80, 78, 71, 13, 10, 26, 10>>
iex> McpServer.Tool.Content.image(image_data, "image/png")
%McpServer.Tool.Content.Image{data: <<137, 80, 78, 71, 13, 10, 26, 10>>, mime_type: "image/png"}
@spec resource( String.t(), keyword() ) :: McpServer.Tool.Content.Resource.t()
Creates an embedded resource content item for tool responses.
Returns a McpServer.Tool.Content.Resource struct.
Parameters
uri(string): the URI of the resourceopts(keyword list): optional keys include::mimeType- MIME type of the resource:text- textual content of the resource:blob- binary content; base64-encoded during JSON serialization
Examples
iex> McpServer.Tool.Content.resource("file:///path/to/file.txt")
%McpServer.Tool.Content.Resource{uri: "file:///path/to/file.txt", text: nil, blob: nil, mime_type: nil}
iex> McpServer.Tool.Content.resource("file:///data.json", mimeType: "application/json", text: ~s({"key": "value"}))
%McpServer.Tool.Content.Resource{
uri: "file:///data.json",
mime_type: "application/json",
text: ~s({"key": "value"}),
blob: nil
}
iex> McpServer.Tool.Content.resource("file:///image.png", mimeType: "image/png", blob: <<255, 216, 255>>)
%McpServer.Tool.Content.Resource{
uri: "file:///image.png",
mime_type: "image/png",
text: nil,
blob: <<255, 216, 255>>
}
@spec text(String.t()) :: McpServer.Tool.Content.Text.t()
Creates a text content item for tool responses.
Returns a McpServer.Tool.Content.Text struct.
Parameters
text(string): the text content to return
Examples
iex> McpServer.Tool.Content.text("Hello, World!")
%McpServer.Tool.Content.Text{text: "Hello, World!"}
iex> McpServer.Tool.Content.text("Operation completed successfully")
%McpServer.Tool.Content.Text{text: "Operation completed successfully"}