Const Alphaimport { withOAuthProtectedResource, withSupabase } from '@supabase/server'
Deno.serve(
withOAuthProtectedResource(
withSupabase({ auth: 'user' }, async (_req, { supabase }) => {
const { data, error } = await supabase.from('items').select('*')
if (error) throw error
return Response.json(data)
}),
),
)
import { withOAuthProtectedResource, fromSupabaseUrl } from '@supabase/server'
export default {
fetch: withOAuthProtectedResource(
{
resourceServer: (req) => new URL(req.url).origin + '/api/mcp',
authorizationServer: fromSupabaseUrl('https://abc123.supabase.co'),
},
handler,
),
}
Alpha. Wraps a request handler with OAuth 2.1 Protected Resource behavior (RFC 9728).
GET {resource}/oauth-protected-resource(with permissive CORS, including theOPTIONSpreflight, so browser-based clients can read it)401from the inner handler withWWW-Authenticate: Bearer resource_metadata="...", unless the handler already set aWWW-Authenticateheader (its value wins)withSupabasereturns for its own configuration failures (500,x-supabase-server-error); a throw from a configuredresourceServerorauthorizationServerfunction is the caller's and propagatesThe metadata route is matched on the path suffix, so any
GETorOPTIONSending in/oauth-protected-resourceis answered here and never reaches the inner handler, at any depth. Other methods pass through.Zero-config on Supabase Edge Functions. Elsewhere OAuthProtectedResourceConfig.resourceServer is required and OAuthProtectedResourceConfig.authorizationServer falls back to
SUPABASE_URL; one that cannot be resolved is reported asMISSING_RESOURCE_SERVER/MISSING_AUTHORIZATION_SERVER.Contributes
ctx.oauthProtectedResource(the resolved metadata URL) to the downstream context. Nested underwithSupabase, the key is typed on the handler'sctxwhen the outermost call is anchored withsatisfies FetchHandler— seewithSupabase's type note. Placed directly afterwithSupabasewith an auth mode that requires credentials, the composition is refused when the stack is built, since the gate would answer discovery and preflight before this middleware runs.The OAuth Protected Resource surface is alpha — the config shape, the contributed context key, and the metadata route may change in a minor release.