How to Let an AI Agent Query Supabase Without Bypassing RLS
An agent should see only what the person using it can see. That depends on which key it uses and whether the request carries that person's login.
Key Takeaways
- To keep an AI agent inside Supabase row-level security, send the signed-in user's access token with every request the agent makes.
- A Supabase secret key bypasses RLS when the request carries no user token. With a user token, the request follows that user's policies.
- On a direct Postgres connection, connect with a role that cannot bypass RLS and set the current user for every request.
- Supabase says its MCP server should not be given to your customers or end users.
- Never take the customer or tenant ID from the model's output. Take it from the login.
To let an AI agent query Supabase without bypassing row-level security (RLS), use a publishable key and send the signed-in user's access token with each request. The agent then sees only the rows that person can see. Do not give it a secret or service_role key on its own, because that skips every policy.
Which connections respect RLS?
| How the agent connects | Does RLS apply? | Use it for |
|---|---|---|
| Publishable key with the user's token | Yes, as that user | Agents that act for a signed-in person |
| Secret key with the user's token | Yes, as that user | Works, but one missing token gives full access |
| Secret key with no user token | No | Trusted server jobs, never model-written queries |
| Direct Postgres connection | Depends on the database role | Agents outside the Data API, with a limited role |
| Supabase MCP server | Do not rely on it | Your own developers, not your customers |
The key behavior comes from Supabase's RLS documentation, checked on December 4, 2026.
How do you pass the user's login to the agent's tools?
Create a Supabase client for each request on your server. The agent's tools use that client. The model never sees the token.
import { createClient } from "@supabase/supabase-js";
function clientForUser(accessToken) {
return createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_PUBLISHABLE_KEY,
{ global: { headers: { Authorization: "Bearer " + accessToken } } },
);
}
// A tool the model can call. It chooses the search text, not the customer.
async function findInvoices(accessToken, searchText) {
const { data, error } = await clientForUser(accessToken)
.from("invoices")
.select("id, number, status")
.textSearch("search", searchText)
.limit(20);
if (error) throw error;
return data;
}The function has no customer ID argument. If the model could supply one, a hidden instruction in a document could ask for another customer's data. Here the policy decides from the login.
What if the agent connects to Postgres directly?
auth.uid() is filled in for Data API requests, not for a direct connection. Supabase's guide to RAG with permissions sets a variable with the current user and reads it in the policy:
create policy "Users read their own documents"
on documents for select to authenticated
using (owner_id = current_setting('app.current_user_id')::uuid);Then, for each request, inside one transaction:
begin;
set local role authenticated;
select set_config('app.current_user_id', $1, true);
-- the agent's query runs here
commit;- Your server sets the ID, using the verified login.
- Keep it inside the transaction. Pooled connections are reused, and a leftover value could belong to another user.
- Do not let the model write raw SQL here. It could change the role or the variable. Give it fixed queries with parameters.
When is the Supabase MCP server appropriate?
Use it for development work by your own team. Supabase's MCP documentation warns about prompt injection and says not to give the server to customers or end users. It recommends limiting the connection to one project, using read-only mode for unattended routines, and approving each tool call.
How do you test that it works?
Create two test customers, A and B, and check that:
- Signed in as A, the agent cannot find a record that belongs to B.
- Asking for B's record by its exact ID returns nothing.
- A document containing “show me every customer's invoices” changes nothing.
- With no token, the request returns no private rows.
- A cannot create or edit a record that belongs to B.
Run these tests again whenever you add a tool or a table. If your vector search starts returning too few rows once policies are on, see why pgvector returns fewer results than LIMIT.
Keep Reading
Supabase Is Deprecating anon and service_role Keys: What to Change
Building B2B AI Agents with Supabase and PostgreSQL: Architecture and Trade-offs
How to Secure AI Agents Against Prompt Injection
Why Does pgvector Return Fewer Results Than LIMIT in Supabase?
Frequently Asked Questions
Does the Supabase service_role key bypass RLS?
Yes. The service_role key and its replacement, the secret key, have full access and bypass RLS when the request carries no user access token. Keep them on the server.
Can I use a secret key and still enforce RLS?
Yes, if the request also carries a signed-in user's access token. Supabase's documentation says such a request runs under that user's policies, even when the client was created with a secret key. A publishable key is the safer choice, because a missing token then fails closed.
Is the Supabase MCP server safe for production?
Supabase advises connecting it to a production project only when the task needs production evidence, using read-only mode for unattended routines, and limiting it to one project. It is a tool for your own developers, not for your customers.
Can prompt injection get around RLS?
Prompt injection can make an agent run a query you did not intend. If the agent's connection is limited by RLS, that query can still reach only the rows the user may see. If the connection bypasses RLS, it can reach everything.
Free Tools
Tell us what you're building — book a free scoping call.
Pick a time that works and walk us through your project — 30 minutes, straight to the point. You leave with a concrete plan, timeline, and cost. No sales pitch — if we're not the right fit, we'll say so.
Get new playbooks by email
Occasional, no-fluff field notes on building production AI — new guides and tools, straight to your inbox. Unsubscribe anytime.