|
| 1 | +package build.serve.mcp; |
| 2 | + |
| 3 | +/*- |
| 4 | + * #%L |
| 5 | + * Serve MCP |
| 6 | + * %% |
| 7 | + * Copyright (C) 2026 Reed von Redwitz |
| 8 | + * %% |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | + |
| 23 | +import build.base.json.Json; |
| 24 | +import build.base.json.JsonArray; |
| 25 | +import build.base.json.JsonBoolean; |
| 26 | +import build.base.json.JsonNull; |
| 27 | +import build.base.json.JsonNumber; |
| 28 | +import build.base.json.JsonObject; |
| 29 | +import build.base.json.JsonString; |
| 30 | +import build.base.json.JsonValue; |
| 31 | + |
| 32 | +import java.io.BufferedReader; |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.InputStreamReader; |
| 35 | +import java.io.OutputStreamWriter; |
| 36 | +import java.io.PipedInputStream; |
| 37 | +import java.io.PipedOutputStream; |
| 38 | +import java.io.PrintWriter; |
| 39 | +import java.io.UncheckedIOException; |
| 40 | +import java.nio.charset.StandardCharsets; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +/** |
| 45 | + * A client that drives an {@link McpServer} over its stdio transport. |
| 46 | + * |
| 47 | + * <p>Spins up a virtual thread running {@link McpServer#stdioLoop} and communicates |
| 48 | + * with it via piped streams. Useful for integration tests and local tool invocation |
| 49 | + * without any HTTP plumbing. |
| 50 | + * |
| 51 | + * <pre>{@code |
| 52 | + * try (var client = McpStdioClient.of(server)) { |
| 53 | + * var result = client.call("my_tool", Map.of("arg", "value")); |
| 54 | + * var data = result.json().asObject(); |
| 55 | + * } |
| 56 | + * }</pre> |
| 57 | + * |
| 58 | + * @author reed.vonredwitz |
| 59 | + * @since Jun-2026 |
| 60 | + */ |
| 61 | +public final class McpStdioClient implements AutoCloseable { |
| 62 | + |
| 63 | + private final PrintWriter writer; |
| 64 | + private final BufferedReader reader; |
| 65 | + private final Thread serverThread; |
| 66 | + private int nextId = 1; |
| 67 | + |
| 68 | + private McpStdioClient(final PrintWriter writer, |
| 69 | + final BufferedReader reader, |
| 70 | + final Thread serverThread) { |
| 71 | + this.writer = writer; |
| 72 | + this.reader = reader; |
| 73 | + this.serverThread = serverThread; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Creates a client connected to the given server and starts its stdio loop |
| 78 | + * on a virtual thread. |
| 79 | + * |
| 80 | + * @param server the server to connect to |
| 81 | + * @return the connected client |
| 82 | + */ |
| 83 | + public static McpStdioClient of(final McpServer server) { |
| 84 | + try { |
| 85 | + final var clientToServer = new PipedOutputStream(); |
| 86 | + final var serverToClient = new PipedOutputStream(); |
| 87 | + final var serverIn = new PipedInputStream(clientToServer); |
| 88 | + final var clientIn = new PipedInputStream(serverToClient); |
| 89 | + |
| 90 | + final var thread = Thread.ofVirtual().start( |
| 91 | + () -> server.stdioLoop(serverIn, serverToClient)); |
| 92 | + |
| 93 | + return new McpStdioClient( |
| 94 | + new PrintWriter(new OutputStreamWriter(clientToServer, StandardCharsets.UTF_8), true), |
| 95 | + new BufferedReader(new InputStreamReader(clientIn, StandardCharsets.UTF_8)), |
| 96 | + thread |
| 97 | + ); |
| 98 | + } catch (final IOException e) { |
| 99 | + throw new UncheckedIOException(e); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Calls the named tool and returns the deserialized {@link McpToolResult}. |
| 105 | + * |
| 106 | + * @param toolName the tool name |
| 107 | + * @param arguments the arguments to pass |
| 108 | + * @return the tool result |
| 109 | + */ |
| 110 | + public McpToolResult call(final String toolName, final Map<String, Object> arguments) { |
| 111 | + final var params = Map.<String, Object>of("name", toolName, "arguments", arguments); |
| 112 | + final var response = send("tools/call", params); |
| 113 | + return McpToolResult.fromJson(response.get("result").asObject()); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sends a raw JSON-RPC request and returns the full response object. |
| 118 | + * |
| 119 | + * @param method the JSON-RPC method name |
| 120 | + * @param params the parameters |
| 121 | + * @return the full response envelope |
| 122 | + */ |
| 123 | + public JsonObject send(final String method, final Map<String, Object> params) { |
| 124 | + writer.println(rpc(method, nextId++, params)); |
| 125 | + try { |
| 126 | + final var line = reader.readLine(); |
| 127 | + if (line == null) { |
| 128 | + throw new IllegalStateException("Server closed stdout before responding"); |
| 129 | + } |
| 130 | + return Json.parse(line).asObject(); |
| 131 | + } catch (final IOException e) { |
| 132 | + throw new UncheckedIOException(e); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Closes the client, signals EOF to the server, and waits for its stdio loop to exit. |
| 138 | + */ |
| 139 | + @Override |
| 140 | + public void close() { |
| 141 | + writer.close(); |
| 142 | + try { |
| 143 | + serverThread.join(5_000); |
| 144 | + } catch (final InterruptedException e) { |
| 145 | + Thread.currentThread().interrupt(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static String rpc(final String method, final int id, final Map<String, Object> params) { |
| 150 | + return JsonObject.builder() |
| 151 | + .put("jsonrpc", "2.0") |
| 152 | + .put("id", id) |
| 153 | + .put("method", method) |
| 154 | + .put("params", toJsonValue(params)) |
| 155 | + .build() |
| 156 | + .toJsonString(); |
| 157 | + } |
| 158 | + |
| 159 | + @SuppressWarnings("unchecked") |
| 160 | + private static JsonValue toJsonValue(final Object value) { |
| 161 | + if (value == null) { |
| 162 | + return JsonNull.INSTANCE; |
| 163 | + } |
| 164 | + if (value instanceof String s) { |
| 165 | + return JsonString.of(s); |
| 166 | + } |
| 167 | + if (value instanceof Number n) { |
| 168 | + return JsonNumber.of(n); |
| 169 | + } |
| 170 | + if (value instanceof Boolean b) { |
| 171 | + return JsonBoolean.of(b); |
| 172 | + } |
| 173 | + if (value instanceof Map<?, ?> m) { |
| 174 | + final var builder = JsonObject.builder(); |
| 175 | + for (final var entry : m.entrySet()) { |
| 176 | + builder.put((String) entry.getKey(), toJsonValue(entry.getValue())); |
| 177 | + } |
| 178 | + return builder.build(); |
| 179 | + } |
| 180 | + if (value instanceof List<?> l) { |
| 181 | + final var builder = JsonArray.builder(); |
| 182 | + for (final var item : l) { |
| 183 | + builder.add(toJsonValue(item)); |
| 184 | + } |
| 185 | + return builder.build(); |
| 186 | + } |
| 187 | + throw new IllegalArgumentException("Unsupported type: " + value.getClass()); |
| 188 | + } |
| 189 | +} |
0 commit comments