Let an n8n AI Agent Query Your SQL Database with MCP

· AI2SQL

Short answer: in n8n, attach an MCP Client Tool to an AI Agent node, set the endpoint to https://builder.ai2sql.io/api/mcp, choose HTTP Streamable as the transport and Bearer authentication with your AI2SQL API key. The agent can then read your schema and run read-only SQL on PostgreSQL, MySQL or SQL Server inside any workflow.

AI2SQL is a hosted MCP server for SQL databases. n8n authenticates with an API key you can revoke; your database credentials stay encrypted on the AI2SQL server.

What this is good for

  • Scheduled reports. Every Monday, ask the agent for last week’s signups and revenue and post the answer to Slack or email.
  • Answering questions from chat. A Slack or Telegram trigger passes a question to the agent, which queries the database and replies.
  • Checks inside a workflow. Before sending an email, look up the customer’s plan and last order.

The agent writes the SQL from your real schema, so you do not maintain queries in the workflow.

1. Create an API key

In the AI2SQL dashboard:

  1. Connect your database under Connections.
  2. Open API keys and create a key, for example n8n-weekly-report. Copy it; it starts with sql_.

API keys are part of the Pro and Team plans.

2. Add the MCP Client Tool

  1. Add an AI Agent node and give it a chat model.
  2. Under Tools, add MCP Client Tool.
  3. Set:
FieldValue
Endpointhttps://builder.ai2sql.io/api/mcp
Server TransportHTTP Streamable
AuthenticationBearer Auth
Tokenyour sql_… key
Tools to IncludeAll

If you prefer header authentication, use Header Auth with the name x-ai2sql-key and the key as the value. To point the agent at one database by default, add a second header, x-ai2sql-connection, with the connection id from the dashboard.

Field names can differ slightly between n8n versions; the endpoint, transport and token are what matter.

3. Tell the agent what to do

In the AI Agent’s system message:

You answer questions about our business data.
First call list_connections, then describe_schema for the database you need.
Write one SELECT query, run it with run_query, and answer in two or three sentences with the numbers.

4. Example: weekly report to Slack

  1. Schedule Trigger: every Monday at 08:00.
  2. AI Agent (with the MCP Client Tool above), prompt: How many new customers and how much revenue did we have last week, compared with the week before?
  3. Slack: post the agent’s output to #metrics.

Each run shows up in Agent activity in AI2SQL under the key’s name, with the exact SQL the agent ran.

Tools the agent gets

ToolWhat it does
list_connectionsLists the databases on your AI2SQL account
describe_schemaTables, columns and types for one connection
run_queryOne read-only SELECT / WITH, up to 1,000 rows

Safety

  • Read-only. Writes and DDL are blocked before they reach the database. See read-only database access.
  • One key per workflow. Name keys after the workflow so Agent activity shows which one ran what, and revoke a key without touching the others.
  • No database password in n8n. The workflow only holds the AI2SQL key.

Troubleshooting

  • 401 Unauthorized. Check the token has no extra spaces and that the key is still active under API keys.
  • The node cannot connect. Make sure the transport is HTTP Streamable, not SSE.
  • “Run queries on your database” refused. Running SQL on your own database needs Pro or Team.

Frequently asked questions

How do I connect n8n to an MCP server?

Add an AI Agent node, attach an MCP Client Tool as one of its tools, set the endpoint to the server's URL and choose HTTP Streamable as the transport. For AI2SQL the endpoint is https://builder.ai2sql.io/api/mcp with Bearer authentication using an AI2SQL API key.

Why use MCP instead of n8n's Postgres or MySQL node?

The database nodes run the SQL you write. With MCP the agent reads the schema and writes the query itself, while AI2SQL keeps it read-only, caps results and logs every call. It also keeps database credentials out of n8n.

Can the n8n agent write to my database?

No. run_query accepts one SELECT or WITH statement. INSERT, UPDATE, DELETE and DDL are blocked before they reach the database.

Which AI2SQL plan do I need for n8n?

API keys and running queries on your own database are part of the Pro and Team plans, which start with a 7-day trial.

My n8n MCP Client only offers SSE. What now?

AI2SQL serves MCP over HTTP Streamable. Update n8n to a version whose MCP Client Tool offers the HTTP Streamable transport.