Tools reference v7

Reference for tool-related functions, views, and types. For guide-style documentation, see Tools. For the full native tool catalog, see Native tools.

Catalog views

aidb.tools

The unified, read-only catalog of every tool AIDB knows about — the union of native tools, registered SQL tools, and cached MCP tools.

ColumnTypeDescription
nametextThe tool's name.
descriptiontextShown to the model as part of the tool's definition.
tool_typetext'native_tool', 'sql_tool', or 'mcp_tool'.
paramsjsonbFor native_tool/sql_tool: an array of {name, type, description} objects. For mcp_tool: the server's raw JSON Schema inputSchema.
read_onlybooleanWhether the tool is known to never write. Always false for mcp_tool rows — see read-only mode.

aidb.sql_tool_registry

Backs registered SQL tools. aidb.tools is the union view most queries should use instead; query this directly to see a SQL tool's stored query text.

ColumnTypeDescription
nametextUnique tool name (primary key).
descriptiontextShown to the model.
query_texttextThe stored SQL statement, with ${name} placeholders.
read_onlybooleanWhether the tool is enforced as read-only.
return_type_hintaidb.ToolParam[]Documented return shape. Not currently surfaced to the model.
paramsaidb.ToolParam[]The tool's declared parameters.
created_attimestampCreation time.
updated_attimestampLast update time.
owner_roletextRole that owns the tool. Defaults to the role that created it.

Every member of aidb_users can read every SQL tool. Only a role that can SET ROLE to a tool's owner_role can change or delete it, with aidb.delete_tool() or with UPDATE and DELETE on the table. A direct write by any other role affects 0 rows. If the owner role is renamed or dropped, a superuser or a member of aidb_admins can delete the tool.

aidb.mcp_registry

Registered external MCP servers.

ColumnTypeDescription
nametextUnique server name (primary key).
urltextThe server's MCP endpoint.
transporttext'streamable_http' or 'sse'.
headersjsonbStored request headers. Not readable by aidb_users (may hold secrets).
headers_envtextEnvironment variable name to read headers from instead. Mutually exclusive with headers and headers_k8s_secret.
headers_k8s_secrettextFile path to read headers from instead. Mutually exclusive with headers and headers_env.
tool_filtertext[]Tool names imported from this server, if filtered.
ownertextRole that registered the server.
sharedbooleanIf true, every member of aidb_users can call the server's tools. If false, only the owner role, its members, members of aidb_admins, and superusers can. See aidb.set_mcp_shared.
created_attimestampRegistration time.
updated_attimestampLast update time.

Tool management functions

aidb.create_sql_tool

Registers a parameterized SQL query as a tool.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredUnique name for the tool.
descriptionTEXTRequiredShown to the model as part of the tool's definition.
sql_statementTEXTRequiredThe query to run, with ${name} placeholders for each declared parameter.
paramsJSONBRequiredThe tool's parameters, built with aidb.tool_params().
read_onlyBOOLEANtrueIf true, reject the statement at registration time unless it's provably read-only.
return_type_hintJSONBNULLDocuments the query's return shape. Not currently surfaced to the model.

Returns

TEXT — the tool's name, on success. Raises if the name is already taken (by any tool type) or the statement isn't a single, invocable, read-only-compliant (if read_only => true) statement.

Also raises if the statement uses positional placeholders such as $1. Write each parameter as ${name} and declare it in params. If the statement has a syntax error and uses another placeholder style, such as :name, $name, or {{name}}, the error shows the ${name} form.

Example

SELECT aidb.create_sql_tool(
    name             => 'orders_by_customer',
    description      => 'Look up recent orders for a customer by id.',
    sql_statement    => 'SELECT id, status, total FROM orders WHERE customer_id = ${customer_id} ORDER BY created_at DESC LIMIT ${limit}',
    params           => aidb.tool_params(
        aidb.tool_param('customer_id', 'INT', 'The customer''s id.'),
        aidb.tool_param('limit', 'INT', 'Maximum number of orders to return.')
    ),
    read_only        => true,  -- optional; enforced, not just descriptive. Default: true
    return_type_hint => NULL   -- optional; documents the returned row shape, same aidb.tool_params() shape as `params`. Default: NULL
);

aidb.delete_tool

Deletes a SQL tool or an MCP server registration by name (removing every tool the server advertised along with it). Built-in native tools can't be deleted.

You also can't delete the SQL tools that a semantic knowledge base publishes with its managed agent plugin. Those tools are named semkb_<kb_name>_search, semkb_<kb_name>_join_path, and semkb_<kb_name>_suggest_joins. AIDB removes them when you delete the knowledge base.

Only a role that can SET ROLE to a SQL tool's owner_role can delete it.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredThe tool or server name.
if_existsBOOLEANfalseIf true and no such tool or server exists, raises a notice and returns NULL.

Returns

TEXT — the deleted name, on success. Raises if no such tool or server exists (unless if_exists is true), or if name is a native tool.


aidb.import_mcp_tools

