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

Function fetch

apps/local/src/serve.ts:412–516  ·  view source on GitHub ↗
(req)

Source from the content-addressed store, hash-verified

410 idleTimeout: 0,
411 routes: { ...staticRoutes },
412 async fetch(req) {
413 const withCors = (response: Response): Response =>
414 withCorsHeaders(req, response, corsAllowedHosts);
415
416 if (req.method === "OPTIONS" && req.headers.has("origin")) {
417 return corsPreflightResponse(req, corsAllowedHosts);
418 }
419
420 const url = new URL(req.url);
421
422 // Unauthenticated liveness probe — carries no data, used by the CLI
423 // reachability check (which therefore never forwards a credential).
424 if (url.pathname === "/api/health" && req.method === "GET") {
425 return withCors(new Response("ok", { headers: { "content-type": "text/plain" } }));
426 }
427
428 // OAuth callbacks and CIMD documents are reached by the external
429 // provider, which cannot carry our local bearer. Everything else under
430 // /api and /mcp requires the bearer.
431 const skipAuth = isUnauthenticatedOAuthPath(url.pathname);
432 const isMcpPath = url.pathname === "/mcp" || url.pathname.startsWith("/mcp/");
433 const isGatedSurface = url.pathname.startsWith("/api") || isMcpPath;
434
435 if (isGatedSurface && !skipAuth && !isAuthorized(req)) {
436 return withCors(
437 new Response("Unauthorized", {
438 status: 401,
439 headers: { "www-authenticate": 'Bearer realm="executor"' },
440 }),
441 );
442 }
443
444 if (isUnauthenticatedOAuthClientMetadataPath(url.pathname) && req.method === "GET") {
445 return withCors(oauthClientMetadataResponse(`${url.pathname}${url.search}`, req));
446 }
447
448 if (isMcpPath) {
449 return withCors(await handlers.mcp.handleRequest(req));
450 }
451
452 if (url.pathname.startsWith("/api/mcp-sessions/")) {
453 // GET → paused-execution detail for the approval page; POST → record the
454 // decision. Both are bearer-gated above.
455 const handler =
456 req.method === "GET"
457 ? handlers.mcp.handlePausedRequest
458 : handlers.mcp.handleApprovalRequest;
459 return withCors(await handler(req));
460 }
461
462 // OAuth result polling — local-only, served outside the typed API
463 // because cloud (Cloudflare Workers, stateless) can't back the
464 // in-memory store. See setOAuthCompletionListener above.
465 // Long-poll: hold the request until the completion listener publishes
466 // for this session, then answer immediately instead of making the
467 // client wait for its next poll tick. On deadline (or client
468 // disconnect) answer null — "still pending" — so the client's outer
469 // retry loop reconnects. `idleTimeout: 0` above permits the hold.

Callers 15

startViteChildFunction · 0.70
proxyToViteFunction · 0.70
serve.test.tsFile · 0.70
cfFunction · 0.50
onRequestFunction · 0.50
fetchStarsFunction · 0.50
fetchNeedsSetupFunction · 0.50
bindFunction · 0.50
decideFunction · 0.50
decideFunction · 0.50

Calls 11

waitForOAuthResultFunction · 0.90
hasFileExtensionFunction · 0.90
withCorsFunction · 0.85
isAuthorizedFunction · 0.85
proxyToViteFunction · 0.85
execMethod · 0.80
corsPreflightResponseFunction · 0.70
handlerFunction · 0.50

Tested by 15

connectFunction · 0.40
withRefusedRefreshFunction · 0.40
completeGoogleConsentFunction · 0.40
connectRefusingFunction · 0.40
mintEmulatorApiKeyFunction · 0.40
completeGoogleConsentFunction · 0.40
runApprovalFunction · 0.40
runBearerScopingFunction · 0.40
mcpPostFunction · 0.40
openSseFunction · 0.40
callbackFunction · 0.40