Hermes.Server.Response (hermes_mcp v0.10.0)
Fluent interface for building MCP component responses.
This module provides builders for tool, prompt, and resource responses that integrate seamlessly with the component system.
Examples
# Tool response
Response.tool()
|> Response.text("Result: " <> result)
|> Response.build()
# Resource response (uri and mime_type come from component)
Response.resource()
|> Response.text(file_contents)
|> Response.build()
# Prompt response
Response.prompt()
|> Response.user_message("What is the weather?")
|> Response.assistant_message("Let me check...")
|> Response.build()
Summary
Functions
Add an assistant message to a prompt response.
Add audio content to a tool response.
Set blob (base64) content for a resource response.
Set optional description for a resource response.
Add an embedded resource reference to a tool response.
Mark a tool response as an error and add error message.
Add image content to a tool response.
Add JSON-encoded content to a tool response.
Set optional name for a resource response.
Start building a prompt response with optional description.
Start building a resource response.
Add a system message to a prompt response.
Add text content to a tool or resource response.
Build the final response structure.
Start building a tool response.
Add a user message to a prompt response.
Types
Functions
Add an assistant message to a prompt response.
Parameters
response
- A prompt response structcontent
- The message content (string or structured content)
Examples
iex> Response.prompt() |> Response.assistant_message("Let me check the weather for you.")
%Response{
type: :prompt,
messages: [%{"role" => "assistant", "content" => "Let me check the weather for you."}]
}
Add audio content to a tool response.
Parameters
response
- A tool response structdata
- Base64 encoded audio datamime_type
- MIME type of the audio (e.g., "audio/wav")opts
- Optional keyword list with::transcription
- Optional text transcription of the audio
Examples
iex> Response.tool() |> Response.audio(audio_data, "audio/wav")
%Response{
type: :tool,
content: [%{"type" => "audio", "data" => audio_data, "mimeType" => "audio/wav"}],
isError: false
}
iex> Response.tool() |> Response.audio(audio_data, "audio/wav", transcription: "Hello")
%Response{
type: :tool,
content: [%{
"type" => "audio",
"data" => audio_data,
"mimeType" => "audio/wav",
"transcription" => "Hello"
}],
isError: false
}
Set blob (base64) content for a resource response.
Parameters
response
- A resource response structdata
- Base64 encoded binary data
Examples
iex> Response.resource() |> Response.blob(base64_data)
%Response{type: :resource, contents: %{"blob" => base64_data}}
Set optional description for a resource response.
Parameters
response
- A resource response structdesc
- Description of the resource
Examples
iex> Response.resource() |> Response.description("Application configuration settings")
%Response{type: :resource, metadata: %{description: "Application configuration settings"}}
Add an embedded resource reference to a tool response.
Parameters
response
- A tool response structuri
- The resource URIopts
- Optional keyword list with::name
- Human-readable name:description
- Resource description:mime_type
- MIME type:text
- Text content (for text resources):blob
- Base64 data (for binary resources)
Examples
iex> Response.tool() |> Response.embedded_resource("file://example.txt",
...> name: "Example File",
...> mime_type: "text/plain",
...> text: "File contents"
...> )
Mark a tool response as an error and add error message.
Parameters
response
- A tool response structmessage
- The error message
Examples
iex> Response.tool() |> Response.error("Division by zero")
%Response{
type: :tool,
content: [%{"type" => "text", "text" => "Error: Division by zero"}],
isError: true
}
Add image content to a tool response.
Parameters
response
- A tool response structdata
- Base64 encoded image datamime_type
- MIME type of the image (e.g., "image/png")
Examples
iex> Response.tool() |> Response.image(base64_data, "image/png")
%Response{
type: :tool,
content: [%{"type" => "image", "data" => base64_data, "mimeType" => "image/png"}],
isError: false
}
Add JSON-encoded content to a tool response.
This is a convenience function that automatically encodes data as JSON and adds it as text content. Useful for returning structured data from tools.
Parameters
response
- A tool response structdata
- Any JSON-encodable data structure
Examples
iex> Response.tool() |> Response.json(%{status: "ok", count: 42})
%Response{
type: :tool,
content: [%{"type" => "text", "text" => "{\"status\":\"ok\",\"count\":42}"}],
isError: false
}
iex> Response.tool() |> Response.json([1, 2, 3])
%Response{
type: :tool,
content: [%{"type" => "text", "text" => "[1,2,3]"}],
isError: false
}
Set optional name for a resource response.
Parameters
response
- A resource response structname
- Human-readable name for the resource
Examples
iex> Response.resource() |> Response.name("Configuration File")
%Response{type: :resource, metadata: %{name: "Configuration File"}}
Start building a prompt response with optional description.
Parameters
description
- Optional description of the prompt
Examples
iex> Response.prompt()
%Response{type: :prompt, messages: []}
iex> Response.prompt("Weather assistant prompt")
%Response{type: :prompt, messages: [], description: "Weather assistant prompt"}
Start building a resource response.
The uri and mimeType are automatically injected from the component's uri/0 and mime_type/0 callbacks when the response is built by the server.
Examples
iex> Response.resource()
%Response{type: :resource, contents: nil}
Add a system message to a prompt response.
Parameters
response
- A prompt response structcontent
- The message content (string or structured content)
Examples
iex> Response.prompt() |> Response.system_message("You are a helpful weather assistant.")
%Response{
type: :prompt,
messages: [%{"role" => "system", "content" => "You are a helpful weather assistant."}]
}
Add text content to a tool or resource response.
For tool responses, adds text to the content array. For resource responses, sets the text content.
Parameters
response
- A tool or resource response structtext
- The text content
Examples
iex> Response.tool() |> Response.text("Hello world")
%Response{
type: :tool,
content: [%{"type" => "text", "text" => "Hello world"}],
isError: false
}
iex> Response.resource() |> Response.text("File contents")
%Response{type: :resource, contents: %{"text" => "File contents"}}
Build the final response structure.
Transforms the response struct into the appropriate format for the MCP protocol.
Parameters
response
- A response struct of any type
Examples
iex> Response.tool() |> Response.text("Hello") |> Response.to_protocol()
%{"content" => [%{"type" => "text", "text" => "Hello"}], "isError" => false}
iex> Response.prompt() |> Response.user_message("Hi") |> Response.to_protocol()
%{"messages" => [%{"role" => "user", "content" => "Hi"}]}
iex> Response.resource() |> Response.text("data") |> Response.to_protocol()
%{"text" => "data"}
Start building a tool response.
Examples
iex> Response.tool()
%Response{type: :tool, content: [], isError: false}
Add a user message to a prompt response.
Parameters
response
- A prompt response structcontent
- The message content (string or structured content)
Examples
iex> Response.prompt() |> Response.user_message("What's the weather?")
%Response{
type: :prompt,
messages: [%{"role" => "user", "content" => "What's the weather?"}]
}