|
25 | 25 | import io.modelcontextprotocol.client.McpSyncClient; |
26 | 26 | import io.modelcontextprotocol.spec.McpSchema; |
27 | 27 | import io.modelcontextprotocol.spec.McpSchema.CallToolResult; |
| 28 | +import io.modelcontextprotocol.spec.McpSchema.ImageContent; |
28 | 29 | import io.modelcontextprotocol.spec.McpSchema.TextContent; |
29 | 30 | import java.util.List; |
30 | 31 | import java.util.Map; |
@@ -53,14 +54,61 @@ public void testWrapCallResult_success() { |
53 | 54 |
|
54 | 55 | Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
55 | 56 |
|
| 57 | + assertThat(map).containsEntry("isError", false); |
56 | 58 | assertThat(map).containsKey("text_output"); |
57 | | - List<?> content = (List<?>) map.get("text_output"); |
58 | | - assertThat(content).hasSize(1); |
| 59 | + List<?> textOutput = (List<?>) map.get("text_output"); |
| 60 | + assertThat(textOutput).hasSize(1); |
59 | 61 |
|
60 | | - Map<?, ?> contentItem = (Map<?, ?>) content.get(0); |
| 62 | + Map<?, ?> contentItem = (Map<?, ?>) textOutput.get(0); |
61 | 63 | assertThat(contentItem).containsEntry("text", "success"); |
62 | 64 | } |
63 | 65 |
|
| 66 | + @Test |
| 67 | + public void testWrapCallResult_mixedContent_success() { |
| 68 | + CallToolResult result = |
| 69 | + new CallToolResult( |
| 70 | + ImmutableList.of( |
| 71 | + new TextContent("first"), new ImageContent(null, "aW1hZ2U=", "image/png", null)), |
| 72 | + false, |
| 73 | + Map.of("count", 2), |
| 74 | + Map.of("traceId", "trace-123")); |
| 75 | + |
| 76 | + Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
| 77 | + |
| 78 | + assertThat(map).containsEntry("isError", false); |
| 79 | + assertThat(map).containsEntry("structuredContent", Map.of("count", 2)); |
| 80 | + assertThat(map).containsEntry("_meta", Map.of("traceId", "trace-123")); |
| 81 | + |
| 82 | + List<?> content = (List<?>) map.get("content"); |
| 83 | + assertThat(content).hasSize(2); |
| 84 | + Map<?, ?> textContent = (Map<?, ?>) content.get(0); |
| 85 | + assertThat(textContent).containsEntry("type", "text"); |
| 86 | + assertThat(textContent).containsEntry("text", "first"); |
| 87 | + Map<?, ?> imageContent = (Map<?, ?>) content.get(1); |
| 88 | + assertThat(imageContent).containsEntry("type", "image"); |
| 89 | + assertThat(imageContent).containsEntry("data", "aW1hZ2U="); |
| 90 | + assertThat(imageContent).containsEntry("mimeType", "image/png"); |
| 91 | + |
| 92 | + List<?> textOutput = (List<?>) map.get("text_output"); |
| 93 | + assertThat(textOutput).containsExactly(Map.of("text", "first")); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testWrapCallResult_nonTextContent_success() { |
| 98 | + CallToolResult result = |
| 99 | + new CallToolResult( |
| 100 | + ImmutableList.of(new ImageContent(null, "aW1hZ2U=", "image/png", null)), |
| 101 | + false, |
| 102 | + null, |
| 103 | + null); |
| 104 | + |
| 105 | + Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
| 106 | + |
| 107 | + assertThat(map).doesNotContainKey("error"); |
| 108 | + assertThat(map).containsEntry("isError", false); |
| 109 | + assertThat((List<?>) map.get("content")).hasSize(1); |
| 110 | + } |
| 111 | + |
64 | 112 | @Test |
65 | 113 | public void instantiateWithToolBuilder_nullDescription_succeeds() { |
66 | 114 | McpSyncClient sessionMock = mock(McpSyncClient.class); |
|
0 commit comments