|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import { AdamcpRouter } from "./router.js"; |
| 3 | +import { readFrames, writeFrame } from "./stdio.js"; |
| 4 | + |
| 5 | +export function parseArgs(argv = process.argv.slice(2), env = process.env) { |
| 6 | + const options = { |
| 7 | + url: env.ADAMCP_URL, |
| 8 | + server: env.ADAMCP_SERVER, |
| 9 | + discoveryDir: env.ADAMCP_DISCOVERY_DIR, |
| 10 | + stdioCommand: env.ADAMCP_STDIO_COMMAND |
| 11 | + }; |
| 12 | + |
| 13 | + for (let index = 0; index < argv.length; index += 1) { |
| 14 | + const arg = argv[index]; |
| 15 | + if (arg === "--url") { |
| 16 | + options.url = argv[++index]; |
| 17 | + } else if (arg === "--server") { |
| 18 | + options.server = argv[++index]; |
| 19 | + } else if (arg === "--discovery-dir") { |
| 20 | + options.discoveryDir = argv[++index]; |
| 21 | + } else if (arg === "--stdio-command") { |
| 22 | + options.stdioCommand = argv[++index]; |
| 23 | + } else if (arg === "--help" || arg === "-h") { |
| 24 | + options.help = true; |
| 25 | + } else { |
| 26 | + throw new Error(`Unknown argument: ${arg}`); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return options; |
| 31 | +} |
| 32 | + |
| 33 | +export async function main(argv = process.argv.slice(2), streams = process) { |
| 34 | + const options = parseArgs(argv); |
| 35 | + if (options.help) { |
| 36 | + streams.stdout.write(helpText()); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (options.stdioCommand) { |
| 41 | + await runChildStdioProxy(options.stdioCommand, streams); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const router = new AdamcpRouter(options); |
| 46 | + const shutdown = async () => { |
| 47 | + await router.close(); |
| 48 | + }; |
| 49 | + process.once("beforeExit", shutdown); |
| 50 | + process.once("SIGINT", async () => { |
| 51 | + await shutdown(); |
| 52 | + process.exit(130); |
| 53 | + }); |
| 54 | + process.once("SIGTERM", async () => { |
| 55 | + await shutdown(); |
| 56 | + process.exit(143); |
| 57 | + }); |
| 58 | + |
| 59 | + for await (const frame of readFrames(streams.stdin)) { |
| 60 | + let request; |
| 61 | + try { |
| 62 | + request = JSON.parse(frame.toString("utf8")); |
| 63 | + } catch { |
| 64 | + writeFrame(streams.stdout, { |
| 65 | + jsonrpc: "2.0", |
| 66 | + id: null, |
| 67 | + error: { code: -32700, message: "Parse error" } |
| 68 | + }); |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + const response = await router.handle(request); |
| 73 | + if (response) { |
| 74 | + writeFrame(streams.stdout, response); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + await shutdown(); |
| 79 | +} |
| 80 | + |
| 81 | +export async function runChildStdioProxy(command, streams = process) { |
| 82 | + const useProcessGroup = process.platform !== "win32"; |
| 83 | + const child = spawn(command, { |
| 84 | + shell: true, |
| 85 | + stdio: ["pipe", "pipe", "pipe"], |
| 86 | + detached: useProcessGroup |
| 87 | + }); |
| 88 | + |
| 89 | + streams.stdin.pipe(child.stdin); |
| 90 | + child.stdout.pipe(streams.stdout); |
| 91 | + child.stderr.pipe(streams.stderr); |
| 92 | + |
| 93 | + const terminateChild = () => { |
| 94 | + if (child.killed) { |
| 95 | + return; |
| 96 | + } |
| 97 | + try { |
| 98 | + if (useProcessGroup) { |
| 99 | + process.kill(-child.pid, "SIGTERM"); |
| 100 | + } else { |
| 101 | + child.kill("SIGTERM"); |
| 102 | + } |
| 103 | + } catch { |
| 104 | + // The process may have exited between the check and signal. |
| 105 | + } |
| 106 | + }; |
| 107 | + process.once("exit", terminateChild); |
| 108 | + |
| 109 | + await new Promise((resolve, reject) => { |
| 110 | + child.once("error", (error) => { |
| 111 | + process.off("exit", terminateChild); |
| 112 | + reject(error); |
| 113 | + }); |
| 114 | + child.once("exit", (code, signal) => { |
| 115 | + process.off("exit", terminateChild); |
| 116 | + if (signal) { |
| 117 | + resolve(); |
| 118 | + } else if (code === 0 || code === null) { |
| 119 | + resolve(); |
| 120 | + } else { |
| 121 | + reject(new Error(`stdio command exited with code ${code}`)); |
| 122 | + } |
| 123 | + }); |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +function helpText() { |
| 128 | + return `Usage: adamcp [--url URL] [--server ID_OR_NAME] [--discovery-dir DIR]\n` + |
| 129 | + ` adamcp --stdio-command COMMAND\n\n` + |
| 130 | + `Environment: ADAMCP_URL, ADAMCP_SERVER, ADAMCP_DISCOVERY_DIR, ADAMCP_STDIO_COMMAND\n`; |
| 131 | +} |
0 commit comments