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.

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.
tool_filtertext[]Tool names imported from this server, if filtered.
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.

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.

Parameters

ParameterTypeDescription
nameTEXTThe tool or server name.

Returns

TEXT — the deleted name, on success. Raises if no such tool or server exists, 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.
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. Name must start with the aidb.env_var_allowed_prefix prefix.

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.

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. 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. Default: NULL
);

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).


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.


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().