批注接口 Tool


Annotation to mark a method as a tool that can be invoked by AI agents.

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 (except ToolEmitter)
  • 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
另请参阅:
  • 元素详细资料

    • name

      String name
      The 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 description
      The 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
      默认值:
      ""
    • converter

      Class<? extends ToolResultConverter> converter
      Custom result converter for this tool.

      Converters transform tool method return values into ToolResultBlock instances 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