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 "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., "@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: pgEdge Postgres MCP Server
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 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
- AlicenseAqualityAmaintenanceAn 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

pgEdge Postgres MCP Serverofficial
Alicense-qualityAmaintenanceEnables SQL queries against PostgreSQL databases through MCP-compatible clients and includes a natural language agent for forming SQL queries from natural language.214PostgreSQL- Alicense-qualityDmaintenanceA read-only MCP server for PostgreSQL that enables safe database introspection and querying via natural language.727MIT
- Flicense-qualityBmaintenanceEnables agents to run SQL queries against PostgreSQL databases through MCP with connection pooling, tenant isolation, and read-only guardrails. Supports introspection of schemas, tables, and columns.
Related MCP Connectors
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
GibsonAI MCP server: manage your databases with natural language
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/oguzhanguler1/mcp-postgres-query-agent'
If you have feedback or need assistance with the MCP directory API, please join our Discord server