NeonDB MCP数据库助手

一个MCP服务器,让AI代理通过LangChain和Google Gemini以自然语言查询NeonDB PostgreSQL数据库,并提供只读安全保护与多轮对话记忆。

  • by Mortarion002
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 Mortarion002/mcp_database

使用说明

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

🚀 Installation

  1. Clone the repository:

    git clone <your-repo-url>
    cd database_mcp
    
  2. Create a virtual environment:

    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    
  4. Set up environment variables:

    Create a .env file in the project root:

    NEON_DATABASE_URL='your_neondb_connection_string'
    GOOGLE_API_KEY='your_google_ai_api_key'
    
  5. 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 users and tasks tables)
  • 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 SELECT statements 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.