|
|
@@ -1,117 +0,0 @@
|
|
|
-package edu.nju.software.aipaasagent.client.mcp;
|
|
|
-
|
|
|
-import edu.nju.software.aipaasagent.client.mcp.dto.ToolCallResult;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.ai.chat.model.ToolContext;
|
|
|
-import org.springframework.ai.tool.ToolCallback;
|
|
|
-import org.springframework.ai.tool.definition.ToolDefinition;
|
|
|
-
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-/**
|
|
|
- * MCP 工具回调实现
|
|
|
- *
|
|
|
- * 将 MCP Hub 中的工具包装为 Spring AI 的 ToolCallback
|
|
|
- * 供 ReAct Agent 使用
|
|
|
- *
|
|
|
- * 架构位置:client/mcp/ - 与 McpClientService 同级
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-public class McpTool implements ToolCallback {
|
|
|
-
|
|
|
- private final String name;
|
|
|
- private final String description;
|
|
|
- private final String group;
|
|
|
- private final String inputSchema;
|
|
|
- private final McpClientService mcpClientService;
|
|
|
-
|
|
|
- public McpTool(McpClientService mcpClientService, String name, String group, String description) {
|
|
|
- this(mcpClientService, name, group, description, null);
|
|
|
- }
|
|
|
-
|
|
|
- public McpTool(McpClientService mcpClientService, String name, String group, String description, String inputSchema) {
|
|
|
- this.mcpClientService = mcpClientService;
|
|
|
- this.name = name;
|
|
|
- this.group = group;
|
|
|
- this.description = description;
|
|
|
- this.inputSchema = inputSchema;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ToolDefinition getToolDefinition() {
|
|
|
- // 如果有实际的 inputSchema,使用它;否则使用通用的 schema
|
|
|
- String schema = (inputSchema != null && !inputSchema.isEmpty())
|
|
|
- ? inputSchema
|
|
|
- : """
|
|
|
- {
|
|
|
- "type": "object",
|
|
|
- "properties": {}
|
|
|
- }
|
|
|
- """;
|
|
|
-
|
|
|
- return ToolDefinition.builder()
|
|
|
- .name(name)
|
|
|
- .description(description)
|
|
|
- .inputSchema(schema)
|
|
|
- .build();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String call(String toolInput) {
|
|
|
- log.info("执行 MCP 工具 [name={}, group={}, input={}]", name, group, toolInput);
|
|
|
-
|
|
|
- try {
|
|
|
- // 解析输入参数
|
|
|
- Map<String, Object> args = parseInput(toolInput);
|
|
|
-
|
|
|
- // 调用 MCP Hub
|
|
|
- ToolCallResult result;
|
|
|
- // 这里简化处理,实际应该根据会话的模式来选择调用方法
|
|
|
- // 暂时使用普通的 callTool 方法
|
|
|
- result = mcpClientService.callTool(group, name, args);
|
|
|
-
|
|
|
- if (result.isError()) {
|
|
|
- log.error("MCP 工具执行失败 [name={}]: {}", name, result.getContent());
|
|
|
- return "工具执行失败:" + result.getContent();
|
|
|
- }
|
|
|
-
|
|
|
- // 返回结果内容
|
|
|
- String content = result.getContent();
|
|
|
- log.info("MCP 工具执行成功 [name={}], 结果长度:{}", name,
|
|
|
- content != null ? content.length() : 0);
|
|
|
-
|
|
|
- return content != null ? content : "工具执行成功,但无返回内容";
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("MCP 工具执行异常 [name={}]: {}", name, e.getMessage(), e);
|
|
|
- return "工具执行异常:" + e.getMessage();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String call(String toolInput, ToolContext toolContext) {
|
|
|
- // ToolContext 包含 conversationId 等信息,但 MCP 工具暂时不需要
|
|
|
- return call(toolInput);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 解析工具输入参数
|
|
|
- */
|
|
|
- private Map<String, Object> parseInput(String toolInput) {
|
|
|
- try {
|
|
|
- // 使用 Jackson 解析 JSON
|
|
|
- com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
|
|
|
- return mapper.readValue(toolInput, new com.fasterxml.jackson.core.type.TypeReference<Map<String, Object>>() {});
|
|
|
- } catch (Exception e) {
|
|
|
- log.warn("解析工具输入失败,使用原始输入:{}", toolInput);
|
|
|
- return Map.of("input", toolInput);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取 MCP 分组
|
|
|
- */
|
|
|
- public String getGroup() {
|
|
|
- return group;
|
|
|
- }
|
|
|
-}
|