Connect Gemini CLI to Your SQL Database with MCP
· AI2SQL
Short answer: run
gemini mcp add --transport http ai2sql https://builder.ai2sql.io/api/chatgpt-mcp
then start gemini, type /mcp auth ai2sql, and sign in with your AI2SQL account. Gemini CLI can now read your real schema and run read-only SQL on PostgreSQL, MySQL or SQL Server.
AI2SQL is a hosted MCP server for SQL databases. Your connection details stay encrypted on the AI2SQL server; Gemini only gets a sign-in token you can revoke.
Before you start
- Gemini CLI installed and signed in to Google.
- A database connected in AI2SQL (Connections → Add database).
1. Add the server
gemini mcp add --transport http ai2sql https://builder.ai2sql.io/api/chatgpt-mcp
By default this writes to the project’s .gemini/settings.json. Add -s user to use AI2SQL in every project:
gemini mcp add -s user --transport http ai2sql https://builder.ai2sql.io/api/chatgpt-mcp
The resulting entry looks like this, if you prefer to edit settings.json by hand:
{
"mcpServers": {
"ai2sql": {
"url": "https://builder.ai2sql.io/api/chatgpt-mcp",
"type": "http"
}
}
}
2. Sign in
Start Gemini CLI and run:
/mcp auth ai2sql
A browser tab opens on AI2SQL. Sign in, press Allow, and return to the terminal. Gemini CLI receives the token on a local localhost callback and stores it for you.
Check the tools with /mcp. You should see the ai2sql server with its tools listed.
3. Ask a question
> Which tables in my database hold orders, and what are the top 5 customers by revenue this month?
Gemini calls list_connections and describe_schema to learn your schema, writes a SELECT, and runs it with run_query. It asks before running a tool unless you have told it to trust the server.
Tools Gemini CLI gets
| Tool | What it does |
|---|---|
list_connections | Lists the databases you connected in AI2SQL |
describe_schema | Tables, columns and types for one connection |
run_query | One read-only SELECT / WITH, up to 1,000 rows |
generate_sql | Writes SQL from a plain-English request |
fix_sql, explain_sql, optimize_sql, convert_sql | Repair, explain, tune or translate a query |
Why not connect Gemini straight to the database?
A local database MCP server needs your connection string on disk and runs whatever SQL the model sends. Through AI2SQL:
- Writes are blocked before they reach the database (why that matters).
- Results are capped at 1,000 rows per call.
- Every call is logged in Agent activity, with the SQL and the result.
- Nothing secret is in
settings.json, so it is safe to commit a project config.
Troubleshooting
/mcpshows the server as disconnected. Run/mcp auth ai2sqlagain; the sign-in may have expired.- The browser did not open. Gemini CLI prints the sign-in URL; open it manually on the same machine.
- Queries on your database are refused. Running SQL on your own database needs Pro or Team.
Other clients
The same server works from VS Code, Cursor, Devin Desktop (Windsurf), Claude and Claude Code, n8n and your own agent.
Frequently asked questions
How do I add an MCP server to Gemini CLI?
Run gemini mcp add --transport http ai2sql https://builder.ai2sql.io/api/chatgpt-mcp, then start gemini and run /mcp auth ai2sql to sign in. Add -s user to make it available in every project.
Does Gemini CLI support OAuth for MCP servers?
Yes. /mcp auth <server> opens a browser sign-in and Gemini CLI receives the token on a local callback. With AI2SQL you sign in with your AI2SQL account; no API key goes into settings.json.
Can Gemini CLI run write queries on my database through AI2SQL?
No. run_query accepts one SELECT or WITH statement; INSERT, UPDATE, DELETE and DDL are blocked before they reach the database.
Which databases can Gemini CLI query through AI2SQL?
PostgreSQL, MySQL and SQL Server connections support live schema reads and queries.
Is it free?
Signing in, listing connections, reading schemas and a few SQL writing requests a day are free. Running queries on your own database needs Pro or Team, which start with a 7-day trial.