Selaa lähdekoodia

mcp单元测试

Grizzly 8 kuukautta sitten
vanhempi
commit
ace1a1dbb1

+ 0 - 117
src/main/java/edu/nju/software/aipaasagent/client/mcp/McpTool.java

@@ -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;
-    }
-}

+ 0 - 45
src/main/java/edu/nju/software/aipaasagent/client/mcp/config/McpToolConfig.java

@@ -1,45 +0,0 @@
-package edu.nju.software.aipaasagent.client.mcp.config;
-
-import lombok.Data;
-
-/**
- * MCP 工具配置
- * 
- * 用于在 ConfigMap 中配置要调用的 MCP 工具
- * 
- * YAML 配置示例:
- * agent:
- *   mcpTools:
- *     - name: "weather_query"
- *       group: "weather-service"
- *       description: "查询指定城市的天气"
- *     - name: "db_query"
- *       group: "database-server"
- *       description: "执行 SQL 查询"
- */
-@Data
-public class McpToolConfig {
-    
-    /**
-     * 工具名称
-     * 对应 MCP Hub 中的工具名
-     */
-    private String name;
-    
-    /**
-     * MCP 分组/服务器名称
-     * 对应同学 MCP Hub 中的 server name
-     */
-    private String group;
-    
-    /**
-     * 工具描述
-     * 用于 ReAct Agent 理解工具用途
-     */
-    private String description;
-    
-    /**
-     * 是否启用
-     */
-    private boolean enabled = true;
-}

+ 0 - 39
src/test/java/edu/nju/software/aipaasagent/client/mcp/McpClientServiceTest.java

@@ -1,39 +0,0 @@
-package edu.nju.software.aipaasagent.client.mcp;
-
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-
-import java.util.concurrent.TimeUnit;
-
-@SpringBootTest
-public class McpClientServiceTest {
-
-    @Autowired
-    private McpClientService mcpClientService;
-
-    @Test
-    public void testConnect() {
-        System.out.println("Testing MCP connection...");
-        try {
-            String sessionId = mcpClientService.connect("default");
-            System.out.println("Session ID: " + sessionId);
-        } catch (Exception e) {
-            System.out.println("Error: " + e.getMessage());
-            e.printStackTrace();
-        }
-    }
-
-    @Test
-    public void testListTools() {
-        System.out.println("Testing list tools...");
-        try {
-            mcpClientService.connect("default");
-            var result = mcpClientService.listTools("default");
-            System.out.println("Tools: " + result);
-        } catch (Exception e) {
-            System.out.println("Error: " + e.getMessage());
-            e.printStackTrace();
-        }
-    }
-}

+ 4 - 0
src/test/java/edu/nju/software/aipaasagent/client/mcp/McpClientTest.java

@@ -0,0 +1,4 @@
+package edu.nju.software.aipaasagent.client.mcp;
+
+public class McpClientTest {
+}

+ 0 - 52
src/test/java/edu/nju/software/aipaasagent/client/mcp/McpConnectionTest.java

@@ -1,52 +0,0 @@
-package edu.nju.software.aipaasagent.client.mcp;
-
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-
-import java.util.Map;
-
-@SpringBootTest
-public class McpConnectionTest {
-
-    @Autowired
-    private McpClientService mcpClientService;
-
-    @Test
-    public void testMcpConnection() {
-        System.out.println("Testing MCP connection...");
-        try {
-            // 测试普通 MCP 连接
-            String sessionId = mcpClientService.connect("default");
-            System.out.println("MCP Session ID: " + sessionId);
-            
-            // 测试获取工具列表
-            var tools = mcpClientService.listTools("default");
-            System.out.println("Tools: " + tools);
-            
-            // 测试调用工具
-            Map<String, Object> arguments = Map.of(
-                    "a", 1,
-                    "b", 2
-            );
-            var result = mcpClientService.callTool("default", "everything_get-sum", arguments);
-            System.out.println("Tool call result: " + result);
-            
-            // 测试智能路由连接
-            String intelligenceSessionId = mcpClientService.connectIntelligence("default");
-            System.out.println("Intelligence Session ID: " + intelligenceSessionId);
-            
-            // 测试搜索工具
-            var searchResult = mcpClientService.searchTools("default", "求和", 5);
-            System.out.println("Search tools result: " + searchResult);
-            
-            // 测试执行工具
-            var executeResult = mcpClientService.executeTool("default", "everything_get-sum", arguments);
-            System.out.println("Execute tool result: " + executeResult);
-            
-        } catch (Exception e) {
-            System.out.println("Error: " + e.getMessage());
-            e.printStackTrace();
-        }
-    }
-}