Files
supabase/apps/studio/lib/ai/tools/registry.server.ts
Charis a05e7ec1d7 refactor(studio): move self-hosted Assistant tools to contracts (#52468)
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>)
2026-10-31 07:44:36 -04:00

67 lines
3.2 KiB
TypeScript

import type { ToolSet } from 'ai'
import { createTool as createCreateNotebook } from './create-notebook.server'
import { createTool as createDeleteNotebook } from './delete-notebook.server'
import { createTool as createDeployEdgeFunction } from './deploy-edge-function.server'
import { createTool as createEscalateToHuman } from './escalate-to-human.server'
import { createTool as createExecuteSql } from './execute-sql.server'
import { createTool as createGetActiveIncidents } from './get-active-incidents.server'
import { createTool as createGetNotebook } from './get-notebook.server'
import { createTool as createGetReport } from './get-report.server'
import { createTool as createListDatabases } from './list-databases.server'
import { createTool as createListFunctions } from './list-functions.server'
import { createTool as createListNotebooks } from './list-notebooks.server'
import { createTool as createListPolicies } from './list-policies.server'
import { createTool as createListReports } from './list-reports.server'
import { createTool as createListTables } from './list-tables.server'
import { createTool as createLoadKnowledge } from './load-knowledge.server'
import {
isToolAvailable,
TOOL_CONTRACTS,
type ContractToolName,
type ToolRequest,
} from './registry'
import { createTool as createRenameChat } from './rename-chat.server'
import { createTool as createResolveSupportConversation } from './resolve-support-conversation.server'
import { createTool as createRunNotebook } from './run-notebook.server'
import type { ToolContext, ToolFactory } from './types'
import { createTool as createUpdateNotebook } from './update-notebook.server'
import { createTool as createUpdateOptInLevel } from './update-opt-in-level.server'
/**
* The server half of every contract. The mapped type makes a missing or mismatched factory a
* type error, so a contract can't be added without a way to build it.
*/
export const TOOL_FACTORIES = {
create_notebook: createCreateNotebook,
delete_notebook: createDeleteNotebook,
deploy_edge_function: createDeployEdgeFunction,
escalate_to_human: createEscalateToHuman,
execute_sql: createExecuteSql,
get_active_incidents: createGetActiveIncidents,
get_notebook: createGetNotebook,
get_report: createGetReport,
list_databases: createListDatabases,
list_functions: createListFunctions,
list_notebooks: createListNotebooks,
list_policies: createListPolicies,
list_reports: createListReports,
list_tables: createListTables,
load_knowledge: createLoadKnowledge,
rename_chat: createRenameChat,
resolve_support_conversation: createResolveSupportConversation,
run_notebook: createRunNotebook,
update_notebook: createUpdateNotebook,
update_opt_in_level: createUpdateOptInLevel,
} as const satisfies { [K in ContractToolName]: ToolFactory<(typeof TOOL_CONTRACTS)[K]> }
/** Builds the contract tools that exist in the request's environment and whose requirements it meets. */
export function buildLocalTools(ctx: ToolContext, request: ToolRequest): ToolSet {
const tools: ToolSet = {}
for (const name of Object.keys(TOOL_FACTORIES) as ContractToolName[]) {
if (!isToolAvailable(TOOL_CONTRACTS[name].availability, request)) continue
tools[name] = TOOL_FACTORIES[name](ctx)
}
return tools
}