Retail Analytics Agent

Retail Analytics Agent

An MCP server that combines SQL and RAG tools into a reasoning agent for answering retail analytics questions using natural language.

Category
Visit Server

README

Retail Analytics Agent

A from-scratch MCP (Model Context Protocol) server combining SQL query tools and RAG (Retrieval-Augmented Generation) into a single ReAct agent. Built without LangChain, LlamaIndex, or any agentic framework — just Python, Flask, FAISS, and the OpenAI API.

What This Is

Most agentic AI tutorials either wrap everything in LangChain and hide what's actually happening, or demo a single tool (SQL or RAG) in isolation.

This project does neither. It builds a multi-tool MCP server where a reasoning agent decides in real time whether a question requires structured data retrieval (SQL), unstructured knowledge lookup (RAG), or both in sequence.

Example:

"What is the return rate for each customer segment? Use the correct metric definition."

The agent:

  1. Calls search_metrics — retrieves the Return Rate definition, learns cancelled orders must be excluded from both numerator and denominator
  2. Calls get_schema — discovers actual table and column names
  3. Calls run_sql with wrong case — gets zeros, self-corrects by checking distinct status values
  4. Calls run_sql again with correct values — returns accurate rates per segment

No framework orchestrated that. The agent reasoned through it.

Tools

Tool Type Description
get_schema SQL Returns all table names, column names, and data types
run_sql SQL Executes a SELECT query, returns rows as JSON
list_metrics RAG Returns all metric names and one-line descriptions
search_metrics RAG Semantic search over the metrics glossary PDF

The Metrics Glossary

The RAG knowledge base is a PDF containing precise business metric definitions with inclusion/exclusion rules. These are the distinctions a naive agent would get wrong without it:

  • Return Rate: cancelled orders excluded from both numerator and denominator
  • LTV Gross: returned orders included — this is a demand-side metric
  • LTV Net: returned orders netted to zero — this is the revenue-side metric
  • Category Affinity: returned items excluded — a return signals category rejection
  • Recent Purchase Activity: returned orders included — engagement, not revenue

Dataset

Synthetic Indian retail database:

  • 15 customers across 6 cities, segmented into Premium / Standard / Budget
  • 15 products across 8 categories with rupee-denominated pricing
  • 90 orders across 2024 with statuses: Completed / Returned / Pending
  • 222 line items with quantity and discount percentage

Seeded deterministically (random.seed(42)) — results are reproducible.

Quickstart

1. Clone and install

git clone https://github.com/sourabhsurana06/retail-analytics-agent cd retail-analytics-agent pip install -r requirements.txt

2. Set up environment

cp .env.example .env

Add your OPENAI_API_KEY

3. Build the database and vector index

python3 core/CreateDB.py python3 build_index.py

4. Start the MCP server

python3 server.py

5. Run the agent (second terminal)

python3 agent.py

Project Structure

retail-analytics-agent/ ├── core/ │ ├── init.py │ ├── database.py │ ├── CreateDB.py │ ├── sql_tools.py │ ├── rag_tools.py │ └── retail_analytics_metric_list.pdf ├── data/ (gitignored — generated files) ├── agent.py ├── server.py ├── build_index.py ├── requirements.txt ├── .env.example └── .gitignore

What This Is Not

  • Not production-ready (SQLite, no auth, single-threaded Flask dev server)
  • Not a framework demo — no LangChain, no LlamaIndex, no AutoGen
  • Not complete (no streaming, no async, no retry logic)

It is a learning system that shows exactly what is happening at each step.

License

MIT

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