MCP (Model Context Protocol) is a standard way for a server to advertise a set of callable tools. aidb.import_mcp_tools() registers an external MCP server and imports the tools it advertises, so your agents can call them alongside AIDB's own native and SQL tools:
SELECT aidb.import_mcp_tools( name => 'weather', url => 'https://weather.example.com/mcp', transport => 'streamable_http', -- optional; 'streamable_http' or 'sse' (see below). 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 ); SELECT aidb.create_agent('trip_planner', 'Help plan trips, checking weather when useful.', 'my_gpt', tools => ARRAY['get_forecast']); -- a tool the weather server advertised
| Parameter | Type | Default | Description |
|---|---|---|---|
name | TEXT | Required | Unique name for the server registration. |
url | TEXT | Required | The server's MCP endpoint URL. |
transport | TEXT | 'streamable_http' | 'streamable_http' or 'sse' — only 'streamable_http' is fully supported today. |
headers | JSONB | NULL | Request headers to send, for example, {"Authorization": "Bearer ..."}. Mutually exclusive with headers_env and headers_k8s_secret. |
tool_filter | TEXT | NULL | Import only these tool names. Omit to import everything the server advertises. |
headers_env | TEXT | NULL | Name of an environment variable holding the headers JSON, read fresh on each use instead of storing it. Superusers and members of aidb_admins only. Mutually exclusive with headers and headers_k8s_secret. |
headers_k8s_secret | TEXT | NULL | Path 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. Superusers and members of aidb_admins only. The path must start with the prefix set by aidb.k8s_secret_allowed_path_prefix. Mutually exclusive with headers and headers_env. |
share | BOOLEAN | NULL | Whether every member of aidb_users can call the server's tools. NULL shares the server only when no headers are passed. See Sharing a server. |
import_mcp_tools validates the server by calling its tools/list endpoint before registering anything — if it's unreachable or advertises nothing matching tool_filter, nothing is stored. The tools it returns are cached immediately, so they show up in aidb.tools right away rather than after a separate refresh.
Only the streamable_http transport (the default) actually works end-to-end today; sse is accepted as a value but isn't fully implemented yet.
AIDB sends Accept: application/json, text/event-stream with every request, and accepts a plain JSON or a server-sent events response. Before each call, AIDB runs the MCP initialize handshake and uses the session ID that the server returns, if any. If a server doesn't complete the handshake, AIDB calls it without one. AIDB sets the Accept, Mcp-Session-Id, and MCP-Protocol-Version headers itself and ignores them in headers.
A server can't advertise a tool with the name of an existing SQL tool. import_mcp_tools and refresh_mcp_tools raise an error for it. Use tool_filter to leave that tool out.
If the server answers with HTTP 421 (Invalid Host header), it checks the Host header of each request. Servers built with the MCP Python SDK do this by default when they listen on localhost. Add the host and port from url to the server's allowed hosts. In the MCP Python SDK, that's allowed_hosts in TransportSecuritySettings.
Reaching an MCP server is an outbound network call — subject to the same aidb.egress_allowlist restriction as model provider calls and HuggingFace downloads.
An MCP tool call isn't subject to purpose enforcement, unlike a native or custom SQL tool call: it's outbound HTTP, with no local Postgres identity to switch. See Governance.
Authentication
Pass headers for a server that needs specific request headers (most commonly an Authorization bearer token):
SELECT aidb.import_mcp_tools( name => 'internal_crm', url => 'https://crm.internal.example.com/mcp', headers => '{"Authorization": "Bearer sk-..."}'::JSONB );
headers is stored in aidb.mcp_registry. Members of aidb_users can't read it through SQL.
To read the headers from the Postgres server instead, pass one of these:
headers_env: the name of an environment variable that holds a JSON object of headers. The name must start with the prefix set byaidb.env_var_allowed_prefix(AIDB_by default).headers_k8s_secret: the path to a file that holds a JSON object of headers. The path must start with the prefix set byaidb.k8s_secret_allowed_path_prefix.
Only superusers and members of aidb_admins can pass them. A registration that uses them works only while its owner is a superuser or a member of aidb_admins. To limit the hosts the headers are sent to, the server operator can add a _HOST variable or file. See Credential environment variables and Kubernetes Secret paths.
The role that calls aidb.import_mcp_tools() owns the registration. Only the owner, or a member of the owner role, can refresh or delete it. AIDB stores the owner by role name. Once the owner role is renamed or dropped, no role can refresh or delete the registration until a role with that name exists again, for example after you rename the role back.
A new role created with that name also becomes the owner. To avoid both cases, delete a role's MCP server registrations before you drop or rename the role.
Sharing a server
A registration that stores headers isn't shared by default. Its tools can be called only by the owner role, members of the owner role, members of aidb_admins, and superusers. Other roles don't see the tools in aidb.tools. This applies to headers, headers_env, and headers_k8s_secret. A registration without headers is shared by default, so every member of aidb_users can call its tools.
To change this, pass share to aidb.import_mcp_tools(), or call aidb.set_mcp_shared() later:
SELECT aidb.set_mcp_shared('internal_crm', true);
Only the owner role, or a member of it, can change sharing.
Filtering which tools are imported
By default, every tool the server advertises is imported. Pass tool_filter to import only specific ones by name:
SELECT aidb.import_mcp_tools( name => 'weather', url => 'https://weather.example.com/mcp', tool_filter => ARRAY['get_forecast', 'get_alerts'] );
Keeping the tool list current
AIDB doesn't re-fetch a server's tool list on every query — aidb.tools only shows entries cached within the last 60 minutes, so a SELECT never has a network side effect. Call aidb.refresh_mcp_tools() to re-fetch and update the cache on demand (for example, after the server adds a new tool):
SELECT aidb.refresh_mcp_tools('weather');
It returns the number of tools now cached (after tool_filter applies). Changing a server's registration — its url, transport, headers, or tool_filter, via re-running import_mcp_tools-equivalent settings — also invalidates its cache automatically.
Removing a server
SELECT aidb.delete_tool('weather');
Deleting an MCP server removes every tool it advertised along with it — there's no way to remove just one of its tools individually; use tool_filter to curate which of its tools are exposed instead.
Exposing AIDB's own tools over MCP
aidb.get_mcp_tools() returns every tool currently in aidb.tools (native, SQL, and imported MCP tools alike), reshaped as MCP tools/list line items — useful if some other system needs AIDB's tool catalog in MCP's own descriptor format:
SELECT * FROM aidb.get_mcp_tools();
This function only converts the catalog into MCP's descriptor shape. The MCP server serves the same catalog to external MCP clients over the network.