Record Class ToolDefinition
- Record Components:
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 invokedoverridesBuiltInTool- whentrue, indicates that this tool intentionally overrides a built-in CLI tool with the same name;nullorfalsemeans the tool is purely customskipPermission- whentrue, the CLI skips the permission request for this tool invocation;nullorfalseuses normal permission handlingdefer- controls whether the tool may be deferred (loaded lazily via tool search) rather than always pre-loaded;nulllets the runtime decide
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
ConstructorsConstructorDescriptionToolDefinition(String name, String description, Object parameters, ToolHandler handler, Boolean overridesBuiltInTool, Boolean skipPermission, ToolDefer defer) Creates an instance of aToolDefinitionrecord class. -
Method Summary
Modifier and TypeMethodDescriptionstatic ToolDefinitionCreates a tool definition with a JSON schema for parameters.static ToolDefinitioncreateOverride(String name, String description, Map<String, Object> schema, ToolHandler handler) Creates a tool definition that overrides a built-in CLI tool.static ToolDefinitioncreateSkipPermission(String name, String description, Map<String, Object> schema, ToolHandler handler) Creates a tool definition that skips the permission request.static ToolDefinitioncreateWithDefer(String name, String description, Map<String, Object> schema, ToolHandler handler, ToolDefer defer) Creates a tool definition with an explicit deferral mode.defer()Returns the value of thedeferrecord component.Returns a copy with thedefermode set.Returns the value of thedescriptionrecord component.final booleanIndicates whether some other object is "equal to" this one.static <T1,T2, R> ToolDefinition Creates a tool definition with a two-argument synchronous handler.static <T1,R> ToolDefinition Creates a tool definition with a one-argument synchronous handler.static <R> ToolDefinitionCreates a tool definition with a zero-argument synchronous handler.static <T1,T2, R> ToolDefinition fromAsync(String name, String description, Param<T1> p1, Param<T2> p2, BiFunction<T1, T2, CompletableFuture<R>> handler) Creates a tool definition with a two-argument asynchronous handler.static <T1,R> ToolDefinition fromAsync(String name, String description, Param<T1> p1, Function<T1, CompletableFuture<R>> handler) Creates a tool definition with a one-argument asynchronous handler.static <R> ToolDefinitionfromAsync(String name, String description, Supplier<CompletableFuture<R>> handler) Creates a tool definition with a zero-argument asynchronous handler.static <T1,R> ToolDefinition fromAsyncWithToolInvocation(String name, String description, Param<T1> p1, BiFunction<T1, ToolInvocation, CompletableFuture<R>> handler) Creates a tool definition with a one-argument asynchronous handler that also receives theToolInvocationcontext.static <R> ToolDefinitionfromAsyncWithToolInvocation(String name, String description, Function<ToolInvocation, CompletableFuture<R>> handler) Creates a tool definition with a zero-argument asynchronous handler that receives theToolInvocationcontext.static List<ToolDefinition> Discovers tool definitions from a class with static@CopilotTool-annotated methods.static List<ToolDefinition> fromObject(Object instance) Discovers tool definitions from an object whose methods are annotated with@CopilotTool.static <T1,R> ToolDefinition fromWithToolInvocation(String name, String description, Param<T1> p1, BiFunction<T1, ToolInvocation, R> handler) Creates a tool definition with a one-argument synchronous handler that also receives theToolInvocationcontext.static <R> ToolDefinitionfromWithToolInvocation(String name, String description, Function<ToolInvocation, R> handler) Creates a tool definition with a zero-argument synchronous handler that receives theToolInvocationcontext.handler()Returns the value of thehandlerrecord component.final inthashCode()Returns a hash code value for this object.name()Returns the value of thenamerecord component.Returns the value of theoverridesBuiltInToolrecord component.overridesBuiltInTool(boolean value) Returns a copy with theoverridesBuiltInToolflag set.Returns the value of theparametersrecord component.Returns the value of theskipPermissionrecord component.skipPermission(boolean value) Returns a copy with theskipPermissionflag set.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
ToolDefinition
public ToolDefinition(String name, String description, Object parameters, ToolHandler handler, Boolean overridesBuiltInTool, Boolean skipPermission, ToolDefer defer) Creates an instance of aToolDefinitionrecord class.- Parameters:
name- the value for thenamerecord componentdescription- the value for thedescriptionrecord componentparameters- the value for theparametersrecord componenthandler- the value for thehandlerrecord componentoverridesBuiltInTool- the value for theoverridesBuiltInToolrecord componentskipPermission- the value for theskipPermissionrecord componentdefer- the value for thedeferrecord component
-
-
Method Details
-
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
-
createOverride
public static ToolDefinition createOverride(String name, String description, Map<String, Object> schema, ToolHandler handler) Creates a tool definition that overrides a built-in CLI tool.Use this factory method when you want your custom tool to replace a built-in tool (e.g.,
grep,read_file) with the same name. SettingoverridesBuiltInTooltotruesignals to the CLI that this is intentional.- Parameters:
name- the name of the built-in tool to overridedescription- a description of what the tool doesschema- the JSON Schema as aMaphandler- the handler function to execute when invoked- Returns:
- a new tool definition with the override flag set
- Since:
- 1.0.11
-
createSkipPermission
public static ToolDefinition createSkipPermission(String name, String description, Map<String, Object> schema, ToolHandler handler) Creates a tool definition that skips the permission request.Use this factory method when the tool is safe to invoke without user permission confirmation. Setting
skipPermissiontotruesignals to the CLI that no permission check is needed.- 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 with permission skipping enabled
- Since:
- 1.0.0
-
createWithDefer
public static ToolDefinition createWithDefer(String name, String description, Map<String, Object> schema, ToolHandler handler, ToolDefer defer) Creates a tool definition with an explicit deferral mode.Use this factory method to control whether the tool may be deferred (loaded lazily via tool search) rather than always pre-loaded. Pass
ToolDefer.AUTOto allow deferral andToolDefer.NEVERto force the tool to always be pre-loaded.- 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 invokeddefer- the deferral mode for the tool- Returns:
- a new tool definition with the deferral mode set
- Since:
- 1.0.0
-
fromObject
Discovers tool definitions from an object whose methods are annotated with@CopilotTool. Requires that theCopilotToolProcessorannotation processor ran at compile time (generating the$$CopilotToolMetacompanion class).- Parameters:
instance- the object containing@CopilotTool-annotated methods- Returns:
- list of tool definitions with working invocation handlers
- Throws:
IllegalStateException- if the generated$$CopilotToolMetaclass is not found (annotation processor did not run)- Since:
- 1.0.6
-
fromClass
Discovers tool definitions from a class with static@CopilotTool-annotated methods. Requires that theCopilotToolProcessorannotation processor ran at compile time (generating the$$CopilotToolMetacompanion class).- Parameters:
clazz- the class containing static@CopilotTool-annotated methods- Returns:
- list of tool definitions with working invocation handlers
- Throws:
IllegalStateException- if the generated$$CopilotToolMetaclass is not found (annotation processor did not run)- Since:
- 1.0.6
-
overridesBuiltInTool
Returns a copy with theoverridesBuiltInToolflag set.- Parameters:
value-trueto indicate this tool intentionally overrides a built-in CLI tool with the same name- Returns:
- a new
ToolDefinitionwith the flag applied - Since:
- 1.0.6
-
skipPermission
Returns a copy with theskipPermissionflag set.- Parameters:
value-trueto skip the permission request for this tool invocation- Returns:
- a new
ToolDefinitionwith the flag applied - Since:
- 1.0.6
-
defer
Returns a copy with thedefermode set.- Parameters:
value- the deferral mode; useToolDefer.AUTOto allow deferral orToolDefer.NEVERto force the tool to always be pre-loaded- Returns:
- a new
ToolDefinitionwith the defer mode applied - Since:
- 1.0.6
-
from
@CopilotExperimental public static <R> ToolDefinition from(String name, String description, Supplier<R> handler) Creates a tool definition with a zero-argument synchronous handler.The handler is a
Supplierthat returns the tool result.Example
ToolDefinition ping = ToolDefinition.from("ping", "Returns a simple pong response", () -> "pong");- Type Parameters:
R- the return type of the handler- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)handler- the zero-argument sync handler- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- ifnameordescriptionis blank, or ifhandleris null- Since:
- 1.0.6
-
from
@CopilotExperimental public static <T1,R> ToolDefinition from(String name, String description, Param<T1> p1, Function<T1, R> handler) Creates a tool definition with a one-argument synchronous handler.Example
ToolDefinition greet = ToolDefinition.from("greet", "Greets a user by name", Param.of(String.class, "name", "The user's name"), name -> "Hello, " + name + "!");- Type Parameters:
T1- the type of the first parameterR- the return type of the handler- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)p1- the first parameter descriptorhandler- the one-argument sync handler- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- if validation fails- Since:
- 1.0.6
-
from
@CopilotExperimental public static <T1,T2, ToolDefinition fromR> (String name, String description, Param<T1> p1, Param<T2> p2, BiFunction<T1, T2, R> handler) Creates a tool definition with a two-argument synchronous handler.Example
ToolDefinition add = ToolDefinition.from("add", "Adds two integers", Param.of(Integer.class, "a", "First number"), Param.of(Integer.class, "b", "Second number"), (a, b) -> a + b);- Type Parameters:
T1- the type of the first parameterT2- the type of the second parameterR- the return type of the handler- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)p1- the first parameter descriptorp2- the second parameter descriptorhandler- the two-argument sync handler- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- if validation fails- Since:
- 1.0.6
-
fromAsync
@CopilotExperimental public static <R> ToolDefinition fromAsync(String name, String description, Supplier<CompletableFuture<R>> handler) Creates a tool definition with a zero-argument asynchronous handler.The handler is a
Supplierreturning aCompletableFuture.Example
ToolDefinition ping = ToolDefinition.fromAsync("ping", "Returns a pong response asynchronously", () -> CompletableFuture.completedFuture("pong"));- Type Parameters:
R- the return type wrapped inCompletableFuture- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)handler- the zero-argument async handler- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- if validation fails- Since:
- 1.0.6
-
fromAsync
@CopilotExperimental public static <T1,R> ToolDefinition fromAsync(String name, String description, Param<T1> p1, Function<T1, CompletableFuture<R>> handler) Creates a tool definition with a one-argument asynchronous handler.Example
ToolDefinition greet = ToolDefinition.fromAsync("greet_async", "Greets a user by name asynchronously", Param.of(String.class, "name", "The user's name"), name -> CompletableFuture.completedFuture("Hello, " + name + "!"));- Type Parameters:
T1- the type of the first parameterR- the return type wrapped inCompletableFuture- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)p1- the first parameter descriptorhandler- the one-argument async handler- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- if validation fails- Since:
- 1.0.6
-
fromAsync
@CopilotExperimental public static <T1,T2, ToolDefinition fromAsyncR> (String name, String description, Param<T1> p1, Param<T2> p2, BiFunction<T1, T2, CompletableFuture<R>> handler) Creates a tool definition with a two-argument asynchronous handler.- Type Parameters:
T1- the type of the first parameterT2- the type of the second parameterR- the return type wrapped inCompletableFuture- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)p1- the first parameter descriptorp2- the second parameter descriptorhandler- the two-argument async handler- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- if validation fails- Since:
- 1.0.6
-
fromWithToolInvocation
@CopilotExperimental public static <R> ToolDefinition fromWithToolInvocation(String name, String description, Function<ToolInvocation, R> handler) Creates a tool definition with a zero-argument synchronous handler that receives theToolInvocationcontext.Example
ToolDefinition sessionInfo = ToolDefinition.fromWithToolInvocation("session_info", "Return the current session id", invocation -> "sessionId=" + invocation.getSessionId());- Type Parameters:
R- the return type of the handler- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)handler- a function accepting theToolInvocationcontext- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- if validation fails- Since:
- 1.0.6
-
fromWithToolInvocation
@CopilotExperimental public static <T1,R> ToolDefinition fromWithToolInvocation(String name, String description, Param<T1> p1, BiFunction<T1, ToolInvocation, R> handler) Creates a tool definition with a one-argument synchronous handler that also receives theToolInvocationcontext.Example
ToolDefinition reportPhase = ToolDefinition.fromWithToolInvocation("report_phase", "Report the current phase along with invocation context", Param.of(String.class, "phase", "Current phase"), (phase, invocation) -> "phase=" + phase + ", toolCallId=" + invocation.getToolCallId());- Type Parameters:
T1- the type of the first parameterR- the return type of the handler- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)p1- the first parameter descriptorhandler- a function accepting the typed argument and theToolInvocationcontext- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- if validation fails- Since:
- 1.0.6
-
fromAsyncWithToolInvocation
@CopilotExperimental public static <R> ToolDefinition fromAsyncWithToolInvocation(String name, String description, Function<ToolInvocation, CompletableFuture<R>> handler) Creates a tool definition with a zero-argument asynchronous handler that receives theToolInvocationcontext.Example
ToolDefinition sessionInfo = ToolDefinition.fromAsyncWithToolInvocation("session_info_async", "Return the current session id asynchronously", invocation -> CompletableFuture.completedFuture("sessionId=" + invocation.getSessionId()));- Type Parameters:
R- the return type wrapped inCompletableFuture- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)handler- a function accepting theToolInvocationcontext, returning aCompletableFuture- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- if validation fails- Since:
- 1.0.6
-
fromAsyncWithToolInvocation
@CopilotExperimental public static <T1,R> ToolDefinition fromAsyncWithToolInvocation(String name, String description, Param<T1> p1, BiFunction<T1, ToolInvocation, CompletableFuture<R>> handler) Creates a tool definition with a one-argument asynchronous handler that also receives theToolInvocationcontext.Example
ToolDefinition reportPhase = ToolDefinition.fromAsyncWithToolInvocation("report_phase_async", "Report the current phase with invocation context asynchronously", Param.of(String.class, "phase", "The current phase"), (phase, invocation) -> CompletableFuture .completedFuture("phase=" + phase + ", toolCallId=" + invocation.getToolCallId()));- Type Parameters:
T1- the type of the first parameterR- the return type wrapped inCompletableFuture- Parameters:
name- the unique name of the tool (must not be blank)description- a description of what the tool does (must not be blank)p1- the first parameter descriptorhandler- a function accepting the typed argument and theToolInvocationcontext, returning aCompletableFuture- Returns:
- a new tool definition
- Throws:
IllegalArgumentException- if validation fails- Since:
- 1.0.6
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
name
-
description
Returns the value of thedescriptionrecord component.- Returns:
- the value of the
descriptionrecord component
-
parameters
Returns the value of theparametersrecord component.- Returns:
- the value of the
parametersrecord component
-
handler
-
overridesBuiltInTool
Returns the value of theoverridesBuiltInToolrecord component.- Returns:
- the value of the
overridesBuiltInToolrecord component
-
skipPermission
Returns the value of theskipPermissionrecord component.- Returns:
- the value of the
skipPermissionrecord component
-
defer
-