
在 Spring AI 开发 AI Agent 的过程中,Tool Calling(工具调用) 是最核心、最能体现 AI 智能化的能力。 很多同学在实战时都会遇到这些问题:
今天这篇就带着大家一次性吃透 Spring AI 3 种标准工具定义方式,全部是可直接复制运行的企业级代码,图文并茂、一看就会!
环境:
Tool Calling 的本质: 让大模型自主判断是否需要调用外部方法 → 自动调用 → 取回结果 → 自然语言回答。

流程步骤:
Spring AI 目前提供 3 种标准工具定义方式,覆盖 99% 开发场景:
直接在普通 Java 方法上加 @Tool 注解,Spring AI 自动识别为可调用工具。
@Component
public class TimeTools {
/**
* 获取当前系统时间工具
*/
@Tool(description = "获取当前系统的日期与时间,格式:yyyy-MM-dd HH:mm:ss")
public String getCurrentTime() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}时间查询、天气查询、数据库操作、订单服务、消息推送等常规业务工具。
这是 Spring AI 原生支持的优雅写法,也是官方文档重点推荐的方式。 不需要类、不需要注解,基于 JDK 函数式接口定义。
// 定义入参、出参
record WeatherRequest(String city, String unit) {}
record WeatherResponse(double temperature, String unit) {}
// 函数式接口
interface WeatherService extends Function<WeatherRequest, WeatherResponse> {
@Override
WeatherResponse apply(WeatherRequest request);
}
@Test
void testFunctionTool() {
// 构建函数式工具
ToolCallback tool = FunctionToolCallback.builder("currentWeather", new WeatherService() {
@Override
public WeatherResponse apply(WeatherRequest request) {
return new WeatherResponse(25.5, request.unit());
}
})
.description("获取指定城市的实时天气")
.inputType(WeatherRequest.class)
.build();
// 调用
String result = ChatClient.create(chatModel)
.prompt("长沙今天天气怎么样?")
.tools(tool)
.call()
.content();
System.out.println(result);
}@Configuration
public class AiToolConfig {
public static final String WEATHER_TOOL = "currentWeather";
@Bean(WEATHER_TOOL)
@Description("获取指定城市的实时天气")
public Function<WeatherRequest, WeatherResponse> currentWeather() {
return request -> new WeatherResponse(30.0, request.unit());
}
}@Test
void testConfigFunctionTool() {
String content = ChatClient.create(chatModel)
.prompt("今天长沙天气怎么样?")
.toolNames(AiToolConfig.WEATHER_TOOL)
.call()
.content();
System.out.println(content);
}轻量工具、结构化工具、快速开发、企业级规范项目。
完全手动构建工具:名称、描述、入参 Schema、执行体全部自己控制。 适合:动态工具、插件化、运行时注册、多租户场景。
// 计算器工具
public class CalcTools {
public Integer add(int a, int b) {
return a + b;
}
}
@Test
void testProgrammaticTool() {
// 反射获取方法
Method method = ReflectionUtils.findMethod(CalcTools.class, "add", int.class, int.class);
// 手动定义工具元数据
ToolDefinition definition = ToolDefinition.builder()
.name("add")
.description("计算两个整数的和")
.inputSchema("""
{
"type": "object",
"properties": {
"a": { "type": "integer", "description": "第一个数字" },
"b": { "type": "integer", "description": "第二个数字" }
},
"required": ["a", "b"]
}
""")
.build();
// 构建 ToolCallback
ToolCallback callback = MethodToolCallback.builder()
.toolDefinition(definition)
.toolMethod(method)
.toolObject(new CalcTools())
.build();
// 调用
String content = ChatClient.builder(chatModel).build()
.prompt("100 加 1000 等于多少?")
.tools(callback)
.call()
.content();
System.out.println(content);
}动态工具、插件化 AI Agent、工作流、多租户、低代码平台。
工具方式 | 难度 | 代码量 | 灵活性 | 企业使用 |
|---|---|---|---|---|
@Tool 注解 | ⭐ | 极少 | 中 | 主流、首选 |
函数式 Function | ⭐⭐ | 极简 | 中高 | 推荐、规范 |
编程式 ToolCallback | ⭐⭐⭐ | 中等 | 最高 | 动态/插件架构 |
一句话选择:
Spring AI Tool Calling 3 种标准工具定义方式:
这一篇吃透,你就能轻松开发企业级 AI 智能体(Agent)!