SQL Stored Procedures As AI Tools
How to expose a database's stored procedures to AI clients as governed tools, from creating the login to an MCP client calling the tool.
The running example is a SQL Server database sales with two procedures: sales.GetOrders, which lists a customer's
orders, and sales.AddOrderNote, which adds a note to an order. Every step applies to PostgreSQL, MySQL/MariaDB and
Oracle alike; the differences are called out where they matter.
What this achieves, and when not to use it
AI clients see one tool per procedure, with a description and a typed input schema, and calling a tool runs the procedure in its own transaction. Use it when the logic already lives in procedures and you want it reachable without writing and operating an HTTP wrapper.
Use an ordinary HTTP API instead when one already exists in front of the database: it usually carries business rules the procedures alone do not.
Before you start, collect
| Value | Example | Where it comes from |
|---|---|---|
| Database engine and host | SQL Server, sql.example.net |
your DBA |
| Database name | sales |
your DBA |
| A login for the tools | apimapper_sales |
created below |
| An LLM model for drafting | the tenant's mapping-enrichment model | LLM Connections |
The host must be in the tenant's outbound allowlist (Outbound Allowlist), or discovery and every
call are refused with host_not_allowed.
1. Create a least-privilege login
The login's permissions are the only real boundary on what the tools can do: the modifies data and idempotent
classification you confirm later describes the tools to AI clients, it does not restrict them. Grant the login
EXECUTE on exactly the procedures to expose, and nothing else.
-- SQL Server
CREATE LOGIN apimapper_sales WITH PASSWORD = '<generated>';
CREATE USER apimapper_sales FOR LOGIN apimapper_sales;
GRANT EXECUTE ON OBJECT::sales.GetOrders TO apimapper_sales;
GRANT EXECUTE ON OBJECT::sales.AddOrderNote TO apimapper_sales;
On PostgreSQL grant EXECUTE ON FUNCTION/PROCEDURE, on MySQL EXECUTE ON PROCEDURE, on Oracle EXECUTE on the
procedure or package. Store the password in the Secret Store, allowed for target systems.
2. Create the database target system
Target Systems → New target system, type Database:
- Engine: SQL Server.
- Connection template: Host
sql.example.net, Databasesales, and the attributeEncrypt=True, which savesServer=sql.example.net;Database=sales;Encrypt=True. No user name or password. - Leave Require TLS on.
- Optionally lower the result limits (rows, value size, result sets) for this database.
On the target system's Auth profiles tab, add one with New auth profile and Database user name and
password: user name apimapper_sales, password secret from step 1. Then press Test connection, which opens the
Connection test section of the Overview tab; choose the profile as the Authentication profile and press
Run test: it logs in and runs a harmless statement, calling no procedure.
3. Discover the procedures
API Onboarding → Import, then SQL stored procedures as the contract family. Under Discover from, either:
- A live database connection: the engine, the same template, and a login that can see the procedures. Use
apimapper_salesitself to see exactly what the tools will be able to call. The login is used for this discovery only and is not saved. - A SQL script file: the engine and a
.sqlfile with the procedures'CREATEstatements. On MySQL and Oracle the script must name the schema (USE sales;orALTER SESSION SET CURRENT_SCHEMA = SALES;).
Procedures with a type the platform cannot carry are listed as blocked, naming the parameter or column and its
type. They cannot be imported; change the procedure (for example, pass geography as well-known text) or leave it out.
4. Draft, review and confirm
Select sales.GetOrders and sales.AddOrderNote and choose Draft with LLM. When the drafts arrive, review each procedure:
- Read and correct the summary and description, and every parameter and column description. These become the tool descriptions AI clients choose tools by.
GetOrdersshould be classified does not modify data (hence idempotent).AddOrderNotemodifies data; it is not idempotent if calling it twice adds two notes.- Confirm both classifications (Confirm classification).
5. Import
Import them as a new API definition named Sales procedures. Both operations appear on the API's Operations tab
with method EXEC.
6. Map, publish and bind
Create a mapping from the definition, enrich and publish it as usual, add its tools to a toolset, and bind both tools
to the sales target system and its profile. An MCP client of the toolset now lists GetOrders with
readOnlyHint = true and AddOrderNote with destructiveHint = true.
Verify
Call GetOrders from the MCP client with a known customer. The answer holds a result-set
array under its reviewed name, plus outputParameters, returnValue and rowsAffected. The call appears in the
audit log with method EXEC and the procedure's name.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
host_not_allowed at discovery or call |
the host is not allowlisted | step "Before you start" |
unavailable |
the database is unreachable, TLS is refused, or the login failed | check the template, Require TLS and the profile's secret |
permission_denied |
the login lacks EXECUTE, or a procedure confirmed read-only tried to write |
grant EXECUTE; correct the classification and re-import |
object_not_found |
the procedure was dropped or renamed since import | re-discover and import a new version |
downstream_result_row_limit_exceeded |
the call returned more rows than the limit | narrow the call's arguments, or raise the target system's limit within the runtime ceiling |
| Discovery lists nothing | the login can execute none of the procedures | grant EXECUTE, or discover with a login that can |
| Drafting is unavailable | the tenant has no model for mapping enrichment | configure one, or import without a draft |
Related
- SQL stored procedures as tools — the full reference: types, limits, errors, what a rolled-back call cannot undo
- Database target systems