AI SQL Agent MCP Server
Allows querying a SQLite database using natural language, with safety guardrails and schema grounding.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@AI SQL Agent MCP ServerWhat were total sales by product category last month?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
AI SQL Agent MCP Server
A production-hardened Model Context Protocol (MCP) server that lets any MCP-compatible AI client (Claude, LangGraph agents, etc.) ask natural-language questions about a SQL database — safely.
Why this exists
Most public "LLM writes SQL, we run it" examples stop at a single unguarded step: generate SQL, execute it, return rows. That's fine for a demo and dangerous in production — a single hallucinated DROP TABLE or an unbounded query is a real incident, not a hypothetical.
This project treats the SQL-generation step as untrusted input and puts a real safety pipeline around it.
Related MCP server: mcp-db-server
Architecture
MCP Client (Claude / LangGraph / MCP Inspector)
│
▼
FastMCP Server (this repo)
│
┌────┼─────────────────────────────┐
▼ ▼ ▼
Schema LLM SQL Generation Guardrail Layer
Reader (schema-grounded (read-only enforcement,
(real prompt, no guessing) forbidden-keyword scan,
tables) statement-stacking block,
│ row-limit injection)
└──────────────┬──────────────────┘
▼
Read-only SQLite connection
│
▼
Structured JSON result + trace logFeatures
Schema-grounded generation — the LLM only ever sees real table/column names pulled live from the database, never guesses.
Guardrail layer — blocks any non-
SELECTstatement, blocks statement-stacking (; DROP TABLE), strips comments before keyword scanning, and caps result size with an injectedLIMIT.Defense in depth — the guardrail check runs and the DB connection itself is opened read-only.
Optional bearer-token auth on the MCP tool.
Structured, correlated logging — every request gets a request ID, logged SQL, latency, and outcome (
success/rejected/error).41 automated tests covering guardrails, schema introspection, generation, and the full integration pipeline — including adversarial cases (SQL hidden in comments, oversized LIMITs, disguised keywords).
Health check endpoint (
/health) for uptime monitoring.
Why this is different from a typical FastMCP demo
FastMCP (Apache-2.0, PrefectHQ) is used here as the underlying server framework — it handles MCP protocol plumbing so this project can focus entirely on the parts that matter for a real data-access agent: safety, grounding, and observability. Everything in guardrails.py, schema_reader.py, sql_generator.py, and observability.py is original to this project.
Tech stack
Python 3.11+ · FastMCP (Apache-2.0) · Anthropic API · SQLite · pytest · Docker · GitHub Actions
Installation
git clone https://github.com/<your-username>/ai-sql-agent-mcp.git
cd ai-sql-agent-mcp
pip install -e ".[dev]"Environment variables
Variable | Required | Description |
| Yes | Your Anthropic API key, used for SQL generation |
| No | Path to the SQLite database (default: |
| No | If set, |
| No | Max rows returned per query (default: |
Usage
python -m ai_sql_agent_mcp.serverConnect with the MCP Inspector to try it interactively:
npx @modelcontextprotocol/inspector python -m ai_sql_agent_mcp.serverTesting
pytest -vAPI
Exposes two MCP tools:
ask_database(question: str, api_token: str | None) -> dict— the main tool.describe_schema() -> dict— returns the live database schema.
Deployment
See docs/DEPLOYMENT.md for the free-tier cloud deployment guide (Docker + Fly.io/Render) and live demo instructions.
Security
Read-only by construction at two independent layers (guardrail + DB connection mode).
No write, DDL, or multi-statement SQL can ever reach the database.
API token auth available; recommend enabling it for any public deployment.
No secrets are logged; only the generated SQL text and metadata are logged.
Limitations
Currently supports SQLite; Postgres/MySQL support would extend
SchemaReader.Guardrails are structural (keyword/statement based), not a full SQL parser — sufficient for this threat model but not a substitute for DB-level permissions in a high-stakes production environment.
Roadmap
Postgres/MySQL schema reader
Query result caching
Evaluation harness (NL→SQL accuracy benchmark)
Rate limiting
License
MIT — see LICENSE. Built on FastMCP (Apache-2.0, PrefectHQ).
Author
Built by [your name] as part of an AI/ML engineering portfolio.
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- Flicense-qualityCmaintenanceEnables AI assistants to query SQL databases safely with read-only access, allowing schema discovery and SELECT queries while blocking writes and DDL operations.
- Alicense-qualityBmaintenanceEnables LLM clients to query SQL databases via natural language with read-only, AST-validated, and capped queries, ensuring safety guarantees.2MIT
- Flicense-qualityBmaintenanceEnables AI tools to understand a database, inspect schema, and run safe SELECT queries with SQL guardrails, plus optional codebase reading.
- Alicense-qualityBmaintenanceConnects AI assistants to PostgreSQL databases with production-grade safety features including query validation, guarded writes, rate limiting, and audit logging.2MIT
Related MCP Connectors
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
The grounded data layer for any LLM: governed SQL, metrics, lineage and catalog over your data.
Ask your app anything — revenue, errors, read-cost, growth — and get rendered charts back.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/rajmyagentit-del/ai-sql-agent-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server