MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / mintBearerFlow

Function mintBearerFlow

e2e/src/surfaces/mcp.ts:223–302  ·  view source on GitHub ↗
(target: Target, email: string)

Source from the content-addressed store, hash-verified

221};
222
223const mintBearerFlow = async (target: Target, email: string): Promise<string> => {
224 const consent = target.mcpConsent?.({
225 label: email,
226 credentials: { email, password: "" },
227 });
228 if (!consent) throw new Error(`target ${target.name} has no mcpConsent strategy`);
229
230 const mcpPath = new URL(target.mcpUrl).pathname;
231 let resourceResponse = await fetch(
232 new URL(`/.well-known/oauth-protected-resource${mcpPath}`, target.baseUrl),
233 );
234 if (resourceResponse.status === 404) {
235 resourceResponse = await fetch(
236 new URL("/.well-known/oauth-protected-resource", target.baseUrl),
237 );
238 }
239 const resource = await jsonFrom<{ authorization_servers?: ReadonlyArray<string> }>(
240 resourceResponse,
241 "mintBearer: protected-resource metadata",
242 );
243 const issuer = resource.authorization_servers?.[0];
244 if (!issuer) throw new Error("mintBearer: no authorization server advertised");
245 const metadata = await jsonFrom<{
246 readonly authorization_endpoint: string;
247 readonly token_endpoint: string;
248 readonly registration_endpoint: string;
249 }>(
250 await fetch(new URL("/.well-known/oauth-authorization-server", issuer)),
251 "mintBearer: authorization-server metadata",
252 );
253
254 const redirectUri = "http://127.0.0.1:9/callback";
255 const registered = await jsonFrom<{ readonly client_id: string }>(
256 await fetch(metadata.registration_endpoint, {
257 method: "POST",
258 headers: { "content-type": "application/json" },
259 body: JSON.stringify({
260 client_name: "executor-e2e",
261 redirect_uris: [redirectUri],
262 grant_types: ["authorization_code", "refresh_token"],
263 response_types: ["code"],
264 token_endpoint_auth_method: "none",
265 }),
266 }),
267 "mintBearer: dynamic client registration",
268 );
269
270 const verifier = randomBytes(32).toString("base64url");
271 const authorizeUrl = new URL(metadata.authorization_endpoint);
272 authorizeUrl.searchParams.set("client_id", registered.client_id);
273 authorizeUrl.searchParams.set("redirect_uri", redirectUri);
274 authorizeUrl.searchParams.set("response_type", "code");
275 authorizeUrl.searchParams.set("state", randomUUID());
276 authorizeUrl.searchParams.set(
277 "code_challenge",
278 createHash("sha256").update(verifier).digest("base64url"),
279 );
280 authorizeUrl.searchParams.set("code_challenge_method", "S256");

Callers 1

makeMcpSurfaceFunction · 0.85

Calls 4

toStringMethod · 0.80
setMethod · 0.65
updateMethod · 0.65
fetchFunction · 0.50

Tested by

no test coverage detected