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.
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
- User input (natural language) is sent via the frontend or CLI.
- The MCP Host routes the input to the appropriate tool.
- OpenAITool generates SQL from the input using LLM reasoning.
- SQLTool executes the query on PostgreSQL, returning raw results.
- NaturalLanguageResponseTool translates results into readable form.
- 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
-
Clone the repository
git clone https://github.com/Ramneek82810/Agentic-AI-MCP-Query-Brain.git cd Agentic-AI-MCP-Query-Brain -
Install dependencies
pip install -r requirements.txt -
Run the FastAPI service
uvicorn main:app --reload -
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
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
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.
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.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
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.
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.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
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.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.