Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springaicommunity.mcp.annotation.McpMeta;
Expand All @@ -41,6 +43,7 @@
* @param <T> The type of the context parameter (e.g., McpTransportContext or
* McpSyncServerExchange)
* @author Christian Tzolov
* @author Ilayaperumal Gopinathan
*/
public abstract class AbstractSyncMcpToolMethodCallback<T> {

Expand Down Expand Up @@ -156,9 +159,15 @@ protected CallToolResult processResult(Object result) {
return CallToolResult.builder().addTextContent(JsonParser.toJson("Done")).build();
}
else if (this.returnMode == ReturnMode.STRUCTURED) {
String jsonOutput = JsonParser.toJson(result);
Map<String, Object> structuredOutput = JsonParser.fromJson(jsonOutput, MAP_TYPE_REFERENCE);
return CallToolResult.builder().structuredContent(structuredOutput).build();
if (result instanceof List<?>) {
List<String> texts = ((List<?>) result).stream().map(String::valueOf).collect(Collectors.toList());
return CallToolResult.builder().textContent(texts).build();
}
else {
String jsonOutput = JsonParser.toJson(result);
Map<String, Object> structuredOutput = JsonParser.fromJson(jsonOutput, MAP_TYPE_REFERENCE);
return CallToolResult.builder().structuredContent(structuredOutput).build();
}
}

// Default to text output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public TestObject returnObjectTool(String name, int value) {
return new TestObject(name, value);
}

@McpTool(name = "return-list-tool", description = "Tool that returns a list")
public List<String> returnListTool() {
return List.of("this", "is", "a", "test");
}

}

public static class TestObject {
Expand Down Expand Up @@ -507,4 +512,23 @@ public void testToolReturningComplexObject() throws Exception {
assertThat(result.structuredContent()).containsEntry("value", 42);
}

@Test
public void testToolReturningList() throws Exception {
TestToolProvider provider = new TestToolProvider();
Method method = TestToolProvider.class.getMethod("returnListTool", null);
SyncMcpToolMethodCallback callback = new SyncMcpToolMethodCallback(ReturnMode.STRUCTURED, method, provider);

McpSyncServerExchange exchange = mock(McpSyncServerExchange.class);
CallToolRequest request = new CallToolRequest("return-list-tool", Map.of());

CallToolResult result = callback.apply(exchange, request);

assertThat(result).isNotNull();
assertThat(result.isError()).isFalse();
assertThat(result.content()).isNotEmpty();
result.content().forEach(textContent -> {
assertThat(((TextContent) textContent).text()).containsAnyOf("this", "is", "a", "test");
});
}

}