xcodebug 1.0.0 → 1.1.0
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.
- package/README.md +91 -0
- package/archive.ts +1 -1
- package/capture.ts +1 -1
- package/changes.ts +5 -2
- package/devices.ts +1 -1
- package/exec.ts +22 -3
- package/index.ts +14 -7
- package/mcp/server.ts +196 -0
- package/package.json +3 -1
- package/xcode.ts +9 -4
package/README.md
CHANGED
|
@@ -32,6 +32,15 @@ pi install npm:xcodebug@1.0.0
|
|
|
32
32
|
pi install /path/to/xcodebug
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
**Upgrading from `xcodebug-extension`:** the package was renamed, so both names
|
|
36
|
+
register the same `xcodebug` tool and the loader refuses to start the second one
|
|
37
|
+
(`Tool "xcodebug" conflicts with ...`). Remove the old package first:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pi remove npm:xcodebug-extension
|
|
41
|
+
pi install npm:xcodebug
|
|
42
|
+
```
|
|
43
|
+
|
|
35
44
|
Or copy the directory into the global extensions dir, which the engine discovers
|
|
36
45
|
on startup:
|
|
37
46
|
|
|
@@ -48,6 +57,87 @@ extension: pi has no reload command at all, and omp's `/reload-plugins` only
|
|
|
48
57
|
clears plugin discovery, not the imported module graph. Editing `index.ts` while
|
|
49
58
|
a session is running therefore has no effect on that session.
|
|
50
59
|
|
|
60
|
+
## Claude Code and Codex (MCP)
|
|
61
|
+
|
|
62
|
+
Neither client has an in-process extension API - tools reach them as MCP servers.
|
|
63
|
+
`mcp/server.ts` is that adapter: it loads `index.ts` with a stub host and serves
|
|
64
|
+
the tool it registers, so it exposes the same actions and arguments as the pi
|
|
65
|
+
extension rather than a parallel implementation.
|
|
66
|
+
|
|
67
|
+
### Set up (once per machine)
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npm i -g xcodebug # >= 1.1.0, the first release that ships mcp/
|
|
71
|
+
codex mcp add xcodebug -- node "$(npm root -g)/xcodebug/mcp/server.ts"
|
|
72
|
+
claude mcp add xcodebug -- node "$(npm root -g)/xcodebug/mcp/server.ts"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Working from a checkout instead? Point both at `mcp/server.ts` in the repo:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
codex mcp add xcodebug -- node /path/to/xcodebug/mcp/server.ts
|
|
79
|
+
claude mcp add xcodebug -- node /path/to/xcodebug/mcp/server.ts
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`node` >= 22.18 is enough: the imports carry explicit `.ts` extensions and the
|
|
83
|
+
package declares `"type": "module"`, so there is no build step and no Bun.
|
|
84
|
+
|
|
85
|
+
Check that the client picked it up:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
codex mcp list # xcodebug node …
|
|
89
|
+
claude mcp list # xcodebug: … ✔ Connected
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Use
|
|
93
|
+
|
|
94
|
+
Start the client **in the project directory**. The server inherits its cwd, and
|
|
95
|
+
that directory is the project root every action resolves against.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
cd ~/myapp # holds the .xcworkspace or .xcodeproj
|
|
99
|
+
codex # or: claude
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Then ask for the outcome; the model calls the tool:
|
|
103
|
+
|
|
104
|
+
```text
|
|
105
|
+
Build and launch on my device, then show me the console.
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The actions that matter in this mode:
|
|
109
|
+
|
|
110
|
+
```text
|
|
111
|
+
action=launch device=<udid> build + install + launch; the capture starts
|
|
112
|
+
action=logs since=0 incremental read - the cursor is per capture
|
|
113
|
+
action=logs grep=Weather narrow to one subsystem
|
|
114
|
+
action=wait match="..." block until a line appears
|
|
115
|
+
action=status captures, line counts, log paths
|
|
116
|
+
action=stop close every running capture
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### What differs from pi
|
|
120
|
+
|
|
121
|
+
| | pi / omp / omo | Claude Code / Codex |
|
|
122
|
+
|---|---|---|
|
|
123
|
+
| how the tool is exposed | registered in-process | MCP server process |
|
|
124
|
+
| status line, widget | yes | none |
|
|
125
|
+
| `push` | mirrors new lines into the session | refused |
|
|
126
|
+
| capture state | in the session | in the server process |
|
|
127
|
+
| lifetime | the session | the client process |
|
|
128
|
+
|
|
129
|
+
- **`push` is refused on purpose.** MCP has no channel that injects content into a
|
|
130
|
+
running turn, and neither client renders `notifications/message` - measured on
|
|
131
|
+
both, where the model reported that nothing arrived on its own between two tool
|
|
132
|
+
calls. Read with `action=logs` or `action=wait`; both are pull-based, and the
|
|
133
|
+
cursor means you only pay for new lines.
|
|
134
|
+
- **Captures end with the client.** The server is a child of Codex/Claude, so
|
|
135
|
+
quitting the client stops the bridges. Relaunch next session.
|
|
136
|
+
|
|
137
|
+
Logs land in `~/.xcodebug/logs/xcodebug/`, or under `XCODEBUG_AGENT_DIR` when set.
|
|
138
|
+
Every response also reports the exact path, so the model can read the file once a
|
|
139
|
+
run is over.
|
|
140
|
+
|
|
51
141
|
## Requirements
|
|
52
142
|
|
|
53
143
|
| Need | Why |
|
|
@@ -56,6 +146,7 @@ a session is running therefore has no effect on that session.
|
|
|
56
146
|
| `idevicesyslog` from libimobiledevice | optional; only `syslog` and the dual-channel safety net |
|
|
57
147
|
| git | change detection; without a repo every run rebuilds |
|
|
58
148
|
| CocoaPods | optional; only projects with a `Podfile` |
|
|
149
|
+
| Node >= 22.18 | only for the MCP clients (Claude Code, Codex); pi runs the extension itself |
|
|
59
150
|
|
|
60
151
|
`xcodebug action=doctor` reports which of these are present.
|
|
61
152
|
|
package/archive.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import { mkdir, readdir, rm, stat } from "node:fs/promises";
|
|
18
18
|
import { dirname, join } from "node:path";
|
|
19
|
-
import { runCommand, timestamp } from "./exec";
|
|
19
|
+
import { runCommand, timestamp } from "./exec.ts";
|
|
20
20
|
|
|
21
21
|
export interface ArchiveResult {
|
|
22
22
|
ok: boolean;
|
package/capture.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { spawn, type ChildProcess } from "node:child_process";
|
|
|
7
7
|
import { createWriteStream, type WriteStream } from "node:fs";
|
|
8
8
|
import { mkdir } from "node:fs/promises";
|
|
9
9
|
import { dirname } from "node:path";
|
|
10
|
-
import { timestamp } from "./exec";
|
|
10
|
+
import { timestamp } from "./exec.ts";
|
|
11
11
|
|
|
12
12
|
const ANSI = /\u001b\[[0-9;?]*[ -/]*[@-~]/g;
|
|
13
13
|
const MAX_BUFFER_LINES = 20_000;
|
package/changes.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
14
14
|
import { dirname, join, relative, resolve } from "node:path";
|
|
15
|
-
import { runCommand } from "./exec";
|
|
15
|
+
import { runCommand } from "./exec.ts";
|
|
16
16
|
|
|
17
17
|
const SOURCE_EXTENSIONS = new Set([
|
|
18
18
|
".swift", ".h", ".m", ".mm", ".c", ".cc", ".cpp", ".metal",
|
|
@@ -334,7 +334,10 @@ export async function runPodInstall(
|
|
|
334
334
|
signal: options.signal,
|
|
335
335
|
});
|
|
336
336
|
const durationMs = Date.now() - started;
|
|
337
|
-
const
|
|
337
|
+
const dropped = result.truncated
|
|
338
|
+
? "[xcodebug] output exceeded the size cap; the beginning was dropped and this log keeps the tail.\n\n"
|
|
339
|
+
: "";
|
|
340
|
+
const output = `$ pod install --no-repo-update (cwd ${podfileDir})\n\n${dropped}${result.stdout}\n${result.stderr}`;
|
|
338
341
|
// The log directory is created lazily by whatever wrote a log first; pod
|
|
339
342
|
// install can be the very first thing a run does, so ensure it here.
|
|
340
343
|
await mkdir(dirname(options.logFile), { recursive: true });
|
package/devices.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* devicectl accepts a uuid, ecid, serial number, udid, name or DNS name, so
|
|
5
5
|
* every identifier we keep is accepted verbatim by later commands.
|
|
6
6
|
*/
|
|
7
|
-
import { asRecord, asString, runCommand } from "./exec";
|
|
7
|
+
import { asRecord, asString, runCommand } from "./exec.ts";
|
|
8
8
|
|
|
9
9
|
export interface DeviceInfo {
|
|
10
10
|
/** CoreDevice identifier (uuid) — accepted by devicectl. */
|
package/exec.ts
CHANGED
|
@@ -11,6 +11,15 @@ export interface CommandResult {
|
|
|
11
11
|
stderr: string;
|
|
12
12
|
code: number;
|
|
13
13
|
timedOut: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* True when a stream exceeded `maxBytes` and its beginning was dropped.
|
|
16
|
+
*
|
|
17
|
+
* The head is what gets dropped because the useful end of a build is the end:
|
|
18
|
+
* a 12MB cap on a four-minute build used to keep thousands of identical
|
|
19
|
+
* compile commands and throw away the `error:` diagnostics and the verdict,
|
|
20
|
+
* leaving a log that says BUILD FAILED and nothing else.
|
|
21
|
+
*/
|
|
22
|
+
truncated: boolean;
|
|
14
23
|
}
|
|
15
24
|
|
|
16
25
|
export interface CommandOptions {
|
|
@@ -33,6 +42,7 @@ export function runCommand(
|
|
|
33
42
|
let stderr = "";
|
|
34
43
|
let timedOut = false;
|
|
35
44
|
let settled = false;
|
|
45
|
+
let truncated = false;
|
|
36
46
|
|
|
37
47
|
// `detached` puts the child in its own process group so the whole tree can
|
|
38
48
|
// be signalled at once. `sh -lc "cd x && npm test"` runs the real work as a
|
|
@@ -66,15 +76,24 @@ export function runCommand(
|
|
|
66
76
|
}, timeoutMs)
|
|
67
77
|
: undefined;
|
|
68
78
|
|
|
69
|
-
|
|
70
|
-
|
|
79
|
+
// Keep the tail, not the head: a build's diagnostics and verdict are at the
|
|
80
|
+
// end. Trim in blocks (down to 75% of the cap) so a long build re-copies the
|
|
81
|
+
// buffer a few times instead of once per chunk, and so memory stays bounded.
|
|
82
|
+
const append = (current: string, chunk: string): string => {
|
|
83
|
+
const next = current + chunk;
|
|
84
|
+
if (next.length <= maxBytes) return next;
|
|
85
|
+
truncated = true;
|
|
86
|
+
// `slice(-0)` is `slice(0)`, which would keep the whole buffer: clamp to at
|
|
87
|
+
// least one character so a tiny cap still truncates.
|
|
88
|
+
return next.slice(-Math.max(1, Math.floor(maxBytes * 0.75)));
|
|
89
|
+
};
|
|
71
90
|
|
|
72
91
|
const finish = (code: number): void => {
|
|
73
92
|
if (settled) return;
|
|
74
93
|
settled = true;
|
|
75
94
|
if (timer) clearTimeout(timer);
|
|
76
95
|
signal?.removeEventListener("abort", onAbort);
|
|
77
|
-
resolve({ stdout, stderr, code, timedOut });
|
|
96
|
+
resolve({ stdout, stderr, code, timedOut, truncated });
|
|
78
97
|
};
|
|
79
98
|
|
|
80
99
|
child.stdout?.setEncoding("utf8");
|
package/index.ts
CHANGED
|
@@ -16,12 +16,12 @@ import { basename, dirname, join } from "node:path";
|
|
|
16
16
|
// one distribution of the engine.
|
|
17
17
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
18
18
|
import { Type } from "typebox";
|
|
19
|
-
import { Capture, type LogLine } from "./capture";
|
|
20
|
-
import { describeDevices, findProcessPid, listDevices, lookupProcess, resolveDevice, type DeviceInfo } from "./devices";
|
|
21
|
-
import { lastLines, runCommand, timestamp } from "./exec";
|
|
22
|
-
import { buildForDevice, consoleLaunchArgs, installApp, terminateApp, type BuildTarget } from "./xcode";
|
|
23
|
-
import { checkPodLockSync, describeChanges, detectChanges, runPodInstall, type ChangeReport } from "./changes";
|
|
24
|
-
import { clearArchives } from "./archive";
|
|
19
|
+
import { Capture, type LogLine } from "./capture.ts";
|
|
20
|
+
import { describeDevices, findProcessPid, listDevices, lookupProcess, resolveDevice, type DeviceInfo } from "./devices.ts";
|
|
21
|
+
import { lastLines, runCommand, timestamp } from "./exec.ts";
|
|
22
|
+
import { buildForDevice, consoleLaunchArgs, installApp, terminateApp, type BuildTarget } from "./xcode.ts";
|
|
23
|
+
import { checkPodLockSync, describeChanges, detectChanges, runPodInstall, type ChangeReport } from "./changes.ts";
|
|
24
|
+
import { clearArchives } from "./archive.ts";
|
|
25
25
|
|
|
26
26
|
const CAPTURE_PREFIX = "xcodebug";
|
|
27
27
|
const PUSH_INTERVAL_MS = 4_000;
|
|
@@ -1874,6 +1874,7 @@ export default function xcodebug(pi: ExtensionAPI): void {
|
|
|
1874
1874
|
const matches: LogLine[] = [];
|
|
1875
1875
|
const seen = new Set<number>();
|
|
1876
1876
|
let finished = false;
|
|
1877
|
+
let cancelled = false;
|
|
1877
1878
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
1878
1879
|
let unsubscribeLine: () => void = () => {};
|
|
1879
1880
|
let unsubscribeExit: () => void = () => {};
|
|
@@ -1883,6 +1884,9 @@ export default function xcodebug(pi: ExtensionAPI): void {
|
|
|
1883
1884
|
const settle = (timedOut: boolean): string => {
|
|
1884
1885
|
const cursor = capture.status().cursor;
|
|
1885
1886
|
if (matches.length === 0) {
|
|
1887
|
+
// Abort also settles through `finish`, so say what happened instead of
|
|
1888
|
+
// reporting a timeout that never elapsed.
|
|
1889
|
+
if (cancelled) return `cancelled while waiting for "${needle}" (cursor ${cursor})`;
|
|
1886
1890
|
return timedOut
|
|
1887
1891
|
? `timed out after ${timeoutMs / 1000}s waiting for "${needle}" (cursor ${cursor})`
|
|
1888
1892
|
: `capture ended before "${needle}" appeared (cursor ${cursor})`;
|
|
@@ -1925,7 +1929,10 @@ export default function xcodebug(pi: ExtensionAPI): void {
|
|
|
1925
1929
|
});
|
|
1926
1930
|
unsubscribeExit = capture.onExit(() => setTimeout(() => finish(false), 0));
|
|
1927
1931
|
timer = setTimeout(() => finish(true), timeoutMs);
|
|
1928
|
-
onAbort = (): void =>
|
|
1932
|
+
onAbort = (): void => {
|
|
1933
|
+
cancelled = true;
|
|
1934
|
+
finish(true);
|
|
1935
|
+
};
|
|
1929
1936
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1930
1937
|
|
|
1931
1938
|
for (const line of capture.read({ since, limit: 500, grep: needle }).lines) collect(line);
|
package/mcp/server.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP entry point: serve the xcodebug tool to MCP clients such as Codex CLI.
|
|
3
|
+
*
|
|
4
|
+
* Codex has no in-process extension API - tools reach it as MCP servers - so this
|
|
5
|
+
* file is the adapter, not a second implementation: it loads index.ts with a stub
|
|
6
|
+
* host, captures the tool the extension registers, and forwards calls to that
|
|
7
|
+
* tool's execute. index.ts stays untouched, which is what keeps the pi path
|
|
8
|
+
* behaving exactly as verified across pi, omp and omo.
|
|
9
|
+
*
|
|
10
|
+
* Run it with node (>= 22.18) or bun: the extension's imports carry explicit
|
|
11
|
+
* `.ts` extensions and the package declares "type": "module", so Node's ESM
|
|
12
|
+
* loader resolves them with no build step.
|
|
13
|
+
*
|
|
14
|
+
* codex mcp add xcodebug -- node /path/to/xcodebug/mcp/server.ts
|
|
15
|
+
*
|
|
16
|
+
* The protocol is hand-written JSON-RPC over stdio because only three methods are
|
|
17
|
+
* needed (initialize, tools/list, tools/call); the official SDK would add ~97
|
|
18
|
+
* transitive packages to every install, including the pi users who never run MCP.
|
|
19
|
+
*/
|
|
20
|
+
import { readFileSync } from "node:fs";
|
|
21
|
+
import { homedir } from "node:os";
|
|
22
|
+
import { join } from "node:path";
|
|
23
|
+
import { createInterface } from "node:readline";
|
|
24
|
+
|
|
25
|
+
/** Only the fields index.ts reads off its context. */
|
|
26
|
+
interface ExtensionContextLike {
|
|
27
|
+
cwd: string;
|
|
28
|
+
agentDir: string;
|
|
29
|
+
ui: { notify(): void; setStatus(): void; setWidget(): void };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface ToolResult {
|
|
33
|
+
content: Array<{ type: "text"; text: string }>;
|
|
34
|
+
isError?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The slice of a pi-registered tool this server forwards to. */
|
|
38
|
+
interface RegisteredTool {
|
|
39
|
+
description: string;
|
|
40
|
+
/** Typebox schema; JSON-serialized before it reaches the client. */
|
|
41
|
+
parameters: unknown;
|
|
42
|
+
execute(
|
|
43
|
+
toolCallId: string,
|
|
44
|
+
params: unknown,
|
|
45
|
+
signal: AbortSignal | undefined,
|
|
46
|
+
onUpdate: undefined,
|
|
47
|
+
ctx: ExtensionContextLike,
|
|
48
|
+
): Promise<ToolResult>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Only the members index.ts calls on the host API. */
|
|
52
|
+
interface StubPi {
|
|
53
|
+
cwd: string;
|
|
54
|
+
on(): void;
|
|
55
|
+
registerCommand(): void;
|
|
56
|
+
sendMessage(): void;
|
|
57
|
+
registerTool(tool: RegisteredTool): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const AGENT_DIR = process.env.XCODEBUG_AGENT_DIR ?? join(homedir(), ".xcodebug");
|
|
61
|
+
const PROTOCOL_VERSION = "2024-11-05";
|
|
62
|
+
|
|
63
|
+
// Reported to clients on initialize; read from the package so a version bump does
|
|
64
|
+
// not leave the handshake advertising the previous release.
|
|
65
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")) as {
|
|
66
|
+
version: string;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
let registered: RegisteredTool | undefined;
|
|
70
|
+
const stubPi: StubPi = {
|
|
71
|
+
cwd: process.cwd(),
|
|
72
|
+
on() {},
|
|
73
|
+
registerCommand() {},
|
|
74
|
+
// Nothing consumes pushed lines here: MCP has no channel that injects messages
|
|
75
|
+
// into the conversation, so push is refused below instead of silently dropped.
|
|
76
|
+
sendMessage() {},
|
|
77
|
+
registerTool(tool) {
|
|
78
|
+
registered = tool;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// Dynamic import because the extension is a plugin: it is loaded at run time,
|
|
83
|
+
// once the stub host exists, rather than imported for its side effects at start
|
|
84
|
+
// up. The cast is the adapter boundary: the extension declares the full
|
|
85
|
+
// ExtensionAPI, this server supplies only the members it reads.
|
|
86
|
+
const extensionModule = (await import("../index.ts")) as unknown as {
|
|
87
|
+
default(pi: StubPi): void;
|
|
88
|
+
};
|
|
89
|
+
extensionModule.default(stubPi);
|
|
90
|
+
if (!registered) {
|
|
91
|
+
console.error("xcodebug-mcp: the extension did not register a tool");
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
const tool = registered;
|
|
95
|
+
|
|
96
|
+
const ctx: ExtensionContextLike = {
|
|
97
|
+
cwd: process.cwd(),
|
|
98
|
+
agentDir: AGENT_DIR,
|
|
99
|
+
ui: { notify() {}, setStatus() {}, setWidget() {} },
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Typebox attaches symbol metadata; JSON round-tripping leaves a plain schema.
|
|
103
|
+
const inputSchema = JSON.parse(JSON.stringify(tool.parameters)) as Record<string, unknown>;
|
|
104
|
+
|
|
105
|
+
function reply(id: unknown, result: unknown): void {
|
|
106
|
+
process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id, result })}\n`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function replyError(id: unknown, code: number, message: string): void {
|
|
110
|
+
process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id, error: { code, message } })}\n`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function callTool(args: Record<string, unknown>, signal: AbortSignal): Promise<ToolResult> {
|
|
114
|
+
// `defaultPushMode` defaults to "context", but this transport cannot deliver
|
|
115
|
+
// pushed lines. Refusing an explicit request beats accepting it and going quiet.
|
|
116
|
+
if (args.push !== undefined && args.push !== "off") {
|
|
117
|
+
return {
|
|
118
|
+
content: [{ type: "text", text: 'MCP has no push channel; use push=off and read with action=logs.' }],
|
|
119
|
+
isError: true,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return tool.execute("mcp", { ...args, push: "off" }, signal, undefined, ctx);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function handle(message: Record<string, unknown>): Promise<void> {
|
|
126
|
+
const id = message.id;
|
|
127
|
+
const method = message.method;
|
|
128
|
+
// Notifications carry no id and must never be answered.
|
|
129
|
+
if (id === undefined) return;
|
|
130
|
+
|
|
131
|
+
if (method === "initialize") {
|
|
132
|
+
const requested = (message.params as { protocolVersion?: string } | undefined)?.protocolVersion;
|
|
133
|
+
reply(id, {
|
|
134
|
+
protocolVersion: requested ?? PROTOCOL_VERSION,
|
|
135
|
+
capabilities: { tools: {} },
|
|
136
|
+
serverInfo: { name: "xcodebug", version: pkg.version },
|
|
137
|
+
});
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (method === "ping") {
|
|
141
|
+
reply(id, {});
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (method === "tools/list") {
|
|
145
|
+
reply(id, { tools: [{ name: "xcodebug", description: tool.description, inputSchema }] });
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (method === "tools/call") {
|
|
149
|
+
const params = (message.params ?? {}) as { name?: string; arguments?: Record<string, unknown> };
|
|
150
|
+
if (params.name !== "xcodebug") {
|
|
151
|
+
replyError(id, -32602, `Unknown tool "${params.name ?? ""}"`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
// Registered before the call starts so a cancellation that arrives while it
|
|
155
|
+
// runs finds the controller; `finally` keeps the map from growing.
|
|
156
|
+
const controller = new AbortController();
|
|
157
|
+
inflight.set(id, controller);
|
|
158
|
+
try {
|
|
159
|
+
reply(id, await callTool(params.arguments ?? {}, controller.signal));
|
|
160
|
+
} catch (error) {
|
|
161
|
+
reply(id, { content: [{ type: "text", text: (error as Error).message }], isError: true });
|
|
162
|
+
} finally {
|
|
163
|
+
inflight.delete(id);
|
|
164
|
+
}
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
replyError(id, -32601, `Unknown method "${String(method)}"`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Requests are serialized: a capture's state is a plain map, and two overlapping
|
|
171
|
+
// calls (launch + stop) would interleave mutations on it.
|
|
172
|
+
let queue: Promise<void> = Promise.resolve();
|
|
173
|
+
/** Calls running right now, so a cancellation can reach the one it names. */
|
|
174
|
+
const inflight = new Map<unknown, AbortController>();
|
|
175
|
+
createInterface({ input: process.stdin }).on("line", (line) => {
|
|
176
|
+
const text = line.trim();
|
|
177
|
+
if (text.length === 0) return;
|
|
178
|
+
let message: Record<string, unknown>;
|
|
179
|
+
try {
|
|
180
|
+
message = JSON.parse(text) as Record<string, unknown>;
|
|
181
|
+
} catch {
|
|
182
|
+
replyError(null, -32700, "Parse error");
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
// Handled here rather than in handle(): the queue serializes calls, so a
|
|
186
|
+
// cancellation queued behind the very call it targets could never arrive in
|
|
187
|
+
// time. It is a notification, so nothing is sent back.
|
|
188
|
+
if (message.method === "notifications/cancelled") {
|
|
189
|
+
const requestId = (message.params as { requestId?: unknown } | undefined)?.requestId;
|
|
190
|
+
inflight.get(requestId)?.abort();
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
queue = queue.then(() => handle(message)).catch((error: unknown) => {
|
|
194
|
+
console.error("xcodebug-mcp:", error);
|
|
195
|
+
});
|
|
196
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xcodebug",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "Build, install and launch an iOS app on a physical device and read its console inside the agent session (pi / omp extension)",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"pi-package",
|
|
@@ -26,6 +27,7 @@
|
|
|
26
27
|
},
|
|
27
28
|
"files": [
|
|
28
29
|
"*.ts",
|
|
30
|
+
"mcp/",
|
|
29
31
|
"README.md",
|
|
30
32
|
"GUIDE.md",
|
|
31
33
|
"LICENSE"
|
package/xcode.ts
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
9
9
|
import { dirname, join } from "node:path";
|
|
10
|
-
import { asRecord, asString, lastLines, runCommand, timestamp } from "./exec";
|
|
11
|
-
import { BuildProgressTracker, formatProgress } from "./progress";
|
|
12
|
-
import type { DeviceInfo } from "./devices";
|
|
10
|
+
import { asRecord, asString, lastLines, runCommand, timestamp } from "./exec.ts";
|
|
11
|
+
import { BuildProgressTracker, formatProgress } from "./progress.ts";
|
|
12
|
+
import type { DeviceInfo } from "./devices.ts";
|
|
13
13
|
|
|
14
14
|
export interface BuildTarget {
|
|
15
15
|
workspace?: string;
|
|
@@ -105,7 +105,12 @@ export async function buildForDevice(
|
|
|
105
105
|
},
|
|
106
106
|
});
|
|
107
107
|
clearInterval(heartbeat);
|
|
108
|
-
|
|
108
|
+
// Say so when the cap cut the head: a log that silently starts mid-build reads
|
|
109
|
+
// like a truncated file the user has to explain away.
|
|
110
|
+
const dropped = result.truncated
|
|
111
|
+
? "[xcodebug] output exceeded the size cap; the beginning was dropped and this log keeps the tail.\n\n"
|
|
112
|
+
: "";
|
|
113
|
+
await writeFile(logFile, `$ xcodebuild ${args.join(" ")}\n\n${dropped}${result.stdout}\n${result.stderr}`);
|
|
109
114
|
|
|
110
115
|
const durationMs = Date.now() - started;
|
|
111
116
|
if (result.code !== 0) {
|