Connectors
connectorsMCP servers per tenant with no, per-user OAuth or header auth; per-user grants that hold only a credential_ref; MCP sessions per chat; and tool list fingerprints that hold a changed list until an admin approves it.
Automationmanaged
PLATFORM_JOBS
send-invoicedone
sync-crmrunning
webhook.retryqueued
sk_live_••••4f2a
Install
The command also installs updated-at, memberships, permissions, the modules it needs.
npx better-supabase add connectorsConfigure
The blocks entries in your config, with the default mode.
import { defineConfig } from "better-supabase/config";
export default defineConfig({
blocks: {
// Required by connectors; add installs them too.
"updated-at": {},
memberships: {},
permissions: {},
// Modes: "managed". The default is "managed".
connectors: { mode: "managed" },
},
});Migrations
The files
npx better-supabase add connectors writes into your declarative schema with the default layout.9 files
supabase/schemas/900_better_supabase_052_connectors.sqlschema503 lines-- better-supabase module: connectors
-- @bsb-block connectors@1 managed
-- @bsb-hash sha256:61f596d089c913526c7a0fa8db34a1e63f44e83063891a2f41fa6b457c9b01b3
-- MCP servers per tenant with no, per-user OAuth or header auth; per-user grants that hold only a credential_ref; MCP sessions per chat; and tool list fingerprints that hold a changed list until an admin approves it.
-- Managed by `better-supabase add`; re-running it rewrites this file unless it was edited by hand.
-- Change it through `blocks` in better-supabase.config.ts and the module's SQL hooks.
-- MCP servers a tenant connects its assistants to. A server names how to
-- authenticate (none, OAuth per user, or a header from credential_ref) and
-- never holds a token itself.
create table "better_supabase"."connector_servers" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"name" text not null constraint bsb_connector_servers_name_check check (length("name") >= 1 and length("name") <= 200),
"url" text not null constraint bsb_connector_servers_url_check check ("url" ~ '^(https://|http://(localhost|127[.]0[.]0[.]1)(:[0-9]+)?(/|$))' and length("url") <= 2000),
"transport" text not null default 'http' constraint bsb_connector_servers_transport_check check ("transport" in ('http', 'sse')),
"auth_type" text not null default 'oauth' constraint bsb_connector_servers_auth_type_check check ("auth_type" in ('none', 'oauth', 'header')),
"credential_ref" jsonb constraint bsb_connector_servers_credential_ref_check check ("credential_ref" is null or (jsonb_typeof("credential_ref") = 'object' and "credential_ref" ? 'provider')),
"scopes" text[] not null default '{}',
"client_metadata" jsonb not null default '{}' constraint bsb_connector_servers_client_metadata_check check (jsonb_typeof("client_metadata") = 'object'),
"enabled" boolean not null default true,
"created_by" uuid references auth.users (id) on delete set null,
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
constraint bsb_connector_servers_check check (("auth_type" = 'header') = ("credential_ref" is not null))
);
create index connector_servers_tenant_idx on "better_supabase"."connector_servers" ("tenant_id");
create index connector_servers_created_by_idx on "better_supabase"."connector_servers" ("created_by");
alter table "better_supabase"."connector_servers" enable row level security;
revoke all on "better_supabase"."connector_servers" from public, anon, authenticated;
grant select on "better_supabase"."connector_servers" to authenticated;
grant all on "better_supabase"."connector_servers" to service_role;
create policy bsb_connector_servers_read on "better_supabase"."connector_servers"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('connectors.read')));
-- A user's connection to a server. credential_ref names where the provider
-- keeps the token (Vault, Vercel Connect); the row holds none.
create table "better_supabase"."connector_grants" (
"id" uuid primary key default gen_random_uuid(),
"user_id" uuid not null references auth.users (id) on delete cascade,
"server_id" uuid not null references "better_supabase"."connector_servers" ("id") on delete cascade,
"tenant_id" uuid not null,
"credential_ref" jsonb not null constraint bsb_connector_grants_credential_ref_check check (jsonb_typeof("credential_ref") = 'object' and "credential_ref" ? 'provider'),
"scopes" text[] not null default '{}',
"expires_at" timestamptz,
"granted_at" timestamptz not null default now(),
"revoked_at" timestamptz
);
create unique index connector_grants_active_idx on "better_supabase"."connector_grants" ("user_id", "server_id") where "revoked_at" is null;
create index connector_grants_server_idx on "better_supabase"."connector_grants" ("server_id");
create index connector_grants_user_idx on "better_supabase"."connector_grants" ("user_id");
alter table "better_supabase"."connector_grants" enable row level security;
revoke all on "better_supabase"."connector_grants" from public, anon, authenticated;
grant select on "better_supabase"."connector_grants" to authenticated;
grant all on "better_supabase"."connector_grants" to service_role;
create policy bsb_connector_grants_read on "better_supabase"."connector_grants"
for select to authenticated
using ("user_id" = (select auth.uid()));
-- The MCP session a user holds with a server per chat, so a later request
-- reuses it instead of initializing again.
create table "better_supabase"."connector_sessions" (
"server_id" uuid not null references "better_supabase"."connector_servers" ("id") on delete cascade,
"user_id" uuid not null references auth.users (id) on delete cascade,
"chat_key" text not null default '',
"session_id" text,
"initialize_result" jsonb,
"last_used_at" timestamptz not null default now(),
"expires_at" timestamptz not null,
primary key ("server_id", "user_id", "chat_key")
);
create index connector_sessions_expires_idx on "better_supabase"."connector_sessions" ("expires_at");
create index connector_sessions_user_idx on "better_supabase"."connector_sessions" ("user_id");
alter table "better_supabase"."connector_sessions" enable row level security;
revoke all on "better_supabase"."connector_sessions" from public, anon, authenticated;
grant select on "better_supabase"."connector_sessions" to authenticated;
grant all on "better_supabase"."connector_sessions" to service_role;
create policy bsb_connector_sessions_read on "better_supabase"."connector_sessions"
for select to authenticated
using ("user_id" = (select auth.uid()));
-- Fingerprints of a server's tool list. A list that changed waits for an
-- admin before an assistant may call it.
create table "better_supabase"."connector_tool_fingerprints" (
"server_id" uuid not null references "better_supabase"."connector_servers" ("id") on delete cascade,
"fingerprint" text not null constraint bsb_connector_tool_fingerprints_fingerprint_check check (length("fingerprint") >= 1 and length("fingerprint") <= 200),
"tools" jsonb not null default '{}' constraint bsb_connector_tool_fingerprints_tools_check check (jsonb_typeof("tools") = 'object'),
"status" text not null default 'pending' constraint bsb_connector_tool_fingerprints_status_check check ("status" in ('pending', 'approved', 'rejected')),
"approved_by" uuid references auth.users (id) on delete set null,
"approved_at" timestamptz,
"created_at" timestamptz not null default now(),
primary key ("server_id", "fingerprint")
);
create index connector_tool_fingerprints_approved_by_idx on "better_supabase"."connector_tool_fingerprints" ("approved_by");
alter table "better_supabase"."connector_tool_fingerprints" enable row level security;
revoke all on "better_supabase"."connector_tool_fingerprints" from public, anon, authenticated;
grant select on "better_supabase"."connector_tool_fingerprints" to authenticated;
grant all on "better_supabase"."connector_tool_fingerprints" to service_role;
create policy bsb_connector_tool_fingerprints_read on "better_supabase"."connector_tool_fingerprints"
for select to authenticated
using (exists (select 1 from "better_supabase"."connector_servers" x where x."id" = "server_id"));
-- Adds a server (id null) or changes one ('connectors.manage').
create or replace function "better_supabase"."upsert_connector_server"(tenant uuid, id uuid default null, fields jsonb default '{}')
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."connector_servers"%rowtype;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', upsert_connector_server.tenant, 'connectors.manage'), false)) then
raise exception 'you may not manage connectors here' using errcode = '42501', hint = 'CONNECTOR_FORBIDDEN';
end if;
if jsonb_typeof(upsert_connector_server.fields) is distinct from 'object' then
raise exception 'fields must be an object' using errcode = '22023', hint = 'CONNECTOR_INVALID';
end if;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) and upsert_connector_server.fields -> 'credential_ref' is not null and jsonb_typeof(upsert_connector_server.fields -> 'credential_ref') <> 'null'
and (jsonb_typeof(upsert_connector_server.fields -> 'credential_ref' -> 'tenant') is distinct from 'string' or (upsert_connector_server.fields -> 'credential_ref' ->> 'tenant') is distinct from (upsert_connector_server.tenant)::text) then
raise exception 'credential_ref must carry the tenant % that owns it', upsert_connector_server.tenant
using errcode = '42501', hint = 'CREDENTIAL_REF_FOREIGN';
end if;
if upsert_connector_server.id is null then
insert into "better_supabase"."connector_servers" ("tenant_id", "name", "url", "auth_type", "credential_ref", "created_by")
values (upsert_connector_server.tenant, upsert_connector_server.fields ->> 'name', upsert_connector_server.fields ->> 'url',
coalesce(upsert_connector_server.fields ->> 'auth_type', 'oauth'), upsert_connector_server.fields -> 'credential_ref', auth.uid())
returning * into v_row;
else
select * into v_row from "better_supabase"."connector_servers" x where x."id" = upsert_connector_server.id and x."tenant_id" = upsert_connector_server.tenant for update;
if not found then
raise exception 'connector % not found', upsert_connector_server.id using errcode = 'P0002', hint = 'CONNECTOR_NOT_FOUND';
end if;
end if;
update "better_supabase"."connector_servers" x set
"name" = coalesce(upsert_connector_server.fields ->> 'name', x."name"),
"url" = coalesce(upsert_connector_server.fields ->> 'url', x."url"),
"transport" = coalesce(upsert_connector_server.fields ->> 'transport', x."transport"),
"auth_type" = coalesce(upsert_connector_server.fields ->> 'auth_type', x."auth_type"),
"credential_ref" = case when upsert_connector_server.fields ? 'credential_ref' then nullif(upsert_connector_server.fields -> 'credential_ref', 'null') else x."credential_ref" end,
"scopes" = case when upsert_connector_server.fields ? 'scopes' then array(select jsonb_array_elements_text(upsert_connector_server.fields -> 'scopes')) else x."scopes" end,
"client_metadata" = coalesce(upsert_connector_server.fields -> 'client_metadata', x."client_metadata"),
"enabled" = coalesce((upsert_connector_server.fields ->> 'enabled')::boolean, x."enabled"),
"updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
if upsert_connector_server.id is null then
null;
else
null;
end if;
return jsonb_build_object('id', v_row."id", 'tenant_id', v_row."tenant_id", 'name', v_row."name", 'url', v_row."url", 'transport', v_row."transport", 'auth_type', v_row."auth_type", 'credential_ref', v_row."credential_ref", 'scopes', v_row."scopes", 'client_metadata', v_row."client_metadata", 'enabled', v_row."enabled", 'created_by', v_row."created_by", 'created_at', v_row."created_at", 'updated_at', v_row."updated_at");
end;
$$;
revoke execute on function "better_supabase"."upsert_connector_server"(uuid, uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."upsert_connector_server"(uuid, uuid, jsonb) to authenticated, service_role;
-- Deletes a server and returns the grants it had, so the caller revokes
-- each credential through its provider.
create or replace function "better_supabase"."delete_connector_server"(id uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."connector_servers"%rowtype;
v_grants jsonb;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."connector_servers" x where x."id" = delete_connector_server.id for update;
if not found or not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', v_row."tenant_id", 'connectors.manage'), false)) then
raise exception 'connector % not found', delete_connector_server.id using errcode = 'P0002', hint = 'CONNECTOR_NOT_FOUND';
end if;
select coalesce(jsonb_agg(jsonb_build_object('id', y."id", 'user_id', y."user_id", 'server_id', y."server_id", 'tenant_id', y."tenant_id", 'credential_ref', y."credential_ref", 'scopes', y."scopes", 'expires_at', y."expires_at", 'granted_at', y."granted_at", 'revoked_at', y."revoked_at")), '[]') into v_grants
from "better_supabase"."connector_grants" y where y."server_id" = v_row."id" and y."revoked_at" is null;
delete from "better_supabase"."connector_servers" x where x."id" = v_row."id";
null;
return v_grants;
end;
$$;
revoke execute on function "better_supabase"."delete_connector_server"(uuid) from public, anon;
grant execute on function "better_supabase"."delete_connector_server"(uuid) to authenticated, service_role;
-- The tenant's servers with the caller's active grant on each.
create or replace function "better_supabase"."list_connector_servers"(tenant uuid)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select coalesce(jsonb_agg(jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'name', x."name", 'url', x."url", 'transport', x."transport", 'auth_type', x."auth_type", 'credential_ref', x."credential_ref", 'scopes', x."scopes", 'client_metadata', x."client_metadata", 'enabled', x."enabled", 'created_by', x."created_by", 'created_at', x."created_at", 'updated_at', x."updated_at") || jsonb_build_object('grant', (
select jsonb_build_object('id', y."id", 'user_id', y."user_id", 'server_id', y."server_id", 'tenant_id', y."tenant_id", 'credential_ref', y."credential_ref", 'scopes', y."scopes", 'expires_at', y."expires_at", 'granted_at', y."granted_at", 'revoked_at', y."revoked_at") from "better_supabase"."connector_grants" y
where y."server_id" = x."id" and y."user_id" = auth.uid() and y."revoked_at" is null
)) order by x."name"), '[]')
from "better_supabase"."connector_servers" x where x."tenant_id" = list_connector_servers.tenant
$$;
revoke execute on function "better_supabase"."list_connector_servers"(uuid) from public, anon;
grant execute on function "better_supabase"."list_connector_servers"(uuid) to authenticated, service_role;
-- A server and the active grant of a user on it: the caller's, or for the
-- service role the one of owner.
create or replace function "better_supabase"."get_connector"(id uuid, owner uuid default null)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
v_user uuid := case when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then get_connector.owner else auth.uid() end;
v_row "better_supabase"."connector_servers"%rowtype;
begin
select * into v_row from "better_supabase"."connector_servers" x where x."id" = get_connector.id;
if not found or not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', v_row."tenant_id", 'connectors.read'), false)) then
return null;
end if;
return jsonb_build_object('id', v_row."id", 'tenant_id', v_row."tenant_id", 'name', v_row."name", 'url', v_row."url", 'transport', v_row."transport", 'auth_type', v_row."auth_type", 'credential_ref', v_row."credential_ref", 'scopes', v_row."scopes", 'client_metadata', v_row."client_metadata", 'enabled', v_row."enabled", 'created_by', v_row."created_by", 'created_at', v_row."created_at", 'updated_at', v_row."updated_at") || jsonb_build_object('grant', (
select jsonb_build_object('id', y."id", 'user_id', y."user_id", 'server_id', y."server_id", 'tenant_id', y."tenant_id", 'credential_ref', y."credential_ref", 'scopes', y."scopes", 'expires_at', y."expires_at", 'granted_at', y."granted_at", 'revoked_at', y."revoked_at") from "better_supabase"."connector_grants" y
where y."server_id" = v_row."id" and y."user_id" = v_user and y."revoked_at" is null
));
end;
$$;
revoke execute on function "better_supabase"."get_connector"(uuid, uuid) from public, anon;
grant execute on function "better_supabase"."get_connector"(uuid, uuid) to authenticated, service_role;
-- Records a user's grant after the provider stored the credential, revoking
-- the previous one (service role). Returns the new grant and the old one.
create or replace function "better_supabase"."record_connector_grant"(server_id uuid, owner uuid, credential_ref jsonb, scopes text[] default '{}', expires_at timestamptz default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_server "better_supabase"."connector_servers"%rowtype;
v_old "better_supabase"."connector_grants"%rowtype;
v_row "better_supabase"."connector_grants"%rowtype;
begin
select * into v_server from "better_supabase"."connector_servers" x where x."id" = record_connector_grant.server_id;
if not found then
raise exception 'connector % not found', record_connector_grant.server_id using errcode = 'P0002', hint = 'CONNECTOR_NOT_FOUND';
end if;
if not coalesce(better_supabase.member_can(record_connector_grant.owner, v_server."tenant_id", 'connectors.use'), false) then
raise exception 'the user may not use connectors here' using errcode = '42501', hint = 'CONNECTOR_FORBIDDEN';
end if;
update "better_supabase"."connector_grants" y set "revoked_at" = now()
where y."server_id" = v_server."id" and y."user_id" = record_connector_grant.owner and y."revoked_at" is null
returning * into v_old;
insert into "better_supabase"."connector_grants" ("user_id", "server_id", "tenant_id", "credential_ref", "scopes", "expires_at")
values (record_connector_grant.owner, v_server."id", v_server."tenant_id", record_connector_grant.credential_ref,
coalesce(record_connector_grant.scopes, '{}'), record_connector_grant.expires_at)
returning * into v_row;
null;
return jsonb_build_object('grant', jsonb_build_object('id', v_row."id", 'user_id', v_row."user_id", 'server_id', v_row."server_id", 'tenant_id', v_row."tenant_id", 'credential_ref', v_row."credential_ref", 'scopes', v_row."scopes", 'expires_at', v_row."expires_at", 'granted_at', v_row."granted_at", 'revoked_at', v_row."revoked_at"), 'replaced', case when v_old."id" is null then null else jsonb_build_object('id', v_old."id", 'user_id', v_old."user_id", 'server_id', v_old."server_id", 'tenant_id', v_old."tenant_id", 'credential_ref', v_old."credential_ref", 'scopes', v_old."scopes", 'expires_at', v_old."expires_at", 'granted_at', v_old."granted_at", 'revoked_at', v_old."revoked_at") end);
end;
$$;
revoke execute on function "better_supabase"."record_connector_grant"(uuid, uuid, jsonb, text[], timestamptz) from public, anon, authenticated;
grant execute on function "better_supabase"."record_connector_grant"(uuid, uuid, jsonb, text[], timestamptz) to service_role;
-- Revokes a grant (its owner or the service role) and returns it, so the
-- caller revokes the credential through its provider.
create or replace function "better_supabase"."revoke_connector_grant"(id uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."connector_grants"%rowtype;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
update "better_supabase"."connector_grants" y set "revoked_at" = now()
where y."id" = revoke_connector_grant.id and y."revoked_at" is null
and (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or y."user_id" = auth.uid())
returning * into v_row;
if not found then
return null;
end if;
delete from "better_supabase"."connector_sessions" z where z."server_id" = v_row."server_id" and z."user_id" = v_row."user_id";
null;
return jsonb_build_object('id', v_row."id", 'user_id', v_row."user_id", 'server_id', v_row."server_id", 'tenant_id', v_row."tenant_id", 'credential_ref', v_row."credential_ref", 'scopes', v_row."scopes", 'expires_at', v_row."expires_at", 'granted_at', v_row."granted_at", 'revoked_at', v_row."revoked_at");
end;
$$;
revoke execute on function "better_supabase"."revoke_connector_grant"(uuid) from public, anon;
grant execute on function "better_supabase"."revoke_connector_grant"(uuid) to authenticated, service_role;
-- Grants that expire before a time, for the renewal job (service role).
create or replace function "better_supabase"."expiring_connector_grants"(before timestamptz, max_rows integer default 100)
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select coalesce(jsonb_agg(jsonb_build_object('id', y."id", 'user_id', y."user_id", 'server_id', y."server_id", 'tenant_id', y."tenant_id", 'credential_ref', y."credential_ref", 'scopes', y."scopes", 'expires_at', y."expires_at", 'granted_at', y."granted_at", 'revoked_at', y."revoked_at") order by y."expires_at"), '[]')
from (
select * from "better_supabase"."connector_grants" y0
where y0."revoked_at" is null and y0."expires_at" is not null and y0."expires_at" < expiring_connector_grants.before
order by y0."expires_at"
limit least(greatest(expiring_connector_grants.max_rows, 1), 1000)
) y
$$;
revoke execute on function "better_supabase"."expiring_connector_grants"(timestamptz, integer) from public, anon, authenticated;
grant execute on function "better_supabase"."expiring_connector_grants"(timestamptz, integer) to service_role;
-- Moves a grant's expiry after the provider refreshed the token (service role).
create or replace function "better_supabase"."renew_connector_grant"(id uuid, expires_at timestamptz)
returns boolean
language sql
security definer
set search_path = ''
as $$
with updated as (
update "better_supabase"."connector_grants" y set "expires_at" = renew_connector_grant.expires_at
where y."id" = renew_connector_grant.id and y."revoked_at" is null
returning 1
)
select exists (select 1 from updated)
$$;
revoke execute on function "better_supabase"."renew_connector_grant"(uuid, timestamptz) from public, anon, authenticated;
grant execute on function "better_supabase"."renew_connector_grant"(uuid, timestamptz) to service_role;
-- The caller's MCP session with a server for a chat key, when it is still fresh.
create or replace function "better_supabase"."get_connector_session"(server_id uuid, chat_key text default '')
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select jsonb_build_object('session_id', z."session_id", 'initialize_result', z."initialize_result", 'expires_at', z."expires_at")
from "better_supabase"."connector_sessions" z
where z."server_id" = get_connector_session.server_id and z."user_id" = auth.uid()
and z."chat_key" = coalesce(get_connector_session.chat_key, '') and z."expires_at" > now()
$$;
revoke execute on function "better_supabase"."get_connector_session"(uuid, text) from public, anon;
grant execute on function "better_supabase"."get_connector_session"(uuid, text) to authenticated, service_role;
-- Saves the caller's MCP session; a null session_id forgets it.
create or replace function "better_supabase"."upsert_connector_session"(server_id uuid, chat_key text default '', session_id text default null, initialize_result jsonb default null)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if auth.uid() is null then
return false;
end if;
if upsert_connector_session.session_id is null and upsert_connector_session.initialize_result is null then
delete from "better_supabase"."connector_sessions" z where z."server_id" = upsert_connector_session.server_id and z."user_id" = auth.uid()
and z."chat_key" = coalesce(upsert_connector_session.chat_key, '');
return true;
end if;
if not exists (select 1 from "better_supabase"."connector_grants" y where y."server_id" = upsert_connector_session.server_id and y."user_id" = auth.uid() and y."revoked_at" is null)
and not exists (select 1 from "better_supabase"."connector_servers" x where x."id" = upsert_connector_session.server_id and x."auth_type" <> 'oauth' and coalesce(better_supabase.can('tenant', x."tenant_id", 'connectors.use'), false)) then
return false;
end if;
insert into "better_supabase"."connector_sessions" ("server_id", "user_id", "chat_key", "session_id", "initialize_result", "expires_at")
values (upsert_connector_session.server_id, auth.uid(), coalesce(upsert_connector_session.chat_key, ''), upsert_connector_session.session_id,
upsert_connector_session.initialize_result, now() + interval '1 hour')
on conflict ("server_id", "user_id", "chat_key") do update set
"session_id" = excluded."session_id", "initialize_result" = excluded."initialize_result",
"last_used_at" = now(), "expires_at" = excluded."expires_at";
return true;
end;
$$;
revoke execute on function "better_supabase"."upsert_connector_session"(uuid, text, text, jsonb) from public, anon;
grant execute on function "better_supabase"."upsert_connector_session"(uuid, text, text, jsonb) to authenticated, service_role;
-- Checks a server's tool list fingerprint: 'approved', 'pending' or
-- 'rejected'. A new fingerprint is recorded as pending, except the first one of a server, which is approved.
create or replace function "better_supabase"."check_connector_fingerprint"(server_id uuid, fingerprint text, tools jsonb default '{}')
returns text
language plpgsql
security definer
set search_path = ''
as $$
declare
v_server "better_supabase"."connector_servers"%rowtype;
v_status text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_server from "better_supabase"."connector_servers" x where x."id" = check_connector_fingerprint.server_id;
if not found or not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', v_server."tenant_id", 'connectors.use'), false)) then
raise exception 'connector % not found', check_connector_fingerprint.server_id using errcode = 'P0002', hint = 'CONNECTOR_NOT_FOUND';
end if;
select p."status" into v_status from "better_supabase"."connector_tool_fingerprints" p
where p."server_id" = v_server."id" and p."fingerprint" = check_connector_fingerprint.fingerprint;
if found then
return v_status;
end if;
v_status := case when exists (select 1 from "better_supabase"."connector_tool_fingerprints" p where p."server_id" = v_server."id") then 'pending' else 'approved' end;
insert into "better_supabase"."connector_tool_fingerprints" ("server_id", "fingerprint", "tools", "status", "approved_at")
values (v_server."id", check_connector_fingerprint.fingerprint, coalesce(check_connector_fingerprint.tools, '{}'), v_status,
case when v_status = 'approved' then now() end)
on conflict do nothing;
return v_status;
end;
$$;
revoke execute on function "better_supabase"."check_connector_fingerprint"(uuid, text, jsonb) from public, anon;
grant execute on function "better_supabase"."check_connector_fingerprint"(uuid, text, jsonb) to authenticated, service_role;
-- Approves or rejects a fingerprint ('connectors.manage').
create or replace function "better_supabase"."decide_connector_fingerprint"(server_id uuid, fingerprint text, approved boolean)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_server "better_supabase"."connector_servers"%rowtype;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_server from "better_supabase"."connector_servers" x where x."id" = decide_connector_fingerprint.server_id;
if not found or not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', v_server."tenant_id", 'connectors.manage'), false)) then
raise exception 'connector % not found', decide_connector_fingerprint.server_id using errcode = 'P0002', hint = 'CONNECTOR_NOT_FOUND';
end if;
update "better_supabase"."connector_tool_fingerprints" p set
"status" = case when decide_connector_fingerprint.approved then 'approved' else 'rejected' end,
"approved_by" = auth.uid(), "approved_at" = now()
where p."server_id" = v_server."id" and p."fingerprint" = decide_connector_fingerprint.fingerprint;
if not found then
return false;
end if;
null;
return true;
end;
$$;
revoke execute on function "better_supabase"."decide_connector_fingerprint"(uuid, text, boolean) from public, anon;
grant execute on function "better_supabase"."decide_connector_fingerprint"(uuid, text, boolean) to authenticated, service_role;
-- Deletes expired sessions (service role).
create or replace function "better_supabase"."purge_connector_sessions"()
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_count integer;
begin
delete from "better_supabase"."connector_sessions" z where z."expires_at" < now();
get diagnostics v_count = row_count;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."purge_connector_sessions"() from public, anon, authenticated;
grant execute on function "better_supabase"."purge_connector_sessions"() to service_role;
-- Tenant isolation (ADR 0099): permissive policies combine with or, so one
-- the app adds would widen the block's. These restrictive policies keep each
-- table to the caller's organizations and what the block's policies grant.
create policy bsb_tenant_scope_select on "better_supabase"."connector_servers" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('connectors.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."connector_servers" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."connector_servers" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."connector_servers" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."connector_grants" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("user_id" = (select auth.uid()))
);
create policy bsb_tenant_scope_insert on "better_supabase"."connector_grants" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."connector_grants" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."connector_grants" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
Client
This block has no React entry yet. Its TypeScript side is in better-supabase/blocks/connectors, which runs on the server.
Events
With the outbox installed, the module writes these in the same transaction as the change.
- connector.created
- connector.deleted
- connector.fingerprint_decided
- connector.updated
- connector_grant.created
- connector_grant.revoked
Dependencies
- Required: the install command adds it too
- Works with: optional
Requires
Works with
No optional integrations.
Recipes
More in Automation
Job queuequeue Typed jobs on Supabase Queues (pgmq) or a plain table (blocks.queue.options.backend): leases, retries with backoff, dead letters, deduplication keys, queue stats, and schedules with pg_cron or the drain route. Idempotency keysidempotency Stores responses by Idempotency-Key so retried requests replay the first result instead of running twice. Webhook inboxwebhook-inbox Stores verified webhooks once per message id, per tenant when given, then processes them with leases, retries and checkpoints, in its own table or one of the app's. Write rate limitsrate-limit Fixed-window limits on Data API writes (POST, PATCH, PUT, DELETE) per user or claim, checked by pgrst.db_pre_request. Over the limit: 429 with Retry-After. Outboxoutbox Events written in the same transaction as the change, read by named consumers with their own cursor and lease, kept for history and relayed as CloudEvents. Durable streamsstreams Ordered text chunks a writer appends and any reader resumes from an index: idempotent batches, a cancel flag the writer reads on its next append, owner reads through RLS, a payload-free Realtime ping on a private topic per stream, and a purge for expired streams.