Retail Analytics Agent
An MCP server that combines SQL and RAG tools into a reasoning agent for answering retail analytics questions using natural language.
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:
- Calls search_metrics — retrieves the Return Rate definition, learns cancelled orders must be excluded from both numerator and denominator
- Calls get_schema — discovers actual table and column names
- Calls run_sql with wrong case — gets zeros, self-corrects by checking distinct status values
- 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
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。