leejunyoung5819mc-paper-plugin-test-mcp-26.3

공개

마인크래프트 26.3 Paper 서버의 플러그인 테스트를 하려는 AI 에이전트를 위한 MCP 도구

125줄4.2 KB
1package dev.mctester.bridge;
2
3import com.google.gson.Gson;
4import com.google.gson.GsonBuilder;
5import com.google.gson.JsonElement;
6import com.google.gson.JsonObject;
7import com.google.gson.JsonParser;
8import com.sun.net.httpserver.HttpExchange;
9import com.sun.net.httpserver.HttpServer;
10
11import java.io.IOException;
12import java.io.OutputStream;
13import java.net.InetAddress;
14import java.net.InetSocketAddress;
15import java.net.URLDecoder;
16import java.nio.charset.StandardCharsets;
17import java.util.Map;
18import java.util.concurrent.ExecutorService;
19import java.util.concurrent.Executors;
20import java.util.function.Function;
21
22/**
23 * Tiny JSON-over-HTTP server bound to 127.0.0.1 only.
24 * Every route takes a JSON object (POST body and/or query string) and returns
25 * {"ok":true,...} or {"ok":false,"error":"..."}.
26 */
27final class BridgeServer {
28 private static final Gson GSON = new GsonBuilder().disableHtmlEscaping().serializeNulls().create();
29
30 private final HttpServer server;
31 private final Map<String, Function<JsonObject, JsonObject>> routes = Map.ofEntries(
32 Map.entry("/health", Actions::health),
33 Map.entry("/state", Actions::state),
34 Map.entry("/screen", Actions::screen),
35 Map.entry("/screenshot", Actions::screenshot),
36 Map.entry("/click_slot", Actions::clickSlot),
37 Map.entry("/mouse", Actions::mouse),
38 Map.entry("/key", Actions::key),
39 Map.entry("/hold", Actions::hold),
40 Map.entry("/type", Actions::type),
41 Map.entry("/close_screen", Actions::closeScreen),
42 Map.entry("/chat", Actions::chat),
43 Map.entry("/chat_log", Actions::chatLog),
44 Map.entry("/entities", Actions::entities),
45 Map.entry("/look", Actions::look),
46 Map.entry("/use", Actions::use),
47 Map.entry("/attack", Actions::attack),
48 Map.entry("/interact_entity", Actions::interactEntity),
49 Map.entry("/use_on_block", Actions::useOnBlock),
50 Map.entry("/hotbar", Actions::hotbar),
51 Map.entry("/connect", Actions::connect),
52 Map.entry("/disconnect", Actions::disconnect),
53 Map.entry("/quit", Actions::quit)
54 );
55
56 BridgeServer(int port) throws IOException {
57 server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), port), 16);
58 ExecutorService pool = Executors.newFixedThreadPool(4, r -> {
59 Thread t = new Thread(r, "qa-bridge-http");
60 t.setDaemon(true);
61 return t;
62 });
63 server.setExecutor(pool);
64 server.createContext("/", this::handle);
65 }
66
67 void start() {
68 server.start();
69 }
70
71 /** Must be called on client shutdown: the JDK HttpServer dispatcher thread is non-daemon and would keep the JVM alive. */
72 void stop() {
73 server.stop(0);
74 }
75
76 private void handle(HttpExchange ex) throws IOException {
77 JsonObject out;
78 int status = 200;
79 try {
80 String path = ex.getRequestURI().getPath();
81 Function<JsonObject, JsonObject> fn = routes.get(path);
82 if (fn == null) {
83 status = 404;
84 out = Actions.err("unknown route " + path + "; routes: " + routes.keySet());
85 } else {
86 JsonObject in = readArgs(ex);
87 out = fn.apply(in);
88 if (!out.has("ok")) out.addProperty("ok", true);
89 }
90 } catch (BridgeException e) {
91 status = 400;
92 out = Actions.err(e.getMessage());
93 } catch (Throwable e) {
94 QaBridgeClient.LOG.error("qa-bridge: request failed", e);
95 status = 500;
96 out = Actions.err(e.getClass().getSimpleName() + ": " + e.getMessage());
97 }
98 byte[] body = GSON.toJson(out).getBytes(StandardCharsets.UTF_8);
99 ex.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
100 ex.sendResponseHeaders(status, body.length);
101 try (OutputStream os = ex.getResponseBody()) {
102 os.write(body);
103 }
104 }
105
106 private static JsonObject readArgs(HttpExchange ex) throws IOException {
107 JsonObject in = new JsonObject();
108 byte[] raw = ex.getRequestBody().readAllBytes();
109 if (raw.length > 0) {
110 JsonElement el = JsonParser.parseString(new String(raw, StandardCharsets.UTF_8));
111 if (el.isJsonObject()) in = el.getAsJsonObject();
112 }
113 String q = ex.getRequestURI().getRawQuery();
114 if (q != null) {
115 for (String part : q.split("&")) {
116 int i = part.indexOf('=');
117 if (i <= 0) continue;
118 String k = URLDecoder.decode(part.substring(0, i), StandardCharsets.UTF_8);
119 String v = URLDecoder.decode(part.substring(i + 1), StandardCharsets.UTF_8);
120 if (!in.has(k)) in.addProperty(k, v);
121 }
122 }
123 return in;
124 }
125}