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

Function renderAuthError

apps/cloud/src/mcp/agent-handler.ts:62–93  ·  view source on GitHub ↗
(
  auth: McpAuthProvider["Service"],
  request: Request,
  outcome: Exclude<AuthOutcome, { readonly _tag: "Authenticated" }>,
)

Source from the content-addressed store, hash-verified

60 : jsonRpcErrorBody(status, code, message, { challenge });
61
62const renderAuthError = (
63 auth: McpAuthProvider["Service"],
64 request: Request,
65 outcome: Exclude<AuthOutcome, { readonly _tag: "Authenticated" }>,
66): Response => {
67 if (Predicate.isTagged(outcome, "Unauthorized")) {
68 return jsonRpcResponse(
69 401,
70 -32001,
71 "Unauthorized",
72 outcome.challenge ?? `Bearer resource_metadata="${auth.resourceMetadataUrl(request)}"`,
73 );
74 }
75 if (Predicate.isTagged(outcome, "Forbidden")) {
76 return jsonRpcResponse(403, outcome.code ?? -32001, outcome.message);
77 }
78 // Unavailable: a transient auth-infra failure (JWKS blip OR a WorkOS
79 // membership-lookup 429/5xx/timeout). Both are retryable, so advertise a
80 // Retry-After so the client (and any polite retry layer) backs off instead of
81 // hammering (same rendering as the shared envelope's Unavailable branch).
82 // Crucially, this path NEVER reaches the session-destroy branch below — a
83 // transient failure must not condemn a live session.
84 //
85 // Note this 503 shares JSON-RPC code -32001 with the terminated-session 404
86 // ("Session timed out, please reconnect"); that is intentional — -32001 is
87 // the generic auth/session envelope code, and the HTTP STATUS is the
88 // discriminator clients act on: 503 = retry the SAME session id, 404 = the
89 // id is dead, reconnect.
90 return jsonRpcErrorBody(503, -32001, outcome.message, {
91 retryAfterSeconds: UNAVAILABLE_RETRY_AFTER_SECONDS,
92 });
93};
94
95/**
96 * A Cloudflare *platform* Durable Object failure happened at one of this

Callers 1

makeCloudMcpAgentHandlerFunction · 0.70

Calls 2

jsonRpcErrorBodyFunction · 0.85
jsonRpcResponseFunction · 0.70

Tested by

no test coverage detected