leejunyoung5819 mc-paper-plugin-test-mcp-26.3
공개마인크래프트 26.3 Paper 서버의 플러그인 테스트를 하려는 AI 에이전트를 위한 MCP 도구
| 1 | // Minimal Source RCON client (what Minecraft servers speak on rcon.port). |
| 2 | import net from 'node:net'; |
| 3 | |
| 4 | const AUTH = 3; |
| 5 | const EXEC = 2; |
| 6 | |
| 7 | export class Rcon { |
| 8 | private sock: net.Socket | null = null; |
| 9 | private buf = Buffer.alloc(0); |
| 10 | private nextId = 1; |
| 11 | private pending = new Map<number, { resolve: (s: string) => void; reject: (e: Error) => void; parts: string[]; sentinel?: number }>(); |
| 12 | private host: string; |
| 13 | private port: number; |
| 14 | private password: string; |
| 15 | |
| 16 | constructor(host: string, port: number, password: string) { |
| 17 | this.host = host; |
| 18 | this.port = port; |
| 19 | this.password = password; |
| 20 | } |
| 21 | |
| 22 | get connected(): boolean { |
| 23 | return this.sock !== null && !this.sock.destroyed; |
| 24 | } |
| 25 | |
| 26 | async connect(): Promise<void> { |
| 27 | if (this.connected) return; |
| 28 | await new Promise<void>((resolve, reject) => { |
| 29 | const s = net.createConnection({ host: this.host, port: this.port }, () => resolve()); |
| 30 | s.once('error', reject); |
| 31 | s.on('data', (d) => this.onData(d as Buffer)); |
| 32 | s.on('close', () => { |
| 33 | this.sock = null; |
| 34 | for (const p of this.pending.values()) p.reject(new Error('RCON connection closed')); |
| 35 | this.pending.clear(); |
| 36 | }); |
| 37 | this.sock = s; |
| 38 | }); |
| 39 | const id = this.nextId++; |
| 40 | const res = new Promise<string>((resolve, reject) => this.pending.set(id, { resolve, reject, parts: [] })); |
| 41 | this.write(id, AUTH, this.password); |
| 42 | await res; |
| 43 | } |
| 44 | |
| 45 | private write(id: number, type: number, body: string): void { |
| 46 | const payload = Buffer.from(body, 'utf8'); |
| 47 | const pkt = Buffer.alloc(14 + payload.length); |
| 48 | pkt.writeInt32LE(10 + payload.length, 0); |
| 49 | pkt.writeInt32LE(id, 4); |
| 50 | pkt.writeInt32LE(type, 8); |
| 51 | payload.copy(pkt, 12); |
| 52 | pkt.writeInt16LE(0, 12 + payload.length); |
| 53 | this.sock!.write(pkt); |
| 54 | } |
| 55 | |
| 56 | private onData(d: Buffer): void { |
| 57 | this.buf = Buffer.concat([this.buf, d]); |
| 58 | while (this.buf.length >= 4) { |
| 59 | const len = this.buf.readInt32LE(0); |
| 60 | if (this.buf.length < 4 + len) return; |
| 61 | const id = this.buf.readInt32LE(4); |
| 62 | const type = this.buf.readInt32LE(8); |
| 63 | const body = this.buf.subarray(12, 4 + len - 2).toString('utf8'); |
| 64 | this.buf = this.buf.subarray(4 + len); |
| 65 | if (id === -1) { |
| 66 | for (const p of this.pending.values()) p.reject(new Error('RCON authentication failed')); |
| 67 | this.pending.clear(); |
| 68 | continue; |
| 69 | } |
| 70 | // Fragmented responses: we send a sentinel (id+1 empty) after each command; collect until it echoes. |
| 71 | const sentinelFor = this.pending.get(id - 1); |
| 72 | if (sentinelFor && sentinelFor.sentinel === id) { |
| 73 | this.pending.delete(id - 1); |
| 74 | sentinelFor.resolve(sentinelFor.parts.join('')); |
| 75 | continue; |
| 76 | } |
| 77 | const p = this.pending.get(id); |
| 78 | if (!p) continue; |
| 79 | if (p.sentinel) { |
| 80 | p.parts.push(body); |
| 81 | } else { |
| 82 | void type; |
| 83 | this.pending.delete(id); |
| 84 | p.resolve(body); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | async command(cmd: string, timeoutMs = 15000): Promise<string> { |
| 90 | await this.connect(); |
| 91 | const id = this.nextId; |
| 92 | this.nextId += 2; |
| 93 | const res = new Promise<string>((resolve, reject) => { |
| 94 | const entry = { resolve, reject, parts: [] as string[], sentinel: id + 1 }; |
| 95 | this.pending.set(id, entry); |
| 96 | setTimeout(() => { |
| 97 | if (this.pending.get(id) === entry) { |
| 98 | this.pending.delete(id); |
| 99 | reject(new Error(`RCON command timed out: ${cmd}`)); |
| 100 | } |
| 101 | }, timeoutMs); |
| 102 | }); |
| 103 | this.write(id, EXEC, cmd); |
| 104 | // An invalid packet type makes the server answer "Unknown request" with the same id, after the real reply. |
| 105 | this.write(id + 1, 200, ''); |
| 106 | const raw = await res; |
| 107 | return stripFormatting(raw).trim(); |
| 108 | } |
| 109 | |
| 110 | close(): void { |
| 111 | this.sock?.destroy(); |
| 112 | this.sock = null; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | export function stripFormatting(s: string): string { |
| 117 | return s.replace(/§[0-9a-fk-orx]/gi, ''); |
| 118 | } |