Class ToolDefinition
Tools extend the assistant's capabilities by allowing it to call back into your application to perform actions or retrieve information. Each tool has a name, description, parameter schema, and a handler function that executes when the tool is invoked.
Example Usage
// Define a record for your tool's arguments
record WeatherArgs(String location) {
}
var tool = ToolDefinition.create("get_weather", "Get the current weather for a location",
Map.of("type", "object", "properties",
Map.of("location", Map.of("type", "string", "description", "City name")), "required",
List.of("location")),
invocation -> {
// Type-safe access with records (recommended)
WeatherArgs args = invocation.getArgumentsAs(WeatherArgs.class);
return CompletableFuture.completedFuture(getWeatherData(args.location()));
// Or use Map-based access
// Map<String, Object> args = invocation.getArguments();
// String location = (String) args.get("location");
});
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty tool definition.ToolDefinition(String name, String description, Object parameters, ToolHandler handler) Creates a tool definition with all properties. -
Method Summary
Modifier and TypeMethodDescriptionstatic ToolDefinitionCreates a tool definition with a JSON schema for parameters.Gets the tool description.Gets the tool handler.getName()Gets the tool name.Gets the parameter schema.setDescription(String description) Sets the tool description.setHandler(ToolHandler handler) Sets the tool handler.Sets the tool name.setParameters(Object parameters) Sets the parameter schema.
-
Constructor Details
-
ToolDefinition
public ToolDefinition()Creates an empty tool definition.Use the setter methods to configure the tool.
-
ToolDefinition
Creates a tool definition with all properties.- Parameters:
name- the unique name of the tooldescription- a description of what the tool doesparameters- the JSON Schema defining the tool's parametershandler- the handler function to execute when invoked
-
-
Method Details
-
getName
Gets the tool name.- Returns:
- the unique name of the tool
-
setName
Sets the tool name.The name should be unique within a session and follow naming conventions similar to function names (e.g., "get_user", "search_files").
- Parameters:
name- the unique name of the tool- Returns:
- this tool definition for method chaining
-
getDescription
Gets the tool description.- Returns:
- the description of what the tool does
-
setDescription
Sets the tool description.The description helps the AI understand when and how to use the tool. Be clear and specific about the tool's purpose and any constraints.
- Parameters:
description- the description of what the tool does- Returns:
- this tool definition for method chaining
-
getParameters
Gets the parameter schema.- Returns:
- the JSON Schema for the tool's parameters
-
setParameters
Sets the parameter schema.The schema should follow JSON Schema format and define the structure of arguments the tool accepts. This is typically a
Mapwith "type", "properties", and "required" fields.- Parameters:
parameters- the JSON Schema for the tool's parameters- Returns:
- this tool definition for method chaining
-
getHandler
Gets the tool handler.- Returns:
- the handler function that executes when the tool is invoked
-
setHandler
Sets the tool handler.The handler is called when the assistant invokes this tool. It receives a
ToolInvocationwith the arguments and should return aCompletableFuturewith the result.- Parameters:
handler- the handler function- Returns:
- this tool definition for method chaining
- See Also:
-
create
public static ToolDefinition create(String name, String description, Map<String, Object> schema, ToolHandler handler) Creates a tool definition with a JSON schema for parameters.This is a convenience factory method for creating tools with a
Map-based parameter schema.- Parameters:
name- the unique name of the tooldescription- a description of what the tool doesschema- the JSON Schema as aMaphandler- the handler function to execute when invoked- Returns:
- a new tool definition
-