Raxol.MCP.ErrorText protocol (Raxol MCP v2.9.2)

Copy Markdown View Source

The text of a tool's error result.

Raxol.MCP.Server answers a tool callback's {:error, reason} with an isError: true result whose text is text(reason). Every reason has a text: the default, the Any implementation, is "Error: " <> inspect(reason). Implement the protocol for your own error struct to supply the exact text instead, with no prefix:

defmodule MyTool.Error do
  defstruct [:code, :detail]

  defimpl Raxol.MCP.ErrorText do
    def text(error), do: Jason.encode!(%{code: error.code})
  end
end

# A callback returning {:error, %MyTool.Error{code: "not_found"}}
# answers: %{content: [%{type: "text", text: ~s({"code":"not_found"})}],
#           isError: true}

The reason itself is unchanged: Raxol.MCP.Registry still hands the struct to the tool's :fault? classifier, so the classifier and the text are decided from the same value. A struct may keep fields the text leaves out.

What is asked

The server asks only a struct. Every other reason (an atom, a tuple, a binary, a map) renders with the default, so an implementation for a built-in type changes nothing. That keeps the reasons the registry and the server answer themselves ({:callback_raised, module}, :circuit_open, :callback_timeout) on their fixed text: a raised exception's message is never rendered, whatever implements this protocol.

Whose text it is

The text reaches the model verbatim. Text an implementation returns is the tool's own responsibility, as anything it puts in an {:ok, result} is; do not interpolate upstream bytes into it.

When an implementation fails

text/1 must return a valid UTF-8 binary, and should be pure and fast: it runs in the tool call's worker, after the callback and under the same :callback_timeout_ms, so a slow one delays every error it answers. One that raises, exits, throws, or returns anything else is logged and answered with the fixed text Error: {:error_text_failed, Module}, which names only the struct's module. No field of the struct reaches the client, because the fields text/1 was written to leave out are exactly the ones it may have failed on.

Summary

Types

t()

All the types that implement this protocol.

Functions

The exact text of the error result this reason answers.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

text(reason)

@spec text(t()) :: String.t()

The exact text of the error result this reason answers.