UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.64 kB
import{l as isRecord,u as isStringArray}from"./record-coerce-BTbqdJ3-.mjs";import{r as truncateUtf16Safe}from"./utf16-slice-DA3Mllbx.mjs";import{n as operatorScopeSatisfied}from"./operator-scope-compat-TmgAKmD2.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{t as respondError}from"./response-BYbuAh7a.mjs";import{t as assertActiveAgentRuntimeAuthority}from"./agent-runtime-authority-Ciwflyog.mjs";import{t as resolveAgentIdOrRespondError}from"./agent-id-shared-BMmKEr8l.mjs";import{l as readCronCallerScope}from"./cron-caller-scope-Cj4two6f.mjs";import{a as requestMcpEvent,i as MCP_EVENTS_PROTOCOL_VERSION,n as prepareMcpEventConnection,r as projectMcpEventRequester}from"./service-mcp-events-CU5mc5du.mjs";import{isDeepStrictEqual}from"node:util"; //!src/gateway/server-methods/mcp-events.ts function boundedString(e,t){return typeof e===`string`&&e.length>0&&e.length<=t}function projectCatalog(e){if(!isRecord(e)||!Array.isArray(e.events)||e.events.length>1e3||Buffer.byteLength(JSON.stringify(e),`utf8`)>1048576||e.nextCursor!==void 0&&!boundedString(e.nextCursor,16384))throw new Error(`Invalid MCP Events catalog response.`);return{events:e.events.map(e=>{if(!isRecord(e)||!boundedString(e.name,256)||!isStringArray(e.delivery)||!(isRecord(e.inputSchema)||typeof e.inputSchema===`boolean`)||!(isRecord(e.payloadSchema)||typeof e.payloadSchema===`boolean`))throw new Error(`Invalid MCP Events catalog definition.`);return{name:e.name,...typeof e.description===`string`?{description:truncateUtf16Safe(e.description,4096)}:{},delivery:e.delivery,inputSchema:e.inputSchema,payloadSchema:e.payloadSchema}}).filter(e=>e.delivery.includes(`webhook`)),...e.nextCursor!==void 0?{nextCursor:e.nextCursor}:{}}}const mcpEventsHandlers={"mcp.events.list":async e=>{const{params:t,client:n,context:r,respond:i,signal:a,hasCurrentClientAuthority:o}=e;if(Object.keys(t).some(e=>![`agentId`,`serverName`,`cursor`].includes(e))||!boundedString(t.serverName,256)||t.agentId!==void 0&&!boundedString(t.agentId,128)||t.cursor!==void 0&&!boundedString(t.cursor,16384)){respondError(i,ErrorCodes.INVALID_REQUEST,`Expected {agentId?, serverName, cursor?}.`);return}const s=t.serverName;const c=t.cursor;const l=r.getRuntimeConfig();const u=r.getGatewayMethodRegistry?.();if(!u?.descriptors().some(e=>e.owner.kind===`plugin`&&e.owner.pluginId===`mcp-events`)){respondError(i,ErrorCodes.UNAVAILABLE,`Enable the MCP Events plugin to browse event sources.`);return}const d=readCronCallerScope(n);const f=resolveAgentIdOrRespondError({cfg:l,respond:i,rawAgentId:t.agentId??d?.agentId});if(!f)return;if(d&&d.agentId!==f.agentId){respondError(i,ErrorCodes.FORBIDDEN,`MCP Events catalog is bound to the caller's agent.`);return}const p=d?.toolsAllowProvenance?.channelRequester;const m={agentId:f.agentId,...d?{sessionKey:d.sessionKey,account:{id:d.accountId,channel:d.toolsAllowProvenance?.callerOrigin?.kind===`external`?d.toolsAllowProvenance.callerOrigin.channel:void 0}}:{},...p?{requester:projectMcpEventRequester(p)}:{}};const h={user:n?.authenticatedUserId,profile:n?.authenticatedUserProfile?.profileId,runtime:n?.internal?.agentRuntimeIdentity};const assertCurrent=()=>{a?.throwIfAborted();n?.connectionSignal?.throwIfAborted();e.sessionMutationCommitGuard?.();if(!n||n.invalidated||o?.()===false||!operatorScopeSatisfied(`operator.read`,n.connect.scopes??[])||r.getRuntimeConfig()!==l||r.getGatewayMethodRegistry?.()!==u||n.authenticatedUserId!==h.user||n.authenticatedUserProfile?.profileId!==h.profile||n.internal?.agentRuntimeIdentity!==h.runtime||!isDeepStrictEqual(readCronCallerScope(n),d))throw new Error(`MCP Events catalog caller authority changed.`);assertActiveAgentRuntimeAuthority(n,r)};try{assertCurrent();const e=await prepareMcpEventConnection({cfg:l,principal:m,serverName:s,assertCurrent});try{const t=await requestMcpEvent({...e,serverName:s,method:`server/discover`,signal:a,assertCurrent:e.assertCurrent});e.assertCurrent();if(!isRecord(t)||!Array.isArray(t.supportedVersions)||!t.supportedVersions.includes(`2026-07-28`)||!isRecord(t.capabilities)||!Object.hasOwn(t.capabilities,`events`))throw new Error(`This MCP server does not advertise the supported Events protocol.`);const n=projectCatalog(await requestMcpEvent({...e,serverName:s,method:`events/list`,params:typeof c===`string`?{cursor:c}:{},signal:a,assertCurrent:e.assertCurrent}));e.assertCurrent();i(true,{serverName:s,...n},void 0)}finally{e.release()}}catch{respondError(i,ErrorCodes.UNAVAILABLE,`MCP event catalog unavailable. Check the server's Events support, your account connection, and current access.`)}}};export{mcpEventsHandlers};