Intelligent PostgreSQL MCP Client
Enables natural language querying of PostgreSQL databases with intelligent SQL generation using LLMs.
README
<div align="center"> <img src="./images/MCP.png" alt="Intelligent PostgreSQL MCP Client" width="100%" style="max-width: 1200px; height: auto; margin: 40px 0;"> </div>
Intelligent PostgreSQL MCP Client
This project provides a powerful integration between a Model Context Protocol (MCP) server for PostgreSQL and Large Language Models (LLMs) using LangChain and Google Gemini. It enables natural language querying of PostgreSQL databases with intelligent SQL generation capabilities.
Overview
The Intelligent PostgreSQL MCP Client allows users to interact with PostgreSQL databases using natural language. Instead of writing complex SQL queries, users can ask questions in plain English, and the system will automatically generate and execute the appropriate SQL queries to retrieve the requested information.
Key advantages:
- Simplified Database Access: Query your databases using natural language
- Intelligent Query Generation: Automatically converts questions to optimized SQL
- Flexible Deployment Options: Use via command line or integrate with MCP-compatible IDEs
- Enhanced Productivity: Reduce time spent writing and debugging SQL queries
Getting Started
Clone and Access the Project
-
Clone the repository:
git clone https://github.com/anass1209/MCP_PostgreSQL.git -
Navigate to the project directory:
cd MCP_PostgreSQL
Install Dependencies
You can install dependencies using Poetry (recommended), pip, or UV:
Using Poetry:
poetry install
Using pip:
pip install -r requirements.txt
Using UV:
uv pip install -r requirements.txt
About MCP
MCP (Model Context Protocol) is a protocol that enables secure, controlled access to external resources for AI models. In this project, it provides a standardized way for LLMs to interact with PostgreSQL databases while maintaining security and proper access controls.
Benefits of using MCP:
- Security: Controlled access to database resources
- Standardization: Consistent interface for AI models to interact with databases
- Extensibility: Easy to add new capabilities and tools
- Interoperability: Works with various LLMs and database systems
Project Structure
This repository contains two main implementations:
1. With_CMD
Use this folder if you want to interact with the MCP server through command line or LCI:
client.py: Client application that connects to the MCP server using an LLMserver.py: MCP server implementationconfig.py: Configuration settings for database connections and API keysdb.py: Database connection and management utilitiessql_tools.py: SQL query tools and utilitiesprompts.py: LLM prompts for intelligent SQL generation.env: Environment variables for API keys and database configuration
2. Integret_with_IDE_MCP
Use this folder if you want to integrate with MCP-compatible IDEs like Claude Desktop:
intelligent_server.py: MCP server that exposes PostgreSQL functionalities with intelligent SQL generationintelligent_server.bat: Batch script to start the intelligent MCP serverconfig.py: Configuration settings for database connections and API keysdb.py: Database connection and management utilitiessql_tools.py: SQL query tools and utilitiesprompts.py: LLM prompts for intelligent SQL generation.env: Environment variables for API keys and database configuration
Common Files
pyproject.toml: Poetry dependency management filepoetry.lock: Lock file for Poetry dependencies
Usage
Prerequisites
-
Ensure PostgreSQL is running and accessible with the connection parameters defined in your configuration.
-
Set up your environment variables in the
.envfile:GOOGLE_API_KEY="your-gemini-api-key" PG_HOST="localhost" PG_PORT= PG_USER="" PG_PASSWORD="" PG_DEFAULT_DB=""
Option 1: Using Command Line Interface (With_CMD folder)
-
Navigate to the With_CMD folder:
cd With_CMD -
Run the client with a specific question:
python client.py "What tables are in the Candidat_DB database?"Or without arguments for interactive mode:
python client.pyThen enter your question when prompted.
Example of With_CMD Usage

The image above shows an example of querying a PostgreSQL database using the command line interface. The user asks a question in natural language, and the system generates and executes the appropriate SQL query to retrieve the requested information.
Option 2: Using MCP-Compatible IDE (Integret_with_IDE_MCP folder)
-
Navigate to the Integret_with_IDE_MCP folder:
cd Integret_with_IDE_MCP -
Configure your IDE to connect to the MCP server:
Add this JSON configuration to your IDE's MCP settings:
{ "mcpServers": { "intelligent-postgres": { "command": "Path to intelligent_server.bat", "args": [], "cwd": "Path to the MCP_Test folder", "env": { "PG_HOST": "localhost", "PG_PORT": "5432", "PG_USER": "Your database username", "PG_PASSWORD": "Your database password", "PG_DEFAULT_DB": "Your default database", "GOOGLE_API_KEY": "Your API key", "PYTHONIOENCODING": "utf-8", "PYTHONUNBUFFERED": "1" } } } } -
Update
intelligent_server.batif needed:- If you use UV, replace
poetry run python intelligent_server.pyin file .bat withuv run python intelligent_server.pyand use absolute path if necessary
- If you use UV, replace
-
Add this task to your IDE and restart to activate your new MCP "Intelligent-postgres"
The client will automatically connect to the MCP server, access PostgreSQL through this server, and use an LLM to answer questions about your databases.
Example of IDE MCP Integration

