使用说明
NeonDB MCP Server with LangChain Agent
A Model Context Protocol (MCP) server that enables AI agents to query and interact with a NeonDB PostgreSQL database. This project demonstrates how to build an intelligent database assistant using MCP, LangChain, and Google's Gemini AI.
🌟 Features
- MCP Server: Exposes database operations as tools via the Model Context Protocol
- AI-Powered Queries: Natural language database interaction using Google Gemini
- Read-Only Safety: Built-in security to prevent destructive operations
- Conversational Memory: Multi-turn conversations with context retention
- Interactive CLI: Terminal-based chat interface for database queries
🏗️ Architecture
┌─────────────────┐
│ User (CLI) │
└────────┬────────┘
│
v
┌─────────────────┐
│ LangChain │
│ React Agent │ ← Powered by Google Gemini
└────────┬────────┘
│
v
┌─────────────────┐
│ MCP Adapter │
└────────┬────────┘
│
v
┌─────────────────┐
│ MCP Server │ ← server.py
│ (FastMCP) │
└────────┬────────┘
│
v
┌─────────────────┐
│ NeonDB │
│ (PostgreSQL) │
└─────────────────┘
📋 Prerequisites
- Python 3.8+
- A NeonDB account (sign up here)
- Google AI API key (get one here)
🚀 Installation
-
Clone the repository:
git clone <your-repo-url> cd database_mcp -
Create a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate -
Install dependencies:
pip install -r requirements.txt -
Set up environment variables:
Create a
.envfile in the project root:NEON_DATABASE_URL='your_neondb_connection_string' GOOGLE_API_KEY='your_google_ai_api_key' -
Seed the database:
python seed_db.py
🎯 Usage
One-Shot Query (Test Mode)
Run a single query to test the setup:
python test.py
This will ask the agent to analyze your database schema and find the largest table.
Interactive Chat Mode
Start a conversational session with persistent memory:
python client.py
Example conversation:
You: What tables do I have?
Agent: You have two tables: 'users' and 'tasks'...
You: How many users are there?
Agent: There are 3 users in the database...
You: Show me all pending tasks
Agent: Here are the pending tasks: ...
Type quit, exit, or q to end the session.
📁 Project Structure
- server.py: MCP server with database tools (
get_database_schema,run_read_only_query) - client.py: Interactive CLI client with conversation memory
- test.py: One-shot testing script
- seed_db.py: Database initialization script (creates
usersandtaskstables) - requirements.txt: Python dependencies
🛠️ Available MCP Tools
The MCP server exposes two tools to the AI agent:
1. get_database_schema()
Returns the complete schema of all tables in the public schema, including column names and data types.
2. run_read_only_query(sql_query: str)
Executes a SELECT query against the database. Automatically blocks non-SELECT statements for security.
🔒 Security Features
- Read-only queries: Only
SELECTstatements are permitted - SQL injection protection: Uses parameterized queries via asyncpg
- Connection pooling: Efficient connection management
- Environment variables: Sensitive credentials stored in
.env(not committed to Git)
🧪 Sample Database Schema
The seed_db.py script creates:
users table:
user_id(SERIAL PRIMARY KEY)name(VARCHAR)email(VARCHAR, UNIQUE)created_at(TIMESTAMP)
tasks table:
task_id(SERIAL PRIMARY KEY)user_id(FOREIGN KEY → users)title(VARCHAR)status(VARCHAR)due_date(DATE)
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
📧 Support
For questions or issues, please open an issue on GitHub.
⚠️ Important: Never commit your .env file to version control. The .gitignore file is configured to prevent this.
