From 4fa1cdc9548f797dcae0f3a0636de177f2a3a0e2 Mon Sep 17 00:00:00 2001 From: jade Date: Tue, 14 Jul 2026 21:17:42 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20ToolExecutionResult=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=20=ED=83=80=EC=9E=85=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20McpSchema=20=EB=A7=A4=ED=95=91=20=EB=B2=84=EA=B7=B8?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RegistryMcpToolSpecificationFactory.java | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/axhub-gateway/src/main/java/io/shinhanlife/axhub/biz/mcp/gateway/sync/RegistryMcpToolSpecificationFactory.java b/axhub-gateway/src/main/java/io/shinhanlife/axhub/biz/mcp/gateway/sync/RegistryMcpToolSpecificationFactory.java index 16377a9..c424fa2 100644 --- a/axhub-gateway/src/main/java/io/shinhanlife/axhub/biz/mcp/gateway/sync/RegistryMcpToolSpecificationFactory.java +++ b/axhub-gateway/src/main/java/io/shinhanlife/axhub/biz/mcp/gateway/sync/RegistryMcpToolSpecificationFactory.java @@ -4,6 +4,7 @@ import io.shinhanlife.axhub.biz.mcp.gateway.dto.ToolMetadata; import io.shinhanlife.axhub.biz.mcp.gateway.service.ExecuteService; import io.modelcontextprotocol.server.McpServerFeatures; import io.modelcontextprotocol.spec.McpSchema; +import io.shinhanlife.axhub.biz.mcp.gateway.tool.result.ToolExecutionResult; import org.springframework.stereotype.Component; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -71,30 +72,40 @@ public class RegistryMcpToolSpecificationFactory { } private McpSchema.CallToolResult toCallToolResult(Object rawResult) { + if (rawResult instanceof ToolExecutionResult result) { + McpSchema.CallToolResult.Builder builder = McpSchema.CallToolResult.builder() + .isError(result.isError()) + .meta(result.metadata()); + + if (result.content() == null || result.content().isEmpty()) { + builder.addTextContent(""); + } else { + for (ToolExecutionResult.ContentItem item : result.content()) { + if ("text".equals(item.type())) { + builder.addTextContent(item.text()); + } + } + } + + if (result.structuredContent() != null) { + builder.structuredContent(result.structuredContent()); + } + return builder.build(); + } + + // Fallback for old map format or unexpected types try { Map resultMap = objectMapper.convertValue(rawResult, new TypeReference>() {}); - Object innerResult = resultMap.get("result"); + if (resultMap.containsKey("resultType") || resultMap.containsKey("structuredContent")) { + ToolExecutionResult result = objectMapper.convertValue(rawResult, ToolExecutionResult.class); + return toCallToolResult(result); + } + + Object innerResult = resultMap.containsKey("result") ? resultMap.get("result") : resultMap; McpSchema.CallToolResult.Builder builder = McpSchema.CallToolResult.builder(); builder.isError(resultMap.containsKey("error")); - - if (innerResult instanceof Map) { - Map innerMap = (Map) innerResult; - // If it follows structured content - if (innerMap.containsKey("content")) { - List> contentList = (List>) innerMap.get("content"); - for (Map item : contentList) { - if ("text".equals(item.get("type"))) { - builder.addTextContent((String) item.get("text")); - } - } - } else { - // Fallback to text string representation - builder.addTextContent(objectMapper.writeValueAsString(innerResult)); - } - } else { - builder.addTextContent(String.valueOf(innerResult)); - } + builder.addTextContent(objectMapper.writeValueAsString(innerResult)); return builder.build(); } catch (Exception e) { return McpSchema.CallToolResult.builder()