|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * @license |
| 5 | + * Copyright 2026 Google LLC |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +import {createConnection} from 'node:net'; |
| 10 | +import process from 'node:process'; |
| 11 | + |
| 12 | +import yargs, {type Options, type PositionalOptions} from 'yargs'; |
| 13 | +import {hideBin} from 'yargs/helpers'; |
| 14 | + |
| 15 | +import { |
| 16 | + getSocketPath, |
| 17 | + isDaemonRunning, |
| 18 | + startDaemon, |
| 19 | + stopDaemon, |
| 20 | +} from './daemonClient.js'; |
| 21 | +import {commands} from './cliDefinitions.js'; |
| 22 | + |
| 23 | +async function sendToDaemon(request: unknown): Promise<any> { |
| 24 | + const socketPath = await getSocketPath(); |
| 25 | + return new Promise((resolve, reject) => { |
| 26 | + const client = createConnection({path: socketPath}, () => { |
| 27 | + client.write(JSON.stringify(request) + '\0'); |
| 28 | + }); |
| 29 | + |
| 30 | + let buffer = ''; |
| 31 | + client.on('data', data => { |
| 32 | + buffer += data.toString(); |
| 33 | + if (buffer.endsWith('\0')) { |
| 34 | + try { |
| 35 | + const response = JSON.parse(buffer.slice(0, -1)); |
| 36 | + client.end(); |
| 37 | + resolve(response); |
| 38 | + } catch (e) { |
| 39 | + reject(e); |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + client.on('error', err => { |
| 45 | + reject(err); |
| 46 | + }); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +const y = yargs(hideBin(process.argv)) |
| 51 | + .scriptName('chrome-devtools') |
| 52 | + .help() |
| 53 | + .showHelpOnFail(true) |
| 54 | + .demandCommand() |
| 55 | + .strict(); |
| 56 | + |
| 57 | +y.command( |
| 58 | + 'start', |
| 59 | + 'Starts or restarts the daemon process', |
| 60 | + y => y.help(false), // Disable help for start command to avoid parsing issues with passed args |
| 61 | + async () => { |
| 62 | + if (await isDaemonRunning()) { |
| 63 | + await stopDaemon(); |
| 64 | + } |
| 65 | + // Extract args after 'start' |
| 66 | + const startIndex = process.argv.indexOf('start'); |
| 67 | + const args = startIndex !== -1 ? process.argv.slice(startIndex + 1) : []; |
| 68 | + await startDaemon(args); |
| 69 | + }, |
| 70 | +); |
| 71 | + |
| 72 | +y.command( |
| 73 | + 'status', |
| 74 | + 'Checks if the MCP server process is running', |
| 75 | + () => {}, |
| 76 | + async () => { |
| 77 | + if (await isDaemonRunning()) { |
| 78 | + console.log('Daemon is running'); |
| 79 | + } else { |
| 80 | + console.log('Daemon is not running'); |
| 81 | + } |
| 82 | + }, |
| 83 | +); |
| 84 | + |
| 85 | +y.command( |
| 86 | + 'stop', |
| 87 | + 'Stop the running MCP server if any', |
| 88 | + () => {}, |
| 89 | + async () => { |
| 90 | + await stopDaemon(); |
| 91 | + }, |
| 92 | +); |
| 93 | + |
| 94 | +for (const [commandName, commandDef] of Object.entries(commands)) { |
| 95 | + const args = commandDef.args; |
| 96 | + const requiredArgNames = Object.keys(args).filter( |
| 97 | + name => args[name].required, |
| 98 | + ); |
| 99 | + |
| 100 | + let commandStr = commandName; |
| 101 | + for (const arg of requiredArgNames) { |
| 102 | + commandStr += ` <${arg}>`; |
| 103 | + } |
| 104 | + |
| 105 | + y.command( |
| 106 | + commandStr, |
| 107 | + commandDef.description, |
| 108 | + y => { |
| 109 | + for (const [argName, opt] of Object.entries(args)) { |
| 110 | + const type = |
| 111 | + opt.type === 'integer' || opt.type === 'number' |
| 112 | + ? 'number' |
| 113 | + : opt.type === 'boolean' |
| 114 | + ? 'boolean' |
| 115 | + : opt.type === 'array' |
| 116 | + ? 'array' |
| 117 | + : 'string'; |
| 118 | + |
| 119 | + if (opt.required) { |
| 120 | + const options: PositionalOptions = { |
| 121 | + describe: opt.description, |
| 122 | + type: type as PositionalOptions['type'], |
| 123 | + }; |
| 124 | + if (opt.default !== undefined) { |
| 125 | + options.default = opt.default; |
| 126 | + } |
| 127 | + if (opt.enum) { |
| 128 | + options.choices = opt.enum as Array<string | number>; |
| 129 | + } |
| 130 | + y.positional(argName, options); |
| 131 | + } else { |
| 132 | + const options: Options = { |
| 133 | + describe: opt.description, |
| 134 | + type: type as Options['type'], |
| 135 | + }; |
| 136 | + if (opt.default !== undefined) { |
| 137 | + options.default = opt.default; |
| 138 | + } |
| 139 | + if (opt.enum) { |
| 140 | + options.choices = opt.enum as Array<string | number>; |
| 141 | + } |
| 142 | + y.option(argName, options); |
| 143 | + } |
| 144 | + } |
| 145 | + }, |
| 146 | + async argv => { |
| 147 | + try { |
| 148 | + if (!(await isDaemonRunning())) { |
| 149 | + await startDaemon(['--via-cli']); |
| 150 | + } |
| 151 | + |
| 152 | + const commandArgs: Record<string, unknown> = {}; |
| 153 | + for (const argName of Object.keys(args)) { |
| 154 | + if (argName in argv) { |
| 155 | + commandArgs[argName] = argv[argName]; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + const response = await sendToDaemon({ |
| 160 | + method: 'invoke_tool', |
| 161 | + tool: commandName, |
| 162 | + args: commandArgs, |
| 163 | + }); |
| 164 | + |
| 165 | + if (response.success) { |
| 166 | + console.log(response.result); |
| 167 | + } else { |
| 168 | + console.error('Error:', response.error); |
| 169 | + process.exit(1); |
| 170 | + } |
| 171 | + } catch (error) { |
| 172 | + console.error('Failed to execute command:', error); |
| 173 | + process.exit(1); |
| 174 | + } |
| 175 | + }, |
| 176 | + ); |
| 177 | +} |
| 178 | + |
| 179 | +await y.parse(); |
0 commit comments