mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Stacked on supabase/supabase#52422. ## What The self-hosted Assistant tools become contracts, so every tool the Assistant can call now comes from the registry and the allowlist is derived from it. * The fallback tools (`getSchemaTables`, `getRlsKnowledge`, `getFunctions`, `getEdgeFunctionKnowledge`) are deleted. Self-hosted now gets the shared tools plus: * `list_tables`: a local contract with the same input as the MCP tool and a similar output, built from pg-meta. It shares its prompt guidance with the remote contract, which moves to `list-tables.remote.ts`. On platform the remote contract is still used. * `list_functions`: self-hosted only, filtered by schema in SQL. * `list_policies`: now available in both environments. * `runPgMetaQuery` (`tool-sql.server.ts`) runs pg-meta-built SQL and validates the rows with the builder's zod schema. Off platform it calls `executeQuery` directly, because the default fetcher goes through a relative API URL that doesn't resolve server-side. * The allowlist comes from the registry: * `ToolName` and the tool set validation schema are built from the contract keys. The tool category map is removed. * `mcp-tools.ts` keeps only tools with a remote contract, before opt-in filtering, so `UI_EXECUTED_TOOLS` is gone. `EXPECTED_MCP_TOOLS` is derived from the contracts and type-checked against the MCP package. * Saved chats: the legacy contracts now trim replayed outputs. `getRlsKnowledge` replays only the saved policy list, without the stale RLS instructions. `getEdgeFunctionKnowledge` replays a pointer to `load_knowledge`. * The logs reference text no longer names `query_logs`. ## Behavior changes * `list_policies` on platform validates rows before formatting, so a row that fails pg-meta's schema now errors the tool call instead of returning text. * `list_tables` on self-hosted doesn't include the MCP tool's optional `advisory` field. * `list_functions` with an empty `schemas` list returns every non-system schema. * On platform, if the MCP tools fail to load, `list_tables` is dropped from the tool set like the other remote tools. ## Tests * Unit tests for `runPgMetaQuery`, the local `list_tables` and `list_functions`, and the output mapping. The SQL boundary is mocked; not yet run against a live self-hosted database. * Legacy contract tests cover trimming, the empty and missing policy cases, and that replaying an already-trimmed output leaves it unchanged. ## Checklist Check all before review: - [X] I have read [CONTRIBUTING.md](<https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md>) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](<https://github.com/supabase/supabase/tree/master/apps/docs/style-guide>)
116 lines
3.8 KiB
TypeScript
116 lines
3.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { z } from 'zod'
|
|
|
|
import { getTools } from './index'
|
|
import { getMcpTools } from './mcp-tools'
|
|
|
|
const state = vi.hoisted(() => ({ isPlatform: true }))
|
|
|
|
vi.mock('common', async (importOriginal) => {
|
|
const actual = await importOriginal<Record<string, unknown>>()
|
|
const mocked = { ...actual }
|
|
Object.defineProperty(mocked, 'IS_PLATFORM', { get: () => state.isPlatform, enumerable: true })
|
|
return mocked
|
|
})
|
|
|
|
vi.mock('./mcp-tools', () => ({ getMcpTools: vi.fn() }))
|
|
|
|
const MCP_TOOL_NAMES = [
|
|
'list_tables',
|
|
'list_extensions',
|
|
'list_edge_functions',
|
|
'list_branches',
|
|
'search_docs',
|
|
'get_advisors',
|
|
'query_logs',
|
|
]
|
|
|
|
const BASE_PARAMS = {
|
|
projectRef: 'abcdefghijklmnopqrst',
|
|
connectionString: 'postgresql://localhost',
|
|
authorization: 'Bearer token',
|
|
aiOptInLevel: 'schema_and_log_and_data' as const,
|
|
accessToken: 'access-token',
|
|
signal: new AbortController().signal,
|
|
}
|
|
|
|
const SHARED = ['execute_sql', 'load_knowledge', 'rename_chat']
|
|
const SELF_HOSTED = ['list_functions', 'list_policies', 'list_tables']
|
|
const REMOVED = ['getEdgeFunctionKnowledge', 'getFunctions', 'getRlsKnowledge', 'getSchemaTables']
|
|
const SUPPORT = ['escalate_to_human', 'resolve_support_conversation']
|
|
const PLATFORM = [
|
|
'deploy_edge_function',
|
|
'get_report',
|
|
'list_databases',
|
|
'list_policies',
|
|
'list_reports',
|
|
'update_opt_in_level',
|
|
]
|
|
const EXPLORER = [
|
|
'create_notebook',
|
|
'delete_notebook',
|
|
'get_notebook',
|
|
'list_notebooks',
|
|
'run_notebook',
|
|
'update_notebook',
|
|
]
|
|
const INCIDENTS = ['get_active_incidents']
|
|
|
|
const sorted = (...groups: string[][]) => groups.flat().sort()
|
|
|
|
describe('ai/tools getTools availability', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(getMcpTools).mockResolvedValue(
|
|
Object.fromEntries(
|
|
MCP_TOOL_NAMES.map((name) => [
|
|
name,
|
|
{ description: 'x', inputSchema: z.object({}), execute: async () => ({}) },
|
|
])
|
|
) as never
|
|
)
|
|
})
|
|
|
|
const cases = [
|
|
// [isPlatform, isExplorerEnabled, supportMode, hasBaseUrl, expected]
|
|
[false, false, false, false, sorted(SHARED, SELF_HOSTED)],
|
|
[false, false, false, true, sorted(SHARED, SELF_HOSTED)],
|
|
[false, false, true, false, sorted(SHARED, SELF_HOSTED)],
|
|
[false, false, true, true, sorted(SHARED, SELF_HOSTED)],
|
|
[false, true, false, false, sorted(SHARED, SELF_HOSTED)],
|
|
[false, true, false, true, sorted(SHARED, SELF_HOSTED)],
|
|
[false, true, true, false, sorted(SHARED, SELF_HOSTED)],
|
|
[false, true, true, true, sorted(SHARED, SELF_HOSTED)],
|
|
[true, false, false, false, sorted(SHARED, PLATFORM, MCP_TOOL_NAMES)],
|
|
[true, false, false, true, sorted(SHARED, PLATFORM, MCP_TOOL_NAMES, INCIDENTS)],
|
|
[true, false, true, false, sorted(SHARED, PLATFORM, MCP_TOOL_NAMES, SUPPORT)],
|
|
[true, false, true, true, sorted(SHARED, PLATFORM, MCP_TOOL_NAMES, SUPPORT, INCIDENTS)],
|
|
[true, true, false, false, sorted(SHARED, PLATFORM, MCP_TOOL_NAMES, EXPLORER)],
|
|
[true, true, false, true, sorted(SHARED, PLATFORM, MCP_TOOL_NAMES, EXPLORER, INCIDENTS)],
|
|
[true, true, true, false, sorted(SHARED, PLATFORM, MCP_TOOL_NAMES, EXPLORER, SUPPORT)],
|
|
[
|
|
true,
|
|
true,
|
|
true,
|
|
true,
|
|
sorted(SHARED, PLATFORM, MCP_TOOL_NAMES, EXPLORER, SUPPORT, INCIDENTS),
|
|
],
|
|
] as const
|
|
|
|
it.each(cases)(
|
|
'IS_PLATFORM=%s isExplorerEnabled=%s supportMode=%s baseUrl=%s',
|
|
async (isPlatform, isExplorerEnabled, supportMode, hasBaseUrl, expected) => {
|
|
state.isPlatform = isPlatform
|
|
|
|
const tools = await getTools({
|
|
...BASE_PARAMS,
|
|
isExplorerEnabled,
|
|
supportMode,
|
|
baseUrl: hasBaseUrl ? 'https://supabase.com/dashboard' : undefined,
|
|
})
|
|
|
|
expect(Object.keys(tools).sort()).toEqual(expected)
|
|
for (const name of REMOVED) expect(tools).not.toHaveProperty(name)
|
|
}
|
|
)
|
|
})
|