mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-17 06:41:36 +00:00
* feat: node-hosted plugins — dynamic tools, MCP servers, and skills Nodes become declarative plugin hosts: - node.pluginTools.update: node hosts publish plugin-registered agent tool descriptors; gateway materializes them as agent tools executing via node.invoke under the node command allowlist, with tools.effective invalidation and node online/offline removal. - Trusted paired-node descriptors: no gateway-side plugin registration required; gateway.nodes.pluginTools.enabled off-switch (default on); description/count caps; deterministic node-prefixed collision names. - Declarative node-hosted MCP: nodeHost.mcp.servers (McpServerConfig shape) starts MCP clients on the node host, publishes tools as pluginId node-mcp, executes via built-in mcp.tools.call.v1 with per-layer timeouts, failure isolation, and orphan-safe shutdown. No re-pairing when servers change. - Node-hosted skills: node.skills.update publishes ~/.openclaw/skills content (64 skills/64KB/512KB caps both sides); gateway merges them into the skills snapshot while connected and exec host=node is available, with node:// locators, node-prefixed collisions, disabled command dispatch, and gateway.nodes.skills.enabled + nodeHost.skills.enabled switches. - Security: node-supplied pluginIds cannot satisfy pluginId-scoped tool allowlists unless gateway-registered; reserved node-mcp id requires the core MCP descriptor shape; protocol registry kept out of public plugin-sdk dts. - E2E: pond harness proves publication, MCP round-trip, skills locator, and disconnect/reconnect for all three surfaces. * style: format node-plugin-tools test * fix(skills): keep status loader unfiltered when eligibility is passed skills.status started passing eligibility for the node-skill merge, which flipped loadWorkspaceSkillEntries into filtered mode and dropped disabled skills from status reports (QA plugin-lifecycle-hot-reload timeout). Status now merges node skills explicitly around an unfiltered load. Also: regen docs_map for new node docs sections; add the intentional node-host MCP onclose suppression to the lint-suppression allowlist.
480 lines
15 KiB
TypeScript
480 lines
15 KiB
TypeScript
/** CLI runner for node-host stdin/stdout command dispatch. */
|
|
import fs from "node:fs";
|
|
import {
|
|
GATEWAY_CLIENT_MODES,
|
|
GATEWAY_CLIENT_NAMES,
|
|
} from "../../packages/gateway-protocol/src/client-info.js";
|
|
import { ConnectErrorDetailCodes } from "../../packages/gateway-protocol/src/connect-error-details.js";
|
|
import { getRuntimeConfig, type OpenClawConfig } from "../config/config.js";
|
|
import { startGatewayClientWhenEventLoopReady } from "../gateway/client-start-readiness.js";
|
|
import {
|
|
GatewayClient,
|
|
GatewayClientRequestError,
|
|
type GatewayReconnectPausedInfo,
|
|
} from "../gateway/client.js";
|
|
import { resolveGatewayConnectionAuth } from "../gateway/connection-auth.js";
|
|
import { loadOrCreateDeviceIdentity } from "../infra/device-identity.js";
|
|
import type { SkillBinTrustEntry } from "../infra/exec-approvals.js";
|
|
import { resolveExecutableFromPathEnv } from "../infra/executable-path.js";
|
|
import { getMachineDisplayName } from "../infra/machine-name.js";
|
|
import {
|
|
NODE_EXEC_APPROVALS_COMMANDS,
|
|
NODE_MCP_TOOLS_CALL_COMMAND,
|
|
NODE_SYSTEM_RUN_COMMANDS,
|
|
} from "../infra/node-commands.js";
|
|
import { ensureOpenClawCliOnPath } from "../infra/path-env.js";
|
|
import { VERSION } from "../version.js";
|
|
import { ensureNodeHostConfig, saveNodeHostConfig, type NodeHostGatewayConfig } from "./config.js";
|
|
import {
|
|
coerceNodeInvokePayload,
|
|
type SkillBinsProvider,
|
|
buildNodeInvokeResultParams,
|
|
handleInvoke,
|
|
} from "./invoke.js";
|
|
import {
|
|
countConfiguredNodeHostMcpServers,
|
|
startNodeHostMcpManager,
|
|
type NodeHostMcpManager,
|
|
} from "./mcp.js";
|
|
import {
|
|
ensureNodeHostPluginRegistry,
|
|
listRegisteredNodeHostCapsAndCommands,
|
|
} from "./plugin-node-host.js";
|
|
import { scanNodeHostedSkills } from "./skills.js";
|
|
|
|
export { buildNodeInvokeResultParams };
|
|
export { buildNodeEventParams } from "./invoke.js";
|
|
|
|
type NodeHostRunOptions = {
|
|
gatewayHost: string;
|
|
gatewayPort: number;
|
|
gatewayTls?: boolean;
|
|
gatewayTlsFingerprint?: string;
|
|
/** Optional WebSocket context path (e.g. "/openclaw-gw"). */
|
|
gatewayContextPath?: string;
|
|
nodeId?: string;
|
|
displayName?: string;
|
|
};
|
|
|
|
const DEFAULT_NODE_PATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
|
|
|
|
export function resolveNodeHostGatewayPlatform(platform: NodeJS.Platform): string {
|
|
switch (platform) {
|
|
case "darwin":
|
|
return "macos";
|
|
case "win32":
|
|
return "windows";
|
|
case "linux":
|
|
return "linux";
|
|
default:
|
|
return "unknown";
|
|
}
|
|
}
|
|
|
|
export function resolveNodeHostGatewayDeviceFamily(platform: NodeJS.Platform): string | undefined {
|
|
switch (platform) {
|
|
case "darwin":
|
|
return "Mac";
|
|
case "win32":
|
|
return "Windows";
|
|
case "linux":
|
|
return "Linux";
|
|
default:
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
function writeStderrLine(message: string): void {
|
|
process.stderr.write(`${message}\n`);
|
|
}
|
|
|
|
const NODE_HOST_EXIT_ON_RECONNECT_PAUSE_CODES: ReadonlySet<string> = new Set([
|
|
ConnectErrorDetailCodes.AUTH_TOKEN_MISSING,
|
|
ConnectErrorDetailCodes.AUTH_TOKEN_MISMATCH,
|
|
ConnectErrorDetailCodes.AUTH_BOOTSTRAP_TOKEN_INVALID,
|
|
ConnectErrorDetailCodes.AUTH_PASSWORD_MISSING,
|
|
ConnectErrorDetailCodes.AUTH_PASSWORD_MISMATCH,
|
|
ConnectErrorDetailCodes.CLIENT_VERSION_MISMATCH,
|
|
]);
|
|
|
|
type NodeHostReconnectPausedDeps = {
|
|
writeLine?: (message: string) => void;
|
|
exit?: (code: number) => void;
|
|
};
|
|
|
|
export function shouldExitNodeHostOnReconnectPaused(detailCode: string | null): boolean {
|
|
return detailCode !== null && NODE_HOST_EXIT_ON_RECONNECT_PAUSE_CODES.has(detailCode);
|
|
}
|
|
|
|
function formatNodeHostReconnectPausedMessage(
|
|
info: GatewayReconnectPausedInfo,
|
|
params?: { exiting?: boolean },
|
|
): string {
|
|
const detail = info.detailCode ? ` detail=${info.detailCode}` : "";
|
|
const reason = info.reason.trim() || "no close reason";
|
|
const action = params?.exiting ? "exiting for supervisor restart" : "waiting for operator action";
|
|
return `node host gateway reconnect paused after close (${info.code}): ${reason}${detail}; ${action}`;
|
|
}
|
|
|
|
export function handleNodeHostReconnectPaused(
|
|
info: GatewayReconnectPausedInfo,
|
|
deps: NodeHostReconnectPausedDeps = {},
|
|
): void {
|
|
const shouldExit = shouldExitNodeHostOnReconnectPaused(info.detailCode);
|
|
const writeLine = deps.writeLine ?? writeStderrLine;
|
|
writeLine(formatNodeHostReconnectPausedMessage(info, { exiting: shouldExit }));
|
|
if (!shouldExit) {
|
|
return;
|
|
}
|
|
const exit = deps.exit ?? ((code: number): never => process.exit(code));
|
|
exit(1);
|
|
}
|
|
|
|
function isUnsupportedNodePluginToolsUpdateError(error: unknown): boolean {
|
|
return (
|
|
error instanceof GatewayClientRequestError &&
|
|
error.gatewayCode === "INVALID_REQUEST" &&
|
|
error.message.includes("unknown method: node.pluginTools.update")
|
|
);
|
|
}
|
|
|
|
function isUnsupportedNodeSkillsUpdateError(error: unknown): boolean {
|
|
return (
|
|
error instanceof GatewayClientRequestError &&
|
|
error.gatewayCode === "INVALID_REQUEST" &&
|
|
error.message.includes("unknown method: node.skills.update")
|
|
);
|
|
}
|
|
|
|
async function publishNodePluginTools(client: GatewayClient, tools: unknown[]): Promise<void> {
|
|
if (tools.length === 0) {
|
|
return;
|
|
}
|
|
try {
|
|
await client.request("node.pluginTools.update", { tools });
|
|
} catch (error) {
|
|
if (isUnsupportedNodePluginToolsUpdateError(error)) {
|
|
return;
|
|
}
|
|
writeStderrLine(`node host plugin tool publish failed: ${String(error)}`);
|
|
}
|
|
}
|
|
|
|
async function publishNodeSkills(client: GatewayClient, skills: unknown[]): Promise<void> {
|
|
try {
|
|
await client.request("node.skills.update", { skills });
|
|
} catch (error) {
|
|
if (isUnsupportedNodeSkillsUpdateError(error)) {
|
|
return;
|
|
}
|
|
writeStderrLine(`node host skill publish failed: ${String(error)}`);
|
|
}
|
|
}
|
|
|
|
function resolveExecutablePathFromEnv(bin: string, pathEnv: string): string | null {
|
|
if (bin.includes("/") || bin.includes("\\")) {
|
|
return null;
|
|
}
|
|
return resolveExecutableFromPathEnv(bin, pathEnv) ?? null;
|
|
}
|
|
|
|
function resolveExecutableTrustPathFromEnv(bin: string, pathEnv: string): string | null {
|
|
const resolvedPath = resolveExecutablePathFromEnv(bin, pathEnv);
|
|
if (!resolvedPath) {
|
|
return null;
|
|
}
|
|
try {
|
|
return fs.realpathSync(resolvedPath);
|
|
} catch {
|
|
return resolvedPath;
|
|
}
|
|
}
|
|
|
|
function resolveSkillBinTrustEntries(bins: string[], pathEnv: string): SkillBinTrustEntry[] {
|
|
const trustEntries: SkillBinTrustEntry[] = [];
|
|
const seen = new Set<string>();
|
|
for (const bin of bins) {
|
|
const name = bin.trim();
|
|
if (!name) {
|
|
continue;
|
|
}
|
|
const resolvedPath = resolveExecutableTrustPathFromEnv(name, pathEnv);
|
|
if (!resolvedPath) {
|
|
continue;
|
|
}
|
|
const key = `${name}\u0000${resolvedPath}`;
|
|
if (seen.has(key)) {
|
|
continue;
|
|
}
|
|
seen.add(key);
|
|
trustEntries.push({ name, resolvedPath });
|
|
}
|
|
return trustEntries.toSorted(
|
|
(left, right) =>
|
|
left.name.localeCompare(right.name) || left.resolvedPath.localeCompare(right.resolvedPath),
|
|
);
|
|
}
|
|
|
|
class SkillBinsCache implements SkillBinsProvider {
|
|
private bins: SkillBinTrustEntry[] = [];
|
|
private lastRefresh = 0;
|
|
private readonly ttlMs = 90_000;
|
|
private readonly fetch: () => Promise<string[]>;
|
|
private readonly pathEnv: string;
|
|
|
|
constructor(fetch: () => Promise<string[]>, pathEnv: string) {
|
|
this.fetch = fetch;
|
|
this.pathEnv = pathEnv;
|
|
}
|
|
|
|
async current(force = false): Promise<SkillBinTrustEntry[]> {
|
|
if (force || Date.now() - this.lastRefresh > this.ttlMs) {
|
|
await this.refresh();
|
|
}
|
|
return this.bins;
|
|
}
|
|
|
|
private async refresh() {
|
|
try {
|
|
const bins = await this.fetch();
|
|
this.bins = resolveSkillBinTrustEntries(bins, this.pathEnv);
|
|
this.lastRefresh = Date.now();
|
|
} catch {
|
|
if (!this.lastRefresh) {
|
|
this.bins = [];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function ensureNodePathEnv(): string {
|
|
ensureOpenClawCliOnPath({ pathEnv: process.env.PATH ?? "" });
|
|
const current = process.env.PATH ?? "";
|
|
if (current.trim()) {
|
|
return current;
|
|
}
|
|
process.env.PATH = DEFAULT_NODE_PATH;
|
|
return DEFAULT_NODE_PATH;
|
|
}
|
|
|
|
export async function resolveNodeHostGatewayCredentials(params: {
|
|
config: OpenClawConfig;
|
|
env?: NodeJS.ProcessEnv;
|
|
}): Promise<{ token?: string; password?: string }> {
|
|
const mode = params.config.gateway?.mode === "remote" ? "remote" : "local";
|
|
const configForResolution =
|
|
mode === "local" ? buildNodeHostLocalAuthConfig(params.config) : params.config;
|
|
return await resolveGatewayConnectionAuth({
|
|
config: configForResolution,
|
|
env: params.env,
|
|
localTokenPrecedence: "env-first",
|
|
localPasswordPrecedence: "env-first", // pragma: allowlist secret
|
|
remoteTokenPrecedence: "env-first",
|
|
remotePasswordPrecedence: "env-first", // pragma: allowlist secret
|
|
});
|
|
}
|
|
|
|
function buildNodeHostLocalAuthConfig(config: OpenClawConfig): OpenClawConfig {
|
|
if (!config.gateway?.remote?.token && !config.gateway?.remote?.password) {
|
|
return config;
|
|
}
|
|
const nextConfig = structuredClone(config);
|
|
if (nextConfig.gateway?.remote) {
|
|
// Local node-host must not inherit gateway.remote.* auth material, which can
|
|
// suppress GatewayClient device-token fallback and cause local token mismatches.
|
|
nextConfig.gateway.remote.token = undefined;
|
|
nextConfig.gateway.remote.password = undefined;
|
|
}
|
|
return nextConfig;
|
|
}
|
|
|
|
export async function runNodeHost(opts: NodeHostRunOptions): Promise<void> {
|
|
const config = await ensureNodeHostConfig();
|
|
const nodeId = opts.nodeId?.trim() || config.nodeId;
|
|
if (nodeId !== config.nodeId) {
|
|
config.nodeId = nodeId;
|
|
}
|
|
const displayName =
|
|
opts.displayName?.trim() || config.displayName || (await getMachineDisplayName());
|
|
config.displayName = displayName;
|
|
|
|
const gateway: NodeHostGatewayConfig = {
|
|
host: opts.gatewayHost,
|
|
port: opts.gatewayPort,
|
|
tls: opts.gatewayTls ?? getRuntimeConfig().gateway?.tls?.enabled ?? false,
|
|
tlsFingerprint: opts.gatewayTlsFingerprint,
|
|
contextPath: opts.gatewayContextPath,
|
|
};
|
|
config.gateway = gateway;
|
|
await saveNodeHostConfig(config);
|
|
|
|
const cfg = getRuntimeConfig();
|
|
await ensureNodeHostPluginRegistry({ config: cfg, env: process.env });
|
|
const pluginNodeHost = listRegisteredNodeHostCapsAndCommands({ config: cfg, env: process.env });
|
|
const { token, password } = await resolveNodeHostGatewayCredentials({
|
|
config: cfg,
|
|
env: process.env,
|
|
});
|
|
|
|
const host = gateway.host ?? "127.0.0.1";
|
|
const port = gateway.port ?? 18789;
|
|
const scheme = gateway.tls ? "wss" : "ws";
|
|
const contextPath = gateway.contextPath
|
|
? gateway.contextPath.startsWith("/")
|
|
? gateway.contextPath
|
|
: `/${gateway.contextPath}`
|
|
: "";
|
|
const url = `${scheme}://${host}:${port}${contextPath}`;
|
|
const pathEnv = ensureNodePathEnv();
|
|
const mcpServers = cfg.nodeHost?.mcp?.servers;
|
|
const hasMcpServers = countConfiguredNodeHostMcpServers(mcpServers) > 0;
|
|
const nodeSkills = cfg.nodeHost?.skills?.enabled === false ? null : scanNodeHostedSkills();
|
|
const mcpStartupAbort = new AbortController();
|
|
const mcpRuntime: {
|
|
manager?: NodeHostMcpManager;
|
|
startup?: Promise<NodeHostMcpManager>;
|
|
} = {};
|
|
let gatewayHelloReceived = false;
|
|
|
|
const publishNodeToolsWhenReady = () => {
|
|
if (!gatewayHelloReceived || !mcpRuntime.manager) {
|
|
return;
|
|
}
|
|
const nodePluginTools = [
|
|
...pluginNodeHost.nodePluginTools,
|
|
...mcpRuntime.manager.descriptors,
|
|
].toSorted(
|
|
(left, right) =>
|
|
left.pluginId.localeCompare(right.pluginId) || left.name.localeCompare(right.name),
|
|
);
|
|
void publishNodePluginTools(client, nodePluginTools);
|
|
};
|
|
const closeMcpRuntime = async () => {
|
|
mcpStartupAbort.abort();
|
|
const manager = mcpRuntime.manager ?? (await mcpRuntime.startup?.catch(() => undefined));
|
|
await manager?.close();
|
|
};
|
|
|
|
const client = new GatewayClient({
|
|
url,
|
|
token: token || undefined,
|
|
password: password || undefined,
|
|
preauthHandshakeTimeoutMs: cfg.gateway?.handshakeTimeoutMs,
|
|
instanceId: nodeId,
|
|
clientName: GATEWAY_CLIENT_NAMES.NODE_HOST,
|
|
clientDisplayName: displayName,
|
|
clientVersion: VERSION,
|
|
platform: resolveNodeHostGatewayPlatform(process.platform),
|
|
deviceFamily: resolveNodeHostGatewayDeviceFamily(process.platform),
|
|
mode: GATEWAY_CLIENT_MODES.NODE,
|
|
role: "node",
|
|
scopes: [],
|
|
caps: ["system", ...(hasMcpServers ? ["mcp"] : []), ...pluginNodeHost.caps],
|
|
commands: [
|
|
...NODE_SYSTEM_RUN_COMMANDS,
|
|
...NODE_EXEC_APPROVALS_COMMANDS,
|
|
...(hasMcpServers ? [NODE_MCP_TOOLS_CALL_COMMAND] : []),
|
|
...pluginNodeHost.commands,
|
|
],
|
|
pathEnv,
|
|
permissions: undefined,
|
|
deviceIdentity: loadOrCreateDeviceIdentity(),
|
|
tlsFingerprint: gateway.tlsFingerprint,
|
|
onEvent: (evt) => {
|
|
if (evt.event !== "node.invoke.request") {
|
|
return;
|
|
}
|
|
const payload = coerceNodeInvokePayload(evt.payload);
|
|
if (!payload) {
|
|
return;
|
|
}
|
|
void handleInvoke(payload, client, skillBins, mcpRuntime.manager);
|
|
},
|
|
onHelloOk: () => {
|
|
writeStderrLine(`node host gateway connected: ${url}`);
|
|
gatewayHelloReceived = true;
|
|
if (nodeSkills) {
|
|
void publishNodeSkills(client, nodeSkills);
|
|
}
|
|
if (mcpRuntime.manager) {
|
|
publishNodeToolsWhenReady();
|
|
} else {
|
|
// Do not make existing plugin tools wait for optional MCP discovery.
|
|
void publishNodePluginTools(client, pluginNodeHost.nodePluginTools);
|
|
}
|
|
},
|
|
onConnectError: (err) => {
|
|
// keep retrying (handled by GatewayClient)
|
|
writeStderrLine(`node host gateway connect failed: ${err.message}`);
|
|
},
|
|
onReconnectPaused: (info) => {
|
|
handleNodeHostReconnectPaused(info, {
|
|
exit: (code) => {
|
|
client.stop();
|
|
// Terminal auth/version pauses restart under a supervisor; close MCP
|
|
// subprocesses first so restart loops cannot orphan server processes.
|
|
void closeMcpRuntime().finally(() => process.exit(code));
|
|
},
|
|
});
|
|
},
|
|
onClose: (code, reason) => {
|
|
writeStderrLine(`node host gateway closed (${code}): ${reason}`);
|
|
},
|
|
});
|
|
|
|
const skillBins = new SkillBinsCache(async () => {
|
|
const res = await client.request<{ bins: Array<unknown> }>("skills.bins", {});
|
|
const bins = Array.isArray(res?.bins) ? res.bins.map((bin) => String(bin)) : [];
|
|
return bins;
|
|
}, pathEnv);
|
|
|
|
let stopping = false;
|
|
let resolveStopped: (() => void) | undefined;
|
|
const stopped = new Promise<void>((resolve) => {
|
|
resolveStopped = resolve;
|
|
});
|
|
const removeSignalHandlers = () => {
|
|
process.off("SIGINT", onSigint);
|
|
process.off("SIGTERM", onSigterm);
|
|
};
|
|
const finish = async (exitCode: number) => {
|
|
if (stopping) {
|
|
return;
|
|
}
|
|
stopping = true;
|
|
removeSignalHandlers();
|
|
client.stop();
|
|
await closeMcpRuntime();
|
|
process.exitCode = exitCode;
|
|
resolveStopped?.();
|
|
};
|
|
const onSigint = () => void finish(130);
|
|
const onSigterm = () => void finish(143);
|
|
process.once("SIGINT", onSigint);
|
|
process.once("SIGTERM", onSigterm);
|
|
|
|
const readinessPromise = startGatewayClientWhenEventLoopReady(client, {
|
|
clientOptions: { preauthHandshakeTimeoutMs: cfg.gateway?.handshakeTimeoutMs },
|
|
});
|
|
// Gateway startup begins first; optional MCP discovery must not delay core node availability.
|
|
mcpRuntime.startup = startNodeHostMcpManager(mcpServers, { signal: mcpStartupAbort.signal }).then(
|
|
(manager) => {
|
|
mcpRuntime.manager = manager;
|
|
publishNodeToolsWhenReady();
|
|
return manager;
|
|
},
|
|
);
|
|
const readiness = await readinessPromise;
|
|
if (!readiness.ready) {
|
|
if (stopping) {
|
|
await stopped;
|
|
return;
|
|
}
|
|
removeSignalHandlers();
|
|
client.stop();
|
|
await closeMcpRuntime();
|
|
throw new Error("node host gateway event loop readiness timeout");
|
|
}
|
|
await stopped;
|
|
}
|