Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: MCP runners from PATH only

Samrat Talukdard9fad23e199c
diff --git a/apps/server/src/mcp-allow/PARITY.sha256 b/apps/server/src/mcp-allow/PARITY.sha256
index fae7cf9d..802bb9d0 100644
--- a/apps/server/src/mcp-allow/PARITY.sha256
+++ b/apps/server/src/mcp-allow/PARITY.sha256
@@ -1,4 +1,4 @@
# The MCP allowlist module: the same files in omnirush-cli (src/mcp/) and omnirush-gui (apps/server/src/mcp-allow/).
# A change to one must be ported to the other and this list updated in both.
-a6fc17499befcd29c47c69563c04f17429f460d2aefad20bc1c5cac383321add mcp-allow.mjs
-1462cdd9278e07c6c8c55ea29abca116956db58ce3c990a62b4231a3426c9c01 mcp-allow.d.mts
+048b83dc8e5e66215a12d88f93b0ea60ca18f65a61eb6010a6e27c499070b37a mcp-allow.mjs
+3a548e530931a24d040fb804aae72607339b38791dafb63b4be02b9cf2933dd3 mcp-allow.d.mts
diff --git a/apps/server/src/mcp-allow/mcp-allow.d.mts b/apps/server/src/mcp-allow/mcp-allow.d.mts
index c086a682..73712bf2 100644
--- a/apps/server/src/mcp-allow/mcp-allow.d.mts
+++ b/apps/server/src/mcp-allow/mcp-allow.d.mts
@@ -8,7 +8,7 @@ export const MCP_REASONS: Record<McpDecisionCode, string>;
export type McpRuntime = "npm" | "pypi";
export type McpDecisionCode =
| "allowed" | "disabled" | "remote" | "unknown_command" | "not_allowed" | "denied"
- | "version" | "outside_project" | "missing_flag" | "missing_arg" | "flag" | "duplicate" | "invalid";
+ | "version" | "outside_project" | "missing_flag" | "missing_arg" | "flag" | "duplicate" | "invalid" | "runner_missing";
export type McpRules = {
path_args?: "positional";
@@ -69,7 +69,8 @@ export type McpDecision = {
export type McpBox = { shell: string; key: string; croot?: string | null };
-export type McpReport = { v: 1; root: string; at: number; enabled: boolean; decisions: McpDecision[] };
+export type McpIgnored = { kind: "plugin" | "lsp" | "formatter"; name: string; source: string | null };
+export type McpReport = { v: 1; root: string; at: number; enabled: boolean; decisions: McpDecision[]; ignored?: McpIgnored[] };
export type McpLaunch = {
runtime: McpRuntime;
@@ -90,13 +91,16 @@ export function normalizePolicy(block: unknown): McpPolicy | null;
export function splitSpec(spec: string, runtime: McpRuntime): { name: string; version: string | null } | null;
export function parseLaunch(command: unknown): McpLaunch | null;
export function launchCommand(entry: McpAllowEntry, launch: McpLaunch, version: string, npmPrefix: string): string[];
+export function resolveRunner(runner: string, options?: { root?: string | null; platform?: string; env?: Record<string, string | undefined>; exists?: (file: string) => boolean }): string | null;
+export function projectCodeIgnored(cfg: unknown): McpIgnored[];
+export function describeIgnored(items: McpIgnored[] | null | undefined): string | null;
export function insideRoot(root: string, p: string, platform?: string): boolean;
export function insideRootReal(root: string, p: string, platform?: string): boolean;
export function shellQuote(word: string): string;
export function evaluateMcp(
mcpMap: unknown,
policy: McpPolicy | null,
- ctx?: { root?: string; platform?: string; box?: McpBox | null; npmPrefix?: string },
+ ctx?: { root?: string; platform?: string; box?: McpBox | null; npmPrefix?: string; resolveRunner?: (runner: string, options: { root: string; platform: string }) => string | null },
): { mcp: Record<string, Record<string, unknown>>; decisions: McpDecision[] };
export function toolPrefix(name: string): string;
export function mcpPermissionRules(decisions: McpDecision[] | null | undefined, mode: unknown): Record<string, "allow" | "ask">;
@@ -116,6 +120,7 @@ export function createMcpPolicyHooks(options?: {
reportDir?: string | null;
box?: McpBox | null | ((root: string) => McpBox | null);
npmPrefix?: string;
+ resolveRunner?: (runner: string, options: { root: string; platform: string }) => string | null;
permissionMode?: string | null | (() => string | null | undefined);
platform?: string;
now?: () => number;
diff --git a/apps/server/src/mcp-allow/mcp-allow.mjs b/apps/server/src/mcp-allow/mcp-allow.mjs
index 26bb772d..e7069e80 100644
--- a/apps/server/src/mcp-allow/mcp-allow.mjs
+++ b/apps/server/src/mcp-allow/mcp-allow.mjs
@@ -43,6 +43,7 @@ export const MCP_REASONS = {
flag: "This MCP server setting is not supported: it reaches outside the project.",
duplicate: "Another entry already starts this MCP server.",
invalid: "This MCP server entry is not valid.",
+ runner_missing: "npx or uvx was not found on this computer outside the project folder, so the server does not start.",
};
const ID = /^[a-z0-9][a-z0-9-]{0,39}$/;
@@ -293,6 +294,27 @@ export function launchCommand(entry, launch, version, npmPrefix) {
return ["uvx", "--no-config", ...(launch.bin ? ["--from", `${entry.package}==${version}`, launch.bin] : [`${entry.package}@${version}`])];
}
+/**
+ * `runner` (npx, uvx) as an absolute path from PATH: only absolute PATH
+ * folders outside the project (after links) count, never the current
+ * folder; on Windows the .exe or .cmd file. null when there is none.
+ */
+export function resolveRunner(runner, { root = null, platform = process.platform, env = process.env, exists = existsSync } = {}) {
+ const lib = platform === "win32" ? path.win32 : path.posix;
+ const raw = platform === "win32" ? (env.Path ?? env.PATH ?? env.path ?? "") : (env.PATH ?? "");
+ const exts = platform === "win32" ? [".exe", ".cmd"] : [""];
+ for (const dir of String(raw).split(platform === "win32" ? ";" : ":")) {
+ const folder = dir.trim().replace(/^"(.*)"$/, "$1");
+ if (!folder || !lib.isAbsolute(folder) || (platform === "win32" && !/^[A-Za-z]:[\\/]|^\\\\/.test(folder))) continue;
+ if (root && insideRootReal(root, folder, platform)) continue;
+ for (const ext of exts) {
+ const file = lib.join(folder, `${runner}${ext}`);
+ if (exists(file)) return file;
+ }
+ }
+ return null;
+}
+
// ---------------------------------------------------------------------- paths
function samePlatformPath(p, platform) {
@@ -511,6 +533,13 @@ export function evaluateMcp(mcpMap, policy, ctx = {}) {
const npmPrefix = box ? BOX_NPM_PREFIX : ctx.npmPrefix || path.join(os.homedir(), ".cache", "omnirush-mcp-npx");
const argv = [...launchCommand(entry, launch, version, npmPrefix), ...rest];
if (launch.runner !== (entry.runtime === "npm" ? "npx" : "uvx")) notes.push(`runs with ${argv[0]}`);
+ if (!box) {
+ // By absolute path: the engine starts the server in the project folder, and on Windows a bare name
+ // is looked up there before PATH (a repo's own npx.cmd would run instead).
+ const found = (ctx.resolveRunner ?? resolveRunner)(argv[0], { root, platform });
+ if (!found) { decisions.push(blocked(name, "runner_missing", { ...base, version })); continue; }
+ argv[0] = found;
+ }
const next = { ...raw, type: "local" };
if (box) {
const envWords = Object.entries(environment).map(([k, v]) => `${k}=${shellQuote(v)}`);
@@ -640,6 +669,34 @@ export function readReport(dir, root) {
}
}
+/**
+ * What the engine left out of the project's own config because it runs code
+ * (`omnirush_ignored`, set by the engine with OMNIRUSH_PROJECT_CODE=0): its
+ * plugins, and lsp / formatter entries that name a command.
+ */
+export function projectCodeIgnored(cfg) {
+ const list = isRecord(cfg) && Array.isArray(cfg.omnirush_ignored) ? cfg.omnirush_ignored : [];
+ return list
+ .filter((x) => isRecord(x) && ["plugin", "lsp", "formatter"].includes(x.kind) && typeof x.name === "string")
+ .slice(0, 50)
+ .map((x) => ({ kind: x.kind, name: x.name.slice(0, 200), source: typeof x.source === "string" ? x.source.slice(0, 400) : null }));
+}
+
+/** One line for a notice: what of the project's own config did not load, and why; null when nothing. */
+export function describeIgnored(items) {
+ const list = Array.isArray(items) ? items : [];
+ if (!list.length) return null;
+ const what = (kind) => list.filter((x) => x.kind === kind).map((x) => (kind === "plugin" ? path.basename(String(x.name).replace(/^file:\/\//, "")) : x.name));
+ const parts = [];
+ const plugins = what("plugin");
+ const lsp = what("lsp");
+ const fmt = what("formatter");
+ if (plugins.length) parts.push(`plugins ${plugins.slice(0, 3).join(", ")}${plugins.length > 3 ? " ..." : ""}`);
+ if (lsp.length) parts.push(`language servers ${lsp.slice(0, 3).join(", ")}`);
+ if (fmt.length) parts.push(`formatters ${fmt.slice(0, 3).join(", ")}`);
+ return `Not loaded from this project's config: ${parts.join("; ")}. A project's own plugins and commands could run code just by opening it; the built-in language servers and formatters still work, and your own settings in ~/.config/omnirush apply as usual.`;
+}
+
/** One line per decision, for a terminal or a notice. */
export function describeDecision(d) {
if (d.status === "allowed") {
@@ -677,7 +734,7 @@ export function createMcpPolicyHooks(options = {}) {
if (!box) {
try { mkdirSync(npmPrefix, { recursive: true }); } catch { /* npx makes it */ }
}
- const { mcp, decisions } = evaluateMcp(target.mcp, policy, { root, platform: options.platform, box, npmPrefix });
+ const { mcp, decisions } = evaluateMcp(target.mcp, policy, { root, platform: options.platform, box, npmPrefix, ...(options.resolveRunner ? { resolveRunner: options.resolveRunner } : {}) });
target.mcp = mcp;
const mode = typeof options.permissionMode === "function" ? options.permissionMode() : options.permissionMode ?? process.env.OMNIRUSH_PERMISSION_MODE;
const rules = mcpPermissionRules(decisions, mode);
@@ -692,7 +749,7 @@ export function createMcpPolicyHooks(options = {}) {
const file = reportPath(options.reportDir, root);
mkdirSync(options.reportDir, { recursive: true });
const tmp = `${file}.${process.pid}.tmp`;
- writeFileSync(tmp, JSON.stringify({ v: MCP_POLICY_FILE_VERSION, root, at: (options.now ?? Date.now)(), enabled: Boolean(policy?.enabled), decisions }), { mode: 0o600 });
+ writeFileSync(tmp, JSON.stringify({ v: MCP_POLICY_FILE_VERSION, root, at: (options.now ?? Date.now)(), enabled: Boolean(policy?.enabled), decisions, ignored: projectCodeIgnored(target) }), { mode: 0o600 });
renameSync(tmp, file);
} catch { /* the report is for display only */ }
}
diff --git a/apps/server/src/mcp-allow/state.test.ts b/apps/server/src/mcp-allow/state.test.ts
index 63f7a97e..f43ccc3a 100644
--- a/apps/server/src/mcp-allow/state.test.ts
+++ b/apps/server/src/mcp-allow/state.test.ts
@@ -5,7 +5,7 @@ import path from "node:path";
import omnirushMcpPolicy from "../opencode-plugins/omnirush-mcp-policy.js";
import { keyOf } from "../box/plugin-lib.js";
-import { readPolicyFile } from "./mcp-allow.mjs";
+import { describeIgnored, readPolicyFile } from "./mcp-allow.mjs";
import {
decideMcpEntry,
mcpEngineEnv,
@@ -28,7 +28,7 @@ function temp(prefix: string): string {
afterEach(() => {
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
- for (const key of ["OMNIRUSH_MCP_POLICY_FILE", "OMNIRUSH_MCP_REPORT_DIR", "OMNIRUSH_MCP_WORKSPACE_DIR", "OMNIRUSH_BOX_HOME"]) {
+ for (const key of ["OMNIRUSH_MCP_POLICY_FILE", "OMNIRUSH_MCP_REPORT_DIR", "OMNIRUSH_MCP_WORKSPACE_DIR", "OMNIRUSH_BOX_HOME", "PATH"]) {
if (saved[key] === undefined) delete process.env[key];
else process.env[key] = saved[key];
}
@@ -53,6 +53,13 @@ describe("the remembered MCP list", () => {
expect(mcpPolicyEnabled(state)).toBe(false);
});
+ test("the engine never takes plugins or commands from a project's config, unless the app itself says so", () => {
+ const state = temp("mcp-state-");
+ expect(mcpEngineEnv(state, {}).OMNIRUSH_PROJECT_CODE).toBe("0");
+ expect(mcpEngineEnv(state, { OMNIRUSH_PROJECT_CODE: "true" }).OMNIRUSH_PROJECT_CODE).toBe("0");
+ expect(mcpEngineEnv(state, { OMNIRUSH_PROJECT_CODE: "1" }).OMNIRUSH_PROJECT_CODE).toBe("1");
+ });
+
test("decides one entry before it is saved", () => {
const state = temp("mcp-state-");
const root = temp("mcp-root-");
@@ -81,7 +88,11 @@ describe("the engine plugin (omnirush-mcp-policy)", () => {
const env = mcpEngineEnv(state);
Object.assign(process.env, env);
process.env.OMNIRUSH_BOX_HOME = path.join(state, "box-home");
- return { state, root };
+ // The runners resolve to absolute paths on PATH, outside the project.
+ const bin = temp("mcp-bin-");
+ for (const name of ["npx", "uvx"]) writeFileSync(path.join(bin, process.platform === "win32" ? `${name}.cmd` : name), "", { mode: 0o755 });
+ process.env.PATH = bin;
+ return { state, root, bin };
}
test("without a list: no MCP at all, and the session environment gets no field", async () => {
@@ -97,7 +108,7 @@ describe("the engine plugin (omnirush-mcp-policy)", () => {
});
test("with the list: config and app-added entries are filtered, renamed to their id and pinned", async () => {
- const { state, root } = setup();
+ const { state, root, bin } = setup();
rememberMcpPolicy(state, { mcp: policyFixture });
writeWorkspaceMcp(state, root, { files: FILESYSTEM, notion: { type: "remote", url: "https://mcp.notion.com/mcp" } });
const hooks = await omnirushMcpPolicy({ directory: root });
@@ -105,9 +116,10 @@ describe("the engine plugin (omnirush-mcp-policy)", () => {
await hooks.config({}, output);
const mcp = output.mcp as Record<string, { command: string[] }>;
expect(Object.keys(mcp).sort()).toEqual(["filesystem", "time"]);
- const npx = ["npx", "--prefix", path.join(process.env.OMNIRUSH_MCP_REPORT_DIR!, "..", "npx-prefix"), "-y"];
+ const runner = (name: string) => path.join(bin, process.platform === "win32" ? `${name}.cmd` : name);
+ const npx = [runner("npx"), "--prefix", path.join(process.env.OMNIRUSH_MCP_REPORT_DIR!, "..", "npx-prefix"), "-y"];
expect(mcp.filesystem!.command).toEqual([...npx, "@modelcontextprotocol/[email protected]", "."]);
- expect(mcp.time!.command).toEqual(["uvx", "--no-config", "[email protected]"]);
+ expect(mcp.time!.command).toEqual([runner("uvx"), "--no-config", "[email protected]"]);
const report = workspaceMcpReport(state, root);
const byName = Object.fromEntries((report?.decisions ?? []).map((decision) => [decision.name, decision]));
expect(byName.script).toMatchObject({ status: "blocked", code: "unknown_command" });
@@ -131,6 +143,30 @@ describe("the engine plugin (omnirush-mcp-policy)", () => {
expect(mcpServersInUse(report)).toEqual([{ id: "filesystem", package: "@modelcontextprotocol/server-filesystem", version: "2026.8.31", runtime: "npm", sandbox: false, enabled: false }]);
});
+ test("no npx outside the project: the server is blocked, never started from the project", async () => {
+ const { state, root } = setup();
+ rememberMcpPolicy(state, { mcp: policyFixture });
+ const empty = temp("mcp-nobin-");
+ // A runner planted in the project is never used.
+ writeFileSync(path.join(root, "npx"), "", { mode: 0o755 });
+ process.env.PATH = `${root}${path.delimiter}${empty}`;
+ const hooks = await omnirushMcpPolicy({ directory: root });
+ const output = { mcp: { filesystem: FILESYSTEM } } as Record<string, unknown>;
+ await hooks.config({}, output);
+ expect(output.mcp).toEqual({});
+ expect(workspaceMcpReport(state, root)?.decisions[0]).toMatchObject({ name: "filesystem", status: "blocked", code: "runner_missing" });
+ });
+
+ test("the report lists what the engine left out of the project's config", async () => {
+ const { state, root } = setup();
+ const hooks = await omnirushMcpPolicy({ directory: root });
+ const ignored = [{ kind: "plugin", name: "file:///repo/.opencode/plugins/evil.ts", source: "/repo/.opencode/plugins" }, { kind: "lsp", name: "custom", source: "/repo/opencode.json" }];
+ await hooks.config({}, { mcp: {}, omnirush_ignored: ignored });
+ const report = workspaceMcpReport(state, root);
+ expect(report?.ignored?.map((item) => item.kind)).toEqual(["plugin", "lsp"]);
+ expect(describeIgnored(report?.ignored)).toContain("Not loaded from this project's config: plugins evil.ts; language servers custom.");
+ });
+
test("a project whose sandbox is on runs its servers through the box shell", async () => {
const { state, root } = setup();
rememberMcpPolicy(state, { mcp: policyFixture });
diff --git a/apps/server/src/mcp-allow/state.ts b/apps/server/src/mcp-allow/state.ts
index 68301405..ff975c61 100644
--- a/apps/server/src/mcp-allow/state.ts
+++ b/apps/server/src/mcp-allow/state.ts
@@ -38,12 +38,18 @@ export function mcpWorkspaceDir(stateDir: string): string {
return path.join(mcpStateDir(stateDir), "workspaces");
}
-/** The engine's environment for its MCP plugin. */
-export function mcpEngineEnv(stateDir: string): Record<string, string> {
+/**
+ * The engine's environment for its MCP plugin, and OMNIRUSH_PROJECT_CODE=0: a
+ * project's own config brings no plugins and no language server or formatter
+ * commands (the engine lists what it left out; the report carries it). Only
+ * the app's own environment can turn that off (OMNIRUSH_PROJECT_CODE=1).
+ */
+export function mcpEngineEnv(stateDir: string, env: Record<string, string | undefined> = process.env): Record<string, string> {
return {
OMNIRUSH_MCP_POLICY_FILE: mcpPolicyFile(stateDir),
OMNIRUSH_MCP_REPORT_DIR: mcpReportDir(stateDir),
OMNIRUSH_MCP_WORKSPACE_DIR: mcpWorkspaceDir(stateDir),
+ OMNIRUSH_PROJECT_CODE: env.OMNIRUSH_PROJECT_CODE?.trim() === "1" ? "1" : "0",
};
}