SQL Query MCP Server
A FastMCP server that enables natural language querying of PostgreSQL databases through LLM integration, allowing users to generate SQL queries from plain English and visualize the results.
README Documentation
π MCP-Powered Streamlit Dashboard with Ollama + PostgreSQL
A modern data analysis tool that lets you:
- Query your PostgreSQL database with natural language
- Run SQL manually or generate it using LLMs (Ollama)
- Analyze data with charts, statistics, and export to CSV
π§ Prompt-to-SQL Flow
graph TD
A[π§ User types a data question] --> B[Streamlit sends prompt to Ollama API]
B --> C[Ollama generates SQL query as text]
C --> D[Streamlit extracts the SQL]
D --> E[Streamlit sends SQL to MCP server]
E --> F[MCP executes query on PostgreSQL]
F --> G[Results returned to Streamlit]
G --> H[π Results shown + Chart + CSV Export]
π¦ Architecture
- Streamlit β UI + charting
- MCP (FastMCP) β Tools/resources for SQL query and table listing
- PostgreSQL β Stores your company data
- Ollama β LLM that translates natural language prompts to SQL
π Features
β
Natural language β SQL
β
Charting (bar/line/time series)
β
CSV download
β
Statistical summary
β
Prompt explainability with raw output
β
Auto-detect date/time fields
β
LLM integration with llama3 (configurable)
π οΈ Getting Started
git clone <this-repo>
cd postgres-mcp-server
docker-compose up --build
- Access UI: http://localhost:8501
- MCP API: http://localhost:3333/mcp
βοΈ Env Configuration
MCP_API_URL=http://mcp-server:3333/mcp
OLLAMA_URL=http://ollama:11434/api/generate
π€ Prompt Example
βList departments with average salary > 50000β
π Translated to SQL:
SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000;
Why is this a good use case for MCP?
π MCP makes it dead simple to expose structured tools like SQL queries to LLMs. π― Agents can discover and call your tools without hardcoding logic. π¬ You get the best of both worlds β interpretability, flexibility, and control.
Whether you're building internal tools, research dashboards, or intelligent agents β this pattern is reusable, secure, and 100% local.
π License
MIT