UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

128 lines (127 loc) • 4.99 kB
import { m as readNonBlankString } from "./string-coerce-CCIiB4H7.mjs"; import { L as resolvePositiveTimerTimeoutMs, h as clampPositiveTimerTimeoutMs } from "./number-coercion-D6u-4YD8.mjs"; import { i as asOptionalObjectRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { a as redactSensitiveUrl, o as redactSensitiveUrlLikeString } from "./redact-sensitive-url-D-E7DGoc.mjs"; import { n as sanitizeForLog } from "./ansi-CZa2t0Hf.mjs"; import { t as createDedupeCache } from "./dedupe-DxT0h2gR.mjs"; import { i as logWarn } from "./logger-CIwfuhOU.mjs"; import { r as resolveConfiguredMcpTransport } from "./mcp-config-normalize-DxJwGDsa.mjs"; import { n as resolveStdioMcpServerLaunchConfig, r as toMcpStringRecord, t as describeStdioMcpServerLaunchConfig } from "./mcp-stdio-BbrQn34X.mjs"; //#region src/agents/mcp-http.ts /** * HTTP MCP launch config normalization. * * MCP server setup uses this to validate SSE/streamable HTTP server records, * sanitize headers, and redact sensitive URLs in diagnostics. */ /** Normalizes an HTTP MCP server config record into a launchable transport config. */ function resolveHttpMcpServerLaunchConfig(e, t) { if (!isRecord(e)) return { ok: false, reason: "server config must be an object" }; if (typeof e.url !== "string" || e.url.trim().length === 0) return { ok: false, reason: "its url is missing" }; const n = e.url.trim(); const r = URL.parse(n); if (!r) return { ok: false, reason: `its url is not a valid URL: ${redactSensitiveUrlLikeString(n)}` }; if (r.protocol !== "http:" && r.protocol !== "https:") return { ok: false, reason: `only http and https URLs are supported, got ${r.protocol}` }; let i; if (e.headers !== void 0 && e.headers !== null) { if (!isRecord(e.headers)) t?.onMalformedHeaders?.(e.headers); else i = toMcpStringRecord(e.headers, { onDroppedEntry: t?.onDroppedHeader }); } return { ok: true, config: { transportType: t?.transportType ?? "sse", url: n, headers: i } }; } //#endregion //#region src/agents/mcp-transport-config.ts /** * Resolves MCP transport command, environment, and timeout configuration. */ const DEFAULT_CONNECTION_TIMEOUT_MS = 3e4; const DEFAULT_REQUEST_TIMEOUT_MS = 6e4; const warnedDroppedStdioEnvKeys = createDedupeCache({ ttlMs: 0, maxSize: 4096 }); function warnDroppedStdioEnvOnce(e, t) { const n = sanitizeForLog(e); const r = sanitizeForLog(t); if (warnedDroppedStdioEnvKeys.check(JSON.stringify([e, t]))) return; logWarn(`bundle-mcp: server "${n}": env "${r}" is blocked for stdio startup safety and was ignored.`); } function resolveMcpRequestTimeoutMs(e, t = DEFAULT_REQUEST_TIMEOUT_MS) { return clampPositiveTimerTimeoutMs(asOptionalObjectRecord(e)?.requestTimeoutMs) ?? resolvePositiveTimerTimeoutMs(t, DEFAULT_REQUEST_TIMEOUT_MS); } /** Resolve one MCP server's launch transport config, or null when unsupported. */ function resolveMcpTransportConfig(e, t, n) { const r = asOptionalObjectRecord(t); const common = () => ({ connectionTimeoutMs: resolvePositiveTimerTimeoutMs(r?.connectionTimeoutMs, DEFAULT_CONNECTION_TIMEOUT_MS), requestTimeoutMs: resolveMcpRequestTimeoutMs(t), supportsParallelToolCalls: r?.supportsParallelToolCalls === true }); const i = n?.logWarnings !== false; const a = resolveConfiguredMcpTransport(t); const o = resolveStdioMcpServerLaunchConfig(t, i ? { onDroppedEnv: (t) => { warnDroppedStdioEnvOnce(e, t); } } : void 0); if (o.ok) return { kind: "stdio", transportType: "stdio", ...o.config, description: describeStdioMcpServerLaunchConfig(o.config), ...common() }; if (a && a !== "sse" && a !== "streamable-http") { if (i) logWarn(`bundle-mcp: skipped server "${sanitizeForLog(e)}" because transport "${sanitizeForLog(a)}" is not supported.`); return null; } const s = a === "streamable-http" ? "streamable-http" : "sse"; const c = resolveHttpMcpServerLaunchConfig(t, i ? { transportType: s, onDroppedHeader: (t) => { logWarn(`bundle-mcp: server "${e}": header "${t}" has an unsupported value type and was ignored.`); }, onMalformedHeaders: () => { logWarn(`bundle-mcp: server "${e}": "headers" must be a JSON object; the value was ignored.`); } } : { transportType: s }); if (!c.ok) { if (i) logWarn(`bundle-mcp: skipped server "${sanitizeForLog(e)}" because ${o.reason} and ${c.reason}.`); return null; } const l = r?.oauth; const u = readNonBlankString(r?.clientCert); const d = readNonBlankString(r?.clientKey); return { kind: "http", transportType: c.config.transportType, url: c.config.url, headers: c.config.headers, ...r?.auth === "oauth" ? { auth: "oauth" } : {}, ...isRecord(l) ? { oauth: l } : {}, ...typeof r?.sslVerify === "boolean" ? { sslVerify: r.sslVerify } : {}, ...u ? { clientCert: u } : {}, ...d ? { clientKey: d } : {}, description: redactSensitiveUrl(c.config.url), ...common() }; } //#endregion export { resolveMcpTransportConfig as n, resolveMcpRequestTimeoutMs as t };