openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.66 kB
JavaScript
import{t as expectDefined}from"./expect-DkJCbmLj.mjs";import{r as compareMcpCatalogTools}from"./agent-bundle-mcp-names-CR4QEOJd.mjs";import{n as resolveMcpTransportConfig}from"./mcp-transport-config-BYp4GLEU.mjs";import{Type}from"typebox";
//!src/agents/agent-bundle-mcp-requester-connect.ts
async function connectRequesterOAuthServer(e){if(!e.publicOrigin){const t=`MCP server "${e.serverName}" needs requester sign-in, but gateway.publicOrigin is not configured. Ask the operator to set the public Gateway HTTP(S) origin.`;return{content:[{type:`text`,text:t}],details:{status:`error`,error:t,mcpServer:e.serverName}}}const t=await e.authorize(new URL(`/oauth/mcp/callback`,e.publicOrigin).href);if(t.status===`authorized`)return{content:[{type:`text`,text:`MCP server "${e.serverName}" is connected. Its tools become available on the next message.`}],details:{mcpServer:e.serverName}};return{content:[{type:`text`,text:`Connect MCP server "${e.serverName}" at ${t.authorizationUrl}\nAfter sign-in completes, the server's tools become available on the next message.`}],details:{mcpConnect:{serverName:e.serverName,authorizationUrl:t.authorizationUrl}}}}function buildRequesterConnectCatalog(e,t){const n=[...e];return{version:1,generatedAt:Date.now(),servers:Object.fromEntries(n.map(e=>[e,{serverName:e,safeServerName:t.get(e),launchSummary:`Requester OAuth`,toolCount:1}])),tools:n.map(e=>({serverName:e,safeServerName:t.get(e)??e,toolName:`connect`,description:`Connect your ${e} account.`,fallbackDescription:`Connect your ${e} account.`,inputSchema:Type.Object({}),oauthConnectBootstrap:true}))}}function listRequesterOAuthServers(e,t){return[...e].toSorted((e,t)=>e.localeCompare(t)).flatMap(e=>{const n=resolveMcpTransportConfig(e,t[e],{logWarnings:false});return n?.kind===`http`&&n.auth===`oauth`&&n.oauth?.identity===`per-requester`?[{serverName:e,resolved:n}]:[]})}function buildConfiguredRequesterConnectCatalog(e){const t=listRequesterOAuthServers(e.serverNames,e.mcpServers);return t.length>0?buildRequesterConnectCatalog(t.map(({serverName:e})=>e),e.safeServerNamesByServer):void 0}async function createRequesterMcpConnect(e){const t=listRequesterOAuthServers(e.serverNames,e.mcpServers);if(t.length===0)return;const{requesterMcpOAuthIdentity:n}=await import(`./mcp-oauth-identity-16XiJc6t.mjs`);const{readMcpOAuthCredentialsStatuses:r,startMcpOAuthAuthorization:i}=await import(`./mcp-oauth-CL-Jy2gP.mjs`);const a=await r(t.map(({serverName:t,resolved:r})=>n(t,r.url,e.requesterScope)));const o=/* @__PURE__ */ new Map;const s=[];for(const[r,{serverName:c,resolved:l}]of t.entries()){const t=expectDefined(a[r],`requester MCP OAuth status`);o.set(c,()=>connectRequesterOAuthServer({serverName:c,publicOrigin:e.cfg?.gateway?.publicOrigin,authorize:t=>i(n(c,l.url,e.requesterScope),l,{redirectUrl:t})}));if(t.state===`authorized`)s.push(c)}const c=JSON.stringify({config:e.configFingerprint,authorizedServerNames:s,publicOrigin:e.cfg?.gateway?.publicOrigin});return{catalog:buildRequesterConnectCatalog(o.keys(),e.safeServerNamesByServer),authorizedServerNames:s,configFingerprint:c,createExecute:e=>o.get(e)}}function mergeMcpConnectCatalog(e,t){const n=t?.catalog;if(!n)return e;const r=new Set(Object.keys(n.servers).filter(t=>!Object.hasOwn(e.servers,t)));if(r.size===0)return e;return{...e,generatedAt:Math.max(e.generatedAt,n.generatedAt),servers:{...e.servers,...Object.fromEntries(Object.entries(n.servers).filter(([e])=>r.has(e)))},tools:[...e.tools,...n.tools.filter(e=>r.has(e.serverName))].toSorted(compareMcpCatalogTools)}}export{createRequesterMcpConnect as n,mergeMcpConnectCatalog as r,buildConfiguredRequesterConnectCatalog as t};