Building an MCP server on Supabase with OAuth 2.1
ReserrvaLinks cited7 min read
On this page · 10 sections
Reserrva is a booking platform for people who work by appointment. Besides the web app and the mobile app for business owners, it has a third client: AI agents. An assistant such as ChatGPT or Claude can connect to our MCP server, find a business, read its real free slots and book one. An owner who grants extra permission can also read and change their own agenda from the assistant.
This post explains how that server is built, one Supabase Edge Function with Supabase Auth as the OAuth 2.1 authorization server, and what turned out to be harder than expected.
This post is published by Reserrva and describes how Reserrva is built. The button below books a free call with Alejandro Mena, its founder.
One function, stateless, one catalogue
The server is a single Edge Function that speaks MCP's Streamable HTTP transport over POST. It keeps no session. Each request builds a fresh MCP server and transport and closes them when the answer is sent, because the runtime isolate can be shared between callers and nothing about one call should outlive it.
The tool list is declared once, in a dependency-free TypeScript file that the Edge Function, the web app's discovery documents and a smoke script all read. Each entry says who may call the tool, whether it writes, what it does in words a model reads, and which error codes it can return.
Tell Alejandro about your project
A free 15 or 30 minute video call with Alejandro Mena, developer. You pick the time and no account is needed.
Book a callTools never embed rules
Tool handlers are thin adapters: they validate the arguments with zod, call Postgres functions through the caller's own Supabase client, and return the result with a one-line summary. A few compose more than one read to enrich what they return. There is no slot arithmetic, no price arithmetic and no insert in the server. If a rule is missing, it goes into a migration, not into the tool.
A simplified handler looks like this:
server.tool("cancel_appointment", schema, async (args, ctx) => {
const input = schema.parse(args);
const { data, error } = await ctx.supabase.rpc("cancel_appointment", input);
if (error) return toolError(error);
return ok("Cancelled.", data);
});
The web panel is built on the same functions, so the agent surface was an adapter from the start. More on that choice in putting domain rules in Postgres.
Agents sign in as users
We did not build an API key system for agents. Supabase Auth can act as an OAuth 2.1 server: an MCP client registers itself through dynamic client registration, sends the user to sign in, and gets back the user's own access token through the authorization code flow with PKCE. The consent screen is a page in our web app, at a path set in the Supabase config.
The MCP authorization specification requires a protected server to publish OAuth 2.0 Protected Resource Metadata (RFC 9728) and to point to it in the WWW-Authenticate header of a 401. We wrap the handler so it serves that document and adds the header, and that is how hosts like ChatGPT and Claude find where to send the user.
Because the token belongs to the user, every call carries that user's identity. Row level security and the authorisation checks inside each function decide what it may do, so an agent can do what the person could do in the app, and no more.
The database decides who the agent is
Supabase's OAuth server documentation notes that its access tokens carry a client_id claim. Our booking functions read it from the verified token and store it on the appointment. No tool accepts it as an argument, so an agent cannot claim to be someone else. Simplified for illustration:
insert into appointments (..., agent_client_id)
values (..., auth.jwt() ->> 'client_id');
That column is how the panel can show that an appointment was booked through an agent, and our database smoke test asserts that a booking made with a client claim is stamped with it.
What needs no sign-in, and what needs consent
Each tool has one of three levels:
- Anonymous. Search, business profiles, availability and a guest booking with a name and contact. This is what the public booking page already shows.
- User. Booking on the user's account, listing and cancelling their own appointments, and requesting a quote. Without a token the server answers with a sign-in challenge.
- Manage. The owner tools: agenda, time off, services, team, settings, requests, clients and stats.
The third level was the interesting one. Supabase Auth issues the standard OAuth scopes, and when we tried to request a custom business:manage scope it was refused. So the permission became a row in Postgres instead of a claim in the token: user, OAuth client and business, written by a second consent screen under the user's own session. It only counts while the user's OAuth consent to that client is still live, so revoking the connection ends it. A professional who is not an owner or admin gets a narrower grant: their own agenda and their own blocked time.
Rate limits that survive a fresh isolate
Every caller is counted in a fixed time window, keyed by the agent's OAuth client, or the user, or the IP address. A write also consumes a second, smaller budget. Edge Functions give us no memory shared between requests, so the counter is a Postgres table incremented by one function. That function is the only thing the server uses the service role key for, and if the counter cannot be reached the server refuses the call instead of running unmetered.
Errors are part of each tool's contract
Postgres functions raise stable codes such as slot_unavailable or too_late_to_cancel. Each tool declares the codes it can return, and the tool description lists them so the model knows what each failure means. A test reads the migrations, works out every code each tool's functions can reach, and fails unless that code is classified as a refusal, a retry, an outcome that already holds, or an unexpected error.
An unexpected error is logged once with safe fields only, and the agent gets a support reference and an instruction: a change may already have been saved, so read the current state before repeating it.
What was hard
- Custom scopes. The permission model had to move from the token into the database, with its own consent page and expiry rules.
- Hosts that drop error bodies. When we tested the owner flow in Claude, a 403 carrying the explanation and the grant link reached the user only as a generic error. On our public address the refusal is now a tool error over HTTP 200, so the link gets through.
- The resource identifier. Behind a proxy, the URL the client called is not the URL the function sees, and RFC 9728 cares about the first.
The owner's side is in the guide on letting an AI agent book into your agenda. How the code itself gets written and reviewed is in our multi-model coding workflow.
Frequently asked questions
Can an MCP server run on Supabase Edge Functions?
Yes. Reserrva's server is one Edge Function speaking the Streamable HTTP transport over POST, stateless, with a new MCP server and transport built for each request.
How do agents authenticate to Reserrva's MCP server?
Through Supabase Auth acting as an OAuth 2.1 server, with dynamic client registration and PKCE. The agent receives the user's own access token, so row level security and the checks inside each function apply to every call.
Can an agent set its own client id on a booking?
No. The booking function reads the client id from the verified token and stores it. No tool takes it as an argument.
How does an owner let an agent manage their business?
On a second consent screen, business by business. Supabase Auth issues only standard scopes, so the business:manage permission is stored in Postgres and checked by every business-side function.
Where this comes from
Everything here is drawn from Reserrva's own repository: the Edge Function, the shared tool catalogue, the migrations, the architecture decisions and the directory submission notes. Reserrva published this post. If you are building an agent surface for your own product, Alejandro Mena offers free 15- and 30-minute video calls every day from 10:00 to 23:00 CET, booked online without an account.
Start with a call
Thirty minutes, free and with no commitment, to work out what you need and where to start.
Book a callLinks cited
The external pages this article links to.
- MCP authorization specification · modelcontextprotocol.io
- OAuth server documentation · supabase.com
Commercial relationship: Reserrva publishes this guide, and Alejandro Mena is Reserrva's founder.