49 lines
1.8 KiB
JavaScript
49 lines
1.8 KiB
JavaScript
#!/usr/bin/env node
|
|
// Adds the opencode Browser MCP server and its usage instructions to
|
|
// opencode's global config, keeping everything else in the file as is.
|
|
// Usage: configure-opencode.mjs <node-binary> <mcp-server.js> <instructions.md>
|
|
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
const [nodeBin, serverPath, instructionsPath] = process.argv.slice(2);
|
|
if (!nodeBin || !serverPath || !instructionsPath) {
|
|
console.error("usage: configure-opencode.mjs <node> <mcp-server.js> <instructions.md>");
|
|
process.exit(1);
|
|
}
|
|
|
|
const dir = process.env.OPENCODE_CONFIG_DIR || path.join(os.homedir(), ".config", "opencode");
|
|
const file = path.join(dir, "opencode.json");
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
|
|
let config = { $schema: "https://opencode.ai/config.json" };
|
|
if (fs.existsSync(file)) {
|
|
const raw = fs.readFileSync(file, "utf-8");
|
|
try {
|
|
config = JSON.parse(raw);
|
|
} catch {
|
|
console.error(`${file} is not plain JSON (comments?); add this by hand:`);
|
|
console.error(JSON.stringify({ mcp: { chrome: { type: "local", command: [nodeBin, serverPath], timeout: 150000 } }, instructions: [instructionsPath] }, null, 2));
|
|
process.exit(1);
|
|
}
|
|
const backup = `${file}.bak-${new Date().toISOString().replace(/[:.]/g, "-")}`;
|
|
fs.writeFileSync(backup, raw);
|
|
console.log(`Backed up ${file} -> ${backup}`);
|
|
}
|
|
|
|
config.mcp ??= {};
|
|
config.mcp.chrome = {
|
|
type: "local",
|
|
command: [nodeBin, serverPath],
|
|
enabled: true,
|
|
// switch_browser waits up to 2 minutes for the user to click Connect.
|
|
timeout: 150000
|
|
};
|
|
const instr = new Set(config.instructions || []);
|
|
instr.add(instructionsPath);
|
|
config.instructions = [...instr];
|
|
|
|
fs.writeFileSync(file, JSON.stringify(config, null, 2) + "\n");
|
|
console.log(`Added the "chrome" MCP server and browser instructions to ${file}`);
|