nimblebrain 0.2.1 → 0.3.0
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.
- package/README.md +2 -0
- package/dist/nimblebrain.js +250 -52
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,6 +28,8 @@ later.
|
|
|
28
28
|
| `update` | Move to the newest release, keeping data |
|
|
29
29
|
| `uninstall` | Remove it and its data |
|
|
30
30
|
| `telemetry [on\|off\|status]` | Whether this install sends anonymous usage data |
|
|
31
|
+
| `mcp add <url>` | Add an MCP server you run on this machine to a workspace (`--name`, `--workspace`) |
|
|
32
|
+
| `mcp list` / `mcp remove <name>` | The servers in a workspace, or remove one |
|
|
31
33
|
|
|
32
34
|
`--json` makes every command answer one JSON object, `{"ok": true, ...}` or
|
|
33
35
|
`{"ok": false, "error": "<CODE>", "fix": "..."}`, for scripts and coding agents.
|
package/dist/nimblebrain.js
CHANGED
|
@@ -8,7 +8,7 @@ import { spawn } from "node:child_process";
|
|
|
8
8
|
import { chmod, mkdir, readFile, rm, rmdir, writeFile } from "node:fs/promises";
|
|
9
9
|
import { createServer } from "node:net";
|
|
10
10
|
import { homedir } from "node:os";
|
|
11
|
-
import { dirname, join } from "node:path";
|
|
11
|
+
import { dirname, join, resolve } from "node:path";
|
|
12
12
|
function realDeps() {
|
|
13
13
|
return {
|
|
14
14
|
exec(cmd, args, opts = {}) {
|
|
@@ -78,7 +78,8 @@ function realDeps() {
|
|
|
78
78
|
`);
|
|
79
79
|
},
|
|
80
80
|
env: process.env,
|
|
81
|
-
home: homedir()
|
|
81
|
+
home: homedir(),
|
|
82
|
+
cwd: process.cwd()
|
|
82
83
|
};
|
|
83
84
|
}
|
|
84
85
|
async function request(url, init, timeoutMs) {
|
|
@@ -151,7 +152,7 @@ function typeHidden(value, chunk) {
|
|
|
151
152
|
return { value: v };
|
|
152
153
|
}
|
|
153
154
|
function installHome(deps, flag) {
|
|
154
|
-
return flag ?? deps.env.NIMBLEBRAIN_HOME ?? join(deps.home, ".nimblebrain-local");
|
|
155
|
+
return resolve(deps.cwd, flag ?? deps.env.NIMBLEBRAIN_HOME ?? join(deps.home, ".nimblebrain-local"));
|
|
155
156
|
}
|
|
156
157
|
|
|
157
158
|
// src/args.ts
|
|
@@ -168,7 +169,7 @@ var ERRORS = {
|
|
|
168
169
|
LOW_MEMORY: "Give Docker at least 2 GB of memory in its settings, then run this again.",
|
|
169
170
|
PORT_IN_USE: "Stop whatever holds the port, or pass --port with a free one.",
|
|
170
171
|
NOT_INSTALLED: "Run `npx nimblebrain` to install it.",
|
|
171
|
-
NOT_OURS: "Another install holds this name or its data. Remove it yourself, or choose
|
|
172
|
+
NOT_OURS: "Another install holds this name or its data. Remove it yourself, or choose a --home whose folder has another name.",
|
|
172
173
|
NETWORK: "Check the network connection, then run the same command again.",
|
|
173
174
|
MODEL_KEY_MISSING: "Run `npx nimblebrain key` in your own terminal to enter a model key.",
|
|
174
175
|
HEALTH_TIMEOUT: "Run `npx nimblebrain logs` and look at the last error line.",
|
|
@@ -183,7 +184,17 @@ var ERRORS = {
|
|
|
183
184
|
LOCAL_MODEL_MISSING: "Download the model with `ollama pull <model>`, or check that Ollama has disk space, then run this again.",
|
|
184
185
|
LOCAL_MODEL_NO_TOOLS: "Choose a model with tool calling, such as gemma4:e4b; https://docs.nimblebrain.ai/config/local-models/ lists the ones run with NimbleBrain.",
|
|
185
186
|
LOCAL_CONTEXT_SMALL: "Run `npx nimblebrain local-model` to prepare the model again with a 32k context window.",
|
|
186
|
-
INTERRUPTED: "Run `npx nimblebrain status` to see what is in place, then run the same command again."
|
|
187
|
+
INTERRUPTED: "Run `npx nimblebrain status` to see what is in place, then run the same command again.",
|
|
188
|
+
UPDATE_REQUIRED: "Run `npx nimblebrain update`; this install's NimbleBrain cannot reach servers on this machine yet.",
|
|
189
|
+
WORKSPACE_NOT_FOUND: "Run `npx nimblebrain status` for the workspace ids, then pass one with --workspace.",
|
|
190
|
+
SERVER_NOT_FOUND: "Run `npx nimblebrain mcp list` for the server names in this workspace.",
|
|
191
|
+
DIRECT_URL_NOT_ALLOWED: "Adding a server by URL needs a local install made by `npx nimblebrain`; run `npx nimblebrain update`, or add the server to the instance's connector catalog.",
|
|
192
|
+
URL_NOT_LOOPBACK: "Run the server on this machine and pass its localhost, 127.0.0.1 or [::1] URL, such as http://127.0.0.1:8000/mcp.",
|
|
193
|
+
URL_INVALID: "Pass the server's full MCP URL, such as http://127.0.0.1:8000/mcp.",
|
|
194
|
+
INVALID_SERVER_NAME: "Pass another name with --name: lowercase letters, digits and hyphens.",
|
|
195
|
+
NAME_TAKEN: "Pass another name with --name, or remove the existing one with `npx nimblebrain mcp remove <serverName>`.",
|
|
196
|
+
NAME_IS_CATALOG_ENTRY: "Pass another name with --name; this one belongs to a catalog connector.",
|
|
197
|
+
REQUEST_FAILED: "Run `npx nimblebrain logs` and look at the last error line, then run this again."
|
|
187
198
|
};
|
|
188
199
|
|
|
189
200
|
class CliError extends Error {
|
|
@@ -218,6 +229,7 @@ var COMMANDS = [
|
|
|
218
229
|
"update",
|
|
219
230
|
"uninstall",
|
|
220
231
|
"telemetry",
|
|
232
|
+
"mcp",
|
|
221
233
|
"local-model"
|
|
222
234
|
];
|
|
223
235
|
var AGENTS = ["claude-code", "cursor", "codex", "none"];
|
|
@@ -232,6 +244,8 @@ var options = {
|
|
|
232
244
|
"local-model": { type: "string" },
|
|
233
245
|
runtime: { type: "string" },
|
|
234
246
|
telemetry: { type: "string" },
|
|
247
|
+
name: { type: "string" },
|
|
248
|
+
workspace: { type: "string" },
|
|
235
249
|
help: { type: "boolean", short: "h" },
|
|
236
250
|
version: { type: "boolean", short: "v" }
|
|
237
251
|
};
|
|
@@ -259,7 +273,9 @@ function parse(argv) {
|
|
|
259
273
|
provider: values.provider,
|
|
260
274
|
localModel: values["local-model"],
|
|
261
275
|
runtime: values.runtime,
|
|
262
|
-
telemetry
|
|
276
|
+
telemetry,
|
|
277
|
+
name: values.name,
|
|
278
|
+
workspace: values.workspace
|
|
263
279
|
};
|
|
264
280
|
if (values.help)
|
|
265
281
|
return { kind: "help", flags };
|
|
@@ -268,24 +284,45 @@ function parse(argv) {
|
|
|
268
284
|
const name = positionals[0] ?? "init";
|
|
269
285
|
if (name === "help")
|
|
270
286
|
return { kind: "help", flags };
|
|
271
|
-
return {
|
|
287
|
+
return {
|
|
288
|
+
kind: "run",
|
|
289
|
+
command: command(name, positionals),
|
|
290
|
+
flags,
|
|
291
|
+
arg: positionals[1],
|
|
292
|
+
operand: positionals[2]
|
|
293
|
+
};
|
|
272
294
|
}
|
|
273
295
|
function telemetryFlag(value) {
|
|
274
296
|
if (value === undefined || value === "on" || value === "off")
|
|
275
297
|
return value;
|
|
276
298
|
throw new CliError("USAGE", `--telemetry must be on or off, not "${value}".`);
|
|
277
299
|
}
|
|
300
|
+
var MCP_SUBCOMMANDS = { add: "url", list: null, remove: "serverName" };
|
|
278
301
|
function command(name, positionals) {
|
|
302
|
+
if (!COMMANDS.includes(name))
|
|
303
|
+
throw new CliError("USAGE", `Unknown command "${name}".`);
|
|
304
|
+
if (name === "mcp")
|
|
305
|
+
return mcpCommand(positionals);
|
|
279
306
|
const maxArgs = name === "telemetry" || name === "local-model" ? 2 : 1;
|
|
280
307
|
if (positionals.length > maxArgs)
|
|
281
308
|
throw new CliError("USAGE", `Unexpected argument "${positionals[maxArgs]}".`);
|
|
282
|
-
if (!COMMANDS.includes(name))
|
|
283
|
-
throw new CliError("USAGE", `Unknown command "${name}".`);
|
|
284
309
|
const arg = positionals[1];
|
|
285
310
|
if (name === "telemetry" && arg !== undefined && !["on", "off", "status"].includes(arg))
|
|
286
311
|
throw new CliError("USAGE", `telemetry takes on, off or status, not "${arg}".`);
|
|
287
312
|
return name;
|
|
288
313
|
}
|
|
314
|
+
function mcpCommand(positionals) {
|
|
315
|
+
const sub = positionals[1];
|
|
316
|
+
if (sub === undefined || !Object.hasOwn(MCP_SUBCOMMANDS, sub))
|
|
317
|
+
throw new CliError("USAGE", `mcp takes add <url>, list or remove <serverName>${sub === undefined ? "" : `, not "${sub}"`}.`);
|
|
318
|
+
const operand = MCP_SUBCOMMANDS[sub];
|
|
319
|
+
const maxArgs = operand === null ? 2 : 3;
|
|
320
|
+
if (positionals.length > maxArgs)
|
|
321
|
+
throw new CliError("USAGE", `Unexpected argument "${positionals[maxArgs]}".`);
|
|
322
|
+
if (operand !== null && positionals[2] === undefined)
|
|
323
|
+
throw new CliError("USAGE", `mcp ${sub} needs a ${operand}.`);
|
|
324
|
+
return "mcp";
|
|
325
|
+
}
|
|
289
326
|
function parseStrict(argv) {
|
|
290
327
|
try {
|
|
291
328
|
return parseArgs({ args: argv, options, allowPositionals: true, strict: true });
|
|
@@ -298,9 +335,15 @@ function wantsJson(argv) {
|
|
|
298
335
|
}
|
|
299
336
|
|
|
300
337
|
// src/compose.ts
|
|
301
|
-
import { join as join2 } from "node:path";
|
|
302
|
-
var
|
|
303
|
-
|
|
338
|
+
import { basename, join as join2 } from "node:path";
|
|
339
|
+
var DEFAULT_PROJECT = "nimblebrain-local";
|
|
340
|
+
function projectFor(home) {
|
|
341
|
+
const slug = basename(home).toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^[^a-z0-9]+/, "").replace(/[^a-z0-9]+$/, "");
|
|
342
|
+
if (!slug)
|
|
343
|
+
return "nimblebrain";
|
|
344
|
+
return slug.startsWith("nimblebrain") ? slug : `nimblebrain-${slug}`;
|
|
345
|
+
}
|
|
346
|
+
var volumeOf = (project) => `${project}_workspace`;
|
|
304
347
|
var RAW = "https://raw.githubusercontent.com/NimbleBrainInc/nimblebrain";
|
|
305
348
|
async function fetchCompose(deps, tag) {
|
|
306
349
|
const res = await deps.getText(`${RAW}/${tag}/docker-compose.yml`, 15000);
|
|
@@ -325,6 +368,9 @@ function keyVars(composeText) {
|
|
|
325
368
|
function asksConsent(composeText) {
|
|
326
369
|
return composeText.includes("NB_TELEMETRY_CONSENT");
|
|
327
370
|
}
|
|
371
|
+
function hostLoopback(composeText) {
|
|
372
|
+
return composeText.includes("NB_HOST_LOOPBACK");
|
|
373
|
+
}
|
|
328
374
|
function providers(composeText) {
|
|
329
375
|
return Object.fromEntries(keyVars(composeText).map((v) => [
|
|
330
376
|
v.replace(/_API_KEY$/, "").replace(/_GENERATIVE_AI$/, "").toLowerCase(),
|
|
@@ -829,7 +875,7 @@ var toLines = (events) => `${events.map((e) => JSON.stringify(e)).join(`
|
|
|
829
875
|
`)}
|
|
830
876
|
`;
|
|
831
877
|
function parseAnswer(answer) {
|
|
832
|
-
return /^
|
|
878
|
+
return /^n/i.test(answer.trim()) ? "off" : "on";
|
|
833
879
|
}
|
|
834
880
|
function seededConsent(envText) {
|
|
835
881
|
const seed = envText.match(/^NB_TELEMETRY_CONSENT=(on|off)\s*$/m)?.[1];
|
|
@@ -888,7 +934,7 @@ async function setRuntimeTelemetry(deps, url, consent) {
|
|
|
888
934
|
// package.json
|
|
889
935
|
var package_default = {
|
|
890
936
|
name: "nimblebrain",
|
|
891
|
-
version: "0.
|
|
937
|
+
version: "0.3.0",
|
|
892
938
|
description: "Install, run and connect NimbleBrain, the open-source runtime for agents and MCP apps, on this machine.",
|
|
893
939
|
license: "Apache-2.0",
|
|
894
940
|
type: "module",
|
|
@@ -935,10 +981,10 @@ async function status(deps, home) {
|
|
|
935
981
|
if (!install)
|
|
936
982
|
throw new CliError("NOT_INSTALLED", `No NimbleBrain install in ${home}.`);
|
|
937
983
|
const url = `http://127.0.0.1:${install.port}`;
|
|
938
|
-
const
|
|
939
|
-
const up = health?.status === 200 && health.body?.status === "ok";
|
|
984
|
+
const up = await isUp(deps, url);
|
|
940
985
|
const workspaces = up ? await readWorkspaces(deps, url) : [];
|
|
941
|
-
const
|
|
986
|
+
const composeText = await deps.readText(join5(home, "docker-compose.yml")) ?? "";
|
|
987
|
+
const vars = keyVars(composeText);
|
|
942
988
|
const envText = await deps.readText(join5(home, ".env")) ?? "";
|
|
943
989
|
const modelKey = hasKey(envText, vars) ? "set" : "missing";
|
|
944
990
|
return {
|
|
@@ -953,10 +999,15 @@ async function status(deps, home) {
|
|
|
953
999
|
skill: install.agent ? await skillInstalled(deps, install.agent) : null
|
|
954
1000
|
},
|
|
955
1001
|
model_key: modelKey,
|
|
1002
|
+
host_loopback: hostLoopback(composeText),
|
|
956
1003
|
local_model: localModelOf(envText) ?? null,
|
|
957
1004
|
telemetry: await currentTelemetry(deps, home, url)
|
|
958
1005
|
};
|
|
959
1006
|
}
|
|
1007
|
+
async function isUp(deps, url) {
|
|
1008
|
+
const health = await deps.getJson(`${url}/v1/health`);
|
|
1009
|
+
return health?.status === 200 && health.body?.status === "ok";
|
|
1010
|
+
}
|
|
960
1011
|
async function readWorkspaces(deps, url) {
|
|
961
1012
|
const boot = await deps.getJson(`${url}/v1/bootstrap`);
|
|
962
1013
|
if (boot?.status !== 200)
|
|
@@ -973,20 +1024,20 @@ async function init(ctx) {
|
|
|
973
1024
|
const { deps, flags, home } = ctx;
|
|
974
1025
|
if (await readInstall(deps, home))
|
|
975
1026
|
return { ...await status(deps, home), already: true };
|
|
976
|
-
const
|
|
1027
|
+
const consent = await chooseConsent(ctx);
|
|
977
1028
|
const rec = recorder2(deps, ctx.version, await reportId(deps, home));
|
|
978
1029
|
const begun = Date.now();
|
|
979
1030
|
rec.record("cli.init_started", { agent: flags.agent ?? null, interactive: deps.interactive });
|
|
980
1031
|
try {
|
|
981
|
-
return await install(ctx,
|
|
1032
|
+
return await install(ctx, consent, rec, begun);
|
|
982
1033
|
} catch (err) {
|
|
983
1034
|
if (!(err instanceof CliError))
|
|
984
1035
|
throw err;
|
|
985
|
-
if (err.code !== "CANCELLED" ||
|
|
986
|
-
await settleFailure(rec, home,
|
|
1036
|
+
if (err.code !== "CANCELLED" || consent !== "undecided")
|
|
1037
|
+
await settleFailure(rec, home, consent, err);
|
|
987
1038
|
throw new CliError(err.code, err.message, {
|
|
988
1039
|
...err.detail,
|
|
989
|
-
telemetry: field(
|
|
1040
|
+
telemetry: field(consent, lockedHere(deps))
|
|
990
1041
|
});
|
|
991
1042
|
}
|
|
992
1043
|
}
|
|
@@ -997,25 +1048,25 @@ async function settleFailure(rec, home, consent, err) {
|
|
|
997
1048
|
}
|
|
998
1049
|
await rec.settle(home, consent);
|
|
999
1050
|
}
|
|
1000
|
-
async function chooseConsent(ctx
|
|
1051
|
+
async function chooseConsent(ctx) {
|
|
1001
1052
|
if (lockedHere(ctx.deps))
|
|
1002
1053
|
return "off";
|
|
1003
1054
|
if (ctx.flags.telemetry)
|
|
1004
1055
|
return ctx.flags.telemetry;
|
|
1005
|
-
if (!
|
|
1056
|
+
if (!asks(ctx))
|
|
1006
1057
|
return "undecided";
|
|
1007
|
-
|
|
1058
|
+
const consent = parseAnswer(await ctx.deps.prompt(`${QUESTION} [Y/n] `));
|
|
1059
|
+
ctx.deps.say(consent === "on" ? "Sending anonymous usage data. Turn it off any time: npx nimblebrain telemetry off" : "No usage data will be sent. Turn it on any time: npx nimblebrain telemetry on");
|
|
1060
|
+
return consent;
|
|
1008
1061
|
}
|
|
1009
|
-
async function install(ctx,
|
|
1062
|
+
async function install(ctx, consent, rec, begun) {
|
|
1010
1063
|
const { deps, flags, home } = ctx;
|
|
1011
1064
|
await doctor(deps, flags.port);
|
|
1012
|
-
await guardOurs(deps, home);
|
|
1065
|
+
const project = await guardOurs(deps, home);
|
|
1013
1066
|
const tag = await runtimeTag(deps, flags.runtime);
|
|
1014
1067
|
const composeText = await fetchCompose(deps, tag);
|
|
1015
1068
|
await confirmStart(ctx, tag);
|
|
1016
|
-
|
|
1017
|
-
const { consent } = answer;
|
|
1018
|
-
await writeInstallFiles(deps, home, tag, flags.port, composeText, envValues(deps, rec.id, consent, asksConsent(composeText)));
|
|
1069
|
+
await writeInstallFiles(deps, home, project, tag, flags.port, composeText, envValues(deps, rec.id, consent, asksConsent(composeText)));
|
|
1019
1070
|
const envText = await deps.readText(paths(home).env) ?? "";
|
|
1020
1071
|
const hasModel = hasKey(envText, keyVars(composeText)) || localModelOf(envText) !== undefined;
|
|
1021
1072
|
const local = flags.localModel ?? (!hasModel && asks(ctx) && takesLocalModel(composeText) ? await chooseModelSource(deps) : undefined);
|
|
@@ -1028,7 +1079,7 @@ async function install(ctx, answer, rec, begun) {
|
|
|
1028
1079
|
const state = {
|
|
1029
1080
|
version: tag.replace(/^v/, ""),
|
|
1030
1081
|
port: flags.port,
|
|
1031
|
-
project
|
|
1082
|
+
project
|
|
1032
1083
|
};
|
|
1033
1084
|
await writeState(deps, home, state);
|
|
1034
1085
|
const manual = await connectChosenAgent(ctx, state);
|
|
@@ -1111,7 +1162,7 @@ async function update(ctx) {
|
|
|
1111
1162
|
throw new CliError("USAGE", `${tag} is older than the installed ${from}; NimbleBrain does not move backwards.`);
|
|
1112
1163
|
}
|
|
1113
1164
|
const composeText = await fetchCompose(deps, tag);
|
|
1114
|
-
await writeInstallFiles(deps, home, tag, install.port, composeText);
|
|
1165
|
+
await writeInstallFiles(deps, home, install.project, tag, install.port, composeText);
|
|
1115
1166
|
deps.say(`Updating NimbleBrain ${from} → ${tag}.`);
|
|
1116
1167
|
const pull = await compose(deps, home, ["pull"], START_TIMEOUT_MS);
|
|
1117
1168
|
if (pull.code !== 0)
|
|
@@ -1245,13 +1296,13 @@ async function waitHealthy(deps, port) {
|
|
|
1245
1296
|
}
|
|
1246
1297
|
throw new CliError("HEALTH_TIMEOUT", "NimbleBrain did not report healthy within 3 minutes.");
|
|
1247
1298
|
}
|
|
1248
|
-
async function writeInstallFiles(deps, home, tag, port, composeText, extra = {}) {
|
|
1299
|
+
async function writeInstallFiles(deps, home, project, tag, port, composeText, extra = {}) {
|
|
1249
1300
|
const p = paths(home);
|
|
1250
1301
|
await deps.writeText(p.compose, composeText);
|
|
1251
1302
|
if (await deps.readText(p.instance) === undefined)
|
|
1252
1303
|
await deps.writeText(p.instance, INSTANCE_JSON);
|
|
1253
1304
|
const env = setEnvLines(await deps.readText(p.env) ?? "", {
|
|
1254
|
-
COMPOSE_PROJECT_NAME:
|
|
1305
|
+
COMPOSE_PROJECT_NAME: project,
|
|
1255
1306
|
NIMBLEBRAIN_TAG: tag,
|
|
1256
1307
|
NIMBLEBRAIN_WEB_PORT: String(port),
|
|
1257
1308
|
...extra
|
|
@@ -1269,22 +1320,146 @@ async function requireInstall({ deps, home }) {
|
|
|
1269
1320
|
return install;
|
|
1270
1321
|
}
|
|
1271
1322
|
async function guardOurs(deps, home) {
|
|
1323
|
+
const project = projectFor(home);
|
|
1272
1324
|
const env = await deps.readText(paths(home).env);
|
|
1273
|
-
|
|
1274
|
-
|
|
1325
|
+
const marked = env?.match(/^COMPOSE_PROJECT_NAME=(.*?)\r?$/m)?.[1];
|
|
1326
|
+
if (marked === project || marked === DEFAULT_PROJECT)
|
|
1327
|
+
return marked;
|
|
1275
1328
|
for (const file of Object.values(paths(home))) {
|
|
1276
1329
|
if (await deps.readText(file) !== undefined) {
|
|
1277
1330
|
throw new CliError("NOT_OURS", `${file} already exists and this CLI did not write it.`);
|
|
1278
1331
|
}
|
|
1279
1332
|
}
|
|
1280
1333
|
const projects = await deps.exec("docker", ["compose", "ls", "-a", "--format", "json"]);
|
|
1281
|
-
const taken = projects.code === 0 &&
|
|
1282
|
-
const volumes = await deps.exec("docker", [
|
|
1334
|
+
const taken = projects.code === 0 && listsProject(projects.stdout, project);
|
|
1335
|
+
const volumes = await deps.exec("docker", [
|
|
1336
|
+
"volume",
|
|
1337
|
+
"ls",
|
|
1338
|
+
"-q",
|
|
1339
|
+
"--filter",
|
|
1340
|
+
`name=^${volumeOf(project)}$`
|
|
1341
|
+
]);
|
|
1283
1342
|
if (taken || volumes.stdout.trim() !== "") {
|
|
1284
|
-
throw new CliError("NOT_OURS", `Docker already has a "${
|
|
1343
|
+
throw new CliError("NOT_OURS", `Docker already has a "${project}" project or volume that this install did not make.`);
|
|
1344
|
+
}
|
|
1345
|
+
return project;
|
|
1346
|
+
}
|
|
1347
|
+
function listsProject(stdout, project) {
|
|
1348
|
+
try {
|
|
1349
|
+
const list = JSON.parse(stdout);
|
|
1350
|
+
return Array.isArray(list) && list.some((p) => p?.Name === project);
|
|
1351
|
+
} catch {
|
|
1352
|
+
return false;
|
|
1285
1353
|
}
|
|
1286
1354
|
}
|
|
1287
1355
|
|
|
1356
|
+
// src/mcp.ts
|
|
1357
|
+
async function mcp(ctx) {
|
|
1358
|
+
const sub = ctx.arg;
|
|
1359
|
+
const target = await reach(ctx, sub === "add");
|
|
1360
|
+
if (sub === "add")
|
|
1361
|
+
return add(target, ctx.operand ?? "", ctx.flags.name);
|
|
1362
|
+
if (sub === "list")
|
|
1363
|
+
return list(target);
|
|
1364
|
+
return remove(target, ctx.operand ?? "");
|
|
1365
|
+
}
|
|
1366
|
+
var RUNTIME_CODES = [
|
|
1367
|
+
"DIRECT_URL_NOT_ALLOWED",
|
|
1368
|
+
"URL_NOT_LOOPBACK",
|
|
1369
|
+
"URL_INVALID",
|
|
1370
|
+
"INVALID_SERVER_NAME",
|
|
1371
|
+
"NAME_TAKEN",
|
|
1372
|
+
"NAME_IS_CATALOG_ENTRY"
|
|
1373
|
+
];
|
|
1374
|
+
var ADD_TIMEOUT_MS = 60000;
|
|
1375
|
+
var CALL_TIMEOUT_MS = 15000;
|
|
1376
|
+
async function reach(ctx, adding) {
|
|
1377
|
+
const { deps, home, flags } = ctx;
|
|
1378
|
+
const install = await readInstall(deps, home);
|
|
1379
|
+
if (!install)
|
|
1380
|
+
throw new CliError("NOT_INSTALLED", `No NimbleBrain install in ${home}.`);
|
|
1381
|
+
if (adding && !hostLoopback(await deps.readText(paths(home).compose) ?? "")) {
|
|
1382
|
+
throw new CliError("UPDATE_REQUIRED", `NimbleBrain ${install.version} cannot reach a server on this machine.`, { version: install.version });
|
|
1383
|
+
}
|
|
1384
|
+
const url = `http://127.0.0.1:${install.port}`;
|
|
1385
|
+
if (!await isUp(deps, url))
|
|
1386
|
+
throw new CliError("NOT_RUNNING", "NimbleBrain is not running.");
|
|
1387
|
+
const workspaces = await readWorkspaces(deps, url);
|
|
1388
|
+
const ws = flags.workspace ? workspaces.find((w) => w.id === flags.workspace) : workspaces[0];
|
|
1389
|
+
if (!ws) {
|
|
1390
|
+
throw new CliError("WORKSPACE_NOT_FOUND", flags.workspace ? `No workspace "${flags.workspace}" on this install.` : "This install has no workspace.", { workspaces: workspaces.map((w) => w.id) });
|
|
1391
|
+
}
|
|
1392
|
+
return { ctx, url, wsId: ws.id };
|
|
1393
|
+
}
|
|
1394
|
+
async function add(t, serverUrl, name) {
|
|
1395
|
+
const protocol = URL.canParse(serverUrl) ? new URL(serverUrl).protocol : "";
|
|
1396
|
+
if (protocol !== "http:" && protocol !== "https:") {
|
|
1397
|
+
throw new CliError("URL_INVALID", `"${serverUrl}" is not an http or https URL.`);
|
|
1398
|
+
}
|
|
1399
|
+
const install = { kind: "direct-url", url: serverUrl, ...name ? { serverName: name } : {} };
|
|
1400
|
+
const s = await call(t, {
|
|
1401
|
+
action: "install",
|
|
1402
|
+
entry: { id: "direct-url", name: name ?? serverUrl, install }
|
|
1403
|
+
}, ADD_TIMEOUT_MS);
|
|
1404
|
+
return {
|
|
1405
|
+
ok: true,
|
|
1406
|
+
serverName: s.serverName,
|
|
1407
|
+
url: s.url,
|
|
1408
|
+
dialed: s.dialed,
|
|
1409
|
+
placements: s.placements,
|
|
1410
|
+
wsId: s.wsId ?? t.wsId,
|
|
1411
|
+
...s.warning ? { warning: s.warning } : {},
|
|
1412
|
+
...s.notice ? { notice: s.notice } : {}
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1415
|
+
async function list(t) {
|
|
1416
|
+
const s = await call(t, { action: "list_installed", scope: "workspace" }, CALL_TIMEOUT_MS);
|
|
1417
|
+
const installed = Array.isArray(s.installed) ? s.installed : [];
|
|
1418
|
+
const servers = installed.map((e) => ({
|
|
1419
|
+
serverName: e.serverName,
|
|
1420
|
+
displayName: e.displayName,
|
|
1421
|
+
url: e.url,
|
|
1422
|
+
state: e.state,
|
|
1423
|
+
status: e.status,
|
|
1424
|
+
statusReason: e.statusReason,
|
|
1425
|
+
toolCount: e.toolCount
|
|
1426
|
+
}));
|
|
1427
|
+
return { ok: true, wsId: t.wsId, servers };
|
|
1428
|
+
}
|
|
1429
|
+
async function remove(t, serverName) {
|
|
1430
|
+
const listed = await call(t, { action: "list_installed", scope: "workspace" }, CALL_TIMEOUT_MS);
|
|
1431
|
+
const installed = Array.isArray(listed.installed) ? listed.installed : [];
|
|
1432
|
+
if (!installed.some((e) => e.serverName === serverName)) {
|
|
1433
|
+
throw new CliError("SERVER_NOT_FOUND", `This workspace has no server named "${serverName}".`, {
|
|
1434
|
+
wsId: t.wsId
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1437
|
+
await call(t, { action: "uninstall", serverName }, CALL_TIMEOUT_MS);
|
|
1438
|
+
return { ok: true, serverName, wsId: t.wsId };
|
|
1439
|
+
}
|
|
1440
|
+
async function call(t, args, timeoutMs) {
|
|
1441
|
+
const res = await t.ctx.deps.postJson(`${t.url}/v1/workspaces/${encodeURIComponent(t.wsId)}/tools/call`, { server: "nb", tool: "manage_connectors", arguments: args }, timeoutMs);
|
|
1442
|
+
if (!res)
|
|
1443
|
+
throw new CliError("NOT_RUNNING", "NimbleBrain did not answer.");
|
|
1444
|
+
const body = res.body ?? {};
|
|
1445
|
+
if (res.status === 404 && body.error === "workspace_error") {
|
|
1446
|
+
throw new CliError("WORKSPACE_NOT_FOUND", `No workspace "${t.wsId}" on this install.`);
|
|
1447
|
+
}
|
|
1448
|
+
if (res.status < 200 || res.status >= 300) {
|
|
1449
|
+
const why = typeof body.message === "string" ? body.message : `HTTP ${res.status}`;
|
|
1450
|
+
throw new CliError("REQUEST_FAILED", `NimbleBrain refused the request: ${why}`, {
|
|
1451
|
+
status: res.status
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1454
|
+
const text = body.content?.find((c) => c.type === "text")?.text;
|
|
1455
|
+
if (body.isError) {
|
|
1456
|
+
const code = body.structuredContent?.error;
|
|
1457
|
+
const known = RUNTIME_CODES.find((c) => c === code);
|
|
1458
|
+
throw new CliError(known ?? "REQUEST_FAILED", text ?? "NimbleBrain refused the request.");
|
|
1459
|
+
}
|
|
1460
|
+
return body.structuredContent ?? {};
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1288
1463
|
// src/main.ts
|
|
1289
1464
|
var HELP = `nimblebrain ${VERSION2}: install and run NimbleBrain on this machine
|
|
1290
1465
|
|
|
@@ -1301,17 +1476,23 @@ Commands:
|
|
|
1301
1476
|
logs Recent runtime log lines
|
|
1302
1477
|
update Move to the newest release, keeping data
|
|
1303
1478
|
uninstall Remove NimbleBrain and its data
|
|
1479
|
+
mcp add <url> Add an MCP server you run on this machine to a workspace
|
|
1480
|
+
mcp list The servers in a workspace
|
|
1481
|
+
mcp remove <name> Remove a server from a workspace
|
|
1304
1482
|
|
|
1305
1483
|
Flags:
|
|
1306
1484
|
--json One JSON object on stdout
|
|
1307
1485
|
--yes, -y Take every default; implied when no terminal is attached
|
|
1308
1486
|
--agent <name> claude-code, cursor, codex or none
|
|
1309
1487
|
--port <n> Local port (default 27246)
|
|
1310
|
-
--home <dir> Where the install lives (default ~/.nimblebrain-local, or NIMBLEBRAIN_HOME)
|
|
1488
|
+
--home <dir> Where the install lives (default ~/.nimblebrain-local, or NIMBLEBRAIN_HOME);
|
|
1489
|
+
a home with another folder name is a separate install
|
|
1311
1490
|
--provider <name> The model provider for key (default anthropic)
|
|
1312
1491
|
--local-model <model> init: run on this Ollama model instead of a hosted key
|
|
1313
1492
|
--runtime <tag> The NimbleBrain release to install or update to (default the newest)
|
|
1314
1493
|
--telemetry <on|off> The person's answer to sending anonymous usage data; without it, init asks or leaves it undecided
|
|
1494
|
+
--name <name> For mcp add: the server name (default from the server's card, else its URL)
|
|
1495
|
+
--workspace <id> For mcp: the workspace (default the one init connected, the first in status)
|
|
1315
1496
|
--help, --version
|
|
1316
1497
|
`;
|
|
1317
1498
|
async function run(argv, deps) {
|
|
@@ -1328,7 +1509,7 @@ async function run(argv, deps) {
|
|
|
1328
1509
|
return parsed.flags.json ? render({ ok: true, version: VERSION2 }, true) : { stdout: `${VERSION2}
|
|
1329
1510
|
`, code: 0 };
|
|
1330
1511
|
}
|
|
1331
|
-
const { command, flags, arg } = parsed;
|
|
1512
|
+
const { command, flags, arg, operand } = parsed;
|
|
1332
1513
|
const home = installHome(deps, flags.home);
|
|
1333
1514
|
let result;
|
|
1334
1515
|
try {
|
|
@@ -1343,7 +1524,7 @@ async function run(argv, deps) {
|
|
|
1343
1524
|
result = { ...await status(deps, home) };
|
|
1344
1525
|
break;
|
|
1345
1526
|
default: {
|
|
1346
|
-
const ctx = { deps, flags, home, version: VERSION2, arg };
|
|
1527
|
+
const ctx = { deps, flags, home, version: VERSION2, arg, operand };
|
|
1347
1528
|
const commands = {
|
|
1348
1529
|
init,
|
|
1349
1530
|
up,
|
|
@@ -1353,7 +1534,8 @@ async function run(argv, deps) {
|
|
|
1353
1534
|
uninstall,
|
|
1354
1535
|
key,
|
|
1355
1536
|
telemetry,
|
|
1356
|
-
"local-model": localModel
|
|
1537
|
+
"local-model": localModel,
|
|
1538
|
+
mcp
|
|
1357
1539
|
};
|
|
1358
1540
|
result = await commands[command](ctx);
|
|
1359
1541
|
}
|
|
@@ -1422,14 +1604,9 @@ function statusLines(r, command) {
|
|
|
1422
1604
|
r.local_model ? ` model: ${r.local_model} on Ollama (NimbleBrain's own agents use it)` : ` model key: ${r.model_key === "set" ? "set (NimbleBrain's own agents use it)" : "missing (run npx nimblebrain key)"}`
|
|
1423
1605
|
];
|
|
1424
1606
|
const agent = r.agent.connected;
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
lines.push(
|
|
1428
|
-
if (!r.agent.skill)
|
|
1429
|
-
lines.push(` skill: not installed; add it with npx skills add nimblebraininc/skills --skill nimblebrain --agent ${agent} -g`);
|
|
1430
|
-
} else if (r.manual) {
|
|
1431
|
-
lines.push(` agent: not connected; add it by hand: ${r.manual}`);
|
|
1432
|
-
}
|
|
1607
|
+
lines.push(...agentLines(r));
|
|
1608
|
+
if (!r.host_loopback)
|
|
1609
|
+
lines.push(" servers on this machine: not reachable until npx nimblebrain update");
|
|
1433
1610
|
lines.push(telemetryLine(r.telemetry));
|
|
1434
1611
|
lines.push(` CLI ${r.cli_version}`);
|
|
1435
1612
|
if (command === "init" && !r.already && r.health === "ok") {
|
|
@@ -1438,6 +1615,18 @@ function statusLines(r, command) {
|
|
|
1438
1615
|
}
|
|
1439
1616
|
return lines;
|
|
1440
1617
|
}
|
|
1618
|
+
function agentLines(r) {
|
|
1619
|
+
const agent = r.agent.connected;
|
|
1620
|
+
if (!agent)
|
|
1621
|
+
return r.manual ? [` agent: not connected; add it by hand: ${r.manual}`] : [];
|
|
1622
|
+
const name = AGENT_NAMES[agent] ?? agent;
|
|
1623
|
+
const lines = [
|
|
1624
|
+
` ${name} can use NimbleBrain's tools (it loads them when its next session starts)`
|
|
1625
|
+
];
|
|
1626
|
+
if (!r.agent.skill)
|
|
1627
|
+
lines.push(` skill: not installed; add it with npx skills add nimblebraininc/skills --skill nimblebrain --agent ${agent} -g`);
|
|
1628
|
+
return lines;
|
|
1629
|
+
}
|
|
1441
1630
|
function changeLines(r) {
|
|
1442
1631
|
if (r.already)
|
|
1443
1632
|
return [" already installed"];
|
|
@@ -1453,13 +1642,22 @@ function genericLines(result) {
|
|
|
1453
1642
|
function field2(key, value) {
|
|
1454
1643
|
if (value === undefined || value === null)
|
|
1455
1644
|
return [];
|
|
1645
|
+
if (key === "servers" && Array.isArray(value)) {
|
|
1646
|
+
if (value.length === 0)
|
|
1647
|
+
return [" no servers in this workspace"];
|
|
1648
|
+
return value.map((s) => ` ${s.serverName}: ${s.url ?? "(no URL)"}${s.status ? ` (${s.status})` : ""}`);
|
|
1649
|
+
}
|
|
1456
1650
|
if (key === "checks" && Array.isArray(value)) {
|
|
1457
1651
|
return value.map((c) => ` ${c.ok ? "✓" : "✗"} ${c.check}: ${c.detail}`);
|
|
1458
1652
|
}
|
|
1459
1653
|
if (typeof value === "object" && !Array.isArray(value)) {
|
|
1460
1654
|
return Object.entries(value).flatMap(([k, v]) => field2(`${key} ${k}`, v));
|
|
1461
1655
|
}
|
|
1462
|
-
|
|
1656
|
+
if (Array.isArray(value)) {
|
|
1657
|
+
const items = value.map((v) => typeof v === "object" ? JSON.stringify(v) : String(v));
|
|
1658
|
+
return [` ${key}: ${items.join(", ")}`];
|
|
1659
|
+
}
|
|
1660
|
+
return [` ${key}: ${String(value)}`];
|
|
1463
1661
|
}
|
|
1464
1662
|
function interrupted(argv, exitCode) {
|
|
1465
1663
|
const err = new CliError("INTERRUPTED", `The CLI stopped before it answered (exit code ${exitCode}).`, { exit_code: exitCode });
|
package/package.json
CHANGED