Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

🐘 Postgres MCP AI Agent

Natural Language Business Intelligence Agent using PostgreSQL + MCP + OpenAI

An autonomous AI Data Analyst Agent that allows users to interact with a PostgreSQL database using natural language instead of SQL.

This system understands business questions, generates optimized SQL queries, executes them safely (read-only), and returns tabular results with intelligent analytical insights.

It simulates the behaviour of a real Business Intelligence Consultant powered by AI.


🚀 Project Vision

Modern businesses generate large amounts of operational data but decision-makers often lack SQL knowledge.

This project demonstrates how an AI Agent can bridge the gap between raw database data and executive-level insights through conversational intelligence.

The agent can:

  • Understand intent
  • Explore schema automatically
  • Generate safe SQL queries
  • Analyse results
  • Provide business explanations
  • Support conversational follow-ups

✨ Key Features

✅ Natural Language → SQL → Insights pipeline
✅ Conversational memory (context-aware responses)
✅ Automatic database schema understanding
✅ Alias-safe SQL generation
✅ Read-only secure execution layer
✅ Business-focused analytical explanations
✅ Tabular result rendering in chat UI
✅ Model Context Protocol (MCP) compatible
✅ Realistic AI automation demo dataset


🧠 Example Questions You Can Ask

  • How is my revenue trend?
  • Which city generates the highest order value?
  • Show pending followups scheduled today
  • Which customers are most active recently?
  • Are AI-booked orders performing better than human bookings?
  • What insights can you give about campaign effectiveness?
  • Explain more about that trend

The agent behaves like a data analyst, not just a query engine.


🏗️ System Architecture

User (Natural Language)
→ AI Reasoning Layer
→ SQL Generation
→ PostgreSQL Execution
→ Result Interpretation
→ Insightful Natural Language Response

This follows a modern Agentic BI Architecture Pattern.


🔐 Safety Design (Read-Only Agent)

The system is intentionally restricted to:

  • SELECT queries only
  • No INSERT / UPDATE / DELETE
  • No schema modification

This ensures:

✔ Database integrity
✔ Safe enterprise analytics
✔ Risk-free AI exploration


🗄️ Demo Database Setup

A realistic AI automation business dataset is included.

It simulates:

  • Customer lifecycle tracking
  • AI service bookings
  • Conversation sentiment analysis
  • Follow-up automation
  • AI task execution monitoring
  • Revenue insights

📁 Database Script Location

/database/ai_agent_demo_dataset.sql

▶️ Import Database

psql -U postgres -d ai_agent_demo
\i database/ai_agent_demo_dataset.sql

This will create tables:

  • customers
  • orders
  • conversations
  • ai_tasks
  • followups
  • ai_insights

with realistic sample data.


⚙️ Installation & Setup

1️⃣ Clone Repository

git clone <your-repository-url>
cd postgres-mcp

2️⃣ Install Dependencies

npm install

3️⃣ Configure Environment Variables

Create .env

PGHOST=localhost
PGPORT=5432
PGDATABASE=ai_agent_demo
PGUSER=postgres
PGPASSWORD=your_password
OPENAI_API_KEY=your_openai_key

4️⃣ Start Application

npm start

Open:

http://localhost:3000

🤖 MCP Mode (Agent Tool Server)

To expose database tools via Model Context Protocol:

node src/server.js --mcp

This enables integration with:

  • Claude Desktop
  • Agent frameworks
  • Autonomous workflows

📊 How the AI Agent Thinks

  1. Reads full database schema dynamically
  2. Maintains conversational memory
  3. Understands business intent
  4. Generates optimized SQL
  5. Executes safely
  6. Converts raw rows into executive insights

This makes the system behave like:

👉 AI Data Analyst
👉 BI Consultant
👉 Decision Support Assistant


🌟 Future Roadmap

  • 📈 Automatic chart generation
  • 🚨 KPI anomaly detection
  • 🔮 Predictive revenue forecasting
  • 🧠 Autonomous insight suggestions
  • 📊 Dashboard API layer
  • 🛠️ Multi-tool planning agent
  • ☁️ SaaS deployment architecture

👨‍💻 Author

Aditya Jadhav
AI Automation & Agent Systems Builder

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages