@benjilabs/beaconfire-mcp 0.1.0 → 0.1.2

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 (2) hide show
  1. package/dist/setup.js +130 -46
  2. package/package.json +2 -1
package/dist/setup.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
2
2
  import { homedir } from "node:os";
3
3
  import { dirname, join, resolve } from "node:path";
4
+ import { createInterface } from "node:readline";
4
5
  import { fileURLToPath } from "node:url";
5
6
  import { parseArgs } from "node:util";
6
7
  import { generateSecretKey, getPublicKey } from "nostr-tools/pure";
@@ -17,12 +18,57 @@ export class SetupError extends Error {
17
18
  this.name = "SetupError";
18
19
  }
19
20
  }
21
+ function promptToken() {
22
+ return new Promise((resolve) => {
23
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
24
+ process.stdout.write("Enrollment token: ");
25
+ rl._writeToOutput = () => { };
26
+ rl.question("", (answer) => {
27
+ process.stdout.write("\n");
28
+ rl.close();
29
+ resolve(answer.trim());
30
+ });
31
+ });
32
+ }
20
33
  function die(msg) {
21
34
  throw new SetupError(msg);
22
35
  }
23
36
  function validName(name) {
24
37
  return NAME_PATTERN.test(name) && !RESERVED.has(name);
25
38
  }
