|
| 1 | +// This file is generated by McpToolProcessor. Do not modify manually. |
| 2 | +package io.github.kliushnichenko.jooby.mcp.example; |
| 3 | + |
| 4 | +import io.github.kliushnichenko.jooby.mcp.JoobyMcpServer; |
| 5 | +import io.github.kliushnichenko.jooby.mcp.ResourceUri; |
| 6 | +import io.github.kliushnichenko.jooby.mcp.internal.MethodInvoker; |
| 7 | +import io.github.kliushnichenko.jooby.mcp.internal.ToolSpec; |
| 8 | +import io.jooby.Jooby; |
| 9 | +import io.modelcontextprotocol.json.McpJsonMapper; |
| 10 | +import io.modelcontextprotocol.server.McpSyncServerExchange; |
| 11 | +import io.modelcontextprotocol.spec.McpSchema; |
| 12 | +import java.lang.Object; |
| 13 | +import java.lang.String; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.function.Function; |
| 19 | +import java.util.function.Supplier; |
| 20 | + |
| 21 | +/** |
| 22 | + * Generated Jooby MCP Server. Do not modify manually. |
| 23 | + */ |
| 24 | +public class ExampleMcpServer implements JoobyMcpServer { |
| 25 | + private Jooby app; |
| 26 | + |
| 27 | + private McpJsonMapper mcpJsonMapper; |
| 28 | + |
| 29 | + /** |
| 30 | + * Map of tool names to its specification. |
| 31 | + */ |
| 32 | + private final Map<String, ToolSpec> tools = new HashMap<>(); |
| 33 | + |
| 34 | + /** |
| 35 | + * Map of tool names to method invokers. |
| 36 | + */ |
| 37 | + private final Map<String, MethodInvoker> toolInvokers = new HashMap<>(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Map of prompt names to its specification. |
| 41 | + */ |
| 42 | + private final Map<String, McpSchema.Prompt> prompts = new HashMap<>(); |
| 43 | + |
| 44 | + /** |
| 45 | + * Map of prompt names to method invokers. |
| 46 | + */ |
| 47 | + private final Map<String, MethodInvoker> promptInvokers = new HashMap<>(); |
| 48 | + |
| 49 | + /** |
| 50 | + * List of completions reference objects. |
| 51 | + */ |
| 52 | + private final List<McpSchema.CompleteReference> completions = new ArrayList<>(); |
| 53 | + |
| 54 | + /** |
| 55 | + * Map of completion key(a composition of <identifier>_<argumentName>) to method invoker. |
| 56 | + */ |
| 57 | + private final Map<String, Function<String, Object>> completionInvokers = new HashMap<>(); |
| 58 | + |
| 59 | + /** |
| 60 | + * List of resources. |
| 61 | + */ |
| 62 | + private final List<McpSchema.Resource> resources = new ArrayList<>(); |
| 63 | + |
| 64 | + /** |
| 65 | + * Map of resource URI to method invoker. |
| 66 | + */ |
| 67 | + private final Map<String, Supplier<Object>> resourceReaders = new HashMap<>(); |
| 68 | + |
| 69 | + /** |
| 70 | + * List of resource templates. |
| 71 | + */ |
| 72 | + private final List<McpSchema.ResourceTemplate> resourceTemplates = new ArrayList<>(); |
| 73 | + |
| 74 | + /** |
| 75 | + * Map of resource URI template to method invoker. |
| 76 | + */ |
| 77 | + private final Map<String, Function<Map<String, Object>, Object>> resourceTemplateReaders = new HashMap<>(); |
| 78 | + |
| 79 | + /** |
| 80 | + * Initialize a new server. |
| 81 | + * @param app the Jooby application instance |
| 82 | + * @param mcpJsonMapper json serializer instance |
| 83 | + */ |
| 84 | + public void init(final Jooby app, final McpJsonMapper mcpJsonMapper) { |
| 85 | + this.app = app; |
| 86 | + this.mcpJsonMapper = mcpJsonMapper; |
| 87 | + |
| 88 | + tools.put("elicitation_example", ToolSpec.builder().name("elicitation_example").description("Request the username over elicitation").inputSchema("{\"type\":\"object\",\"properties\":{},\"additionalProperties\":false}").requiredArguments(List.of()).build()); |
| 89 | + tools.put("add", ToolSpec.builder().name("add").description("Adds two numbers together").inputSchema("{\"type\":\"object\",\"properties\":{\"first\":{\"type\":\"integer\",\"description\":\"First number to add\"},\"second\":{\"type\":\"integer\",\"description\":\"Second number to add\"}},\"required\":[\"first\",\"second\"],\"additionalProperties\":false}").outputSchema("{\"type\":\"object\",\"properties\":{\"operation\":{\"type\":\"string\"},\"result\":{\"type\":\"number\"},\"expression\":{\"type\":\"string\"}},\"required\":[\"operation\",\"result\",\"expression\"],\"additionalProperties\":false}").requiredArguments(List.of("first", "second")).build()); |
| 90 | + tools.put("subtract", ToolSpec.builder().name("subtract").inputSchema("{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"integer\"},\"b\":{\"type\":\"integer\"}},\"required\":[\"a\",\"b\"],\"additionalProperties\":false}").requiredArguments(List.of("a", "b")).build()); |
| 91 | + tools.put("pi_sign_image", ToolSpec.builder().name("pi_sign_image").description("Returns an image of the Pi").inputSchema("{\"type\":\"object\",\"properties\":{},\"additionalProperties\":false}").requiredArguments(List.of()).build()); |
| 92 | + tools.put("get_client_info", ToolSpec.builder().name("get_client_info").description("Returns the information about the client initiated the request").inputSchema("{\"type\":\"object\",\"properties\":{},\"additionalProperties\":false}").requiredArguments(List.of()).build()); |
| 93 | + |
| 94 | + toolInvokers.put("elicitation_example", (args, exchange) -> app.require(ElicitationExample.class).requestUsername(exchange)); |
| 95 | + toolInvokers.put("add", (args, exchange) -> app.require(ToolsExample.class).add((int) args.get("first"), (int) args.get("second"))); |
| 96 | + toolInvokers.put("subtract", (args, exchange) -> app.require(ToolsExample.class).subtract((int) args.get("a"), (int) args.get("b"), exchange)); |
| 97 | + toolInvokers.put("pi_sign_image", (args, exchange) -> app.require(ToolsExample.class).getPiSignImage()); |
| 98 | + toolInvokers.put("get_client_info", (args, exchange) -> app.require(ToolsExample.class).getClientInfo(exchange)); |
| 99 | + |
| 100 | + prompts.put("summarizeText", new McpSchema.Prompt("summarizeText", "", "Summarizes the provided text into a specified number of sentences", List.of(new McpSchema.PromptArgument("text_to_summarize", null, true), new McpSchema.PromptArgument("maxSentences", null, true)))); |
| 101 | + prompts.put("code_review", new McpSchema.Prompt("code_review", "", "Code Review Prompt", List.of(new McpSchema.PromptArgument("codeSnippet", null, true), new McpSchema.PromptArgument("language", null, true), new McpSchema.PromptArgument("scrutinyLevel", null, true)))); |
| 102 | + |
| 103 | + promptInvokers.put("summarizeText", (args, exchange) -> app.require(PromptsExample.class).summarizeText((String) args.get("text_to_summarize"), (String) args.get("maxSentences"))); |
| 104 | + promptInvokers.put("code_review", (args, exchange) -> app.require(PromptsExample.class).codeReviewPrompt((String) args.get("codeSnippet"), (String) args.get("language"), (String) args.get("scrutinyLevel"))); |
| 105 | + completions.add(new McpSchema.PromptReference("code_review")); |
| 106 | + completions.add(new McpSchema.PromptReference("code_review")); |
| 107 | + completions.add(new McpSchema.ResourceReference("file:///project/{name}")); |
| 108 | + |
| 109 | + completionInvokers.put("code_review_language", (input) -> app.require(PromptsExample.class).completeCodeReviewLang(input)); |
| 110 | + completionInvokers.put("code_review_scrutinyLevel", (input) -> app.require(PromptsExample.class).completeScrutinyLevel(input)); |
| 111 | + completionInvokers.put("file:///project/{name}_name", (input) -> app.require(ResourceTemplateExamples.class).projectNameCompletion(input)); |
| 112 | + |
| 113 | + resources.add(McpSchema.Resource.builder().name("README.md").title("README.md").uri("file:///project/README.md").mimeType("text/markdown").build()); |
| 114 | + resources.add(McpSchema.Resource.builder().name("blobResource").uri("file:///blob").build()); |
| 115 | + resources.add(McpSchema.Resource.builder().name("threadStone").uri("file:///project/thread-stone").size(Long.valueOf(10563)).annotations(new McpSchema.Annotations(List.of(McpSchema.Role.USER, McpSchema.Role.ASSISTANT), 0.3, null)).build()); |
| 116 | + resources.add(McpSchema.Resource.builder().name("blackBriar").uri("file:///project/blackbriar/metadata.json").build()); |
| 117 | + |
| 118 | + resourceReaders.put("file:///project/README.md", () -> app.require(ResourceExamples.class).textResource()); |
| 119 | + resourceReaders.put("file:///blob", () -> app.require(ResourceExamples.class).blobResource()); |
| 120 | + resourceReaders.put("file:///project/thread-stone", () -> app.require(ResourceExamples.class).threadStone()); |
| 121 | + resourceReaders.put("file:///project/blackbriar/metadata.json", () -> app.require(ResourceExamples.class).blackBriar()); |
| 122 | + |
| 123 | + resourceTemplates.add(McpSchema.ResourceTemplate.builder().name("get_project").uriTemplate("file:///project/{name}").build()); |
| 124 | + |
| 125 | + resourceTemplateReaders.put("file:///project/{name}", (args) -> app.require(ResourceTemplateExamples.class).getProject((String) args.get("name"), new ResourceUri((String) args.get("__resourceUri")))); |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Invokes a tool by name with the provided arguments. |
| 131 | + * @param toolName the name of the tool to invoke |
| 132 | + * @param args the arguments to pass to the tool |
| 133 | + * @return the result of the tool invocation |
| 134 | + */ |
| 135 | + public Object invokeTool(final String toolName, final Map<String, Object> args, |
| 136 | + final McpSyncServerExchange exchange) { |
| 137 | + MethodInvoker invoker = toolInvokers.get(toolName); |
| 138 | + return invoker.invoke(args, exchange); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Invokes a prompt by name with the provided arguments. |
| 143 | + * @param promptName the name of the prompt to invoke |
| 144 | + * @param args the arguments to pass to the prompt |
| 145 | + * @return the result of the prompt invocation |
| 146 | + */ |
| 147 | + public Object invokePrompt(final String promptName, final Map<String, Object> args, |
| 148 | + final McpSyncServerExchange exchange) { |
| 149 | + MethodInvoker invoker = promptInvokers.get(promptName); |
| 150 | + return invoker.invoke(args, exchange); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Invokes a completion by identifier(prompt or resource name) and argumentName with the provided |
| 155 | + * argument value. |
| 156 | + * @param identifier prompt or resource template name |
| 157 | + * @param argumentName the name of an argument in prompt or resource template |
| 158 | + * @param input incoming argument value |
| 159 | + * @return the result of the completion invocation |
| 160 | + */ |
| 161 | + public Object invokeCompletion(final String identifier, final String argumentName, |
| 162 | + final String input) { |
| 163 | + var completionKey = identifier + '_' + argumentName; |
| 164 | + var invoker = completionInvokers.get(completionKey); |
| 165 | + if (invoker == null) { |
| 166 | + return List.of(); |
| 167 | + } |
| 168 | + return invoker.apply(input); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Reads a resource by URI |
| 173 | + * @param uri Resource URI |
| 174 | + * @return resource content |
| 175 | + */ |
| 176 | + public Object readResource(final String uri) { |
| 177 | + var reader = resourceReaders.get(uri); |
| 178 | + return reader.get(); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Reads a resource by URI according to template |
| 183 | + * @param uriTemplate Resource URI template |
| 184 | + * @return resource content |
| 185 | + */ |
| 186 | + public Object readResourceByTemplate(final String uriTemplate, final Map<String, Object> args) { |
| 187 | + var reader = resourceTemplateReaders.get(uriTemplate); |
| 188 | + return reader.apply(args); |
| 189 | + } |
| 190 | + |
| 191 | + public Map<String, ToolSpec> getTools() { |
| 192 | + return tools; |
| 193 | + } |
| 194 | + |
| 195 | + public Map<String, McpSchema.Prompt> getPrompts() { |
| 196 | + return prompts; |
| 197 | + } |
| 198 | + |
| 199 | + public List<McpSchema.CompleteReference> getCompletions() { |
| 200 | + return completions; |
| 201 | + } |
| 202 | + |
| 203 | + public List<McpSchema.Resource> getResources() { |
| 204 | + return resources; |
| 205 | + } |
| 206 | + |
| 207 | + public List<McpSchema.ResourceTemplate> getResourceTemplates() { |
| 208 | + return resourceTemplates; |
| 209 | + } |
| 210 | + |
| 211 | + public String getServerKey() { |
| 212 | + return "example"; |
| 213 | + } |
| 214 | +} |
0 commit comments