Interface ToolHandler
- All Known Subinterfaces:
ToolHandler.Cancellable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
The body of an MCP tool: takes the parsed argument JSON and the shared
ServerContext, returns the structured result payload.
Contract:
argumentsis nevernull- a call with no arguments arrives as an empty object node;- the returned
ObjectNodebecomes the tool result'sstructuredContent(and, serialized, its text content); - throwing any exception is safe: the server converts it into an MCP tool
error result (
isError: truewith the exception message) rather than a protocol failure, so validation can simplythrow new IllegalArgumentException("field 'x' is required"); - text that comes from a remote system (an Equinix API field, a PeeringDB name, a pricing
error) is put into the payload through
UntrustedText, so the server can sanitise it and label it as data.
Cancellation
The server always invokes
handle(JsonNode, ServerContext, CancellationSignal). Its default checks the signal once
and then runs handle(JsonNode, ServerContext), which suits a read-only tool: a call the
client already cancelled does not start. A handler that must observe cancellation while it runs
(for example before sending an irreversible request) implements ToolHandler.Cancellable.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA handler that observes its cancellation signal while it runs. -
Method Summary
Modifier and TypeMethodDescriptioncom.fasterxml.jackson.databind.node.ObjectNodehandle(com.fasterxml.jackson.databind.JsonNode arguments, ServerContext context) Executes the tool.default com.fasterxml.jackson.databind.node.ObjectNodehandle(com.fasterxml.jackson.databind.JsonNode arguments, ServerContext context, CancellationSignal cancellation) Executes the tool under a cancellation signal.
-
Method Details
-
handle
com.fasterxml.jackson.databind.node.ObjectNode handle(com.fasterxml.jackson.databind.JsonNode arguments, ServerContext context) throws Exception Executes the tool.- Parameters:
arguments- the parsed tool-call arguments (an object node, nevernull)context- the shared server context of lazily-built SDK facades- Returns:
- the structured result payload
- Throws:
Exception- on any failure - surfaced to the caller as a tool error result
-
handle
default com.fasterxml.jackson.databind.node.ObjectNode handle(com.fasterxml.jackson.databind.JsonNode arguments, ServerContext context, CancellationSignal cancellation) throws Exception Executes the tool under a cancellation signal. The default refuses to start a call that is already cancelled and otherwise delegates tohandle(JsonNode, ServerContext).- Parameters:
arguments- the parsed tool-call arguments (an object node, nevernull)context- the shared server contextcancellation- the call's cancellation signal; nevernull- Returns:
- the structured result payload
- Throws:
Exception- on any failure - surfaced to the caller as a tool error result
-