UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 7.72 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{r as truncateUtf16Safe}from"./utf16-slice-DA3Mllbx.mjs";import{i as getPluginToolMeta,o as setPluginToolMeta}from"./tool-metadata-Cb5nBZYD.mjs";import{t as prepareConversationCapabilityProfile}from"./conversation-capability-profile-DiRRWN6T.mjs";import{u as partitionMcpServersByConnectionScope}from"./bundle-mcp-config-C-R5gRDz.mjs";import{t as loadSessionMcpConfig}from"./agent-bundle-mcp-runtime-config-pqoelK3d.mjs";import{r as mergeMcpConnectCatalog,t as buildConfiguredRequesterConnectCatalog}from"./agent-bundle-mcp-requester-connect-BISFIYTK.mjs";import{a as getAdvertisedScopedMcpCatalog,c as rememberAdvertisedScopedMcpCatalog,l as retireSessionMcpRuntime,n as acquireSessionMcpRuntime,t as acquireRequesterScopedMcpRuntime}from"./agent-bundle-mcp-manager-api-CpNDElt4.mjs";import{r as materializeBundleMcpToolsForRun,t as buildBundleMcpToolsFromCatalog}from"./agent-bundle-mcp-materialize-BrUeyxQs.mjs";import{t as applyFinalEffectiveToolPolicy}from"./effective-tool-policy-CLE4YHgE.mjs";import{t as applyEmbeddedAttemptToolsAllow}from"./attempt-tool-construction-plan-DxPA0wqy.mjs";import{i as resolveProjectedMcpCodexToolApprovalMode,r as requiresMcpCodexToolApproval,t as formatMcpCodexApprovalRemedy}from"./mcp-codex-tool-approval-BEHr8MWJ.mjs"; //!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,n){const r=n?`Requester has not connected MCP server "${e}" (tool "${t}") for this turn.`:`MCP server "${e}" (tool "${t}") connects with each user's own account, and this turn has no verified user (for example a scheduled run, a subagent or a completion). No other account is used. Ask the user to request this in their own message.`;return{content:[{type:`text`,text:r}],details:{status:`error`,error:r,mcpServer:e,mcpTool:t}}}function buildSenderlessConnectCatalog(e){const{loaded:t,safeServerNamesByServer:n}=loadSessionMcpConfig({workspaceDir:e.workspaceDir,cfg:e.cfg,manifestRegistry:e.manifestRegistry,toolOverrides:e.toolOverrides,logDiagnostics:false});return buildConfiguredRequesterConnectCatalog({serverNames:partitionMcpServersByConnectionScope(t.mcpServers).oauthRequesterServerNames,mcpServers:t.mcpServers,safeServerNamesByServer:n})}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})}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&&e.step.source?.kind!==`config`)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=>truncateUtf16Safe(e.replaceAll(/\s+/g,` `).trim(),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}}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=normalizeOptionalString(e.requesterSenderId)!==void 0;const s=getAdvertisedScopedMcpCatalog(e.sessionId)??(a?mergeMcpConnectCatalog(a,r?.requesterConnect):o?void 0:buildSenderlessConnectCatalog(e));if(!s||s.tools.length===0){await i?.dispose();return}const c=e.reservedToolNames?Array.from(e.reservedToolNames):void 0;const l=r?.requesterConnect;const u=buildBundleMcpToolsFromCatalog({catalog:s,reservedToolNames:c,createExecute:e=>l?.createExecute(e.serverName)??(async()=>notConnectedToolResult(e.serverName,e.toolName,o))});const d=new Map((i?.tools??[]).map(e=>[e.name,e]));const f=applyHarnessToolPolicy(u.map(e=>d.get(e.name)??e),t);const p=applyHarnessToolPolicy(u,t);return{tools:e.requestInteractiveCodexApproval?applyConfiguredMcpApproval(f,{fullPermission:e.autoApproveCodexAppServerApprovals===true,requestApproval:e.requestInteractiveCodexApproval}):f,advertisedTools:p,dispose:i?.dispose??(async()=>{})}}catch(e){await i?.dispose();throw e}}export{materializeStaticMcpToolsForHarnessRunCore as n,materializeRequesterScopedMcpToolsForHarnessRunCore as t};