Ragex. MCP. Formatter
(Ragex v0.15.0)
View Source
Centralized response formatting for MCP tool results.
Sits between Tools.call_tool/2 and the JSON serializer in
Server.handle_tools_call/2. Every tool result passes through
format/3 which applies compaction, token budgeting, and smart
suggestions based on the formatting options.
Modes
:compact(default) -- truncates lists, strips verbose fields (docs, specs, long strings), and appends next-step suggestions.:verbose-- passes the result through unchanged (current behavior).
Token Budget
When max_tokens is specified, the formatter estimates the JSON size
of the result (~4 chars per token) and truncates to fit.
Smart Suggestions
In compact mode, the formatter appends _suggestions with actionable
next-step hints based on the result shape and tool name.
Usage
The formatter is invoked automatically by Server.handle_tools_call/2.
Tool handlers do not need to call it directly.
Options are extracted from tool arguments:
"verbose"(boolean, defaultfalse) -- if true, skip compaction"max_tokens"(integer, optional) -- token budget for the response
Summary
Functions
Estimate the number of tokens a value will consume when JSON-encoded.
Extract formatting options from MCP tool arguments.
Format a tool result based on options.
Generate next-step suggestions based on the result shape and tool name.
Functions
@spec estimate_tokens(term()) :: non_neg_integer()
Estimate the number of tokens a value will consume when JSON-encoded.
Uses ~4 characters per token as a rough heuristic.
Extract formatting options from MCP tool arguments.
Pulls verbose and max_tokens from the arguments map,
returning a keyword list suitable for format/3.
Format a tool result based on options.
Parameters
result-- the raw result fromTools.call_tool/2(the value inside{:ok, result})tool_name-- string name of the tool that produced the resultopts-- formatting options::verbose-- boolean (defaultfalse):max_tokens-- integer token budget (optional)
Returns
The formatted result (same shape as input, but potentially compacted).
Generate next-step suggestions based on the result shape and tool name.
Returns a list of actionable hint strings.