leejunyoung5819 mc-paper-plugin-test-mcp-26.3
공개마인크래프트 26.3 Paper 서버의 플러그인 테스트를 하려는 AI 에이전트를 위한 MCP 도구
| 1 | // Client side of the daemon RPC, with auto-start of the daemon. |
| 2 | import http from 'node:http'; |
| 3 | import fs from 'node:fs'; |
| 4 | import path from 'node:path'; |
| 5 | import { spawn } from 'node:child_process'; |
| 6 | import { DIRS, ROOT, ensureDirs, loadConfig } from './config.ts'; |
| 7 | |
| 8 | const cfg = loadConfig(); |
| 9 | |
| 10 | function request(method: string, route: string, body?: unknown): Promise<any> { |
| 11 | return new Promise((resolve, reject) => { |
| 12 | const data = body === undefined ? undefined : Buffer.from(JSON.stringify(body)); |
| 13 | const req = http.request( |
| 14 | { host: '127.0.0.1', port: cfg.daemonPort, path: route, method, headers: data ? { 'Content-Type': 'application/json', 'Content-Length': data.length } : {} }, |
| 15 | (res) => { |
| 16 | const chunks: Buffer[] = []; |
| 17 | res.on('data', (c) => chunks.push(c)); |
| 18 | res.on('end', () => { |
| 19 | try { |
| 20 | resolve(JSON.parse(Buffer.concat(chunks).toString('utf8'))); |
| 21 | } catch (e) { |
| 22 | reject(e); |
| 23 | } |
| 24 | }); |
| 25 | }, |
| 26 | ); |
| 27 | req.on('error', reject); |
| 28 | if (data) req.write(data); |
| 29 | req.end(); |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | export async function daemonUp(): Promise<boolean> { |
| 34 | try { |
| 35 | const r = await request('GET', '/health'); |
| 36 | return !!r.ok; |
| 37 | } catch { |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | export async function ensureDaemon(): Promise<void> { |
| 43 | if (await daemonUp()) return; |
| 44 | ensureDirs(); |
| 45 | const out = fs.openSync(path.join(DIRS.run, 'daemon.out'), 'a'); |
| 46 | const child = spawn(process.execPath, [path.join(ROOT, 'harness', 'daemon.ts')], { |
| 47 | cwd: ROOT, |
| 48 | detached: true, |
| 49 | stdio: ['ignore', out, out], |
| 50 | env: process.env, |
| 51 | }); |
| 52 | child.unref(); |
| 53 | const t0 = Date.now(); |
| 54 | while (Date.now() - t0 < 15_000) { |
| 55 | if (await daemonUp()) return; |
| 56 | await new Promise((r) => setTimeout(r, 200)); |
| 57 | } |
| 58 | throw new Error(`daemon did not start; see ${path.join(DIRS.run, 'daemon.out')}`); |
| 59 | } |
| 60 | |
| 61 | export async function call(tool: string, params: object = {}): Promise<any> { |
| 62 | await ensureDaemon(); |
| 63 | const r = await request('POST', '/rpc', { tool, params }); |
| 64 | if (!r.ok) throw new Error(r.error); |
| 65 | return r.result; |
| 66 | } |
| 67 | |
| 68 | /** Ask the daemon to stop clients + server and exit; waits until the process is gone. */ |
| 69 | export async function shutdownDaemon(timeoutMs = 90_000): Promise<boolean> { |
| 70 | let pid: number | null = null; |
| 71 | try { |
| 72 | const h = await request('GET', '/health'); |
| 73 | if (!h.ok) return false; |
| 74 | pid = h.pid; |
| 75 | } catch { |
| 76 | return false; |
| 77 | } |
| 78 | await request('POST', '/shutdown', {}); |
| 79 | const t0 = Date.now(); |
| 80 | while (Date.now() - t0 < timeoutMs) { |
| 81 | try { |
| 82 | process.kill(pid!, 0); |
| 83 | } catch { |
| 84 | return true; // process gone |
| 85 | } |
| 86 | await new Promise((r) => setTimeout(r, 300)); |
| 87 | } |
| 88 | throw new Error(`daemon pid ${pid} still running after ${timeoutMs}ms`); |
| 89 | } |