前言
在《Tool 与 MCP 设计思路》一节中我们提到了几个工具,那么这一节我们就来手把手的写2个工具,并交给大模型使用。
核心代码目录:src/main/java/org/example/agent/tool
当前时间查询工具
按照ai框架的规范用@Tool来表示工具:
第一个参数是
toolName,用于表示工具名第二个参数是
toolDesc,用于告诉大模型这个工具的功能
@Component
public class DateTimeTools {
@Tool(name = "getCurrentDateTime", description = "Get the current date and time in the user's timezone")
public String getCurrentDateTime() {
return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
}
}
腾讯云日志MCP工具
通过 MCP Server 查询日志服务 CLS 中存储的日志数据,以实现大模型平台/工具与日志数据的结合。例如使用自然语言查询日志,降低日志查询复杂度 https://cloud.tencent.com/developer/mcp/server/11710
MCP配置:[环境准备教程](/oncall/智能 OnCall Agent 项目/第八章 _ 实战演练与运行项目/环境准备教程/)
Java代码的MCP使用了Spring AI的能力。(QueryLogsTools,它只是本地模拟工具,不是真实的MCP)
也就是说:
业务代码没有
new McpClient(...)MCP Client 是由 Spring AI 自动配置创建的
触发自动配置的是
pom.xml里的依赖:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client-webflux</artifactId>
</dependency>
实际链路是:
spring-ai-starter-mcp-client-webflux
↓
Spring Boot AutoConfiguration
↓
创建 MCP Client / MCP Session / ToolCallbackProvider
↓
注入到 ChatService.tools
↓
tools.getToolCallbacks()
↓
ReactAgent.builder().tools(...)
getToolCallbacks() 这里没有创建 MCP Client。它只是使用 Spring 容器里已经自动创建好的 Bean:
@Autowired
private ToolCallbackProvider tools;
/**
* 获取工具回调列表,mcp服务提供的工具
*/
public ToolCallback[] getToolCallbacks() {
return tools.getToolCallbacks();
}