Content
# MCP Tools + Ollama LLM Testing Notebook
## Overview
This Jupyter notebook demonstrates how to integrate **MCP (Model Context Protocol) tools** with **Ollama LLM** for building an intelligent personal shopping assistant.
## What This Notebook Does
The notebook tests and demonstrates the complete workflow:
1. **Load E-Commerce Dataset** - Explores product inventory with categories, prices, popularity
2. **Verify Ollama Connection** - Ensures LLM is available and accessible
3. **Define MCP Tools** - Registers `search_inventory` and `get_recommendations` tools
4. **Test Tools Directly** - Validates tool functionality with sample queries
5. **Call Ollama LLM** - Tests basic LLM capabilities
6. **LLM + Tool Integration** - Shows how LLM can use tool results for reasoning
7. **Tool Selection by LLM** - Demonstrates LLM deciding which tool to use based on user request
## Key Concepts
### MCP (Model Context Protocol)
- Standardized protocol for tools/resources that LLMs can access
- Allows LLMs to call external tools and use results in reasoning
- Enables structured tool registration and execution
### Ollama
- Local LLM runtime (no API calls needed)
- Supports various models (Mistral, Llama, etc.)
- Runs on `http://localhost:11434`
### MCP Tools Used
- **search_inventory**: Find products by category, price range, seasonality
- **get_recommendations**: Get occasion-based product suggestions
## Installation
### Prerequisites
- Python 3.8+
- Ollama installed and running
- Jupyter or JupyterLab
### Step 1: Install Ollama
#### macOS
```bash
# Download from official website
# https://ollama.ai
# Or use Homebrew
brew install ollama
# Start Ollama service
ollama serve
```
#### Linux
```bash
# Download and install
curl https://ollama.ai/install.sh | sh
# Start Ollama
ollama serve
```
#### Windows
```powershell
# Download from https://ollama.ai/download
# Or use Windows Package Manager
winget install Ollama.Ollama
# Start Ollama
ollama serve
```
### Step 2: Download a Model
Open a new terminal (keep `ollama serve` running) and pull a model:
```bash
# Pull Mistral (recommended, ~4GB)
ollama pull mistral
# Or pull other models:
# ollama pull llama2 # Larger, more capable
# ollama pull neural-chat # Optimized for chat
```
Verify installation:
```bash
ollama list
```
### Step 3: Install Python Dependencies
```bash
pip install -r requirements.txt
```
### Step 4: Launch Jupyter
```bash
jupyter notebook
# or
jupyter lab
```
Then open `test_mcp_llm.ipynb` and run the cells.
## Dataset
The notebook uses `diversified_ecommerce_dataset.csv` located at:
```
https://www.kaggle.com/datasets/malaiarasugraj/e-commerce-dataset
```
**Dataset Structure:**
- **Columns**: Product ID, Product Name, Category, Price, Popularity Index, Seasonality
- **Categories**: Apparel, Electronics, Home Appliances, Beauty, Sports_Outdoors, Toys_Games, Books_Media
- **Products**: 10,000+ items with various price ranges
## Usage
### Run Individual Tests
Each cell can be run independently to test specific functionality:
1. **Setup** (Cell 1-2) - Load data and verify Ollama
2. **Dataset Exploration** (Cell 3-4) - View statistics
3. **Tool Testing** (Cell 8-9) - Test search_inventory and get_recommendations
4. **LLM Integration** (Cell 10-14) - See LLM using tools
### Run Full Notebook
```bash
jupyter nbconvert --to notebook --execute test_mcp_llm.ipynb
```
## Expected Output
### Test 1: Search Inventory
```
🔧 TEST: search_inventory(category='Footwear', max_price=100)
Found 156 Footwear products.
Top 5:
• Hiking Shoes (P7188): $29.23 (Popularity: 100/100, Seasonality: Summer)
• Sandals (P2275): $45.12 (Popularity: 98/100, Seasonality: Summer)
...
```
### Test 2: Get Recommendations
```
🔧 TEST: get_recommendations(occasion='wedding', max_budget=200)
Top 5 recommendations for 'wedding' (budget: $200):
• Formal Dress (P1234): $189.99 (Category: Apparel, Popularity: 100/100)
• Heels (P5259): $38.45 (Category: Apparel, Popularity: 100/100)
...
```
### Test 3: LLM Reasoning
The LLM receives tool results and provides recommendations based on only those products.
## Architecture
```
User Request
↓
[Jupyter Notebook]
↓
LLM (Ollama)
↓
Tool Router (search_inventory / get_recommendations)
↓
Database (CSV)
↓
Results → LLM → Output
```
## Category Mapping
The notebook includes category normalization to handle various user inputs:
| User Input | Mapped Category |
|-----------|------------------|
| shoes, footwear | Apparel |
| gadgets, tech | Electronics |
| home, appliances, kitchen | Home Appliances |
| beauty, cosmetics | Beauty |
| sports, outdoor | Sports_Outdoors |
| toys, games | Toys_Games |
## Troubleshooting
### "Connection refused" Error
- **Issue**: Ollama not running
- **Solution**: Start Ollama with `ollama serve`
### "No module named 'pandas'" Error
- **Issue**: Dependencies not installed
- **Solution**: Run `pip install -r requirements.txt`
### "File not found" Error
- **Issue**: Dataset path incorrect
- **Solution**: Verify dataset exists at `/Users/nithiyashrig/Desktop/Geetha/Code/MCP_Datahour/diversified_ecommerce_dataset.csv`
### "No products found" Error
- **Issue**: Category name doesn't match dataset
- **Solution**: Check available categories with `df['Category'].unique()`
## Next Steps
1. **Deploy as Web Service** - Use Flask to expose tools as APIs
2. **Add More Tools** - Implement cart management, reviews, etc.
3. **Fine-tune LLM Prompts** - Improve recommendation quality
4. **Add Logging** - Track tool usage and performance
5. **Build UI** - Create web interface for interactive shopping
## Files Reference
- `test_mcp_llm.ipynb` - Main notebook (this file)
- `requirements.txt` - Python dependencies
- `README.md` - This documentation
## Related Projects
- **MCP_Datahour/mcp_llm/** - Web server implementation with Flask
- **personal_shopper_agent/** - Full production agent
## License
Educational use. Modify as needed for your projects.
## Contact & Questions
For issues or questions about this notebook, refer to the main project documentation.
Connection Info
You Might Also Like
No recommendations available