Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class Tool
- Object
- Tool
A function the model can call. parametersJsonSchema is a raw
JSON-Schema string; each provider wraps it differently on the wire
(OpenAI {type:"function",function:{...}}, Anthropic
{name,description,input_schema}, Gemini functionDeclarations),
but the inner schema shape is the same across all of them, so we
hand it through as a string and let the provider client wrap.
Linking a tool to its executor
Pass an optional ToolHandler at construction time and the
matching ToolCall can dispatch through it without the caller
having to match names by hand:
Tool weather = new Tool(
"get_weather",
"Returns the current weather for a location",
"{\"type\":\"object\",\"properties\":{" +
"\"location\":{\"type\":\"string\"}}," +
"\"required\":[\"location\"]}",
argumentsJson -> {
Map args = JSONParser.parseJSON(argumentsJson);
return "{\"temp\":21,\"city\":\""
+ JSONParser.getString(args, "location") + "\"}";
});
// Later, when the model returns a ToolCall:
for (ToolCall call : response.getToolCalls()) {
String resultJson = call.execute(Arrays.asList(weather));
conversation.add(ChatMessage.toolResult(call.getId(), resultJson));
}
The handler is optional – a Tool constructed without one is a
pure description for the model, and the caller can dispatch
however they like via the raw ToolCall.getName /
ToolCall.getArgumentsJson accessors.
Constructors
public Tool(String name, String description, String parametersJsonSchema) | Creates a description-only tool for manual dispatch. |
public Tool(String name, String description, String parametersJsonSchema, ToolHandler handler) | Creates a tool definition with an application executor. |
Methods
Inherited methods
Constructor details
Tool
public Tool(String name, String description, String parametersJsonSchema)Parameters
nameString- provider-visible function name
descriptionString- guidance that helps the model choose the tool
parametersJsonSchemaString- JSON Schema for accepted arguments
Tool
public Tool(String name, String description, String parametersJsonSchema, ToolHandler handler)Parameters
nameString- provider-visible function name
descriptionString- guidance that helps the model choose the tool
parametersJsonSchemaString- JSON Schema for accepted arguments
handlerToolHandler- executor called by
invoke(String), ornull
Method details
getName
public String getName()Returns
getDescription
public String getDescription()Returns
getParametersJsonSchema
public String getParametersJsonSchema()Returns
getHandler
public ToolHandler getHandler()null for description-only tools.Returns
nullinvoke
public String invoke(String argumentsJson)
throws ExceptionIllegalStateException when no handler was registered.Parameters
argumentsJsonString- model-generated arguments
Returns
Throws
Exception- when the handler rejects or cannot execute the call