openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
373 lines (372 loc) • 12.4 kB
JavaScript
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { d as isStringRecord, l as isRecord, u as isStringArray } from "./record-coerce-CVPuDqcc.mjs";
import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs";
import "./utils-C8YECn2Q.mjs";
import { r as resolveActivePluginInstallRoots } from "./install-root-context-CeM6pu6m.mjs";
import { a as pluginCacheRealpathSync, r as pluginCacheExistsSync } from "./plugin-cache-files-e1aGp2KM.mjs";
import { o as resolveBundleComponentPaths, t as BUNDLE_MANIFEST_PATHS } from "./bundle-manifest-js7o0H7U.mjs";
import { t as encodePluginInstallDirName } from "./install-paths-Bwm0P5EG.mjs";
import { t as applyMergePatch } from "./merge-patch-D-7aajuy.mjs";
import { i as resolveOpenClawMcpTransportAlias, r as resolveConfiguredMcpTransport } from "./mcp-config-normalize-DxJwGDsa.mjs";
import { n as resolveMcpTransportConfig } from "./mcp-transport-config-r6XeT3o8.mjs";
import { n as loadEnabledBundleConfig, r as readBundleJsonObject, t as extractBundleServerMap } from "./bundle-config-shared-P1HNI6RX.mjs";
import path from "node:path";
//#region src/plugins/bundle-mcp.ts
const CLAUDE_PLUGIN_ROOT_PLACEHOLDER = "${CLAUDE_PLUGIN_ROOT}";
const AGENT_PLUGIN_ROOT_PLACEHOLDER = "${PLUGIN_ROOT}";
const AGENT_PLUGIN_DATA_PLACEHOLDER = "${PLUGIN_DATA}";
const AGENT_MCP_SCHEMA = "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json";
const BUNDLE_PLACEHOLDER_PATTERN = /\$\{(?:CLAUDE_PLUGIN_ROOT|PLUGIN_ROOT|PLUGIN_DATA)\}/g;
const AGENT_MCP_TOP_LEVEL_KEYS = /* @__PURE__ */ new Set(["$schema", "mcpServers"]);
const AGENT_STDIO_KEYS = /* @__PURE__ */ new Set([
"type",
"command",
"args",
"env",
"cwd"
]);
const AGENT_HTTP_KEYS = /* @__PURE__ */ new Set([
"type",
"url",
"headers"
]);
function resolveBundleMcpConfigPaths(e) {
if (e.bundleFormat === "agent") return pluginCacheExistsSync(path.join(e.rootDir, "mcp.json")) ? ["mcp.json"] : [];
return resolveBundleComponentPaths(e.raw.mcpServers, e.rootDir, [".mcp.json"]);
}
function extractMcpServerMap(e) {
return normalizeImportedMcpServers(extractBundleServerMap(e, ["mcpServers", "servers"]));
}
function normalizeImportedMcpServers(e) {
return Object.fromEntries(Object.entries(e).map(([e, t]) => {
const n = normalizeLowercaseStringOrEmpty(t.type);
const r = n === "stdio" ? "stdio" : resolveOpenClawMcpTransportAlias(n);
if (!r) return [e, n ? {
...t,
transport: resolveConfiguredMcpTransport(t) ?? n
} : t];
const { type: i, ...a } = t;
return [e, {
...a,
transport: resolveConfiguredMcpTransport(t) ?? r
}];
}));
}
function isExplicitRelativePath(e) {
return e === "." || e === ".." || e.startsWith("./") || e.startsWith("../");
}
function expandBundleRootPlaceholders(e) {
return e.value.replace(BUNDLE_PLACEHOLDER_PATTERN, (t) => {
if (t === CLAUDE_PLUGIN_ROOT_PLACEHOLDER || t === AGENT_PLUGIN_ROOT_PLACEHOLDER && e.pluginDataDir) return e.rootDir;
return e.pluginDataDir ?? t;
});
}
function normalizeExpandedAbsolutePath(e) {
return path.isAbsolute(e) ? path.normalize(e) : e;
}
function absolutizeBundleMcpServer(e) {
const t = { ...e.server };
const expand = (t) => expandBundleRootPlaceholders({
value: t,
rootDir: e.rootDir,
pluginDataDir: e.pluginDataDir
});
const resolveArgument = (t) => {
if (typeof t !== "string") return t;
const n = expand(t);
return isExplicitRelativePath(n) ? path.resolve(e.baseDir, n) : normalizeExpandedAbsolutePath(n);
};
if (typeof t.command === "string" && typeof t.cwd !== "string" && typeof t.workingDirectory !== "string") t.cwd = e.baseDir;
if (typeof t.command === "string") t.command = resolveArgument(t.command);
const n = t.cwd;
if (typeof n === "string") {
const r = expand(n);
t.cwd = path.isAbsolute(r) ? r : path.resolve(e.baseDir, r);
}
const r = t.workingDirectory;
if (typeof r === "string") {
const n = expand(r);
t.workingDirectory = path.isAbsolute(n) ? path.normalize(n) : path.resolve(e.baseDir, n);
}
if (Array.isArray(t.args)) t.args = t.args.map(resolveArgument);
if (isRecord(t.env)) t.env = Object.fromEntries(Object.entries(t.env).map(([e, t]) => [e, typeof t === "string" ? normalizeExpandedAbsolutePath(expand(t)) : t]));
if (e.pluginDataDir && typeof t.command === "string") t.env = {
...isRecord(t.env) ? t.env : {},
PLUGIN_ROOT: e.rootDir,
PLUGIN_DATA: e.pluginDataDir
};
return t;
}
function hasOnlyKeys(e, t) {
return Object.keys(e).every((e) => t.has(e));
}
function isValidAgentCommand(e, t) {
if (typeof e !== "string" || e.length === 0) return false;
if (e.startsWith("./")) return e.length > 2 && isPathInside(t, path.resolve(t, e));
return !/[\s/\\]/.test(e);
}
function isValidAgentCwd(e, t, n) {
if (typeof e !== "string") return false;
let r;
if (e.startsWith("./")) r = t;
else if (e === AGENT_PLUGIN_ROOT_PLACEHOLDER || e.startsWith(`${AGENT_PLUGIN_ROOT_PLACEHOLDER}/`)) r = t;
else if (e === AGENT_PLUGIN_DATA_PLACEHOLDER || e.startsWith(`${AGENT_PLUGIN_DATA_PLACEHOLDER}/`)) r = n;
else return false;
const i = expandBundleRootPlaceholders({
value: e,
rootDir: t,
pluginDataDir: n
});
return isPathInside(r, path.resolve(r, i));
}
function validateAgentMcpServer(e) {
if (!isRecord(e.raw) || typeof e.raw.type !== "string") return {
ok: false,
error: "configuration must be an object with a supported type"
};
const t = e.raw.type;
if (t === "stdio") {
if (!hasOnlyKeys(e.raw, AGENT_STDIO_KEYS)) return {
ok: false,
error: "stdio configuration contains unknown fields"
};
if (!isValidAgentCommand(e.raw.command, e.rootDir)) return {
ok: false,
error: "stdio command must be a bare name or ./-relative path"
};
if (e.raw.args !== void 0 && !isStringArray(e.raw.args)) return {
ok: false,
error: "stdio args must be an array of strings"
};
if (e.raw.env !== void 0 && !isStringRecord(e.raw.env)) return {
ok: false,
error: "stdio env must contain only string values"
};
if (isRecord(e.raw.env) && (Object.hasOwn(e.raw.env, "PLUGIN_ROOT") || Object.hasOwn(e.raw.env, "PLUGIN_DATA"))) return {
ok: false,
error: "stdio env must not define PLUGIN_ROOT or PLUGIN_DATA"
};
if (e.raw.cwd !== void 0 && !isValidAgentCwd(e.raw.cwd, e.rootDir, e.pluginDataDir)) return {
ok: false,
error: "stdio cwd must remain within PLUGIN_ROOT or PLUGIN_DATA"
};
} else if (t === "streamable-http" || t === "sse") {
if (!hasOnlyKeys(e.raw, AGENT_HTTP_KEYS)) return {
ok: false,
error: `${t} configuration contains unknown fields`
};
if (typeof e.raw.url !== "string" || e.raw.url.length === 0) return {
ok: false,
error: `${t} url must be a non-empty string`
};
if (e.raw.headers !== void 0 && !isStringRecord(e.raw.headers)) return {
ok: false,
error: `${t} headers must contain only string values`
};
} else return {
ok: false,
error: `unsupported type: ${t}`
};
const n = {
...e.raw,
transport: t
};
delete n.type;
return {
ok: true,
server: n
};
}
function resolveAgentPluginDataDir(e) {
return path.join(resolveActivePluginInstallRoots().stateDir, "plugin-data", encodePluginInstallDirName(e));
}
function extractAgentMcpServerMap(e) {
if (e.raw.$schema !== AGENT_MCP_SCHEMA || !hasOnlyKeys(e.raw, AGENT_MCP_TOP_LEVEL_KEYS) || !isRecord(e.raw.mcpServers)) return {
servers: {},
diagnostics: [`invalid mcp.json: expected only $schema=${AGENT_MCP_SCHEMA} and object mcpServers`]
};
const t = resolveAgentPluginDataDir(e.pluginId);
const n = {};
const r = [];
for (const [i, a] of Object.entries(e.raw.mcpServers)) {
const o = validateAgentMcpServer({
raw: a,
rootDir: e.rootDir,
pluginDataDir: t
});
if (!o.ok) {
r.push(`invalid MCP server "${i}" in mcp.json: ${o.error}`);
continue;
}
n[i] = o.server;
}
if (!Object.values(n).some((e) => e.transport === "stdio")) return {
servers: n,
diagnostics: r
};
return {
servers: n,
diagnostics: r,
pluginDataDir: t
};
}
function loadBundleFileBackedMcpConfig(e) {
const t = e.bundleFormat === "agent" ? pluginCacheRealpathSync(e.rootDir) : path.resolve(e.rootDir);
const n = path.resolve(t, e.relativePath);
const r = readBundleJsonObject({
rootDir: t,
relativePath: e.relativePath,
allowMissing: e.bundleFormat !== "agent"
});
if (!r.ok) return {
config: {
mcpServers: {},
prepareDataDirsByServer: {}
},
diagnostics: [r.reason === "open" ? r.error : `unable to read ${e.relativePath}: ${r.error}`]
};
const i = e.bundleFormat === "agent" ? extractAgentMcpServerMap({
raw: r.raw,
pluginId: e.pluginId,
rootDir: t
}) : void 0;
const a = i?.servers ?? extractMcpServerMap(r.raw);
const o = path.dirname(n);
return {
config: {
mcpServers: Object.fromEntries(Object.entries(a).map(([e, n]) => [e, absolutizeBundleMcpServer({
rootDir: t,
baseDir: o,
server: n,
pluginDataDir: i?.pluginDataDir
})])),
prepareDataDirsByServer: Object.fromEntries(Object.entries(a).map(([t, n]) => [t, i?.pluginDataDir && n.transport === "stdio" ? {
pluginId: e.pluginId,
dataDir: i.pluginDataDir
} : null]))
},
diagnostics: i?.diagnostics ?? []
};
}
function loadRootRelativeMcpConfig(e) {
const t = path.resolve(e.rootDir);
const n = normalizeImportedMcpServers(e.mcpServers);
return {
config: {
mcpServers: Object.fromEntries(Object.entries(n).map(([e, n]) => [e, absolutizeBundleMcpServer({
rootDir: t,
baseDir: t,
server: n
})])),
prepareDataDirsByServer: Object.fromEntries(Object.keys(n).map((e) => [e, null]))
},
diagnostics: []
};
}
function loadBundleMcpConfig(e) {
const t = BUNDLE_MANIFEST_PATHS[e.bundleFormat];
const n = readBundleJsonObject({
rootDir: e.rootDir,
relativePath: t,
allowMissing: e.bundleFormat === "claude"
});
if (!n.ok) return {
config: {
mcpServers: {},
prepareDataDirsByServer: {}
},
diagnostics: [n.error]
};
let r = {
mcpServers: {},
prepareDataDirsByServer: {}
};
const i = resolveBundleMcpConfigPaths({
raw: n.raw,
rootDir: e.rootDir,
bundleFormat: e.bundleFormat
});
const a = [];
for (const t of i) {
const n = loadBundleFileBackedMcpConfig({
pluginId: e.pluginId,
rootDir: e.rootDir,
relativePath: t,
bundleFormat: e.bundleFormat
});
a.push(...n.diagnostics);
r = applyMergePatch(r, n.config);
}
if (e.bundleFormat !== "agent") r = applyMergePatch(r, loadRootRelativeMcpConfig({
rootDir: e.rootDir,
mcpServers: extractMcpServerMap(n.raw.mcpServers)
}).config);
return {
config: r,
diagnostics: a
};
}
function inspectBundleMcpRuntimeSupport(e) {
return inspectMcpServerRuntimeSupport(loadBundleMcpConfig(e));
}
function inspectNativePluginMcpRuntimeSupport(e) {
return inspectMcpServerRuntimeSupport(loadRootRelativeMcpConfig(e));
}
function inspectMcpServerRuntimeSupport(e) {
const t = [];
const n = [];
const r = [];
for (const [i, a] of Object.entries(e.config.mcpServers)) {
const e = resolveMcpTransportConfig(i, a, { logWarnings: false });
if (e?.kind === "stdio") {
t.push(i);
n.push(i);
continue;
}
if (e?.kind === "http") {
t.push(i);
continue;
}
r.push(i);
}
return {
supportedServerNames: t,
stdioServerNames: n,
unsupportedServerNames: r,
diagnostics: e.diagnostics
};
}
function loadEnabledBundleMcpConfig(e) {
const t = loadEnabledBundleConfig({
workspaceDir: e.workspaceDir,
cfg: e.cfg,
manifestRegistry: e.manifestRegistry,
createEmptyConfig: () => ({
mcpServers: {},
prepareDataDirsByServer: {},
pluginIdsByServer: {}
}),
loadBundleConfig: (e) => withMcpPluginOwnership(e.pluginId, loadBundleMcpConfig(e)),
loadNativePluginConfig: ({ record: e }) => e.mcpServers ? withMcpPluginOwnership(e.id, loadRootRelativeMcpConfig({
rootDir: e.rootDir,
mcpServers: e.mcpServers
})) : void 0
});
return {
config: { mcpServers: t.config.mcpServers },
diagnostics: t.diagnostics,
pluginIdsByServer: t.config.pluginIdsByServer,
prepareDataDirsByServer: Object.fromEntries(Object.entries(t.config.prepareDataDirsByServer).filter((e) => e[1] !== null))
};
}
function withMcpPluginOwnership(e, t) {
return {
...t,
config: {
...t.config,
pluginIdsByServer: Object.fromEntries(Object.keys(t.config.mcpServers).map((t) => [t, e]))
}
};
}
//#endregion
export { loadEnabledBundleMcpConfig as i, inspectBundleMcpRuntimeSupport as n, inspectNativePluginMcpRuntimeSupport as r, extractMcpServerMap as t };