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.
This server cannot be deployed
Maintenance
ActivityInactive
ResponsivenessNo issues