批注接口 Tool
Methods annotated with @Tool are automatically registered with the toolkit and made
available to agents for execution. The toolkit uses reflection to discover tool methods and
generate appropriate JSON schemas for LLM consumption.
Usage Example:
public class WeatherTools {
@Tool(name = "get_weather", description = "Get current weather for a city")
public String getWeather(
@ToolParam(name = "city", description = "City name") String city,
@ToolParam(name = "unit", description = "Temperature unit") String unit) {
// Implementation
return "Weather data...";
}
}
Requirements:
- All parameters must be annotated with
ToolParam(exceptToolEmitter) - Return type must be String, Mono<String>, or other reactive types
- Tool names should follow snake_case convention for LLM compatibility
- Descriptions should clearly explain what the tool does and when to use it
- 另请参阅:
-
可选元素概要
可选元素修饰符和类型可选元素说明booleanWhether the tool is safe to invoke concurrently with itself.Class<? extends ToolResultConverter> Custom result converter for this tool.String[]Sensitive directory names whose presence in any path segment marks the path dangerous.String[]Sensitive filenames that must require explicit permission for this tool.The description of the tool that explains its purpose and usage.booleanWhether the tool is executed outside the framework.The name of the tool.booleanWhether the tool only reads data without observable side effects.booleanWhether the tool requires the agent state to be injected at call time.booleanWhether to enable strict schema mode for this tool.
-
元素详细资料
-
name
String nameThe name of the tool.If not provided, the method name will be used. Tool names should follow snake_case convention (e.g., "get_weather", "send_email") for compatibility with various LLM providers.
- 返回:
- The tool name, or empty string to use method name
- 默认值:
""
-
description
String descriptionThe description of the tool that explains its purpose and usage.This description is sent to the LLM to help it decide when to invoke the tool. It should clearly explain:
- What the tool does
- When it should be used
- What kind of results it returns
If not provided, a generic description based on the method name will be generated.
- 返回:
- The tool description, or empty string to auto-generate
- 默认值:
""
-
strict
boolean strictWhether to enable strict schema mode for this tool.When enabled, compatible model providers can enforce stronger adherence to the declared JSON schema for tool arguments.
- 返回:
- true to enable strict mode for this tool
- 默认值:
false
-
readOnly
boolean readOnlyWhether the tool only reads data without observable side effects.Read-only tools are auto-allowed under
PermissionMode.EXPLOREandACCEPT_EDITS, mirroring theToolBase.isReadOnly()contract.- 返回:
- true if this tool performs no mutation
- 默认值:
false
-
concurrencySafe
boolean concurrencySafeWhether the tool is safe to invoke concurrently with itself.When false, the framework serialises invocations of this tool inside a parallel batch. Defaults to
trueto match the typical pure-function shape of@Toolmethods.- 返回:
- true if multiple invocations may run in parallel without coordination
- 默认值:
true
-
externalTool
boolean externalToolWhether the tool is executed outside the framework.External tools never run their
callAsyncbody. Instead the framework throws aToolSuspendExceptionso the agent loop can surface the call to the caller via aTOOL_SUSPENDEDmessage.- 返回:
- true if this tool should be surfaced as a suspended call
- 默认值:
false
-
stateInjected
boolean stateInjectedWhether the tool requires the agent state to be injected at call time.When true, the method signature must declare exactly one
io.agentscope.core.state.AgentStateparameter; the framework excludes that parameter from the JSON schema and binds the live state at invocation.- 返回:
- true if the tool method consumes the agent state
- 默认值:
false
-
dangerousFiles
String[] dangerousFilesSensitive filenames that must require explicit permission for this tool.An empty array sticks with the default list maintained by
ToolBase.- 返回:
- additional dangerous filenames; empty means use defaults
- 默认值:
{}
-
dangerousDirectories
String[] dangerousDirectoriesSensitive directory names whose presence in any path segment marks the path dangerous.An empty array sticks with the default list maintained by
ToolBase.- 返回:
- additional dangerous directory names; empty means use defaults
- 默认值:
{}
-
converter
Class<? extends ToolResultConverter> converterCustom result converter for this tool.Converters transform tool method return values into
ToolResultBlockinstances suitable for LLM consumption. Use custom converters to:- Filter sensitive data from results
- Format output in specific ways
- Add metadata to results
- Compress or summarize large outputs
Usage Example:
@Tool( name = "get_data", converter = CustomJsonConverter.class ) public MyData getData(String id) { return dataService.findById(id); }If not specified, the default converter (
DefaultToolResultConverter) is used, which provides JSON serialization with schema information.Note: If you need complex processing with multiple steps, implement your own converter that combines the necessary logic.
- 返回:
- Converter class
- 另请参阅:
- 默认值:
io.agentscope.core.tool.DefaultToolResultConverter.class
-