openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.03 kB
JavaScript
import{r as truncateUtf16Safe}from"./utf16-slice-DA3Mllbx.mjs";import{a as sanitizeExecApprovalWarningText,r as sanitizeExecApprovalDisplayText}from"./exec-approval-text-sanitize-h0o6Uj2u.mjs";import{l as truncatePluginApprovalDetail}from"./plugin-approvals-NfNDMxex.mjs";import{t as bindApprovalRequesterMetadata}from"./approval-shared-D5K52j8A.mjs";import{t as handlePendingApprovalRequestWithDelivery}from"./approval-request-delivery-D7M7U3li.mjs";import{randomUUID}from"node:crypto";
//!src/gateway/mcp-app-tool-approval.ts
async function requestMcpAppToolApproval(e){const{options:t,assertCurrent:n,view:r,requesterId:i}=e;n();const a=JSON.stringify([e.serverName,e.toolName]);if(r?.toolApprovalGrants?.get(i)?.has(a))return;const o=t.context.pluginApprovalManager;if(!o)throw new Error(`MCP App approval service is unavailable`);const s=truncatePluginApprovalDetail(sanitizeExecApprovalWarningText(JSON.stringify({server:e.serverName,tool:e.toolName,arguments:e.input},null,2)));const c=12e4;const l=randomUUID();const u={pluginId:`bundle-mcp`,title:truncateUtf16Safe(sanitizeExecApprovalDisplayText(`Run `+e.toolName+`?`),80),description:truncateUtf16Safe(sanitizeExecApprovalWarningText(`Allow this MCP App to call `+e.serverName+`/`+e.toolName+(r?` once, or while this App stays open?`:` once?`)),512),detail:s,severity:`warning`,scope:null,toolName:sanitizeExecApprovalDisplayText(e.toolName),toolCallId:l,agentId:e.agentId,sessionKey:e.sessionKey,runId:null,allowedDecisions:r?[`allow-once`,`allow-always`,`deny`]:[`allow-once`,`deny`],actions:[{kind:`decision`,decision:`allow-once`,label:`Allow once`,command:`allow-once`},...r?[{kind:`decision`,decision:`allow-always`,label:`Allow while this App is open`,command:`allow-always`}]:[],{kind:`decision`,decision:`deny`,label:`Deny`,command:`deny`}],turnSourceChannel:null,turnSourceTo:null,turnSourceAccountId:null,turnSourceThreadId:null};const d=o.create(u,c,`plugin:`+randomUUID());d.approvalAuthority=()=>{n();return true};d.approvalSignals=[t.signal,e.signal].filter(e=>e!==void 0);bindApprovalRequesterMetadata({record:d,client:t.client});const{decision:f}=await o.register(d,c);n();await handlePendingApprovalRequestWithDelivery({approvalKind:`plugin`,manager:o,record:d,respond:()=>{},context:t.context,twoPhase:false,forwardRequest:t.context.forwardPluginApprovalRequest,getIosPushDelivery:()=>t.context.pluginApprovalIosPushDelivery});n();const p=o.projectDecisionIfActive(d.id,await f);if(p!==`allow-once`&&!(p===`allow-always`&&r))throw new Error(`MCP App tool approval was denied, cancelled, or expired`);n();if(p===`allow-once`&&!await o.consumeAllowOnce(d.id,`mcp.app:`+l))throw new Error(`MCP App tool approval is no longer available`);n();if(o.projectDecisionIfActive(d.id,p)!==p)throw new Error(`MCP App tool approval authority changed`);if(p===`allow-always`&&r){r.toolApprovalGrants??=/* @__PURE__ */ new Map;const e=r.toolApprovalGrants.get(i)??/* @__PURE__ */ new Set;e.add(a);r.toolApprovalGrants.set(i,e)}}export{requestMcpAppToolApproval as t};