UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

221 lines (220 loc) • 8.12 kB
import { i as getPluginToolMeta, o as setPluginToolMeta } from "./tool-metadata-rqUpUHlb.mjs"; import { t as prepareConversationCapabilityProfile } from "./conversation-capability-profile-Be5DqCww.mjs"; import { n as mergeMcpConnectCatalog } from "./agent-bundle-mcp-requester-connect-DWToqIx_.mjs"; import { a as getAdvertisedScopedMcpCatalog, c as rememberAdvertisedScopedMcpCatalog, l as retireSessionMcpRuntime, n as acquireSessionMcpRuntime, t as acquireRequesterScopedMcpRuntime } from "./agent-bundle-mcp-manager-api-Um6GkoIv.mjs"; import { r as materializeBundleMcpToolsForRun, t as buildBundleMcpToolsFromCatalog } from "./agent-bundle-mcp-materialize-DM8C34_b.mjs"; import { t as applyFinalEffectiveToolPolicy } from "./effective-tool-policy-DMFsqGJo.mjs"; import { t as applyEmbeddedAttemptToolsAllow } from "./attempt-tool-construction-plan-Cp8YcsYs.mjs"; import { i as resolveProjectedMcpCodexToolApprovalMode, r as requiresMcpCodexToolApproval, t as formatMcpCodexApprovalRemedy } from "./mcp-codex-tool-approval-C6Ex4SQ6.mjs"; //#region src/agents/agent-bundle-mcp-harness.ts function applyConfiguredMcpApproval(e, t) { return e.flatMap((e) => { const n = getPluginToolMeta(e)?.mcp; if (n?.operation !== "tool" || n.oauthConnectBootstrap === true) return [e]; const r = resolveProjectedMcpCodexToolApprovalMode(n.serverName, {}, t.projectedMcpServers?.[n.serverName], n.toolName) ?? n.codexApproval?.mode; if (!requiresMcpCodexToolApproval({ ...n.codexApproval, mode: r, fullPermission: t.fullPermission })) return [e]; const i = r === "prompt" ? "prompt" : "auto"; const a = t.requestApproval; if (!a) { t.onOmitted?.(`${n.serverName}/${n.toolName}: requires interactive Codex approval (${i}); ${formatMcpCodexApprovalRemedy(n.serverName)}`); return []; } const o = getPluginToolMeta(e); const s = e.execute; const c = { ...e, execute: async (t, r, o, c) => { let l = true; try { await a({ signal: o, safeToolName: e.name, toolCallId: t, serverName: n.serverName, toolName: n.toolName, mode: i, isActive: () => l }); return await s(t, r, o, c); } finally { l = false; } } }; setPluginToolMeta(c, o); return [c]; }); } function notConnectedToolResult(e, t) { const n = `Requester has not connected MCP server "${e}" (tool "${t}") for this turn.`; return { content: [{ type: "text", text: n }], details: { status: "error", error: n, mcpServer: e, mcpTool: t } }; } async function resolveHarnessCapabilityProfile(e) { return e.conversationCapabilityProfile ?? (e.policyContext ? prepareConversationCapabilityProfile({ ...e.policyContext, runtimeToolAllowlist: e.toolsAllow }) : void 0); } function applyHarnessToolPolicy(e, t) { if (e.length === 0) return e; const n = applyEmbeddedAttemptToolsAllow(e, t.toolsAllow, { toolMeta: (e) => getPluginToolMeta(e) }); const r = t.conversationCapabilityProfile; if (!r) return n; return applyFinalEffectiveToolPolicy({ bundledTools: n, config: t.policyContext?.config ?? t.cfg, conversationCapabilityProfile: r, warn: t.warn ?? (() => void 0), onFilter: t.onFilter }); } /** * Materialize static configured MCP for a Codex harness turn. * No requester identity is accepted here, so requester resolvers stay unreachable. */ async function materializeStaticMcpToolsForHarnessRunCore(e) { const t = await resolveHarnessCapabilityProfile(e); const n = await acquireSessionMcpRuntime({ sessionId: e.sessionId, sessionKey: e.sessionKey, agentId: e.agentId ?? t?.agentId, workspaceDir: e.workspaceDir, agentDir: e.agentDir, cfg: e.cfg, manifestRegistry: e.manifestRegistry, toolOverrides: e.toolOverrides, toolDenylist: t?.policy.explicitToolDenylist }); const r = e.retireSessionRuntimeAfterDispose ? async () => { await retireSessionMcpRuntime({ sessionId: e.sessionId, reason: "scheduled-authority-snapshot-complete" }); } : void 0; const i = await materializeBundleMcpToolsForRun({ ...n, agentId: e.agentId, reservedToolNames: e.reservedToolNames, ...r ? { disposeRuntime: r } : {} }); try { const n = []; const r = { ...e, conversationCapabilityProfile: t, onFilter: (e) => { const t = e.before.length - e.after.length; if (t > 0) n.push(`${e.step.label}: ${t} configured MCP tool(s) omitted by policy`); } }; const a = e.autoApproveCodexAppServerApprovals === true; const o = applyHarnessToolPolicy(i.tools, r); const s = e.projectedMcpServers ? { projectedMcpServers: e.projectedMcpServers } : {}; const c = applyConfiguredMcpApproval(o, { fullPermission: a, ...s, ...e.requestInteractiveCodexApproval ? { requestApproval: e.requestInteractiveCodexApproval } : {}, onOmitted: (e) => n.push(e) }); i.restrictAppTools?.(applyConfiguredMcpApproval(applyHarnessToolPolicy(i.appTools ?? i.tools, r), { fullPermission: a, ...s, ...e.requestInteractiveCodexApproval ? {} : { onOmitted: (e) => n.push(e) } })); const l = [...(i.diagnostics ?? []).map((e) => `${e.serverName}: ${e.message}`), ...n]; const u = e.requestInteractiveCodexApproval ? "this run" : "this scheduled run"; const d = [...new Set(l)].map((e) => e.replaceAll(/\s+/g, " ").trim().slice(0, 180)).filter(Boolean).slice(0, 4); const f = d.length > 0 ? `Configured MCP is incomplete for ${u}: ${d.join("; ")}. Do not claim MCP-backed work succeeded; report this blocker to the operator.` : void 0; return { tools: c, ...f ? { diagnosticNotice: f } : {}, dispose: i.dispose }; } catch (e) { await i.dispose(); throw e; } } /** * Materialize requester-scoped MCP tools for a harness run (e.g. Codex dynamic tools). * Updates the session advertised-catalog cache when a requester resolves a catalog. * Before any requester resolves in the session, returns undefined (nothing to advertise). */ async function materializeRequesterScopedMcpToolsForHarnessRunCore(e) { const t = { ...e, conversationCapabilityProfile: await resolveHarnessCapabilityProfile(e) }; const n = await acquireRequesterScopedMcpRuntime({ sessionId: e.sessionId, sessionKey: e.sessionKey, agentId: e.agentId ?? t.conversationCapabilityProfile?.agentId, workspaceDir: e.workspaceDir, agentDir: e.agentDir, cfg: e.cfg, manifestRegistry: e.manifestRegistry, toolOverrides: e.toolOverrides, requesterSenderId: e.requesterSenderId, agentAccountId: e.agentAccountId, messageChannel: e.messageChannel, toolDenylist: t.conversationCapabilityProfile?.policy.explicitToolDenylist }); const r = n?.runtime; let i; let a; try { if (r) { i = await materializeBundleMcpToolsForRun({ runtime: r, releaseLease: n?.releaseLease, agentId: e.agentId, reservedToolNames: e.reservedToolNames }); a = r.peekCatalog() ?? await r.getCatalog(); if (a.tools.length > 0 && n) rememberAdvertisedScopedMcpCatalog(n, a); } const o = getAdvertisedScopedMcpCatalog(e.sessionId) ?? (a ? mergeMcpConnectCatalog(a, r?.requesterConnect) : void 0); if (!o || o.tools.length === 0) { await i?.dispose(); return; } const s = e.reservedToolNames ? Array.from(e.reservedToolNames) : void 0; const c = r?.requesterConnect; const l = buildBundleMcpToolsFromCatalog({ catalog: o, reservedToolNames: s, createExecute: (e) => c?.createExecute(e.serverName) ?? (async () => notConnectedToolResult(e.serverName, e.toolName)) }); const u = new Map((i?.tools ?? []).map((e) => [e.name, e])); const d = applyHarnessToolPolicy(l.map((e) => u.get(e.name) ?? e), t); const f = applyHarnessToolPolicy(l, t); return { tools: e.requestInteractiveCodexApproval ? applyConfiguredMcpApproval(d, { fullPermission: e.autoApproveCodexAppServerApprovals === true, requestApproval: e.requestInteractiveCodexApproval }) : d, advertisedTools: f, dispose: i?.dispose ?? (async () => {}) }; } catch (e) { await i?.dispose(); throw e; } } //#endregion export { materializeRequesterScopedMcpToolsForHarnessRunCore, materializeStaticMcpToolsForHarnessRunCore };