Self-Improving SQL Agent
Allows executing SELECT-only SQL queries against a PostgreSQL database, enabling natural-language to SQL conversion, schema linking, and sandboxed query execution as part of a self-improving SQL agent.
Allows executing SELECT-only SQL queries against a SQLite database, enabling natural-language to SQL conversion, schema linking, and sandboxed query execution as part of a self-improving SQL agent.
Click on "Deploy 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., "@Self-Improving SQL AgentFind the top 5 genres by total track sales and show the SQL."
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.
Self-Improving SQL Agent
LangGraph agent that turns natural-language questions into SQL over a music-store schema, executes them in a SELECT-only sandbox, and stores lessons from failed queries in a persistent playbook.
Optional Mem0 (or a local store) keeps user preferences across sessions. The same execute path is exposed as an MCP tool.
Features
Schema linking and Text-to-SQL generation
SELECT / WITH-only execution with statement timeout
Retry with reflection on executor or guardrail errors
Playbook memory for cross-query improvement
Holdout execution-accuracy eval (modes A / B / C)
Streamlit UI for SQL, results, and playbook inspection
Related MCP server: readonly-postgres-mcp
Quickstart
python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env # set GROQ_API_KEY, GOOGLE_API_KEY, or USE_OLLAMA=1
make init
make test
make run # http://localhost:8501
make eval # writes evals/results.mdPostgreSQL (optional; requires Docker):
# DATABASE_URL=postgresql://sqlagent:sqlagent@127.0.0.1:5432/sqlagent
make up
make psqlArchitecture
question
→ schema linker
→ user memory (optional)
→ playbook retrieve
→ generate SQL
→ SELECT-only guardrail
→ sandbox execute
→ on error: reflect → update playbook → retry (max 3)
→ result tableMode | Behavior |
A | Single generation, no memory |
B | Retry on errors, no playbook |
C | Retry, playbook, and user memory |
Evaluation uses 30 learn questions (playbook may update) and 20 frozen holdout questions. Primary metric is execution accuracy: result rows match gold SQL, ignoring column names.
Stack
Python 3.12, LangGraph, PostgreSQL 16 / SQLite, Pydantic, Mem0 (optional), MCP, Streamlit, pytest, Docker Compose.
License
MIT
This server cannot be deployed
Maintenance
Related MCP Connectors
Hosted Google Ads MCP with OAuth, bounded reads, and prepare/confirm writes.
Read-only MCP server for ClassQuill, a tutoring-business-management platform.
Read-only MCP server for AIStatusDashboard status, incidents, metrics, and fallback recommendations.
Query your org's data in natural language — read-only MCP access to SQL, NoSQL, files & warehouses.
Related MCP Servers
- FlicenseAqualityCmaintenanceRead-only MySQL database access via MCP. Enables listing databases, tables, schemas, running SELECT queries, and EXPLAIN plans.6-
- AlicenseNot gradedqualityCmaintenanceReadonly PostgreSQL MCP server with SQL guardrails for analytical queries and schema introspection.21 npmMIT
- FlicenseNot gradedqualityCmaintenanceEnables running read-only SQL queries and exploring DuckDB databases through MCP tools like listing tables, describing schemas, and fetching paginated data.-

MCP DB Gatewayofficial
AlicenseNot gradedqualityBmaintenanceProvides governed, read-only PostgreSQL access for AI agents via MCP. Enforces schema/table allowlists, query limits, and audit events.MIT