Skip to main content
Glama
Ramneek82810

Agentic-AI-MCP-Query-Brain

by Ramneek82810
README.md
# 🧠 Agentic-AI-MCP-Query-Brain

An intelligent, agentic system built with **Model Context Protocol (MCP)** that transforms natural language queries into SQL, executes them against a database, and returns human-friendly results. Powered by modular microservices, Redis memory, and PostgreSQL for robust, context-aware querying.

---
   
## πŸ“Œ Overview

This project enables you to ask questions in plain English and receive structured data answers. It does so using:

- A **modular MCP architecture** for agent-to-tool communication  
- **FastAPI microservices** hosting API endpoints   
- **Redis memory** for storing conversational context  
- **OpenAI / LLM integration** for generating SQL  
- **PostgreSQL backend** for executing queries  
- **Docker + NGINX** setup for production scalability  

---

## 🧠 Tech Stack

| Component             | Technology                          |
|-----------------------|--------------------------------------|
| Language              | Python 3.12                          |
| Web Framework         | FastAPI                              |
| AI / LLM Integration  | OpenAI (via LLM)                     |
| Memory Store          | Redis                                |
| Database              | PostgreSQL                           |
| Containerization      | Docker & Docker Compose              |
| Reverse Proxy / Load Balancer | NGINX                      |
| Communication         | JSON over standard I/O / HTTP        |

---

## πŸ“ Project Structure

```
Agentic-AI-MCP-Query-Brain/
β”œβ”€β”€ agent/                     # Core MCP agent logic
β”œβ”€β”€ api_client/                # Client side communication logic
β”œβ”€β”€ api_service/               # FastAPI based endpoints
β”œβ”€β”€ docker/                    # Dockerfiles & container setup
β”œβ”€β”€ memory/                    # Redis memory and context logic
β”œβ”€β”€ models/                    # Data models & schema definitions
β”œβ”€β”€ sdk/                       # MCP SDK & router utilities
β”œβ”€β”€ services/                  # Tool registry and helper services
β”œβ”€β”€ sql_tool/                  # SQL execution, explanation & validation
β”‚
β”œβ”€β”€ main.py                     # FastAPI entry point
β”œβ”€β”€ main_stdio.py               # MCP host via stdio runner
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ docker-compose.yml          # Multi-container orchestration
β”œβ”€β”€ nginx.conf                  # NGINX configuration
└── README.md                   # This documentation
```

---

## 🧩 Key Tools & Modules

- **OpenAITool** β€” Converts natural language queries to SQL  
- **SQLTool** β€” Executes SQL on PostgreSQL securely  
- **ExplainSQLTool** β€” Converts SQL into readable descriptions  
- **QueryCacheTool** β€” Caches commonly run queries  
- **FeedbackLoggingTool** β€” Logs user feedback for model tuning  
- **NaturalLanguageResponseTool** β€” Turns SQL results into textual responses  
- **RateLimiterTool** β€” Controls request throughput  
- **TableSchemaTool** β€” Retrieves schema metadata for better query accuracy  

---

## 🧠 How It Works

1. **User input** (natural language) is sent via the frontend or CLI.  
2. The **MCP Host** routes the input to the appropriate tool.  
3. **OpenAITool** generates SQL from the input using LLM reasoning.  
4. **SQLTool** executes the query on PostgreSQL, returning raw results.  
5. **NaturalLanguageResponseTool** translates results into readable form.  
6. **Redis memory** retains conversation context for follow-up queries.

---

## βš™οΈ Example Configuration Snippet (VS Code / MCP)

Use this example in your MCP setup (sensitive keys masked for security):

```json
{
  "mcpServers": {
    "vartopia-sql-agent": {
      "command": "D:/vartopia/.venv/Scripts/python.exe",
      "args": [
        "-u",
        "D:/vartopia/main_stdio.py"
      ],
      "env": {
        "OPENAI_API_KEY": "sk-proj-********-REDACTED",
        "DB_URL": "postgresql://mcp_postgres_user:********@render.com/mcp_postgres",
        "REDIS_URL": "redis://localhost:6379"
      },
      "transport": "stdio",
      "workingDirectory": "D:/vartopia"
    }
  }
}
```

---

## ▢️ Getting Started

### βœ… Prerequisites

- Python 3.12+  
- PostgreSQL database  
- Redis server  
- Docker & Docker Compose (optional, but recommended)

### πŸ›  Setup Steps

1. **Clone the repository**
   ```bash
   git clone https://github.com/Ramneek82810/Agentic-AI-MCP-Query-Brain.git
   cd Agentic-AI-MCP-Query-Brain
   ```

2. **Install dependencies**
   ```bash
   pip install -r requirements.txt
   ```

3. **Run the FastAPI service**
   ```bash
   uvicorn main:app --reload
   ```

4. **Or start with Docker (multi-container setup)**
   ```bash
   docker-compose up --build
   ```

---

## 🧠 Architecture Flow

```
User Input
   ↓
MCP Client β†’ MCP Host (FastAPI)
   ↓
Tool Router β†’ [OpenAITool ⇄ SQLTool ⇄ MemoryTool]
   ↓
Redis Memory ↔ PostgreSQL
   ↓
Formatted JSON or Natural Language Response
```

---

## 🧩 Example Use Case

**Input:**  
> β€œShow the top 5 sales by department for the last quarter.”

**Pipeline:**  
- OpenAITool β†’ Generates SQL  
- SQLTool β†’ Executes query  
- NaturalLanguageResponseTool β†’ Formats the results  

**Output:**  
> β€œHere are the top 5 departments by sales last quarter: Electronics, Home, Fashion, Sports, and Toys.”

---

## πŸ“ˆ Future Enhancements

- πŸ—„ Multi-database support (MySQL, MongoDB)  
- 🧠 Custom fine-tuned LLMs for SQL generation  
- πŸ›‘ Role-based authentication & access control  
- πŸ€– Multi-agent orchestration for complex workflows  

---

## πŸ“„ License

This project is licensed under the **MIT License** β€” free to use, modify, and distribute with attribution.