mcp-postgres-query-agent
Allows querying a PostgreSQL database using natural language, providing tools to list tables, describe schemas, and execute read-only SELECT queries.
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., "@mcp-postgres-query-agentWhich department has the highest average salary?"
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.
MCP PostgreSQL Query Agent
A read-only natural-language database agent built with FastMCP, LangChain, SQLAlchemy, and PostgreSQL. The MCP server exposes schema-discovery and SELECT tools, while the LangChain client decides which tools to call, generates SQL, and turns the result into a natural-language answer.
What it demonstrates
Serving database capabilities as MCP tools
Discovering tables, columns, and foreign-key relationships
Translating natural-language questions into SQL
Answering questions that require joins across related tables
Restricting the exposed query tool to a single read-only
SELECTConnecting LangChain agents to a streamable HTTP MCP server
Related MCP server: MCP PostgreSQL
Architecture
User question
│
▼
LangChain agent + OpenRouter model
│
▼
MCP client (streamable HTTP)
│
▼
FastMCP server
├── list_tables
├── describe_table
└── run_select
│
▼
PostgreSQLProject structure
.
├── hw_db.py # Shared SQLAlchemy connection setup
├── hw_db_seed.py # Demo schema and sample data
├── hw_db_server.py # FastMCP database server
├── hw_db_client.py # LangChain MCP client and agent
├── docker-compose.yml # Local PostgreSQL service
├── .env.example # Required environment variables
└── pyproject.tomlRequirements
Python 3.11+
Docker, or an existing PostgreSQL instance
An OpenRouter API key
Setup
Clone the repository and enter the project directory:
git clone https://github.com/YOUR_USERNAME/mcp-postgres-query-agent.git
cd mcp-postgres-query-agentCreate the environment file:
cp .env.example .envAdd your OpenRouter key to .env:
PG_DSN=postgresql+psycopg://postgres:postgres@localhost:5432/mcp_demo
OPENROUTER_API_KEY=your_openrouter_api_keyInstall the dependencies with uv:
uv syncOr with pip:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtRun locally
Start PostgreSQL:
docker compose up -dCreate and seed the demo tables:
python hw_db_seed.pyStart the MCP server in one terminal:
python hw_db_server.pyRun the client in another terminal:
python hw_db_client.pyYou can also pass a custom question:
python hw_db_client.py "Which department has the highest average salary?"MCP tools
Tool | Purpose |
| Lists available database tables |
| Returns columns and foreign keys for a selected table |
| Executes one read-only |
Demo schema
The seed script creates two related tables:
departments: department name and locationemployees: employee name, salary, country, and department reference
The default question requires the agent to inspect both schemas and join the tables before answering.
Safety notes
The MCP query tool rejects non-SELECT statements and multiple SQL statements. This is a useful application-level guard, but it is not a complete database security boundary. For real deployments, connect with a PostgreSQL role that has read-only permissions and access only to the intended schemas.
Possible extensions
Add a dedicated read-only PostgreSQL role
Validate generated SQL with a parser instead of prefix checks
Add query timeouts and row-level access controls
Support multiple databases through separate MCP servers
Add tracing and evaluation for generated SQL accuracy
License
This project is available under the MIT License.
This server cannot be deployed
Maintenance
Related MCP Connectors
- dataOAuthco.thinair
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
Query your org's data in natural language — read-only MCP access to SQL, NoSQL, files & warehouses.
Safe, read-only Postgres and MySQL access for AI agents. Audit log + column-level controls.
Related MCP Servers
- AlicenseAqualityBmaintenanceAn MCP server that gives AI agents direct read-only access to PostgreSQL databases, enabling natural language analytics through tools for schema exploration, querying, trend analysis, and data quality checks.115MIT
- AlicenseNot gradedqualityDmaintenanceA read-only MCP server for PostgreSQL that enables safe database introspection and querying via natural language.442 npmMIT
- AlicenseNot gradedqualityCmaintenanceA read-only MCP server for querying PostgreSQL databases. It exposes a tool that runs SELECT queries and returns results to the agent.33 npmMIT
- FlicenseNot gradedqualityCmaintenanceEnables LLM agents to explore a Postgres database and answer questions by running safe, read-only SQL queries through MCP tools.-