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 the value of thedescriptionrecord component.final booleanIndicates whether some other object is "equal to" this one.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.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.Returns the value of theparametersrecord component.Returns the value of theskipPermissionrecord component.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.2
-
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.2
-
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
-