Skip to content
Merged
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 @@ -251,7 +251,7 @@ protected CallToolResult mapValueToCallToolResult(Object value) {

if (this.returnMode == ReturnMode.STRUCTURED) {
String jsonOutput = JsonParser.toJson(value);
Map<String, Object> structuredOutput = JsonParser.fromJson(jsonOutput, MAP_TYPE_REFERENCE);
Object structuredOutput = JsonParser.fromJson(jsonOutput, MAP_TYPE_REFERENCE);
return CallToolResult.builder().structuredContent(structuredOutput).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected CallToolResult processResult(Object result) {

if (this.returnMode == ReturnMode.STRUCTURED) {
String jsonOutput = JsonParser.toJson(result);
Map<String, Object> structuredOutput = JsonParser.fromJson(jsonOutput, MAP_TYPE_REFERENCE);
Object structuredOutput = JsonParser.fromJson(jsonOutput, Object.class);
return CallToolResult.builder().structuredContent(structuredOutput).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public void testConstructorParameters() {
}

@Test
public void testToolWithStructuredOutput() throws Exception {
public void testToolWithTextOutput() throws Exception {
TestToolProvider provider = new TestToolProvider();
Method method = TestToolProvider.class.getMethod("processObject", TestObject.class);
SyncMcpToolMethodCallback callback = new SyncMcpToolMethodCallback(ReturnMode.TEXT, method, provider);
Expand Down Expand Up @@ -546,6 +546,28 @@ public void testToolReturningComplexListObject() throws Exception {
[{"name":"test","value":42}]"""));
}

@Test
public void testToolReturningStructuredComplexListObject() throws Exception {
TestToolProvider provider = new TestToolProvider();
Method method = TestToolProvider.class.getMethod("returnListObjectTool", String.class, int.class);
SyncMcpToolMethodCallback callback = new SyncMcpToolMethodCallback(ReturnMode.STRUCTURED, method, provider);

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

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

assertThat(result).isNotNull();
assertThat(result.isError()).isFalse();

assertThat(result.structuredContent()).isNotNull();
assertThat(result.structuredContent()).isInstanceOf(List.class);
assertThat((List<?>) result.structuredContent()).hasSize(1);
Map<String, Object> firstEntry = ((List<Map<String, Object>>) result.structuredContent()).get(0);
assertThat(firstEntry).containsEntry("name", "test");
assertThat(firstEntry).containsEntry("value", 42);
}

@Test
public void testToolReturningStringList() throws Exception {
TestToolProvider provider = new TestToolProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;

import io.modelcontextprotocol.common.McpTransportContext;
import io.modelcontextprotocol.server.McpSyncServerExchange;
import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
import io.modelcontextprotocol.spec.McpSchema.TextContent;
Expand Down Expand Up @@ -128,6 +129,11 @@ public String toolWithContextAndRequest(McpTransportContext context, CallToolReq
return "Context present, Tool: " + request.name();
}

@McpTool(name = "return-list-object-tool", description = "Tool that returns a list of complex objects")
public List<TestObject> returnListObjectTool(String name, int value) {
return List.of(new TestObject(name, value));
}

/**
* Tool with McpMeta parameter
*/
Expand Down Expand Up @@ -543,6 +549,28 @@ public void testToolReturningComplexObject() throws Exception {
assertThat((Map<String, Object>) result.structuredContent()).containsEntry("value", 42);
}

@Test
public void testToolReturningStructuredComplexListObject() throws Exception {
TestToolProvider provider = new TestToolProvider();
Method method = TestToolProvider.class.getMethod("returnListObjectTool", String.class, int.class);
SyncMcpToolMethodCallback callback = new SyncMcpToolMethodCallback(ReturnMode.STRUCTURED, method, provider);

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

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

assertThat(result).isNotNull();
assertThat(result.isError()).isFalse();

assertThat(result.structuredContent()).isNotNull();
assertThat(result.structuredContent()).isInstanceOf(List.class);
assertThat((List<?>) result.structuredContent()).hasSize(1);
Map<String, Object> firstEntry = ((List<Map<String, Object>>) result.structuredContent()).get(0);
assertThat(firstEntry).containsEntry("name", "test");
assertThat(firstEntry).containsEntry("value", 42);
}

@Test
public void testVoidReturnMode() throws Exception {
TestToolProvider provider = new TestToolProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,47 @@ public List<CustomResult> listResponseTool(String input) {
[{"message":"Processed: test"}]"""));
}

@Test
void testToolWithStructuredListReturnType() {

record CustomResult(String message) {
}

class ListResponseTool {

@McpTool(name = "list-response", description = "Tool List response", generateOutputSchema = true)
public List<CustomResult> listResponseTool(String input) {
return List.of(new CustomResult("Processed: " + input));
}

}

ListResponseTool toolObject = new ListResponseTool();
SyncMcpToolProvider provider = new SyncMcpToolProvider(List.of(toolObject));

List<SyncToolSpecification> toolSpecs = provider.getToolSpecifications();

assertThat(toolSpecs).hasSize(1);
SyncToolSpecification toolSpec = toolSpecs.get(0);

assertThat(toolSpec.tool().name()).isEqualTo("list-response");
assertThat(toolSpec.tool().outputSchema()).isNotNull();

BiFunction<McpSyncServerExchange, CallToolRequest, McpSchema.CallToolResult> callHandler = toolSpec
.callHandler();

McpSchema.CallToolResult result = callHandler.apply(mock(McpSyncServerExchange.class),
new CallToolRequest("list-response", Map.of("input", "test")));

assertThat(result).isNotNull();
assertThat(result.isError()).isFalse();

assertThat(result.structuredContent()).isInstanceOf(List.class);
assertThat((List<?>) result.structuredContent()).hasSize(1);
Map<String, Object> firstEntry = ((List<Map<String, Object>>) result.structuredContent()).get(0);
assertThat(firstEntry).containsEntry("message", "Processed: test");
}

@Test
void testToolWithPrimitiveReturnTypeNoOutputSchema() {
class PrimitiveTool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,46 @@ public List<CustomResult> listResponseTool(String input) {
[{"message":"Processed: test"}]"""));
}

@Test
void testToolWithStructuredListReturnType() {

record CustomResult(String message) {
}

class ListResponseTool {

@McpTool(name = "list-response", description = "Tool List response", generateOutputSchema = true)
public List<CustomResult> listResponseTool(String input) {
return List.of(new CustomResult("Processed: " + input));
}

}

ListResponseTool toolObject = new ListResponseTool();
SyncStatelessMcpToolProvider provider = new SyncStatelessMcpToolProvider(List.of(toolObject));

List<SyncToolSpecification> toolSpecs = provider.getToolSpecifications();

assertThat(toolSpecs).hasSize(1);
SyncToolSpecification toolSpec = toolSpecs.get(0);

assertThat(toolSpec.tool().name()).isEqualTo("list-response");
assertThat(toolSpec.tool().outputSchema()).isNotNull();

BiFunction<McpTransportContext, CallToolRequest, McpSchema.CallToolResult> callHandler = toolSpec.callHandler();

McpSchema.CallToolResult result = callHandler.apply(mock(McpTransportContext.class),
new CallToolRequest("list-response", Map.of("input", "test")));

assertThat(result).isNotNull();
assertThat(result.isError()).isFalse();

assertThat(result.structuredContent()).isInstanceOf(List.class);
assertThat((List<?>) result.structuredContent()).hasSize(1);
Map<String, Object> firstEntry = ((List<Map<String, Object>>) result.structuredContent()).get(0);
assertThat(firstEntry).containsEntry("message", "Processed: test");
}

@Test
void testToolWithPrimitiveReturnTypeNoOutputSchema() {
class PrimitiveTool {
Expand Down