xcodebug 1.2.1 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { createInterface } from "node:readline";
4
+ import { fileURLToPath } from "node:url";
5
+ import { dirname, join } from "node:path";
6
+ import { spawn, spawnSync } from "node:child_process";
7
+
8
+ const TOOL_TIMEOUT_SECONDS = 4_200;
9
+ const packageRoot = dirname(dirname(fileURLToPath(import.meta.url)));
10
+ const serverPath = join(packageRoot, "mcp", "server.ts");
11
+ const codex = process.env.CODEX_BIN ?? "codex";
12
+ const runtime = process.env.XCODEBUG_MCP_RUNTIME ?? "bun";
13
+
14
+ function run(command, args) {
15
+ const result = spawnSync(command, args, { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
16
+ if (result.error) throw result.error;
17
+ return result;
18
+ }
19
+
20
+ function ensureServerRegistration() {
21
+ const existing = run(codex, ["mcp", "get", "xcodebug"]);
22
+ if (existing.status === 0) return;
23
+
24
+ const runtimeCheck = run(runtime, ["--version"]);
25
+ if (runtimeCheck.status !== 0) {
26
+ throw new Error(`xcodebug is not registered and ${runtime} is unavailable; install Bun or set XCODEBUG_MCP_RUNTIME`);
27
+ }
28
+ const added = run(codex, ["mcp", "add", "xcodebug", "--", runtime, serverPath]);
29
+ if (added.status !== 0) {
30
+ throw new Error((added.stderr || added.stdout || "codex mcp add failed").trim());
31
+ }
32
+ }
33
+
34
+ function startAppServer() {
35
+ const child = spawn(codex, ["app-server", "--stdio"], { stdio: ["pipe", "pipe", "pipe"] });
36
+ let stderr = "";
37
+ child.stderr.setEncoding("utf8");
38
+ child.stderr.on("data", chunk => { stderr += chunk; });
39
+ const pending = new Map();
40
+ let nextId = 0;
41
+ const input = createInterface({ input: child.stdout });
42
+ input.on("line", line => {
43
+ let message;
44
+ try { message = JSON.parse(line); } catch { return; }
45
+ const request = pending.get(message.id);
46
+ if (!request) return;
47
+ pending.delete(message.id);
48
+ clearTimeout(request.timer);
49
+ if (message.error) request.reject(new Error(JSON.stringify(message.error)));
50
+ else request.resolve(message.result);
51
+ });
52
+ const request = (method, params) => new Promise((resolve, reject) => {
53
+ const id = ++nextId;
54
+ const timer = setTimeout(() => {
55
+ pending.delete(id);
56
+ reject(new Error(`codex app-server timed out during ${method}`));
57
+ }, 15_000);
58
+ pending.set(id, { resolve, reject, timer });
59
+ child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
60
+ });
61
+ const close = async () => {
62
+ for (const { timer } of pending.values()) clearTimeout(timer);
63
+ input.close();
64
+ child.stdin.end();
65
+ await new Promise(resolve => {
66
+ const timer = setTimeout(() => { child.kill(); resolve(); }, 3_000);
67
+ child.once("exit", () => { clearTimeout(timer); resolve(); });
68
+ });
69
+ };
70
+ return { child, request, close, get stderr() { return stderr; } };
71
+ }
72
+
73
+ try {
74
+ ensureServerRegistration();
75
+ const app = startAppServer();
76
+ try {
77
+ await app.request("initialize", { clientInfo: { name: "xcodebug-setup-codex", version: "1" } });
78
+ app.child.stdin.write('{"jsonrpc":"2.0","method":"initialized"}\n');
79
+ const result = await app.request("config/value/write", {
80
+ keyPath: "mcp_servers.xcodebug.tool_timeout_sec",
81
+ value: TOOL_TIMEOUT_SECONDS,
82
+ mergeStrategy: "upsert",
83
+ });
84
+ if (result?.status !== "ok") throw new Error(`Codex rejected the config write: ${JSON.stringify(result)}`);
85
+ console.log(`Configured xcodebug MCP timeout: ${TOOL_TIMEOUT_SECONDS}s`);
86
+ } finally {
87
+ await app.close();
88
+ }
89
+ } catch (error) {
90
+ console.error(`xcodebug setup-codex failed: ${error instanceof Error ? error.message : String(error)}`);
91
+ process.exitCode = 1;
92
+ }