The image above demonstrates how to interact with PostgreSQL databases using natural language queries through an MCP-compatible IDE. The IDE provides a seamless interface for asking questions and receiving formatted results directly in your development environment.
Best Practices for Queries
-
Specify Database and Table: When you have multiple databases and tables, always specify the database and table in your query for more accurate results. For example:
Which employees are managers and have no manager themselves? use Candidates_DB and employees table -
Be Specific: Provide clear, specific questions that include all relevant information about the data you're looking for.
-
Include Context: When possible, include context about the data structure or relationships you're querying.
Technical Requirements
Dependencies
- Python 3.11+
- PostgreSQL database server
- Python packages:
- psycopg2-binary: PostgreSQL database adapter
- fastmcp: Fast MCP server implementation
- mcp: Model Context Protocol core library
- langchain-mcp-adapters: LangChain adapters for MCP
- langgraph: Graph-based workflow management
- langchain-google-genai: Google Gemini integration for LangChain
Key Features
- Intelligent SQL Generation: The MCP server includes intelligent tools that automatically generate optimized SQL queries based on natural language questions.
- Multi-step Database Analysis: Step-by-step database discovery, table selection, schema analysis, and query generation for comprehensive data exploration.
- Error Handling: Robust error handling with automatic query correction and retry mechanisms for reliable operation.
- LLM Integration: Seamless integration with Google Gemini for advanced natural language understanding.
- Dual Implementation: Choose between command-line interface or IDE integration based on your workflow preferences.
- Developer-Friendly: Well-structured codebase with clear separation of concerns and comprehensive documentation.
Contributing
- If you have any bug fixes or improvements, please submit a pull request or open an issue!
- Follow the existing code style and include appropriate tests for new features.
- For major changes, please open an issue first to discuss what you would like to change.
Getting API Keys
- Google Gemini API key: https://aistudio.google.com/apikey
License
This project is licensed under the MIT License - see the LICENSE file for details.
MCP Server Tools
The intelligent MCP server provides the following tools:
list_databases: List all available databaseslist_tables: List tables in a specific databasedescribe_table: Describe the schema of a tablerun_sql: Execute secure SELECT SQL queriessample_data: Display sample data from a tableStep-by-step analysis tools: (step1 through step9) for intelligent query generationdebug_connection: Debug tool for testing connections and functionality
Frequently Asked Questions (FAQ)
Which folder should I use?
- Use the With_CMD folder if you want to interact with the MCP server through command line or LCI.
- Use the Integret_with_IDE_MCP folder if you want to integrate with MCP-compatible IDEs like Claude Desktop.
How do I specify which database to query?
Include the database and table name in your question. For example:
Which employees are managers and have no manager themselves? use Candidates_DB and employees table
Can I use other LLMs besides Google Gemini?
The current implementation is optimized for Google Gemini, but the architecture is designed to be adaptable to other LLMs with minimal changes.
How secure is the database access?
The MCP protocol ensures that database access is controlled and limited to read-only SELECT queries by default, providing a secure layer between the LLM and your database. The system implements multiple security measures to prevent any destructive operations:
-
SQL Query Filtering: All SQL queries are filtered through a regular expression pattern that blocks potentially destructive operations such as:
ALTER, CREATE, DELETE, DROP, INSERT, UPDATE, TRUNCATE, GRANT, REVOKE -
Error Handling Process: When an error occurs during query execution, the system attempts to correct the query rather than allowing potentially harmful operations.
-
Read-Only Operations: The system is designed to only execute SELECT statements, ensuring that no data modification can occur.
-
Comprehensive Logging: All query attempts are logged for security auditing and debugging purposes.
These protective measures ensure that even if a language model generates an inappropriate query, it will be blocked before reaching your database.
What types of questions can I ask?
You can ask any question that can be answered with data from your PostgreSQL database. The more specific your question and the more context you provide (like database and table names), the better the results will be.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。