Context passed to handle_call_tool/4 handlers during tool execution.
Provides an API for tool handlers to interact with the MCP server while the tool is running — sending notifications (logging, progress) and making server-to-client requests (sampling, elicitation).
Example
def handle_call_tool("my_tool", args, ctx, state) do
ToolContext.log(ctx, "info", "Starting tool execution")
ToolContext.send_progress(ctx, 0, 100)
# Request LLM sampling from the client
{:ok, result} = ToolContext.request_sampling(ctx, %{
"messages" => [%{"role" => "user", "content" => %{"type" => "text", "text" => "Hello"}}],
"maxTokens" => 100
})
ToolContext.send_progress(ctx, 100, 100)
{:ok, [%{"type" => "text", "text" => "Done"}], state}
end
Summary
Functions
Sends a log notification to the client.
Sends a server-to-client JSON-RPC request and waits for the response.
Sends an elicitation/create request to the client.
Sends a sampling/createMessage request to the client.
Sends a JSON-RPC notification to the client during tool execution.
Sends a progress notification to the client.
Types
Functions
Sends a log notification to the client.
Convenience wrapper around send_notification/3 for logging.
Sends a server-to-client JSON-RPC request and waits for the response.
Used for sampling, elicitation, and other bidirectional operations during tool execution. The request is routed to the client on the same SSE stream, and the client responds via a new POST.
Returns {:ok, result} or {:error, reason}.
Sends an elicitation/create request to the client.
Returns {:ok, result} or {:error, reason}.
Sends a sampling/createMessage request to the client.
Returns {:ok, result} or {:error, reason}.
Sends a JSON-RPC notification to the client during tool execution.
The notification is routed through the MCP server and delivered to the client on the same SSE stream as the tool call response.
Sends a progress notification to the client.
Uses the progressToken from _meta if available.