Files
openwork/apps/server/src/mcp.ts
Omar McAdam 2b91b4d777 refactor: repo folder structure (#1038)
* refactor(repo): move OpenWork apps into apps and ee layout

Rebase the monorepo layout migration onto the latest dev changes so the moved app, desktop, share, and cloud surfaces keep working from their new paths. Carry the latest deeplink, token persistence, build, Vercel, and docs updates forward to avoid stale references and broken deploy tooling.

* chore(repo): drop generated desktop artifacts

Ignore the moved Tauri target and sidecar paths so local cargo checks do not pollute the branch. Remove the accidentally committed outputs from the repo while keeping the layout migration intact.

* fix(release): drop built server cli artifact

Stop tracking the locally built apps/server/cli binary so generated server outputs do not leak into commits. Also update the release workflow to check the published scoped package name for @openwork/server before deciding whether npm publish is needed.

* fix(workspace): add stable CLI bin wrappers

Point the server and router package bins at committed wrapper scripts so workspace installs can create shims before dist outputs exist. Keep the wrappers compatible with built binaries and source checkouts to avoid Vercel install warnings without changing runtime behavior.
2026-03-19 11:41:38 -07:00

97 lines
3.8 KiB
TypeScript

import { minimatch } from "minimatch";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
import type { McpItem } from "./types.js";
import { readJsoncFile, updateJsoncTopLevel } from "./jsonc.js";
import { opencodeConfigPath } from "./workspace-files.js";
import { validateMcpConfig, validateMcpName } from "./validators.js";
function globalOpenCodeConfigPath(): string {
const base = join(homedir(), ".config", "opencode");
const jsonc = join(base, "opencode.jsonc");
const json = join(base, "opencode.json");
if (existsSync(jsonc)) return jsonc;
if (existsSync(json)) return json;
return jsonc; // fall back to jsonc (readJsoncFile handles missing files gracefully)
}
function getMcpConfig(config: Record<string, unknown>): Record<string, Record<string, unknown>> {
const mcp = config.mcp;
if (!mcp || typeof mcp !== "object") return {};
return mcp as Record<string, Record<string, unknown>>;
}
function getDeniedToolPatterns(config: Record<string, unknown>): string[] {
const tools = config.tools;
if (!tools || typeof tools !== "object") return [];
const deny = (tools as { deny?: unknown }).deny;
if (!Array.isArray(deny)) return [];
return deny.filter((item) => typeof item === "string") as string[];
}
function isMcpDisabledByTools(config: Record<string, unknown>, name: string): boolean {
const patterns = getDeniedToolPatterns(config);
if (patterns.length === 0) return false;
const candidates = [`mcp.${name}`, `mcp.${name}.*`, `mcp:${name}`, `mcp:${name}:*`, "mcp.*", "mcp:*"];
return patterns.some((pattern) => candidates.some((candidate) => minimatch(candidate, pattern)));
}
export async function listMcp(workspaceRoot: string): Promise<McpItem[]> {
const { data: config } = await readJsoncFile(opencodeConfigPath(workspaceRoot), {} as Record<string, unknown>);
const { data: globalConfig } = await readJsoncFile(globalOpenCodeConfigPath(), {} as Record<string, unknown>);
const projectMcpMap = getMcpConfig(config);
const globalMcpMap = getMcpConfig(globalConfig);
const items: McpItem[] = [];
// Global MCPs first; project-level entries override global ones with the same name.
for (const [name, entry] of Object.entries(globalMcpMap)) {
if (Object.prototype.hasOwnProperty.call(projectMcpMap, name)) continue;
items.push({
name,
config: entry,
source: "config.global",
disabledByTools:
(isMcpDisabledByTools(globalConfig, name) || isMcpDisabledByTools(config, name)) || undefined,
});
}
// Project MCPs (highest priority).
for (const [name, entry] of Object.entries(projectMcpMap)) {
items.push({
name,
config: entry,
source: "config.project",
disabledByTools: isMcpDisabledByTools(config, name) || undefined,
});
}
return items;
}
export async function addMcp(
workspaceRoot: string,
name: string,
config: Record<string, unknown>,
): Promise<{ action: "added" | "updated" }> {
validateMcpName(name);
validateMcpConfig(config);
const { data } = await readJsoncFile(opencodeConfigPath(workspaceRoot), {} as Record<string, unknown>);
const mcpMap = getMcpConfig(data);
const existed = Object.prototype.hasOwnProperty.call(mcpMap, name);
mcpMap[name] = config;
await updateJsoncTopLevel(opencodeConfigPath(workspaceRoot), { mcp: mcpMap });
return { action: existed ? "updated" : "added" };
}
export async function removeMcp(workspaceRoot: string, name: string): Promise<boolean> {
const { data } = await readJsoncFile(opencodeConfigPath(workspaceRoot), {} as Record<string, unknown>);
const mcpMap = getMcpConfig(data);
if (!Object.prototype.hasOwnProperty.call(mcpMap, name)) return false;
delete mcpMap[name];
await updateJsoncTopLevel(opencodeConfigPath(workspaceRoot), { mcp: mcpMap });
return true;
}