openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
112 lines (111 loc) • 4.02 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { c as filterStringRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as createLazyRuntimeMethod } from "./lazy-runtime-zGjszCtt.mjs";
import { n as resolveMcpTransportConfig } from "./mcp-transport-config-r6XeT3o8.mjs";
import { a as withoutMcpAuthorizationHeader, n as buildMcpOAuthAuthorizationFetch } from "./mcp-http-fetch-BUTs6xsJ.mjs";
import crypto from "node:crypto";
//#region src/agents/mcp-auth-profile.ts
/**
* Auth-profile backed bearer injection for remote MCP servers.
*/
/** Returns the refresh-capable auth profile selected for one MCP server. */
function resolveMcpAuthProfileId(e) {
if (!isRecord(e) || e.auth !== "oauth" || !isRecord(e.oauth)) return;
return normalizeOptionalString(e.oauth.authProfileId);
}
/** Returns whether a server needs an OpenClaw-managed bearer projected externally. */
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-yyLNlCV7.mjs"), import("./mcp-oauth-Bzsr2nlm.mjs")]);
return await i({
identity: r(e.serverName, n.url),
config: n.oauth,
fetchFn: buildMcpOAuthAuthorizationFetch(n)
});
}
/** Wraps HTTP MCP fetch with same-origin, refreshed bearer injection. */
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`;
}
/** Resolves OAuth-backed MCP servers into bearer headers for external runtimes. */
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
};
}
//#endregion
export { withMcpAuthProfileBearer as i, resolveMcpAuthProfileId as n, resolveMcpBearerBundleConfig as r, requiresMcpBearerProjection as t };