39
+ function tomlEntry(serverPath, configPath) {
40
+ return [
41
+ "[mcp_servers.beaconfire]",
42
+ `command = "node"`,
43
+ `args = ${JSON.stringify([serverPath])}`,
44
+ `env = { BEACONFIRE_CONFIG = ${JSON.stringify(configPath)} }`,
45
+ `default_tools_approval_mode = "approve"`,
46
+ "",
47
+ ].join("\n");
48
+ }
49
+ /**
50
+ * Merge a beaconfire TOML entry into existing TOML text. Returns the merged string, or null if the
51
+ * beaconfire section already exists and force is false.
52
+ */
53
+ export function mergeToml(existing, entry, force) {
54
+ const lines = existing.split("\n");
55
+ const start = lines.findIndex((l) => l.trim() === "[mcp_servers.beaconfire]");
56
+ if (start === -1) {
57
+ const trimmed = existing.trimEnd();
58
+ return (trimmed ? trimmed + "\n\n" : "") + entry;
59
+ }
60
+ if (!force)
61
+ return null;
62
+ let end = lines.length;
63
+ for (let i = start + 1; i < lines.length; i++) {
64
+ const l = (lines[i] ?? "").trim();
65
+ if (l.startsWith("[") && !l.startsWith("[mcp_servers.beaconfire")) {
66
+ end = i;
67
+ break;
68
+ }
69
+ }
70
+ return [...lines.slice(0, start), entry.trimEnd(), "", ...lines.slice(end)].join("\n");
71
+ }
26
72
  /**
27
73
  * Runs `beaconfire-mcp setup`. Throws `SetupError` on invalid input; other throws are unexpected.
28
74
  *
@@ -43,6 +89,7 @@ export async function runSetup(argv, opts) {
43
89
  "state-dir": { type: "string" },
44
90
  project: { type: "string" },
45
91
  session: { type: "string" },
92
+ codex: { type: "boolean", default: false },
46
93
  force: { type: "boolean", default: false },
47
94
  },
48
95
  });
@@ -55,17 +102,23 @@ export async function runSetup(argv, opts) {
55
102
  const hasRelay = values.relay !== undefined;
56
103
  // --token wins over the env var; both are checked before anything is written.
57
104
  // Empty string from either source is treated as unset.
58
- const token = (values.token || undefined) ?? (process.env.BEACONFIRE_TOKEN || undefined);
105
+ let token = (values.token || undefined) ?? (process.env.BEACONFIRE_TOKEN || undefined);
59
106
  const hasToken = token !== undefined;
60
107
  if (hasSite && hasRelay) {
61
108
  die("--site and --relay cannot both be given; use --site + --token for private, --relay for public-only");
62
109
  }
63
- if (hasSite && !hasToken)
64
- die("a non-empty token is required; use --token or BEACONFIRE_TOKEN");
65
- if (values.token !== undefined && !hasSite)
66
- die("--token requires --site");
110
+ if (hasToken && !hasSite)
111
+ die("a token was given but --site is missing; use --site for private mode");
67
112
  if (!hasSite && !hasRelay)
68
113
  die("one of --site + --token (private) or --relay (public-only) is required");
114
+ if (hasSite && !hasToken) {
115
+ const canPrompt = opts?.readTokenImpl != null || !!process.stdin.isTTY;
116
+ if (!canPrompt)
117
+ die("a non-empty token is required; use --token or BEACONFIRE_TOKEN");
118
+ token = await (opts?.readTokenImpl ?? promptToken)();
119
+ if (!token)
120
+ die("enrollment token is required");
121
+ }
69
122
  const isPrivate = hasSite;
70
123
  let siteOrigin;
71
124
  if (isPrivate) {
@@ -102,35 +155,47 @@ export async function runSetup(argv, opts) {
102
155
  die(`config ${configPath} already exists; use --force to overwrite`);
103
156
  }
104
157
  let mcpExisting = {};
158
+ let tomlExisting = "";
105
159
  if (values.project !== undefined) {
106
160
  const projectDir = resolve(values.project);
107
161
  if (!existsSync(projectDir) || !statSync(projectDir).isDirectory()) {
108
162
  die(`--project: directory ${projectDir} does not exist`);
109
163
  }
110
- const mcpPath = join(projectDir, ".mcp.json");
111
- if (existsSync(mcpPath)) {
112
- let parsed;
113
- try {
114
- parsed = JSON.parse(readFileSync(mcpPath, "utf8"));
115
- }
116
- catch {
117
- die(`${mcpPath} is not valid JSON`);
164
+ if (values.codex) {
165
+ const tomlPath = join(projectDir, ".codex", "config.toml");
166
+ if (existsSync(tomlPath)) {
167
+ tomlExisting = readFileSync(tomlPath, "utf8");
168
+ if (tomlExisting.includes("[mcp_servers.beaconfire]") && !values.force) {
169
+ die(`${tomlPath} already has a beaconfire entry; use --force to replace it`);
170
+ }
118
171
  }
119
- if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
120
- die(`${mcpPath} is not a JSON object`);
121
- }
122
- const obj = parsed;
123
- if (obj.mcpServers !== undefined &&
124
- (typeof obj.mcpServers !== "object" ||
125
- obj.mcpServers === null ||
126
- Array.isArray(obj.mcpServers))) {
127
- die(`${mcpPath}: mcpServers is not an object`);
128
- }
129
- const servers = obj.mcpServers;
130
- if (servers?.beaconfire !== undefined && !values.force) {
131
- die(`${mcpPath} already has a beaconfire entry; use --force to replace it`);
172
+ }
173
+ else {
174
+ const mcpPath = join(projectDir, ".mcp.json");
175
+ if (existsSync(mcpPath)) {
176
+ let parsed;
177
+ try {
178
+ parsed = JSON.parse(readFileSync(mcpPath, "utf8"));
179
+ }
180
+ catch {
181
+ die(`${mcpPath} is not valid JSON`);
182
+ }
183
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
184
+ die(`${mcpPath} is not a JSON object`);
185
+ }
186
+ const obj = parsed;
187
+ if (obj.mcpServers !== undefined &&
188
+ (typeof obj.mcpServers !== "object" ||
189
+ obj.mcpServers === null ||
190
+ Array.isArray(obj.mcpServers))) {
191
+ die(`${mcpPath}: mcpServers is not an object`);
192
+ }
193
+ const servers = obj.mcpServers;
194
+ if (servers?.beaconfire !== undefined && !values.force) {
195
+ die(`${mcpPath} already has a beaconfire entry; use --force to replace it`);
196
+ }
197
+ mcpExisting = obj;
132
198
  }
133
- mcpExisting = obj;
134
199
  }
135
200
  }
136
201
  // ── set up directories ───────────────────────────────────────────────────────
@@ -186,27 +251,46 @@ export async function runSetup(argv, opts) {
186
251
  // validate the config we just wrote
187
252
  loadConfig(configPath);
188
253
  const serverPath = opts?.serverPath ?? join(dirname(fileURLToPath(import.meta.url)), "index.js");
189
- const mcpEntry = {
190
- command: "node",
191
- args: [serverPath],
192
- env: { BEACONFIRE_CONFIG: configPath },
193
- };
194
- if (values.project !== undefined) {
195
- const projectDir = resolve(values.project);
196
- const mcpPath = join(projectDir, ".mcp.json");
197
- const updated = {
198
- ...mcpExisting,
199
- mcpServers: {
200
- ...mcpExisting.mcpServers,
201
- beaconfire: mcpEntry,
202
- },
203
- };
204
- writeFileSync(mcpPath, `${JSON.stringify(updated, null, 2)}\n`);
205
- console.log(`wrote ${mcpPath}`);
254
+ if (values.codex) {
255
+ const entry = tomlEntry(serverPath, configPath);
256
+ if (values.project !== undefined) {
257
+ const projectDir = resolve(values.project);
258
+ const tomlPath = join(projectDir, ".codex", "config.toml");
259
+ mkdirSync(dirname(tomlPath), { recursive: true });
260
+ const merged = mergeToml(tomlExisting, entry, values.force ?? false);
261
+ if (merged === null)
262
+ die(`${tomlPath} already has a beaconfire entry; use --force to replace it`);
263
+ writeFileSync(tomlPath, merged.trimEnd() + "\n");
264
+ console.log(`wrote ${tomlPath}`);
265
+ }
266
+ else {
267
+ console.log("\nAdd to your project's .codex/config.toml:");
268
+ console.log(entry);
269
+ }
206
270
  }
207
271
  else {
208
- console.log("\nAdd to your project's .mcp.json:");
209
- console.log(JSON.stringify({ mcpServers: { beaconfire: mcpEntry } }, null, 2));
272
+ const mcpEntry = {
273
+ command: "node",
274
+ args: [serverPath],
275
+ env: { BEACONFIRE_CONFIG: configPath },
276
+ };
277
+ if (values.project !== undefined) {
278
+ const projectDir = resolve(values.project);
279
+ const mcpPath = join(projectDir, ".mcp.json");
280
+ const updated = {
281
+ ...mcpExisting,
282
+ mcpServers: {
283
+ ...mcpExisting.mcpServers,
284
+ beaconfire: mcpEntry,
285
+ },
286
+ };
287
+ writeFileSync(mcpPath, `${JSON.stringify(updated, null, 2)}\n`);
288
+ console.log(`wrote ${mcpPath}`);
289
+ }
290
+ else {
291
+ console.log("\nAdd to your project's .mcp.json:");
292
+ console.log(JSON.stringify({ mcpServers: { beaconfire: mcpEntry } }, null, 2));
293
+ }
210
294
  }
211
295
  if (isPrivate) {
212
296
  console.log(`\nDone. Send messages to this agent with to: "${name}"`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@benjilabs/beaconfire-mcp",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "engines": { "node": ">=22.18" },
6
6
  "main": "dist/index.js",
@@ -8,6 +8,7 @@
8
8
  "bin": { "beaconfire-mcp": "dist/index.js" },
9
9
  "scripts": {
10
10
  "build": "tsc -p tsconfig.build.json",
11
+ "prepublishOnly": "npm run build",
11
12
  "typecheck": "tsc --noEmit",
12
13
  "lint": "biome check src test scripts",
13
14
  "test": "node --test test/*.test.ts",