Registers an external MCP server and imports the tools it advertises.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredUnique name for the server registration.
urlTEXTRequiredThe server's MCP endpoint URL.
transportTEXT'streamable_http''streamable_http' or 'sse' — only 'streamable_http' is fully supported today.
headersJSONBNULLRequest headers to send. Mutually exclusive with headers_env and headers_k8s_secret.
tool_filterTEXT[]NULLImport only these tool names. Omit to import everything the server advertises.
headers_envTEXTNULLEnvironment variable holding the headers JSON, read fresh on each use instead of storing it. Mutually exclusive with headers and headers_k8s_secret. Name must start with the aidb.env_var_allowed_prefix prefix.
headers_k8s_secretTEXTNULLPath to a file holding the headers JSON, read on each use instead of storing it. Meant for a Kubernetes Secret mounted into the Postgres pod. Path must start with the aidb.k8s_secret_allowed_path_prefix prefix. Mutually exclusive with headers and headers_env.
shareBOOLEANNULLIf true, every member of aidb_users can call the server's tools. If false, only the owner role, its members, members of aidb_admins, and superusers can. NULL shares the server only when you pass none of headers, headers_env, and headers_k8s_secret.

Returns

TEXT — the server's name, on success. Raises if the name is taken, the URL is unreachable, or no advertised tool matches tool_filter. Also raises if one of the server's tools has the name of an existing SQL tool. Use tool_filter to leave that tool out.

Example

SELECT aidb.import_mcp_tools(
    name        => 'weather',
    url         => 'https://weather.example.com/mcp',
    transport   => 'streamable_http',  -- optional; 'streamable_http' or 'sse'. Default: 'streamable_http'
    headers     => NULL,               -- optional; request headers to send, for example, an auth token. Mutually exclusive with headers_env and headers_k8s_secret. Default: NULL
    tool_filter => NULL,               -- optional; import only these tool names. Default: NULL (import everything the server advertises)
    headers_env => NULL,               -- optional; env var holding the headers JSON instead of storing it. Mutually exclusive with headers and headers_k8s_secret. Default: NULL
    headers_k8s_secret => NULL,        -- optional; file path to a mounted Kubernetes Secret holding the headers JSON. Mutually exclusive with headers and headers_env. Default: NULL
    share       => NULL                -- optional; let every aidb_users member call the tools. Default: NULL (shared only when no headers are passed)
);

aidb.refresh_mcp_tools

Re-fetches an already-registered MCP server's advertised tools and refreshes the cache.

Parameters

ParameterTypeDescription
nameTEXTThe registered server's name.

Returns

INTEGER — the number of tools now cached (after tool_filter applies). Raises if one of the server's tools has the name of an existing SQL tool.


aidb.set_mcp_shared

Changes whether every member of aidb_users can call a registered MCP server's tools. Only the role that registered the server, or a member of that role, can call it. The cached tools stay as they are.

Parameters

ParameterTypeDescription
nameTEXTThe registered server's name.
shareBOOLEANtrue lets every member of aidb_users call the tools. false limits them to the owner role, its members, members of aidb_admins, and superusers.

Returns

TEXT — the server's name, on success. Raises if the server doesn't exist or the caller isn't allowed to change it.

Example

SELECT aidb.set_mcp_shared('weather', true);

aidb.get_mcp_tools

Converts every currently registered tool (native, SQL, and MCP alike) into an MCP tools/list-shaped descriptor.

Returns

TABLE(name TEXT, description TEXT, input_schema JSONB) — one row per tool in aidb.tools.


aidb.run_tool

Runs any tool in aidb.tools by name, whether native, SQL, or MCP, and returns its result. The tool runs as the caller's own session role. A read_only SQL tool runs read only. That doesn't change the caller's transaction, which can still write afterward. The MCP server dispatches catalog tools through this function.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredThe tool name.
argumentsJSONB'{}'The tool's arguments, as a JSON object. Omit it or pass NULL for a tool without parameters, such as aidb.run_tool('list_models'). Every declared parameter of a SQL tool is required.

Returns

JSONB, the tool's result. The function raises an error if:

  • The tool doesn't exist.
  • arguments isn't a JSON object.
  • arguments has a key the tool doesn't declare.
  • arguments leaves out a required argument.
  • An MCP tool returns an isError result.
  • The tool is an MCP tool whose server isn't shared, and the caller isn't the owner role, a member of it, a member of aidb_admins, or a superuser.
  • The name belongs to both a SQL tool and an MCP tool. Delete the SQL tool, or leave the MCP tool out with tool_filter.

Example

SELECT aidb.run_tool('catalog_list_objects', '{"schema": "public", "object_type": "table"}');

Parameter helpers

aidb.tool_param

Builds one SQL tool parameter.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredThe parameter's name.
typeTEXTRequiredThe parameter's SQL type.
descriptionTEXTRequiredDescription shown to the model.

Returns

JSONB — pass one or more of these to aidb.tool_params().


aidb.tool_params

Combines one or more aidb.tool_param() results, or (called with no arguments) declares an empty parameter list.

Parameters

ParameterTypeDescription
paramsVARIADIC JSONB[]Zero or more aidb.tool_param() results.

Returns

JSONB — pass to aidb.create_sql_tool()'s params or return_type_hint.


aidb.param / aidb.params

Generic parameter-spec constructors, used by native tools that accept an enum-constrained argument. aidb.param() additionally accepts enum_values TEXT[]. Not typically needed directly — aidb.tool_param()/aidb.tool_params() are the constructors for create_sql_tool().