UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.1 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{c as filterStringRecord,l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{t as createLazyRuntimeMethod}from"./lazy-runtime-Ds6M7vp4.mjs";import{n as resolveMcpTransportConfig}from"./mcp-transport-config-BYp4GLEU.mjs";import{a as withoutMcpAuthorizationHeader,n as buildMcpOAuthAuthorizationFetch}from"./mcp-http-fetch-4bDZjC2p.mjs";import crypto from"node:crypto"; //!src/agents/mcp-auth-profile.ts function resolveMcpAuthProfileId(e){if(!isRecord(e)||e.auth!==`oauth`||!isRecord(e.oauth))return;return normalizeOptionalString(e.oauth.authProfileId)}function requiresMcpBearerProjection(e){if(!isRecord(e)||e.auth!==`oauth`)return false;return Boolean(resolveMcpAuthProfileId(e)||typeof e.url===`string`)}const resolveMcpAuthProfileBearerToken=createLazyRuntimeMethod(()=>import(`./agents/mcp-auth-profile.runtime.js`),e=>e.resolveMcpAuthProfileBearerToken);async function resolveMcpBearerToken(e){const t=resolveMcpAuthProfileId(e.server);if(t)return await resolveMcpAuthProfileBearerToken({serverName:e.serverName,profileId:t,cfg:e.cfg,agentDir:e.agentDir});if(e.server.auth!==`oauth`)return;const n=resolveMcpTransportConfig(e.serverName,e.server);if(!n||n.kind!==`http`)return;const[{operatorMcpOAuthIdentity:r},{resolveMcpOAuthAccessToken:i}]=await Promise.all([import(`./mcp-oauth-identity-16XiJc6t.mjs`),import(`./mcp-oauth-CL-Jy2gP.mjs`)]);return await i({identity:r(e.serverName,n.url),config:n.oauth,fetchFn:buildMcpOAuthAuthorizationFetch(n)})}function withMcpAuthProfileBearer(e){const t=new URL(e.resourceUrl).origin;const n=withoutMcpAuthorizationHeader(e.headers);return async(r,i)=>{if(new URL(r).origin!==t)return e.fetchFn(r,i);const a=new Headers(n);for(const[e,t]of new Headers(i?.headers))if(e.toLowerCase()!==`authorization`)a.set(e,t);const o=await resolveMcpAuthProfileBearerToken({serverName:e.serverName,profileId:e.authProfileId,cfg:e.cfg,agentDir:e.agentDir});a.set(`authorization`,`Bearer ${o}`);return e.fetchFn(r,{...i,headers:a})}}function buildTokenEnvVarName(e){return`OPENCLAW_MCP_AUTH_${crypto.createHash(`sha256`).update(e).digest(`hex`).slice(0,12).toUpperCase()}_TOKEN`}async function resolveMcpBearerBundleConfig(e){let t;let n=e.env;const r=e.tokenProjection??`env`;for(const[i,a]of Object.entries(e.config.mcpServers)){let o;try{o=await resolveMcpBearerToken({serverName:i,server:a,cfg:e.cfg,agentDir:e.agentDir})}catch(n){if(!e.omitUnavailableOAuthServers||!requiresMcpBearerProjection(a))throw n;t??={...e.config.mcpServers};delete t[i];e.onServerUnavailable?.(i,n);continue}if(!o)continue;let s;if(r===`literal`)s=`Bearer ${o}`;else{const t=buildTokenEnvVarName(i);if(!n||n===e.env)n={...e.env};n[t]=o;s=`Bearer \${${t}}`}const c=withoutMcpAuthorizationHeader(filterStringRecord(a.headers));t??={...e.config.mcpServers};const{auth:l,oauth:u,...d}=a;t[i]={...d,headers:{...c,Authorization:s}}}return{config:t?{mcpServers:t}:e.config,env:n}}export{withMcpAuthProfileBearer as i,resolveMcpAuthProfileId as n,resolveMcpBearerBundleConfig as r,requiresMcpBearerProjection as t};