@kodelyth/acpx 2026.5.39 → 2026.5.42

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.
Files changed (47) hide show
  1. package/AGENTS.md +54 -0
  2. package/CLAUDE.md +54 -0
  3. package/dist/index.js +14 -0
  4. package/dist/process-reaper-DdVqzAA_.js +370 -0
  5. package/dist/register.runtime.js +53 -0
  6. package/dist/runtime-D9qhNKmy.js +741 -0
  7. package/dist/runtime-api.js +4 -0
  8. package/dist/service-CXeUME_-.js +1483 -0
  9. package/dist/setup-api.js +16 -0
  10. package/index.test.ts +119 -0
  11. package/index.ts +19 -0
  12. package/klaw.plugin.json +12 -27
  13. package/package.json +2 -2
  14. package/register.runtime.test.ts +104 -0
  15. package/register.runtime.ts +86 -0
  16. package/runtime-api.ts +49 -0
  17. package/setup-api.ts +18 -0
  18. package/src/acpx-runtime-compat.d.ts +65 -0
  19. package/src/claude-agent-acp-completion.test.ts +187 -0
  20. package/src/codex-auth-bridge.test.ts +688 -0
  21. package/src/codex-auth-bridge.ts +780 -0
  22. package/src/codex-trust-config.ts +297 -0
  23. package/src/config-schema.ts +118 -0
  24. package/src/config.test.ts +285 -0
  25. package/src/config.ts +281 -0
  26. package/src/manifest.test.ts +21 -0
  27. package/src/process-lease.test.ts +89 -0
  28. package/src/process-lease.ts +179 -0
  29. package/src/process-reaper.test.ts +330 -0
  30. package/src/process-reaper.ts +434 -0
  31. package/src/runtime-internals/error-format.mjs +6 -0
  32. package/src/runtime-internals/mcp-command-line.mjs +123 -0
  33. package/src/runtime-internals/mcp-command-line.test.ts +59 -0
  34. package/src/runtime-internals/mcp-proxy.mjs +121 -0
  35. package/src/runtime-internals/mcp-proxy.test.ts +130 -0
  36. package/src/runtime.test.ts +1817 -0
  37. package/src/runtime.ts +1261 -0
  38. package/src/service.test.ts +802 -0
  39. package/src/service.ts +630 -0
  40. package/tsconfig.json +16 -0
  41. package/index.js +0 -7
  42. package/register.runtime.js +0 -7
  43. package/runtime-api.js +0 -7
  44. package/setup-api.js +0 -7
  45. /package/{error-format.mjs → dist/error-format.mjs} +0 -0
  46. /package/{mcp-command-line.mjs → dist/mcp-command-line.mjs} +0 -0
  47. /package/{mcp-proxy.mjs → dist/mcp-proxy.mjs} +0 -0
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { spawn } from "node:child_process";
4
+ import path from "node:path";
5
+ import { createInterface } from "node:readline";
6
+ import { pathToFileURL } from "node:url";
7
+ import { formatErrorMessage } from "./error-format.mjs";
8
+ import { splitCommandLine } from "./mcp-command-line.mjs";
9
+
10
+ function decodePayload(argv) {
11
+ const payloadIndex = argv.indexOf("--payload");
12
+ if (payloadIndex < 0) {
13
+ throw new Error("Missing --payload");
14
+ }
15
+ const encoded = argv[payloadIndex + 1];
16
+ if (!encoded) {
17
+ throw new Error("Missing MCP proxy payload value");
18
+ }
19
+ const parsed = JSON.parse(Buffer.from(encoded, "base64url").toString("utf8"));
20
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
21
+ throw new Error("Invalid MCP proxy payload");
22
+ }
23
+ if (typeof parsed.targetCommand !== "string" || parsed.targetCommand.trim() === "") {
24
+ throw new Error("MCP proxy payload missing targetCommand");
25
+ }
26
+ const mcpServers = Array.isArray(parsed.mcpServers) ? parsed.mcpServers : [];
27
+ return {
28
+ targetCommand: parsed.targetCommand,
29
+ mcpServers,
30
+ };
31
+ }
32
+
33
+ function shouldInject(method) {
34
+ return method === "session/new" || method === "session/load" || method === "session/fork";
35
+ }
36
+
37
+ function rewriteLine(line, mcpServers) {
38
+ if (!line.trim()) {
39
+ return line;
40
+ }
41
+ try {
42
+ const parsed = JSON.parse(line);
43
+ if (
44
+ !parsed ||
45
+ typeof parsed !== "object" ||
46
+ Array.isArray(parsed) ||
47
+ !shouldInject(parsed.method) ||
48
+ !parsed.params ||
49
+ typeof parsed.params !== "object" ||
50
+ Array.isArray(parsed.params)
51
+ ) {
52
+ return line;
53
+ }
54
+ const next = {
55
+ ...parsed,
56
+ params: {
57
+ ...parsed.params,
58
+ mcpServers,
59
+ },
60
+ };
61
+ return JSON.stringify(next);
62
+ } catch {
63
+ return line;
64
+ }
65
+ }
66
+
67
+ export function createTargetSpawnOptions(platform = process.platform) {
68
+ const options = {
69
+ stdio: ["pipe", "pipe", "inherit"],
70
+ env: process.env,
71
+ };
72
+ if (platform === "win32") {
73
+ options.windowsHide = true;
74
+ }
75
+ return options;
76
+ }
77
+
78
+ function isMainModule() {
79
+ const mainPath = process.argv[1];
80
+ if (!mainPath) {
81
+ return false;
82
+ }
83
+ return import.meta.url === pathToFileURL(path.resolve(mainPath)).href;
84
+ }
85
+
86
+ function main() {
87
+ const { targetCommand, mcpServers } = decodePayload(process.argv.slice(2));
88
+ const target = splitCommandLine(targetCommand);
89
+ const child = spawn(target.command, target.args, createTargetSpawnOptions());
90
+
91
+ if (!child.stdin || !child.stdout) {
92
+ throw new Error("Failed to create MCP proxy stdio pipes");
93
+ }
94
+
95
+ const input = createInterface({ input: process.stdin });
96
+ input.on("line", (line) => {
97
+ child.stdin.write(`${rewriteLine(line, mcpServers)}\n`);
98
+ });
99
+ input.on("close", () => {
100
+ child.stdin.end();
101
+ });
102
+
103
+ child.stdout.pipe(process.stdout);
104
+
105
+ child.on("error", (error) => {
106
+ process.stderr.write(`${formatErrorMessage(error)}\n`);
107
+ process.exit(1);
108
+ });
109
+
110
+ child.on("close", (code, signal) => {
111
+ if (signal) {
112
+ process.kill(process.pid, signal);
113
+ return;
114
+ }
115
+ process.exit(code ?? 0);
116
+ });
117
+ }
118
+
119
+ if (isMainModule()) {
120
+ main();
121
+ }
@@ -0,0 +1,130 @@
1
+ import { spawn } from "node:child_process";
2
+ import { chmod, mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import { pathToFileURL } from "node:url";
6
+ import { bundledPluginFile } from "klaw/plugin-sdk/test-fixtures";
7
+ import { afterEach, describe, expect, it } from "vitest";
8
+
9
+ const tempDirs: string[] = [];
10
+ const proxyPath = path.resolve(bundledPluginFile("acpx", "src/runtime-internals/mcp-proxy.mjs"));
11
+
12
+ async function makeTempScript(name: string, content: string): Promise<string> {
13
+ const dir = await mkdtemp(path.join(os.tmpdir(), "klaw-acpx-mcp-proxy-"));
14
+ tempDirs.push(dir);
15
+ const scriptPath = path.join(dir, name);
16
+ await writeFile(scriptPath, content, "utf8");
17
+ await chmod(scriptPath, 0o755);
18
+ return scriptPath;
19
+ }
20
+
21
+ afterEach(async () => {
22
+ while (tempDirs.length > 0) {
23
+ const dir = tempDirs.pop();
24
+ if (!dir) {
25
+ continue;
26
+ }
27
+ await rm(dir, { recursive: true, force: true });
28
+ }
29
+ });
30
+
31
+ describe("mcp-proxy", () => {
32
+ it("hides the target MCP process window on Windows only", async () => {
33
+ const moduleUrl = pathToFileURL(proxyPath).href;
34
+ const { createTargetSpawnOptions } = (await import(moduleUrl)) as {
35
+ createTargetSpawnOptions: (platform?: NodeJS.Platform) => Record<string, unknown>;
36
+ };
37
+
38
+ expect(createTargetSpawnOptions("win32")).toEqual({
39
+ env: process.env,
40
+ stdio: ["pipe", "pipe", "inherit"],
41
+ windowsHide: true,
42
+ });
43
+ expect(createTargetSpawnOptions("darwin")).not.toHaveProperty("windowsHide");
44
+ expect(createTargetSpawnOptions("linux")).not.toHaveProperty("windowsHide");
45
+ });
46
+
47
+ it("injects configured MCP servers into ACP session bootstrap requests", async () => {
48
+ const echoServerPath = await makeTempScript(
49
+ "echo-server.cjs",
50
+ String.raw`#!/usr/bin/env node
51
+ const { createInterface } = require("node:readline");
52
+ const rl = createInterface({ input: process.stdin });
53
+ rl.on("line", (line) => process.stdout.write(line + "\n"));
54
+ `,
55
+ );
56
+
57
+ const payload = Buffer.from(
58
+ JSON.stringify({
59
+ targetCommand: `${process.execPath} ${echoServerPath}`,
60
+ mcpServers: [
61
+ {
62
+ name: "canva",
63
+ command: "npx",
64
+ args: ["-y", "mcp-remote@latest", "https://mcp.canva.com/mcp"],
65
+ env: [{ name: "CANVA_TOKEN", value: "secret" }],
66
+ },
67
+ ],
68
+ }),
69
+ "utf8",
70
+ ).toString("base64url");
71
+
72
+ const child = spawn(process.execPath, [proxyPath, "--payload", payload], {
73
+ stdio: ["pipe", "pipe", "inherit"],
74
+ cwd: process.cwd(),
75
+ });
76
+
77
+ let stdout = "";
78
+ child.stdout.on("data", (chunk) => {
79
+ stdout += String(chunk);
80
+ });
81
+
82
+ child.stdin.write(
83
+ `${JSON.stringify({
84
+ jsonrpc: "2.0",
85
+ id: 1,
86
+ method: "session/new",
87
+ params: { cwd: process.cwd(), mcpServers: [] },
88
+ })}\n`,
89
+ );
90
+ child.stdin.write(
91
+ `${JSON.stringify({
92
+ jsonrpc: "2.0",
93
+ id: 2,
94
+ method: "session/load",
95
+ params: { cwd: process.cwd(), sessionId: "sid-1", mcpServers: [] },
96
+ })}\n`,
97
+ );
98
+ child.stdin.write(
99
+ `${JSON.stringify({
100
+ jsonrpc: "2.0",
101
+ id: 3,
102
+ method: "session/prompt",
103
+ params: { sessionId: "sid-1", prompt: [{ type: "text", text: "hello" }] },
104
+ })}\n`,
105
+ );
106
+ child.stdin.end();
107
+
108
+ const exitCode = await new Promise<number | null>((resolve) => {
109
+ child.once("close", (code) => resolve(code));
110
+ });
111
+
112
+ expect(exitCode).toBe(0);
113
+ const lines = stdout
114
+ .trim()
115
+ .split(/\r?\n/)
116
+ .map((line) => JSON.parse(line) as { method: string; params: Record<string, unknown> });
117
+
118
+ expect(lines[0].params.mcpServers).toEqual([
119
+ {
120
+ name: "canva",
121
+ command: "npx",
122
+ args: ["-y", "mcp-remote@latest", "https://mcp.canva.com/mcp"],
123
+ env: [{ name: "CANVA_TOKEN", value: "secret" }],
124
+ },
125
+ ]);
126
+ expect(lines[1].params.mcpServers).toEqual(lines[0].params.mcpServers);
127
+ expect(lines[2].method).toBe("session/prompt");
128
+ expect(lines[2].params.mcpServers).toBeUndefined();
129
+ });
130
+ });