The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
refactor(studio): migrate more remote MCP tools to contracts (#52226)
Charisaf876fafc0b4
diff --git a/apps/studio/lib/ai/tool-filter.test.ts b/apps/studio/lib/ai/tool-filter.test.tsindex d5ddf565d6e..189206bbacf 100644--- a/apps/studio/lib/ai/tool-filter.test.ts+++ b/apps/studio/lib/ai/tool-filter.test.ts@@ -25,8 +25,7 @@ import { contract as updateNotebookContract } from './tools/update-notebook'describe('TOOL_CATEGORY_MAP', () => {it('should categorize tools correctly', () => {expect(TOOL_CATEGORY_MAP['search_docs']).toBe(TOOL_CATEGORIES.UI)- expect(TOOL_CATEGORY_MAP['list_tables']).toBe(TOOL_CATEGORIES.SCHEMA)- expect(TOOL_CATEGORY_MAP['get_advisors']).toBe(TOOL_CATEGORIES.LOG)+ expect(TOOL_CATEGORY_MAP['getSchemaTables']).toBe(TOOL_CATEGORIES.SCHEMA)})})diff --git a/apps/studio/lib/ai/tool-filter.ts b/apps/studio/lib/ai/tool-filter.tsindex b2b02c078fd..2a762d4b421 100644--- a/apps/studio/lib/ai/tool-filter.ts+++ b/apps/studio/lib/ai/tool-filter.ts@@ -94,18 +94,11 @@ export const TOOL_CATEGORY_MAP: Record<// UI tools - always availablesearch_docs: TOOL_CATEGORIES.UI,- // Schema tools - MCP- list_tables: TOOL_CATEGORIES.SCHEMA,- list_edge_functions: TOOL_CATEGORIES.SCHEMA,- list_branches: TOOL_CATEGORIES.SCHEMA,+ // Schema toolsgetSchemaTables: TOOL_CATEGORIES.SCHEMA,getRlsKnowledge: TOOL_CATEGORIES.SCHEMA,getFunctions: TOOL_CATEGORIES.SCHEMA,getEdgeFunctionKnowledge: TOOL_CATEGORIES.SCHEMA,-- // Log tools - MCP and local- get_advisors: TOOL_CATEGORIES.LOG,- query_logs: TOOL_CATEGORIES.LOG,}/**diff --git a/apps/studio/lib/ai/tools/get-advisors.ts b/apps/studio/lib/ai/tools/get-advisors.tsnew file mode 100644index 00000000000..ad41da80695--- /dev/null+++ b/apps/studio/lib/ai/tools/get-advisors.ts@@ -0,0 +1,7 @@+import type { RemoteToolContract } from './types'++export const contract = {+ name: 'get_advisors',+ minOptInLevel: 'schema_and_log',+ needsApproval: false,+} as const satisfies RemoteToolContractdiff --git a/apps/studio/lib/ai/tools/list-branches.ts b/apps/studio/lib/ai/tools/list-branches.tsnew file mode 100644index 00000000000..d35742f24b5--- /dev/null+++ b/apps/studio/lib/ai/tools/list-branches.ts@@ -0,0 +1,7 @@+import type { RemoteToolContract } from './types'++export const contract = {+ name: 'list_branches',+ minOptInLevel: 'schema',+ needsApproval: false,+} as const satisfies RemoteToolContractdiff --git a/apps/studio/lib/ai/tools/list-edge-functions.ts b/apps/studio/lib/ai/tools/list-edge-functions.tsnew file mode 100644index 00000000000..5003f1bed67--- /dev/null+++ b/apps/studio/lib/ai/tools/list-edge-functions.ts@@ -0,0 +1,7 @@+import type { RemoteToolContract } from './types'++export const contract = {+ name: 'list_edge_functions',+ minOptInLevel: 'schema',+ needsApproval: false,+} as const satisfies RemoteToolContractdiff --git a/apps/studio/lib/ai/tools/list-tables.ts b/apps/studio/lib/ai/tools/list-tables.tsnew file mode 100644index 00000000000..0603d9e0d06--- /dev/null+++ b/apps/studio/lib/ai/tools/list-tables.ts@@ -0,0 +1,7 @@+import type { RemoteToolContract } from './types'++export const contract = {+ name: 'list_tables',+ minOptInLevel: 'schema',+ needsApproval: false,+} as const satisfies RemoteToolContractdiff --git a/apps/studio/lib/ai/tools/mcp-tools.test.ts b/apps/studio/lib/ai/tools/mcp-tools.test.tsindex 3cdc208cf7c..1e67d7190c0 100644--- a/apps/studio/lib/ai/tools/mcp-tools.test.ts+++ b/apps/studio/lib/ai/tools/mcp-tools.test.ts@@ -56,7 +56,16 @@ describe('ai/tools/mcp-tools getMcpTools', () => {it('takes needsApproval from the contract', async () => {const result = await getMcpTools(BASE_PARAMS)- expect(result.list_extensions?.needsApproval).toBe(false)+ for (const name of [+ 'list_tables',+ 'list_extensions',+ 'list_edge_functions',+ 'list_branches',+ 'get_advisors',+ 'query_logs',+ ] as const) {+ expect(result[name]?.needsApproval).toBe(false)+ }})it('warns when the remote server is missing an expected tool (contract drift)', async () => {@@ -123,4 +132,13 @@ describe('ai/tools/mcp-tools getMcpTools', () => {await expect(getMcpTools(BASE_PARAMS)).rejects.toThrow('MCP tools validation failed')expect(close).toHaveBeenCalledTimes(1)})++ it('rejects a malformed contract tool even when the opt-in level would stub it', async () => {+ tools.mockResolvedValueOnce({ ...FULL_REMOTE_TOOLS, list_tables: 'not-an-object' })++ await expect(getMcpTools({ ...BASE_PARAMS, aiOptInLevel: 'disabled' })).rejects.toThrow(+ 'MCP tools validation failed'+ )+ expect(close).toHaveBeenCalledTimes(1)+ })})diff --git a/apps/studio/lib/ai/tools/mcp-tools.ts b/apps/studio/lib/ai/tools/mcp-tools.tsindex 9d4ff58892b..9528fe61a5b 100644--- a/apps/studio/lib/ai/tools/mcp-tools.ts+++ b/apps/studio/lib/ai/tools/mcp-tools.ts@@ -83,10 +83,15 @@ export const getMcpTools = async ({const availableMcpTools: ToolSet = { ...remoteTools }for (const contract of Object.values(REMOTE_TOOL_CONTRACTS)) {+ if (!(contract.name in remoteTools)) continue+ // Reject a malformed entry here: if the opt-in filter below stubbed it, the spread would+ // turn it into an object and it would pass validation.const remoteTool = remoteTools[contract.name]- if (remoteTool) {- availableMcpTools[contract.name] = { ...remoteTool, needsApproval: contract.needsApproval }+ if (typeof remoteTool !== 'object' || remoteTool === null) {+ console.error(`MCP tool ${contract.name} is not an object`)+ throw new Error('Internal error: MCP tools validation failed')}+ availableMcpTools[contract.name] = { ...remoteTool, needsApproval: contract.needsApproval }}// Runtime drift detection: `EXPECTED_MCP_TOOLS` is compile-time-checkeddiff --git a/apps/studio/lib/ai/tools/query-logs.ts b/apps/studio/lib/ai/tools/query-logs.tsnew file mode 100644index 00000000000..12bdeedc23a--- /dev/null+++ b/apps/studio/lib/ai/tools/query-logs.ts@@ -0,0 +1,7 @@+import type { RemoteToolContract } from './types'++export const contract = {+ name: 'query_logs',+ minOptInLevel: 'schema_and_log',+ needsApproval: false,+} as const satisfies RemoteToolContractdiff --git a/apps/studio/lib/ai/tools/registry.ts b/apps/studio/lib/ai/tools/registry.tsindex 384400e6a84..d8d81f13e3e 100644--- a/apps/studio/lib/ai/tools/registry.ts+++ b/apps/studio/lib/ai/tools/registry.ts@@ -7,14 +7,19 @@ import { contract as deployEdgeFunction } from './deploy-edge-function'import { contract as escalateToHuman } from './escalate-to-human'import { contract as executeSql } from './execute-sql'import { contract as getActiveIncidents } from './get-active-incidents'+import { contract as getAdvisors } from './get-advisors'import { contract as getNotebook } from './get-notebook'import { contract as getReport } from './get-report'+import { contract as listBranches } from './list-branches'import { contract as listDatabases } from './list-databases'+import { contract as listEdgeFunctions } from './list-edge-functions'import { contract as listExtensions } from './list-extensions'import { contract as listNotebooks } from './list-notebooks'import { contract as listPolicies } from './list-policies'import { contract as listReports } from './list-reports'+import { contract as listTables } from './list-tables'import { contract as loadKnowledge } from './load-knowledge'+import { contract as queryLogs } from './query-logs'import { contract as renameChat } from './rename-chat'import { contract as resolveSupportConversation } from './resolve-support-conversation'import { contract as runNotebook } from './run-notebook'@@ -83,7 +88,12 @@ export function getToolContract(toolName: string): ToolContract | undefined {* part of `StudioUIMessage`.*/export const REMOTE_TOOL_CONTRACTS = {+ get_advisors: getAdvisors,+ list_branches: listBranches,+ list_edge_functions: listEdgeFunctions,list_extensions: listExtensions,+ list_tables: listTables,+ query_logs: queryLogs,} as const satisfies Partial<Record<ToolName, RemoteToolContract>>export type RemoteContractToolName = keyof typeof REMOTE_TOOL_CONTRACTS