The MCP server speaks streamable HTTP at /mcp, which serves the default database. When the endpoint serves several databases, each one has its own path, /mcp/<database>. See Serving multiple databases. Point any MCP client that supports streamable HTTP and HTTP Basic authentication at the endpoint's URL, and give it a Postgres username and password. To create one, see Configuring your agent role. The examples below use http://<mcp-host>:8765/mcp as a placeholder. Substitute 127.0.0.1 if your client runs on the database host itself (the loopback default), or the endpoint's routable host and https if you've set up Serving remote clients.
The steps below show the wire protocol with curl. The endpoint answers in the server-sent events format, so the JSON-RPC response is the line that starts with data: rather than the whole response body.
Open a session. The handshake needs no credentials — that's true for a remote client too, since only
tools/listandtools/callcarry the agent's Postgres username and password.curl -sS -D - http://<mcp-host>:8765/mcp \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -d '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "example", "version": "0"}}}'
The response headers include
mcp-session-id. Export it as an environment variable and reuse it on every later request:export SESSION_ID=<value-from-the-mcp-session-id-header>
Acknowledge the handshake.
curl -sS http://<mcp-host>:8765/mcp \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "mcp-session-id: $SESSION_ID" \ -d '{"jsonrpc": "2.0", "method": "notifications/initialized"}'
List tools as a Postgres role.
tools/listreturns the built-inexecute_sql, then every tool the catalog holds, then, for a member ofaidb_users, every AIDB agent. For the arguments and result of an agent call, see Calling agents as tools.curl -sS http://<mcp-host>:8765/mcp \ -u <username>:<password> \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "mcp-session-id: $SESSION_ID" \ -d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}'
<username>:<password>is a Postgres role you've created and granted access to. See Provisioning example for a worked example with real role names and grants. Thedata:line'sresult.toolsarray lists each tool's name, description, and argument schema, for example:{"jsonrpc": "2.0", "id": 2, "result": {"tools": [ {"name": "execute_sql", "description": "Run a single SQL statement", "inputSchema": {"type": "object", "properties": {"sql": {"type": "string"}}, "required": ["sql"]}}, {"name": "add_order", "description": "Insert an order", "inputSchema": {"type": "object", "properties": {"item": {"type": "string"}, "amount": {"type": "number"}}, "required": ["item", "amount"]}}, {"name": "db_helper", "description": "Delegate part of the request to the 'db_helper' agent: ...", "inputSchema": {"type": "object", "properties": {"prompt": {"type": "string", "description": "The request to hand off to this delegate agent."}, "conversation_id": {"type": "string", "description": "Conversation id returned by a previous call to this delegate, to continue that conversation. Omit to start a new one."}}, "required": ["prompt"]}} ]}}
The result also carries
"ttlMs": 0and"cacheScope": "private". The endpoint reads the catalog live, as the caller, on everytools/list. Don't cache the listing or share it between users.Call a tool with
tools/call, naming the tool and its arguments from thetools/listoutput:curl -sS http://<mcp-host>:8765/mcp \ -u <username>:<password> \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "mcp-session-id: $SESSION_ID" \ -d '{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "add_order", "arguments": {"item": "gizmo", "amount": 4.20}}}'
The built-in
execute_sqltool is called the same way and needs no prior registration — its single argument issql:curl -sS http://<mcp-host>:8765/mcp \ -u <username>:<password> \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "mcp-session-id: $SESSION_ID" \ -d '{"jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": {"name": "execute_sql", "arguments": {"sql": "SELECT 41 + 1 AS answer"}}}'
The
data:line carries a result whosecontent[0].textis the JSON string[{"answer":42}]. Eachexecute_sqlcall runs as one statement in autocommit mode, or in a read-only transaction for a read-only role, soBEGIN/COMMITacross separate calls aren't supported. See Result encoding forexecute_sqlfor the full constraints.
What an agent can do once provisioned
After the handshake, an agent's calls behave according to its own grants, not any setting on the endpoint itself. See Configuring your agent role for how those grants are set up.
| Call | Behavior |
|---|---|
tools/list | Returns every tool in the catalog regardless of the caller's grants, plus, for a member of aidb_users, every AIDB agent. The listing shows what exists, not what the caller may run. |
tools/call on a catalog tool | Succeeds if the role holds the privileges the tool's SQL needs. Otherwise Postgres returns permission denied and no rows are affected. |
tools/call on an AIDB agent | Runs the agent through aidb.agent_converse() as the role. For the arguments and result, see Calling agents as tools. |
tools/call with a wrong password | isError with password authentication failed. |
Arguments are bound as query parameters, and arguments a tool didn't declare are rejected with unknown argument '<name>', so an agent can't ask a tool to run as someone else.
See Provisioning example for these behaviors demonstrated with two agents holding different grants.
Each call opens a fresh Postgres connection as the authenticated role. Postgres errors pass through unchanged: tools/call returns them as MCP isError results, tools/list as a JSON-RPC error. A wrong password surfaces as password authentication failed, a missing grant as permission denied, and an unknown tool as tool '<name>' not found in aidb.tools.
MCP clients can send a notifications/cancelled message to cancel one of their own in-flight requests, naming the id the client assigned to that request — the same id value it sent in the original tools/call, such as 3 in the add_order example above. The endpoint cancels the Postgres statement behind that request, including a statement running inside a catalog tool, and the session keeps serving afterward.
curl -sS http://<mcp-host>:8765/mcp \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "mcp-session-id: $SESSION_ID" \ -d '{"jsonrpc": "2.0", "method": "notifications/cancelled", "params": {"requestId": 3, "reason": "user aborted"}}'
Calling agents as tools
For a member of aidb_users, every AIDB agent is also an MCP tool under its own name, listed after the catalog tools. Calling one runs aidb.agent_converse() as the authenticated role, the same function you call from psql. The tool's description includes the agent's instructions and its plugins' descriptions, so the client and its model can read them. To control which agents appear and how name collisions resolve, see Granting access to AIDB agents. Each agent call holds one Postgres connection for its whole run. To cap how many agent calls run at once, see Limiting concurrent AIDB agent calls.
Calling an agent
Pass two arguments: prompt, a required string, and conversation_id, an optional string. The endpoint refuses any other argument and names it in the error.
curl -sS http://<mcp-host>:8765/mcp \ -u <username>:<password> \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "mcp-session-id: $SESSION_ID" \ -d '{"jsonrpc": "2.0", "id": 5, "method": "tools/call", "params": {"name": "db_helper", "arguments": {"prompt": "How many rows are in the orders table?"}}}'
Read the result from the data: line. content[0].text is a JSON object with the agent's answer and the conversation's id:
{"answer":"The orders table has 4,213 rows.","conversation_id":"8f14e45f-ceea-467e-9575-a3d1a2c1a123"}
Continuing a conversation
To continue the conversation, pass the conversation_id back on the next call:
curl -sS http://<mcp-host>:8765/mcp \ -u <username>:<password> \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "mcp-session-id: $SESSION_ID" \ -d '{"jsonrpc": "2.0", "id": 6, "method": "tools/call", "params": {"name": "db_helper", "arguments": {"prompt": "And how many are still pending?", "conversation_id": "8f14e45f-ceea-467e-9575-a3d1a2c1a123"}}}'
A read-only role gets "conversation_id": null, and so does any call on a read replica, because a read-only run keeps no history to resume. When the agent fails, the result is isError and carries the error from agent_converse's error column.
Setting timeouts for agent calls
An agent call runs under a longer limit than other calls, which keep the endpoint's 30-second limit:
- The connection's
statement_timeoutis the agent'stimeout_secondsplus 10 seconds, so the agent can report its own timeout first. timeout_secondsis capped at 900 (15 minutes). An agent with no timeout set uses the 300-second default.- If Postgres hasn't answered 10 seconds after
statement_timeout, the endpoint closes the connection and returns anisErrorresult. An agent call therefore lasts at most 920 seconds.
Errors from an agent call name the agent's timeout and the 15-minute ceiling, so you can tell when the ceiling cut a call off. Raise your MCP client's request timeout for long-running agents. The MCP TypeScript SDK, for example, gives up after 60 seconds by default.
Canceling an agent call
To cancel an agent call, send notifications/cancelled with the call's id, as shown for a catalog tool in What an agent can do once provisioned. The endpoint sends Postgres a cancel request and also closes the connection, because a tool inside the agent's run can catch the cancel request and keep going.
Result encoding for execute_sql
Each execute_sql call is one statement in autocommit mode. For a role whose default_transaction_read_only is on, the statement instead runs inside a BEGIN READ ONLY block that's never committed. To set up a read-only role, see Configuring read-only roles. Transactions can't span calls, so BEGIN and COMMIT in separate calls aren't supported. Results are capped at 10000 rows and 10 MiB serialized, and a query over either cap returns an error with no rows. DML without RETURNING returns {"rows_affected": N}, and row-returning statements return a JSON array of objects keyed by column name.
The built-in execute_sql tool renders each column from its binary value, so the output doesn't depend on session settings such as DateStyle, IntervalStyle, or bytea_output. Unsupported column types are rejected before any row is returned, so SELECT 1.00::money WHERE false fails the same way as SELECT 1.00::money. User-defined enums, arrays, ranges, and domains over supported types work.
| Postgres type | JSON rendering |
|---|---|
bool, int2, int4, float4, float8 | Native scalar. Non-finite floats are an error. |
int8 | Number when within ±(2^53 - 1), otherwise a decimal string. If any value in a column overflows, the whole column is rendered as strings. |
json, jsonb | The document itself. |
text, varchar, bpchar, name, enums | String. |
timestamptz | RFC 3339 in UTC with a Z suffix. infinity and -infinity as strings. |
timestamp, date, time, timetz | ISO 8601 string. Years outside 0000 to 9999 use the expanded form with a sign, such as +10000-01-01. Years before the Common Era carry a BC suffix. Dates beyond year 262142 are an error with a hint to cast to TEXT. |
interval | ISO 8601 duration such as P1Y2M3DT4H5M6S, signed per component. |
numeric | Full-precision decimal string, or NaN, Infinity, -Infinity. |
uuid, inet, cidr, macaddr | Postgres text form. |
bytea | Padded base64. |
| Arrays | JSON array, nested per dimension, with elements rendered per this table. |
| Ranges | Postgres literal such as [1,10), with bounds rendered per this table and quoted as range_out would. |
money, composite types, multiranges, PostGIS types | Error with a hint to cast the column to TEXT. |
money is unsupported because its text form depends on lc_monetary. A result with two columns of the same name, such as SELECT 1 AS id, 2 AS id, is an error. No column is dropped.
Serving remote clients
By default the endpoint listens on loopback over plain HTTP, which is fine when the MCP client runs on the database host. To serve clients on other hosts, set edb.endpoints_mcp_host (or the general edb.endpoints_host, which MCP uses when its own is empty) to a routable address in postgresql.conf and give the endpoint a certificate and key:
edb.endpoints_mcp_host = '0.0.0.0' edb.endpoints_mcp_tls_cert = '/etc/edb/endpoints/server.crt' edb.endpoints_mcp_tls_key = '/etc/edb/endpoints/server.key'
To serve MCP and the OTLP receiver with one address and one certificate, set the general edb.endpoints_host, edb.endpoints_tls_cert, and edb.endpoints_tls_key instead. The receiver never reads the edb.endpoints_mcp_* host or TLS settings. See Configuring the endpoint.
Then reload with SELECT pg_reload_conf();. The supervisor enforces these constraints:
edb.endpoints_mcp_tls_certandedb.endpoints_mcp_tls_keyare set together or not at all, and so areedb.endpoints_tls_certandedb.endpoints_tls_key. All are PEM files.- Plain HTTP is only served on loopback (
127.0.0.1,::1, orlocalhost). The supervisor refuses to listen on a non-loopback host without the TLS pair. - The supervisor checks that both files exist before starting the endpoint. A wrong path idles the supervisor with a warning in the Postgres log.
On a non-loopback MCP host, /mcp accepts any Host header, so remote clients can reach the endpoint by its public hostname. A loopback host keeps checking that the Host header is localhost, 127.0.0.1, or ::1, over HTTPS too, and answers any other name with 403. The check protects against DNS rebinding.
To renew a certificate:
Write the new certificate and key to the same paths.
Reload Postgres from your renewal hook. The endpoint doesn't watch the certificate files, so without a reload the old certificate keeps serving until it expires. Run
SELECT pg_reload_conf();orpg_ctl reload. The endpoint reads every listener's certificate and key again without restarting.
Open connections keep the old certificate, and new connections get the new one. If you change either path instead, the endpoint restarts. A reload that fails, such as one that runs before the key is written, logs a warning and keeps the previous pair serving.
The connection from the endpoint to Postgres uses the local Unix socket with sslmode=disable, because it never leaves the host (see How it works for the TCP fallback). Agent credentials are protected on the wire by TLS between the client and the endpoint, and Postgres authenticates them with whatever method pg_hba.conf names for that connection.
The three curl steps above — open a session, acknowledge the handshake, and call a tool — are the same for a remote client: only the URL changes. edb.endpoints_mcp_host (or edb.endpoints_host) is the address the endpoint binds to, not what the client connects to — the client uses the host name the certificate covers, over https, on the same port:
curl -sS -D - https://db.example.com:8765/mcp \ --cacert /path/to/ca.crt \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -d '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "example", "version": "0"}}}'
--cacert is only needed when the certificate isn't from a CA the client already trusts. The handshake still needs no credentials. Every tools/list and tools/call still carries the agent's Postgres username and password as HTTP Basic auth, which is why plain HTTP is refused off loopback in the first place — the password would otherwise cross the network in the clear.
To let MCP clients sign in through your identity provider instead of sending a password, see Authenticating with OAuth.