|
5 | 5 | */ |
6 | 6 | package io.jooby.mcp; |
7 | 7 |
|
| 8 | +import static io.modelcontextprotocol.spec.McpSchema.ErrorCodes.INTERNAL_ERROR; |
| 9 | +import static io.modelcontextprotocol.spec.McpSchema.Role.USER; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Objects; |
| 14 | + |
| 15 | +import io.jooby.SneakyThrows; |
8 | 16 | import io.modelcontextprotocol.json.McpJsonMapper; |
| 17 | +import io.modelcontextprotocol.spec.McpError; |
9 | 18 | import io.modelcontextprotocol.spec.McpSchema; |
10 | 19 |
|
11 | 20 | public class McpResult { |
12 | 21 |
|
13 | | - public McpResult(McpJsonMapper json) {} |
| 22 | + private final McpJsonMapper json; |
| 23 | + |
| 24 | + public McpResult(McpJsonMapper json) { |
| 25 | + this.json = json; |
| 26 | + } |
14 | 27 |
|
15 | | - public McpSchema.CallToolResult toCallToolResult(Object result) { |
16 | | - return null; |
| 28 | + public McpSchema.CallToolResult toCallToolResult(Object result, boolean structuredContent) { |
| 29 | + try { |
| 30 | + if (result == null) { |
| 31 | + return buildTextResult("null", false); |
| 32 | + } else if (result instanceof McpSchema.CallToolResult callToolResult) { |
| 33 | + return callToolResult; |
| 34 | + } else if (result instanceof String str) { |
| 35 | + return buildTextResult(str, false); |
| 36 | + } else if (result instanceof McpSchema.Content content) { |
| 37 | + return McpSchema.CallToolResult.builder().content(List.of(content)).isError(false).build(); |
| 38 | + } else { |
| 39 | + if (structuredContent) { |
| 40 | + return McpSchema.CallToolResult.builder() |
| 41 | + .structuredContent(result) |
| 42 | + .isError(false) |
| 43 | + .build(); |
| 44 | + } else { |
| 45 | + return buildTextResult(json.writeValueAsString(result), false); |
| 46 | + } |
| 47 | + } |
| 48 | + } catch (Exception x) { |
| 49 | + throw SneakyThrows.propagate(x); |
| 50 | + } |
17 | 51 | } |
18 | 52 |
|
19 | 53 | public McpSchema.GetPromptResult toPromptResult(Object result) { |
20 | | - return null; |
| 54 | + if (result == null) { |
| 55 | + return new McpSchema.GetPromptResult(null, List.of()); |
| 56 | + } else if (result instanceof McpSchema.GetPromptResult promptResult) { |
| 57 | + return promptResult; |
| 58 | + } else if (result instanceof McpSchema.PromptMessage promptMessage) { |
| 59 | + return new McpSchema.GetPromptResult(null, List.of(promptMessage)); |
| 60 | + } else if (result instanceof McpSchema.Content content) { |
| 61 | + var promptMessage = new McpSchema.PromptMessage(USER, content); |
| 62 | + return new McpSchema.GetPromptResult(null, List.of(promptMessage)); |
| 63 | + } else if (result instanceof String str) { |
| 64 | + var promptMessage = new McpSchema.PromptMessage(USER, new McpSchema.TextContent(str)); |
| 65 | + return new McpSchema.GetPromptResult(null, List.of(promptMessage)); |
| 66 | + } else if (result instanceof List<?> items) { |
| 67 | + //noinspection unchecked |
| 68 | + return handleListReturnType((List<McpSchema.PromptMessage>) result, items); |
| 69 | + } else { |
| 70 | + var promptMessage = |
| 71 | + new McpSchema.PromptMessage(USER, new McpSchema.TextContent(result.toString())); |
| 72 | + return new McpSchema.GetPromptResult(null, List.of(promptMessage)); |
| 73 | + } |
21 | 74 | } |
22 | 75 |
|
23 | | - public McpSchema.ReadResourceResult toResourceResult(Object result) { |
24 | | - return null; |
| 76 | + public McpSchema.ReadResourceResult toResourceResult(String uri, Object result) { |
| 77 | + try { |
| 78 | + if (result == null) { |
| 79 | + return new McpSchema.ReadResourceResult(List.of()); |
| 80 | + } else if (result instanceof McpSchema.ReadResourceResult resourceResult) { |
| 81 | + return resourceResult; |
| 82 | + } else if (result instanceof McpSchema.ResourceContents resourceContents) { |
| 83 | + return new McpSchema.ReadResourceResult(List.of(resourceContents)); |
| 84 | + } else if (result instanceof List<?> contents) { |
| 85 | + return handleListReturnType(result, uri, json, contents); |
| 86 | + } else { |
| 87 | + return toJsonResult(result, uri, json); |
| 88 | + } |
| 89 | + } catch (Exception x) { |
| 90 | + throw SneakyThrows.propagate(x); |
| 91 | + } |
25 | 92 | } |
26 | 93 |
|
27 | 94 | public McpSchema.CompleteResult toCompleteResult(Object result) { |
28 | | - return null; |
| 95 | + try { |
| 96 | + Objects.requireNonNull(result, "Completion result cannot be null"); |
| 97 | + |
| 98 | + if (result instanceof McpSchema.CompleteResult completeResult) { |
| 99 | + return completeResult; |
| 100 | + } else if (result instanceof McpSchema.CompleteResult.CompleteCompletion completion) { |
| 101 | + return new McpSchema.CompleteResult(completion); |
| 102 | + } else if (result instanceof List<?> values) { |
| 103 | + if (values.isEmpty()) { |
| 104 | + return new McpSchema.CompleteResult( |
| 105 | + new McpSchema.CompleteResult.CompleteCompletion(List.of(), 0, false)); |
| 106 | + } else { |
| 107 | + var item = values.getFirst(); |
| 108 | + if (item instanceof String) { |
| 109 | + //noinspection unchecked |
| 110 | + return new McpSchema.CompleteResult( |
| 111 | + new McpSchema.CompleteResult.CompleteCompletion( |
| 112 | + (List<String>) values, values.size(), false)); |
| 113 | + } |
| 114 | + } |
| 115 | + } else if (result instanceof String singleValue) { |
| 116 | + var completion = |
| 117 | + new McpSchema.CompleteResult.CompleteCompletion(List.of(singleValue), 1, false); |
| 118 | + return new McpSchema.CompleteResult(completion); |
| 119 | + } |
| 120 | + |
| 121 | + throw new IllegalStateException("Unexpected error occurred while handling completion result"); |
| 122 | + } catch (Exception ex) { |
| 123 | + throw new McpError( |
| 124 | + new McpSchema.JSONRPCResponse.JSONRPCError(INTERNAL_ERROR, ex.getMessage(), null)); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static McpSchema.ReadResourceResult handleListReturnType( |
| 129 | + Object result, String uri, McpJsonMapper mcpJsonMapper, List<?> contents) throws IOException { |
| 130 | + if (contents.isEmpty()) { |
| 131 | + return new McpSchema.ReadResourceResult(List.of()); |
| 132 | + } else { |
| 133 | + var item = contents.getFirst(); |
| 134 | + if (item instanceof McpSchema.ResourceContents) { |
| 135 | + //noinspection unchecked |
| 136 | + return new McpSchema.ReadResourceResult((List<McpSchema.ResourceContents>) contents); |
| 137 | + } else { |
| 138 | + return toJsonResult(result, uri, mcpJsonMapper); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + static McpSchema.ReadResourceResult toJsonResult( |
| 144 | + Object result, String uri, McpJsonMapper mcpJsonMapper) throws IOException { |
| 145 | + var resultStr = mcpJsonMapper.writeValueAsString(result); |
| 146 | + var content = new McpSchema.TextResourceContents(uri, "application/json", resultStr); |
| 147 | + return new McpSchema.ReadResourceResult(List.of(content)); |
| 148 | + } |
| 149 | + |
| 150 | + private McpSchema.CallToolResult buildTextResult(String text, boolean isError) { |
| 151 | + return McpSchema.CallToolResult.builder().addTextContent(text).isError(isError).build(); |
| 152 | + } |
| 153 | + |
| 154 | + private static McpSchema.GetPromptResult handleListReturnType( |
| 155 | + List<McpSchema.PromptMessage> result, List<?> items) { |
| 156 | + if (items.isEmpty()) { |
| 157 | + return new McpSchema.GetPromptResult(null, List.of()); |
| 158 | + } else { |
| 159 | + var item = items.getFirst(); |
| 160 | + if (item instanceof McpSchema.PromptMessage) { |
| 161 | + return new McpSchema.GetPromptResult(null, result); |
| 162 | + } else { |
| 163 | + var msgs = |
| 164 | + items.stream() |
| 165 | + .map( |
| 166 | + i -> new McpSchema.PromptMessage(USER, new McpSchema.TextContent(i.toString()))) |
| 167 | + .toList(); |
| 168 | + return new McpSchema.GetPromptResult(null, msgs); |
| 169 | + } |
| 170 | + } |
29 | 171 | } |
30 | 172 | } |
0 commit comments