Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
On Windows a lent MCP program starts directly, and only a .cmd shim goes through cmd.exe
milind-soni668a218b9f9d
diff --git a/electron/lent-mcp.d.mts b/electron/lent-mcp.d.mtsindex e0b8c1e2b5..ed6b9e0f12 100644--- a/electron/lent-mcp.d.mts+++ b/electron/lent-mcp.d.mts@@ -7,6 +7,7 @@ export function isLoopbackAddress(url: string): boolean;export function lentMcpRefusal(grant: { cloud?: unknown; terminal?: boolean }, server: LentMcpServer): string | null;export function ownEnvironmentIds(dataDirs: ReadonlyArray<string | null | undefined>): string[];export function inheritedEnvironment(env?: NodeJS.ProcessEnv): Record<string, string>;+export function windowsLaunch(command: string, args: string[], env: Record<string, string | undefined>, exists?: (file: string) => boolean): { file: string; args: string[]; verbatim: boolean } | null;export function createLentMcp(options?: {environment?: () => Record<string, string> | Promise<Record<string, string>>;home?: string;diff --git a/electron/lent-mcp.mjs b/electron/lent-mcp.mjsindex ef95443bad..2271e73071 100644--- a/electron/lent-mcp.mjs+++ b/electron/lent-mcp.mjs@@ -26,7 +26,7 @@ const MAX_PAGES = 10;const PROTOCOL = "2025-03-26";/** What a lent server inherits from this app: where things live, never a key. */-const INHERITED = ["HOME", "USERPROFILE", "USER", "LOGNAME", "SHELL", "LANG", "TMPDIR", "TEMP", "TMP", "PATHEXT", "SystemRoot", "SystemDrive", "windir", "APPDATA", "LOCALAPPDATA", "ProgramData"];+const INHERITED = ["HOME", "USERPROFILE", "USER", "LOGNAME", "SHELL", "LANG", "TMPDIR", "TEMP", "TMP", "PATHEXT", "SystemRoot", "SystemDrive", "windir", "ComSpec", "APPDATA", "LOCALAPPDATA", "ProgramData"];export const inheritedEnvironment = (env = process.env) => Object.fromEntries(INHERITED.filter(key => env[key]).map(key => [key, env[key]]));const record = value => Boolean(value) && typeof value === "object" && !Array.isArray(value);@@ -78,8 +78,18 @@ const failure = message => new Error(message);/** The server answered the request with an error: it is still running. */const refusal = message => Object.assign(new Error(message), { answered: true });-/** Windows starts a .cmd shim (npx.cmd, uvx.cmd) only through cmd.exe, so there each word is quoted for it. */-const windowsWord = word => (/[\s"]/.test(word) ? `"${word.replace(/"/g, '\\"')}"` : word);+/** How Windows starts `command`: a program directly, found on PATH with PATHEXT as Windows finds it, and a .cmd or .bat+ * shim (npx.cmd, uvx.cmd) through cmd.exe, which alone runs one, each word quoted for it. Null when it isn't there. */+export function windowsLaunch(command, args, env, exists = fs.existsSync) {+ const extensions = (env.PATHEXT || ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean).map(extension => extension.toLowerCase());+ const names = path.win32.extname(command) ? [command] : extensions.map(extension => `${command}${extension}`);+ const dirs = /[\\/]/.test(command) ? [""] : (env.PATH || env.Path || "").split(";").filter(Boolean);+ const file = dirs.flatMap(dir => names.map(name => (dir ? path.win32.join(dir, name) : name))).find(candidate => exists(candidate));+ if (!file) return null;+ if (!/\.(?:cmd|bat)$/i.test(file)) return { file, args, verbatim: false };+ const line = [file, ...args].map(word => `"${word.replace(/"/g, '""')}"`).join(" ");+ return { file: env.ComSpec || "cmd.exe", args: ["/d", "/s", "/c", `"${line}"`], verbatim: true };+}function killTree(child) {if (!child.pid) return;@@ -91,10 +101,15 @@ function killTree(child) {}}+const notHere = server => failure(`${server.name} could not start: ${path.basename(server.command)} is not on this computer`);+function stdioClient(server, { environment, home }) {const windows = process.platform === "win32";- const child = spawn(windows ? windowsWord(server.command) : server.command, windows ? server.args.map(windowsWord) : server.args, {- cwd: home, detached: !windows, windowsHide: true, shell: windows, stdio: ["pipe", "pipe", "ignore"], env: { ...environment, ...server.env },+ const env = { ...environment, ...server.env };+ const launch = windows ? windowsLaunch(server.command, server.args, { ...env, ComSpec: env.ComSpec || process.env.ComSpec }) : { file: server.command, args: server.args, verbatim: false };+ if (!launch) throw notHere(server);+ const child = spawn(launch.file, launch.args, {+ cwd: home, detached: !windows, windowsHide: true, windowsVerbatimArguments: launch.verbatim, stdio: ["pipe", "pipe", "ignore"], env,});let sequence = 0; let closed = null; let bytes = 0;const pending = new Map();@@ -104,7 +119,7 @@ function stdioClient(server, { environment, home }) {for (const item of pending.values()) item.reject(error);pending.clear(); killTree(child);};- child.on("error", error => end(failure(error.code === "ENOENT" ? `${server.name} could not start: ${path.basename(server.command)} is not on this computer` : `${server.name} could not start`)));+ child.on("error", error => end(error.code === "ENOENT" ? notHere(server) : failure(`${server.name} could not start`)));child.stdin.on("error", () => end(failure(`${server.name} stopped`)));child.on("exit", () => end(failure(`${server.name} stopped`)));child.stdout.on("data", chunk => { bytes += chunk.length; if (bytes > RESULT_LIMIT) end(failure(`${server.name} answered with more than ${RESULT_LIMIT} bytes`)); });diff --git a/electron/lent-mcp.node-test.mjs b/electron/lent-mcp.node-test.mjsindex 52c0656350..29d72e7ac3 100644--- a/electron/lent-mcp.node-test.mjs+++ b/electron/lent-mcp.node-test.mjs@@ -5,7 +5,7 @@ import { mkdtemp, mkdir, realpath, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import path from "node:path";import { randomUUID } from "node:crypto";-import { createLentMcp, isLoopbackAddress, lentMcpRefusal, ownEnvironmentIds, validLentMcpServer } from "./lent-mcp.mjs";+import { createLentMcp, inheritedEnvironment, isLoopbackAddress, lentMcpRefusal, ownEnvironmentIds, validLentMcpServer, windowsLaunch } from "./lent-mcp.mjs";import { executeSharedOperation } from "./shared-computer-access.mjs";import { validSharedOperation } from "./computer-sharing.mjs";@@ -33,7 +33,7 @@ async function stdioFixture(t) {await writeFile(script, SERVER_SCRIPT);const home = path.join(dir, "home");await mkdir(home);- const lent = createLentMcp({ environment: () => ({ PATH: process.env.PATH ?? "" }), home });+ const lent = createLentMcp({ environment: () => ({ ...inheritedEnvironment(), PATH: process.env.PATH ?? "" }), home });t.after(() => lent.close());return { dir, home, script, lent, server: { name: "notes", command: process.execPath, args: [script], env: { NOTES_TOKEN: "saved-env-key" } } };}@@ -165,6 +165,18 @@ test("an address that refuses its keys, redirects or isn't there says so plainlyawait assert.rejects(lent.call({ action: "mcp_tools", server: http("http://127.0.0.1:9/mcp") }, signal()), /local is not answering at 127\.0\.0\.1:9 on this computer/);});+test("on Windows a program starts directly, a .cmd shim through cmd.exe with each word quoted, and a missing one is named", () => {+ const env = { PATH: "C:\\Tools;C:\\Program Files\\nodejs", PATHEXT: ".COM;.EXE;.BAT;.CMD", ComSpec: "C:\\Windows\\system32\\cmd.exe" };+ const on = files => candidate => files.some(file => file.toLowerCase() === candidate.toLowerCase());+ assert.deepEqual(windowsLaunch("npx", ["-y", "notes mcp", 'say "hi"'], env, on(["C:\\Program Files\\nodejs\\npx.cmd"])), {+ file: "C:\\Windows\\system32\\cmd.exe", verbatim: true,+ args: ["/d", "/s", "/c", '""C:\\Program Files\\nodejs\\npx.cmd" "-y" "notes mcp" "say ""hi""""'],+ });+ assert.deepEqual(windowsLaunch("uvx", ["mcp"], env, on(["C:\\Tools\\uvx.exe", "C:\\Tools\\uvx.cmd"])), { file: "C:\\Tools\\uvx.exe", args: ["mcp"], verbatim: false }, "PATHEXT's order");+ assert.deepEqual(windowsLaunch("C:\\Users\\Ada\\bin\\notes.exe", [], env, on(["C:\\Users\\Ada\\bin\\notes.exe"])), { file: "C:\\Users\\Ada\\bin\\notes.exe", args: [], verbatim: false });+ assert.equal(windowsLaunch("notes-mcp", [], env, on([])), null);+});+test("this computer's own installs, by the environment id each keeps", async t => {const dir = await realpath(await mkdtemp(path.join(tmpdir(), "omb-lent-mcp-ids-")));t.after(() => rm(dir, { recursive: true, force: true }));