Agentic-AI-MCP-Query-Brain

Agentic-AI-MCP-Query-Brain

An MCP server that transforms natural language questions into SQL, executes queries on PostgreSQL, and returns human-friendly responses, with Redis memory for context.

Category
Visit Server

README

🧠 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):

{
  "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

    git clone https://github.com/Ramneek82810/Agentic-AI-MCP-Query-Brain.git
    cd Agentic-AI-MCP-Query-Brain
    
  2. Install dependencies

    pip install -r requirements.txt
    
  3. Run the FastAPI service

    uvicorn main:app --reload
    
  4. Or start with Docker (multi-container setup)

    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.

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured