UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

212 lines (211 loc) • 7.29 kB
import { r as resolveGlobalMap } from "./global-singleton-D7yf05nR.mjs"; import { s as raceWithTimeout } from "./src-BPmQBLJp.mjs"; import { t as createDeferredCore } from "./deferred-D0La5CRk.mjs"; //#region src/gateway/mcp-http.loopback-runtime.ts let activeRuntime; let nextToolCallCaptureGeneration = 0; const toolCallCaptures = resolveGlobalMap(Symbol.for("openclaw.mcpLoopbackToolCallCaptures"), (e) => { for (const t of e.keys()) deleteMcpLoopbackToolCallCapture(t); }); function observeCapture(e) { try { return e(); } catch { return; } } function deleteMcpLoopbackToolCallCapture(e) { const t = toolCallCaptures.get(e); if (!t) return; toolCallCaptures.delete(e); for (const e of t.activityWaiters) e(); t.activityWaiters.clear(); } function notifyMcpLoopbackToolCallCaptureActivity(e) { for (const t of e.activityWaiters) t(); e.activityWaiters.clear(); } /** Start loopback tool-call result capture for one serialized CLI invocation. */ function beginMcpLoopbackToolCallCapture({ captureKey: e, ...t }) { const n = e.trim(); if (!n) return; nextToolCallCaptureGeneration += 1; toolCallCaptures.set(n, { ...t, generation: nextToolCallCaptureGeneration, inFlight: 0, activityWaiters: /* @__PURE__ */ new Set() }); } /** Resolve yield state bound to the request's admitted CLI capture generation. */ function resolveMcpLoopbackYieldContext(e) { const t = e?.capture; if (!t?.onYield) return; return { cacheKey: String(t.generation), onYield: async (e, n) => { await t.onYield?.(e, n); } }; } /** Bind an authenticated HTTP request to the active capture generation before reading its body. */ function markMcpLoopbackRequestStarted(e) { const t = e?.trim() ?? ""; if (!t) return; const n = toolCallCaptures.get(t); if (!n) return; n.inFlight += 1; notifyMcpLoopbackToolCallCaptureActivity(n); observeCapture(() => n.onRequestStart?.()); return { capture: n, classified: false, finished: false }; } /** Mark a request body as parsed so it no longer represents an unknown possible send. */ function markMcpLoopbackRequestClassified(e) { if (!e || e.classified || e.finished) return; e.classified = true; observeCapture(() => e.capture.onRequestClassified?.()); } /** Mark an authenticated request as settled and wake capture drains. */ function markMcpLoopbackRequestFinished(e) { if (!e || e.finished) return; markMcpLoopbackRequestClassified(e); e.finished = true; const { capture: t } = e; t.inFlight = Math.max(0, t.inFlight - 1); notifyMcpLoopbackToolCallCaptureActivity(t); } /** Mark a captured loopback tool call as in flight. */ function markMcpLoopbackToolCallStarted(e) { const t = e.toolName.trim(); if (!t || e.requestCaptureHandle?.finished) return; const n = e.captureKey?.trim() ?? ""; const r = e.requestCaptureHandle?.capture ?? toolCallCaptures.get(n); if (!r) return; const i = { toolName: t, args: e.args }; r.inFlight += 1; notifyMcpLoopbackToolCallCaptureActivity(r); const a = observeCapture(() => r.onToolCallStart?.(i)); return { capture: r, call: i, correlationId: typeof a === "string" ? a : void 0, prepared: false, finished: false }; } /** Update an admitted call with the final arguments produced by gateway hooks. */ function updateMcpLoopbackToolCallCapture(e, t) { if (!e || e.finished) return; const n = e.call; e.call = t; e.prepared = true; observeCapture(() => { e.capture.onToolCallUpdate?.({ previous: n, current: t }); }); } /** Report a completed call without letting observer failures alter tool execution. */ function recordMcpLoopbackToolCallResult(e) { const t = e.toolName.trim(); if (!t) return; observeCapture(() => { const n = e.outcome === "blocked" ? { outcome: "blocked", deniedReason: e.deniedReason } : { outcome: e.outcome, result: e.result }; e.captureHandle.capture.onToolCallResult({ toolName: t, args: e.args, ...n, ...e.captureHandle.correlationId ? { correlationId: e.captureHandle.correlationId } : {} }); }); } /** Mark a captured loopback tool call as settled and wake idle drains. */ function markMcpLoopbackToolCallFinished(e) { if (!e || e.finished) return; e.finished = true; const { capture: t } = e; observeCapture(() => { t.onToolCallFinish?.(e.call, { prepared: e.prepared }); }); t.inFlight = Math.max(0, t.inFlight - 1); notifyMcpLoopbackToolCallCaptureActivity(t); } async function waitForMcpLoopbackToolCallCaptureActivity(e, t) { const n = createDeferredCore(); const resolveActivity = () => n.resolve(true); e.activityWaiters.add(resolveActivity); try { return await raceWithTimeout(n.promise, Math.max(0, t), () => false, { ref: false }); } finally { e.activityWaiters.delete(resolveActivity); } } /** Wait for admitted requests and tool calls to settle before closing their grant. */ async function waitForMcpLoopbackToolCallCaptureIdle(e, t) { const n = e.trim(); const r = toolCallCaptures.get(n); if (!r) return true; const i = performance.now() + Math.max(0, t.timeoutMs); while (toolCallCaptures.get(n) === r) { const e = i - performance.now(); if (e <= 0) return false; if (r.inFlight > 0) { await waitForMcpLoopbackToolCallCaptureActivity(r, e); continue; } return true; } return true; } /** Clear observers for this capture key. Grant admission is fenced separately. */ function clearMcpLoopbackToolCallCapture(e) { deleteMcpLoopbackToolCallCapture(e.trim()); } /** Return a copy of the active loopback runtime, if one has been installed. */ function getActiveMcpLoopbackRuntime() { return activeRuntime ? { ...activeRuntime } : void 0; } /** Install the active loopback runtime used by in-process MCP callers. */ function setActiveMcpLoopbackRuntime(e) { activeRuntime = { ...e }; } /** Clear loopback runtime only when the owning token matches the active runtime. */ function clearActiveMcpLoopbackRuntimeByOwnerToken(e) { if (activeRuntime?.ownerToken === e) activeRuntime = void 0; } const MCP_AUTH_HEADERS = { Authorization: "Bearer ${OPENCLAW_MCP_TOKEN}" }; const MCP_CAPTURE_HEADERS = { "x-openclaw-cli-capture-key": "${OPENCLAW_MCP_CLI_CAPTURE_KEY}" }; function createMcpServerConfig(e, t, n = true) { return { mcpServers: { openclaw: { type: "http", url: `http://127.0.0.1:${e}/mcp`, alwaysLoad: n, headers: t } } }; } /** Build the MCP server config injected into agents for loopback tool access. */ function createMcpLoopbackServerConfig(e, t) { return createMcpServerConfig(e, { ...MCP_AUTH_HEADERS, ...MCP_CAPTURE_HEADERS }, !t?.deferTools); } function createMcpAttachGrantServerConfig(e) { return createMcpServerConfig(e, MCP_AUTH_HEADERS); } //#endregion export { createMcpLoopbackServerConfig as a, markMcpLoopbackRequestFinished as c, markMcpLoopbackToolCallStarted as d, recordMcpLoopbackToolCallResult as f, waitForMcpLoopbackToolCallCaptureIdle as g, updateMcpLoopbackToolCallCapture as h, createMcpAttachGrantServerConfig as i, markMcpLoopbackRequestStarted as l, setActiveMcpLoopbackRuntime as m, clearActiveMcpLoopbackRuntimeByOwnerToken as n, getActiveMcpLoopbackRuntime as o, resolveMcpLoopbackYieldContext as p, clearMcpLoopbackToolCallCapture as r, markMcpLoopbackRequestClassified as s, beginMcpLoopbackToolCallCapture as t, markMcpLoopbackToolCallFinished as u };