|
|
@@ -0,0 +1,90 @@
|
|
|
+package com.zsjz.ai.common.mcp;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.zsjz.ai.common.mcp.tools.CurrentTimeTool;
|
|
|
+import com.zsjz.ai.common.mcp.tools.EchoTool;
|
|
|
+import io.modelcontextprotocol.json.McpJsonMapper;
|
|
|
+import io.modelcontextprotocol.json.jackson2.JacksonMcpJsonMapper;
|
|
|
+import io.modelcontextprotocol.server.McpServerFeatures;
|
|
|
+import io.modelcontextprotocol.spec.McpSchema;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+
|
|
|
+/**
|
|
|
+ * {@link McpToolRegistry} 的契约:工具规格生成、调用结果包装、异常转错误、重名校验。
|
|
|
+ */
|
|
|
+class McpToolRegistryTest {
|
|
|
+
|
|
|
+ private final McpJsonMapper jsonMapper = new JacksonMcpJsonMapper(new ObjectMapper());
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("工具清单:名字、schema、描述都进入 MCP Tool 规格")
|
|
|
+ void buildsSpecifications() {
|
|
|
+ McpToolRegistry registry = new McpToolRegistry(
|
|
|
+ List.of(new EchoTool(), new CurrentTimeTool()), jsonMapper);
|
|
|
+
|
|
|
+ assertEquals(List.of("echo", "current_time"), registry.toolNames());
|
|
|
+
|
|
|
+ List<McpServerFeatures.SyncToolSpecification> specifications = registry.specifications();
|
|
|
+ assertEquals(2, specifications.size());
|
|
|
+
|
|
|
+ McpSchema.Tool echo = specifications.get(0).tool();
|
|
|
+ assertEquals("echo", echo.name());
|
|
|
+ assertEquals("Echo", echo.title());
|
|
|
+ assertNotNull(echo.description());
|
|
|
+ assertNotNull(echo.inputSchema());
|
|
|
+ assertEquals("object", echo.inputSchema().type());
|
|
|
+ assertTrue(echo.inputSchema().required().contains("message"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("成功调用:返回文本且 isError=false")
|
|
|
+ void callsToolSuccessfully() {
|
|
|
+ McpToolRegistry registry = new McpToolRegistry(List.of(new EchoTool()), jsonMapper);
|
|
|
+
|
|
|
+ McpSchema.CallToolResult result = registry.specifications().get(0)
|
|
|
+ .callHandler()
|
|
|
+ .apply(null, new McpSchema.CallToolRequest("echo", Map.of("message", "你好")));
|
|
|
+
|
|
|
+ assertFalse(result.isError());
|
|
|
+ assertEquals("你好", ((McpSchema.TextContent) result.content().get(0)).text());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("工具抛异常:转成 isError=true 的可读文本,而不是协议错误")
|
|
|
+ void wrapsToolFailure() {
|
|
|
+ McpToolRegistry registry = new McpToolRegistry(List.of(new EchoTool()), jsonMapper);
|
|
|
+
|
|
|
+ McpSchema.CallToolResult result = registry.specifications().get(0)
|
|
|
+ .callHandler()
|
|
|
+ .apply(null, new McpSchema.CallToolRequest("echo", Map.of()));
|
|
|
+
|
|
|
+ assertTrue(result.isError());
|
|
|
+ String text = ((McpSchema.TextContent) result.content().get(0)).text();
|
|
|
+ assertTrue(text.contains("message 不能为空"), text);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("工具名重复:启动时直接失败,避免 tools/list 出现歧义")
|
|
|
+ void rejectsDuplicateToolNames() {
|
|
|
+ McpTool duplicate = new EchoTool() {
|
|
|
+ @Override
|
|
|
+ public String name() {
|
|
|
+ return "echo";
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ IllegalStateException e = assertThrows(IllegalStateException.class,
|
|
|
+ () -> new McpToolRegistry(List.of(new EchoTool(), duplicate), jsonMapper));
|
|
|
+ assertTrue(e.getMessage().contains("echo"), e.getMessage());
|
|
|
+ }
|
|
|
+}
|