|
21 | 21 | import static org.mockito.Mockito.mock; |
22 | 22 |
|
23 | 23 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.google.adk.JsonBaseModel; |
24 | 25 | import com.google.common.collect.ImmutableList; |
| 26 | +import com.google.common.collect.ImmutableMap; |
25 | 27 | import io.modelcontextprotocol.client.McpSyncClient; |
26 | 28 | import io.modelcontextprotocol.spec.McpSchema; |
27 | 29 | import io.modelcontextprotocol.spec.McpSchema.CallToolResult; |
28 | | -import io.modelcontextprotocol.spec.McpSchema.TextContent; |
29 | | -import java.util.List; |
| 30 | +import io.modelcontextprotocol.spec.McpSchema.ImageContent; |
30 | 31 | import java.util.Map; |
31 | 32 | import org.junit.Before; |
32 | 33 | import org.junit.Test; |
|
36 | 37 | @RunWith(JUnit4.class) |
37 | 38 | public final class AbstractMcpToolTest { |
38 | 39 |
|
| 40 | + private static final ImageContent IMAGE = ImageContent.builder("aW1hZ2U=", "image/png").build(); |
| 41 | + private static final ImmutableMap<String, Object> IMAGE_JSON = |
| 42 | + ImmutableMap.of("type", "image", "data", "aW1hZ2U=", "mimeType", "image/png"); |
| 43 | + |
39 | 44 | private ObjectMapper objectMapper; |
40 | 45 |
|
41 | 46 | @Before |
42 | 47 | public void setUp() { |
43 | | - objectMapper = new ObjectMapper(); |
| 48 | + // The mapper McpTool uses by default, so tests see the production serialization. |
| 49 | + objectMapper = JsonBaseModel.getMapper(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void wrapCallResult_textOnly_returnsOnlyTextOutput() { |
| 54 | + CallToolResult result = |
| 55 | + CallToolResult.builder().addTextContent("first").addTextContent("{\"a\":1}").build(); |
| 56 | + |
| 57 | + Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
| 58 | + |
| 59 | + assertThat(map) |
| 60 | + .containsExactly( |
| 61 | + "text_output", |
| 62 | + ImmutableList.of(ImmutableMap.of("text", "first"), ImmutableMap.of("a", 1))); |
44 | 63 | } |
45 | 64 |
|
46 | 65 | @Test |
47 | | - public void testWrapCallResult_success() { |
| 66 | + public void wrapCallResult_mixedContent_keepsTextOutputAndAddsContentAndStructuredContent() { |
48 | 67 | CallToolResult result = |
49 | 68 | CallToolResult.builder() |
50 | | - .content(ImmutableList.of(new TextContent("success"))) |
| 69 | + .addTextContent("first") |
| 70 | + .addTextContent("second") |
| 71 | + .addContent(IMAGE) |
| 72 | + .structuredContent(ImmutableMap.of("count", 2)) |
51 | 73 | .isError(false) |
52 | 74 | .build(); |
53 | 75 |
|
54 | 76 | Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
55 | 77 |
|
56 | | - assertThat(map).containsKey("text_output"); |
57 | | - List<?> content = (List<?>) map.get("text_output"); |
58 | | - assertThat(content).hasSize(1); |
| 78 | + assertThat(map) |
| 79 | + .containsExactly( |
| 80 | + "text_output", |
| 81 | + ImmutableList.of(ImmutableMap.of("text", "first"), ImmutableMap.of("text", "second")), |
| 82 | + "content", |
| 83 | + ImmutableList.of( |
| 84 | + ImmutableMap.of("type", "text", "text", "first"), |
| 85 | + ImmutableMap.of("type", "text", "text", "second"), |
| 86 | + IMAGE_JSON), |
| 87 | + "structuredContent", |
| 88 | + ImmutableMap.of("count", 2)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void wrapCallResult_textWithStructuredContent_keepsTextOutputAndAddsStructuredContent() { |
| 93 | + CallToolResult result = |
| 94 | + CallToolResult.builder() |
| 95 | + .addTextContent("{\"count\":2}") |
| 96 | + .structuredContent(ImmutableMap.of("count", 2)) |
| 97 | + .build(); |
| 98 | + |
| 99 | + Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
| 100 | + |
| 101 | + assertThat(map) |
| 102 | + .containsExactly( |
| 103 | + "text_output", |
| 104 | + ImmutableList.of(ImmutableMap.of("count", 2)), |
| 105 | + "structuredContent", |
| 106 | + ImmutableMap.of("count", 2)); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void wrapCallResult_nonTextOnly_returnsContentWithoutError() { |
| 111 | + CallToolResult result = CallToolResult.builder().addContent(IMAGE).build(); |
| 112 | + |
| 113 | + Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
| 114 | + |
| 115 | + assertThat(map).containsExactly("content", ImmutableList.of(IMAGE_JSON)); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void wrapCallResult_emptyContent_returnsEmptyMap() { |
| 120 | + CallToolResult result = CallToolResult.builder().build(); |
| 121 | + |
| 122 | + Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
| 123 | + |
| 124 | + assertThat(map).isEmpty(); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void wrapCallResult_emptyContentWithStructuredContentAndMeta_returnsBoth() { |
| 129 | + CallToolResult result = |
| 130 | + CallToolResult.builder() |
| 131 | + .structuredContent(ImmutableMap.of("count", 2)) |
| 132 | + .meta(ImmutableMap.of("trace", "abc")) |
| 133 | + .build(); |
| 134 | + |
| 135 | + Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
| 136 | + |
| 137 | + assertThat(map) |
| 138 | + .containsExactly( |
| 139 | + "structuredContent", |
| 140 | + ImmutableMap.of("count", 2), |
| 141 | + "_meta", |
| 142 | + ImmutableMap.of("trace", "abc")); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void wrapCallResult_error_returnsOnlyError() { |
| 147 | + CallToolResult result = |
| 148 | + CallToolResult.builder() |
| 149 | + .addTextContent("boom") |
| 150 | + .structuredContent(ImmutableMap.of("count", 2)) |
| 151 | + .isError(true) |
| 152 | + .build(); |
| 153 | + |
| 154 | + Map<String, Object> map = AbstractMcpTool.wrapCallResult(objectMapper, "my_tool", result); |
59 | 155 |
|
60 | | - Map<?, ?> contentItem = (Map<?, ?>) content.get(0); |
61 | | - assertThat(contentItem).containsEntry("text", "success"); |
| 156 | + assertThat(map).containsExactly("error", "Tool execution failed. Details: boom"); |
62 | 157 | } |
63 | 158 |
|
64 | 159 | @Test |
|
0 commit comments