Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(studio): add list_databases tool for the AI assistant (#49328)

Charis0f730de9e0d7
diff --git a/apps/studio/lib/ai/tool-filter.ts b/apps/studio/lib/ai/tool-filter.ts
index 9b296d0c0da..6de6ad5921d 100644
--- a/apps/studio/lib/ai/tool-filter.ts
+++ b/apps/studio/lib/ai/tool-filter.ts
@@ -37,6 +37,7 @@ export const toolSetValidationSchema = z.record(
'list_policies',
'list_reports',
'get_report',
+ 'list_databases',
'list_notebooks',
'get_notebook',
'create_notebook',
@@ -92,6 +93,7 @@ export const TOOL_CATEGORY_MAP: Record<string, ToolCategory> = {
list_policies: TOOL_CATEGORIES.SCHEMA,
list_reports: TOOL_CATEGORIES.SCHEMA,
get_report: TOOL_CATEGORIES.SCHEMA,
+ list_databases: TOOL_CATEGORIES.SCHEMA,
list_notebooks: TOOL_CATEGORIES.SCHEMA,
get_notebook: TOOL_CATEGORIES.SCHEMA,
create_notebook: TOOL_CATEGORIES.SCHEMA,
diff --git a/apps/studio/lib/ai/tools/mock-tools.ts b/apps/studio/lib/ai/tools/mock-tools.ts
index d288e17744f..deabe227dd0 100644
--- a/apps/studio/lib/ai/tools/mock-tools.ts
+++ b/apps/studio/lib/ai/tools/mock-tools.ts
@@ -411,14 +411,36 @@ function createMockNotebookStore() {
type MockNotebookStore = ReturnType<typeof createMockNotebookStore>
-// All four notebook tools are real, locally-defined ai-SDK tools, so wrap them and
+const MOCK_DATABASES_DATA = [
+ {
+ identifier: 'mock-project-ref',
+ is_primary: true,
+ region: 'us-east-1',
+ status: 'ACTIVE_HEALTHY',
+ },
+ {
+ identifier: 'mock-project-ref-replica-1',
+ is_primary: false,
+ region: 'us-west-1',
+ status: 'ACTIVE_HEALTHY',
+ },
+]
+
+// All five notebook tools are real, locally-defined ai-SDK tools, so wrap them and
// override only execute/needsApproval — evals must validate the model's arguments
// against the exact schemas production uses (agentCellSchema's `.strict()` rejection of
// agent-authored cell ids, update_notebook's real operations schema, etc).
function createMockNotebookTools(store: MockNotebookStore) {
- const { list_notebooks, get_notebook, create_notebook, update_notebook } = getNotebookTools()
+ const { list_databases, list_notebooks, get_notebook, create_notebook, update_notebook } =
+ getNotebookTools()
return {
+ list_databases: {
+ ...list_databases,
+ execute: async (_args: object, _options: ToolExecutionOptions<unknown>) => ({
+ databases: MOCK_DATABASES_DATA,
+ }),
+ },
list_notebooks: {
...list_notebooks,
execute: async (
diff --git a/apps/studio/lib/ai/tools/notebook-tools.test.ts b/apps/studio/lib/ai/tools/notebook-tools.test.ts
index fb692afbf20..918a702210d 100644
--- a/apps/studio/lib/ai/tools/notebook-tools.test.ts
+++ b/apps/studio/lib/ai/tools/notebook-tools.test.ts
@@ -52,10 +52,11 @@ const NOTEBOOK_CONTENT = {
describe('ai/tools/notebook-tools', () => {
describe('getNotebookTools', () => {
- it('should return list_notebooks, get_notebook, create_notebook, and update_notebook tools', () => {
+ it('should return list_databases, list_notebooks, get_notebook, create_notebook, and update_notebook tools', () => {
const tools = getNotebookTools()
expect(Object.keys(tools)).toEqual([
+ 'list_databases',
'list_notebooks',
'get_notebook',
'create_notebook',
@@ -78,6 +79,69 @@ describe('ai/tools/notebook-tools', () => {
})
})
+ describe('list_databases', () => {
+ it('should list databases with a computed is_primary flag', async () => {
+ addAPIMock({
+ method: 'get',
+ path: '/platform/projects/:ref/databases',
+ response: () =>
+ HttpResponse.json<components['schemas']['DatabaseDetailResponse'][]>([
+ {
+ identifier: 'test-project',
+ region: 'us-east-1',
+ status: 'ACTIVE_HEALTHY',
+ cloud_provider: 'AWS',
+ db_host: 'db.test-project.supabase.co',
+ db_name: 'postgres',
+ db_port: 5432,
+ db_user: 'postgres',
+ inserted_at: '2026-01-01T00:00:00.000Z',
+ restUrl: 'https://test-project.supabase.co/rest/v1',
+ size: 't4g.micro',
+ },
+ {
+ identifier: 'test-project-replica-1',
+ region: 'us-west-1',
+ status: 'COMING_UP',
+ cloud_provider: 'AWS',
+ db_host: 'db.test-project-replica-1.supabase.co',
+ db_name: 'postgres',
+ db_port: 5432,
+ db_user: 'postgres',
+ inserted_at: '2026-01-01T00:00:00.000Z',
+ restUrl: 'https://test-project-replica-1.supabase.co/rest/v1',
+ size: 't4g.micro',
+ },
+ ]),
+ })
+
+ const tools = getNotebookTools({ projectRef: 'test-project' })
+ if (!tools.list_databases.execute) throw new Error('execute is undefined')
+
+ const result = await tools.list_databases.execute(
+ {},
+ { toolCallId: 'test', messages: [], context: {} }
+ )
+
+ expect(result).toEqual({
+ databases: [
+ {
+ identifier: 'test-project',
+ is_primary: true,
+ region: 'us-east-1',
+ status: 'ACTIVE_HEALTHY',
+ },
+ {
+ identifier: 'test-project-replica-1',
+ is_primary: false,
+ region: 'us-west-1',
+ status: 'COMING_UP',
+ },
+ ],
+ })
+ })
+ })
+
describe('list_notebooks', () => {
it('should list notebooks with summary fields, forwarding the authorization header and cursor', async () => {
let capturedRequest: Request | undefined
diff --git a/apps/studio/lib/ai/tools/notebook-tools.ts b/apps/studio/lib/ai/tools/notebook-tools.ts
index 2c4498d459e..b0dfae57074 100644
--- a/apps/studio/lib/ai/tools/notebook-tools.ts
+++ b/apps/studio/lib/ai/tools/notebook-tools.ts
@@ -17,6 +17,7 @@ import {
} from '@/data/content/notebooks/notebook-schema'
import { createNotebook, upsertNotebook } from '@/data/content/notebooks/notebook-upsert-mutation'
import { acceptUntrustedLogsSql, untrustedLogSql } from '@/data/logs/safe-analytics-sql'
+import { getReadReplicas } from '@/data/read-replicas/replicas-query'
import type { Notebooks } from '@/types'
export type NotebookToolsContext = {
@@ -77,6 +78,23 @@ export const getNotebookTools = (ctx: NotebookToolsContext = {}) => {
const authHeaders = authorization ? { Authorization: authorization } : undefined
return {
+ list_databases: tool({
+ description:
+ 'List the databases available for this project — the primary and any read replicas.',
+ inputSchema: z.object({}),
+ execute: async () => {
+ const databases = await getReadReplicas({ projectRef }, undefined, authHeaders)
+
+ return {
+ databases: (databases ?? []).map((database) => ({
+ identifier: database.identifier,
+ is_primary: database.identifier === projectRef,
+ region: database.region,
+ status: database.status,
+ })),
+ }
+ },
+ }),
list_notebooks: tool({
description: 'List the notebooks saved for this project',
inputSchema: z.object({