Skip to content

Commit ce46978

Browse files
committed
feat: Add Shopping Agent MCP Server
1 parent 831211f commit ce46978

File tree

10 files changed

+579
-1220
lines changed

10 files changed

+579
-1220
lines changed

mcp/shopping_agent/ARCHITECTURE.md

Lines changed: 224 additions & 210 deletions
Large diffs are not rendered by default.

mcp/shopping_agent/QUICKSTART.md

Lines changed: 135 additions & 101 deletions
Large diffs are not rendered by default.

mcp/shopping_agent/README.md

Lines changed: 136 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
11
# Shopping Agent MCP Tool
22

3-
A Model Context Protocol (MCP) server that provides intelligent shopping recommendations using LangChain, LangGraph, OpenAI, and SerpAPI.
3+
A Model Context Protocol (MCP) server that provides product search capabilities using SerpAPI. The server returns structured product data, and the calling agent provides intelligent reasoning and recommendations.
44

55
## Features
66

7-
- **AI-Powered Recommendations**: Uses OpenAI's GPT-4 to understand natural language queries and generate personalized product recommendations
8-
- **Real-time Search**: Leverages SerpAPI to search across multiple retailers for the best products
9-
- **LangGraph Agent**: Implements a sophisticated multi-step agent workflow with:
10-
- Query parsing and understanding
11-
- Product search across retailers
12-
- Recommendation generation with reasoning
13-
- **Configurable Results**: Limit recommendations (default 5, max 20) based on your needs
7+
- **Product Search**: Leverages SerpAPI (Google Shopping) to search across multiple retailers
8+
- **Structured Data**: Returns rich product info including titles, prices, descriptions, thumbnails, ratings, and reviews
9+
- **Agent-Driven Analysis**: The MCP server returns raw data; your AI agent analyzes and provides reasoning
10+
- **Budget Awareness**: Product data includes prices for the agent to consider constraints
11+
- **Configurable Results**: Limit search results (default 10, max 20) based on your needs
1412

1513
## Tools
1614

1715
### 1. `recommend_products`
1816

19-
Recommends products based on a natural language query with budget and preferences.
17+
Searches for products based on a natural language query and returns structured product data. The calling agent should analyze this data and provide intelligent reasoning.
2018

2119
**Parameters:**
2220
- `query` (string, required): Natural language product request (e.g., "I want to buy a scarf for 40 dollars")
23-
- `maxResults` (integer, optional): Maximum number of recommendations (default: 5, max: 20)
21+
- `maxResults` (integer, optional): Maximum number of results (default: 10, max: 20)
2422

2523
**Returns:**
2624
```json
2725
{
2826
"query": "I want to buy a scarf for 40 dollars",
29-
"recommendations": [
27+
"products": [
3028
{
3129
"name": "Cashmere Blend Scarf",
3230
"price": "$35.99",
33-
"description": "Soft and warm cashmere blend scarf in multiple colors",
31+
"description": "Soft and warm cashmere blend scarf in multiple colors...",
3432
"url": "https://example.com/product",
35-
"reason": "High quality within budget with excellent reviews"
33+
"thumbnail": "https://example.com/image.jpg",
34+
"source": "Amazon",
35+
"rating": 4.5,
36+
"reviews": 120
3637
}
3738
],
3839
"count": 5
3940
}
4041
```
4142

43+
**Features:**
44+
- Returns rich structured product data including thumbnails and ratings
45+
- The calling agent should analyze products and provide reasoning
46+
- Products include names, prices, descriptions, and purchase URLs
47+
- Only requires `SERPAPI_API_KEY` to function
48+
4249
### 2. `search_products`
4350

4451
Search for products across retailers (lower-level tool for raw search results).
@@ -56,13 +63,11 @@ Raw search results from SerpAPI.
5663

5764
- Python 3.10 or higher
5865
- [uv](https://docs.astral.sh/uv/) package manager
59-
- OpenAI API key
60-
- SerpAPI key
66+
- SerpAPI key (required for product search)
6167

6268
### Installation
6369

64-
1. **Get API Keys:**
65-
- OpenAI API key: https://platform.openai.com/api-keys
70+
1. **Get API Key:**
6671
- SerpAPI key: https://serpapi.com/manage-api-key
6772

6873
2. **Install Dependencies:**
@@ -74,16 +79,15 @@ uv pip install -e .
7479

7580
### Configuration
7681

77-
Set the required environment variables:
82+
Set the required environment variable:
7883

7984
```bash
80-
export OPENAI_API_KEY="your-openai-api-key"
8185
export SERPAPI_API_KEY="your-serpapi-key"
8286
```
8387

8488
Optional configuration:
8589
```bash
86-
export HOST="0.0.0.0" # Server host (default: 0.0.0.0)
90+
export HOST="0.0.0.0" # Server host (default: 0.0.0.0)
8791
export PORT="8000" # Server port (default: 8000)
8892
export MCP_TRANSPORT="http" # Transport type (default: http, Inspector-ready)
8993
export MCP_JSON_RESPONSE="1" # Force JSON responses (default: enabled)
@@ -96,12 +100,11 @@ export LOG_LEVEL="INFO" # Logging level (default: INFO)
96100

97101
```bash
98102
cd mcp/shopping_agent
99-
export OPENAI_API_KEY="your-key"
100-
export SERPAPI_API_KEY="your-key"
103+
export SERPAPI_API_KEY="your-serpapi-key"
101104
python shopping_agent.py
102105
```
103106

104-
The server will start on `http://0.0.0.0:8000` by default.
107+
The server will start on `http://0.0.0.0:8000` by default and will return structured product data for your agent to analyze.
105108

106109
### Command-line options
107110

@@ -122,7 +125,6 @@ Follow these steps to debug the shopping agent with the official MCP Inspector U
122125
1. Start the server on its own port using HTTP transport:
123126
```bash
124127
cd mcp/shopping_agent
125-
export OPENAI_API_KEY="your-key"
126128
export SERPAPI_API_KEY="your-key"
127129
MCP_TRANSPORT=http PORT=8001 python shopping_agent.py
128130
```
@@ -148,31 +150,47 @@ docker build -t shopping-agent-mcp .
148150

149151
# Run the container
150152
docker run -p 8000:8000 \
151-
-e OPENAI_API_KEY="your-openai-api-key" \
152153
-e SERPAPI_API_KEY="your-serpapi-key" \
153154
shopping-agent-mcp
154155
```
155156

156157
## Architecture
157158

158-
The shopping agent uses LangGraph to implement a multi-step workflow:
159+
The shopping agent MCP server provides product search data. The calling agent (your AI) provides reasoning and recommendations:
159160

160161
```
161-
User Query → Parse Query → Search Products → Generate Recommendations → Return Results
162+
User Query → [MCP Tool] Search Products → Return Structured Data → [Your Agent] Analyze & Recommend
162163
```
163164

164-
### LangGraph Workflow
165+
### Workflow
166+
167+
1. **MCP Server**: Receives query, searches SerpAPI, returns structured product data (no reasoning)
168+
2. **Your Agent** (e.g., Claude, GPT-4): Analyzes the product data and provides intelligent reasoning for recommendations
169+
170+
### Suggested Agent System Prompt
165171

166-
1. **Parse Query Node**: Uses OpenAI to extract product type and budget from natural language
167-
2. **Search Products Node**: Queries SerpAPI for relevant products across retailers
168-
3. **Generate Recommendations Node**: Uses OpenAI to analyze results and create personalized recommendations
172+
When using this MCP server, configure your agent with a system prompt like this:
173+
174+
```
175+
When users ask for product recommendations:
176+
1. Use the recommend_products tool to search for products
177+
2. Analyze each product considering:
178+
- How well it matches the user's requirements
179+
- Whether it fits within their budget
180+
- The value proposition (quality vs. price)
181+
- Any specific features mentioned in the query
182+
3. For each product, provide:
183+
- A brief explanation of why it's a good match
184+
- A recommendation score (1-10)
185+
- Any caveats or considerations
186+
4. Rank products by how well they match the user's needs
187+
5. Present the top 3-5 recommendations with your reasoning
188+
```
169189

170190
### Technologies Used
171191

172192
- **FastMCP**: MCP server framework
173-
- **LangChain**: LLM application framework
174-
- **LangGraph**: Agent workflow orchestration
175-
- **OpenAI GPT-4**: Natural language understanding and generation
193+
- **LangChain Community**: SerpAPI wrapper for product search
176194
- **SerpAPI**: Real-time product search across retailers
177195

178196
## Usage Examples
@@ -183,18 +201,39 @@ User Query → Parse Query → Search Products → Generate Recommendations →
183201
# Query
184202
"I want to buy a scarf for 40 dollars"
185203

186-
# Response
204+
# MCP Server Response (raw product data)
187205
{
188-
"recommendations": [
206+
"query": "I want to buy a scarf for 40 dollars",
207+
"products": [
189208
{
190209
"name": "Winter Wool Scarf",
191210
"price": "$38.99",
192-
"description": "100% merino wool, various colors",
193-
"reason": "High quality, within budget, great reviews"
211+
"description": "100% merino wool...",
212+
"url": "https://example.com/product",
213+
"thumbnail": "https://...",
214+
"source": "Retailer A"
194215
},
195-
// ... 4 more recommendations
196-
]
216+
{
217+
"name": "Cashmere Blend Scarf",
218+
"price": "$35.99",
219+
"description": "Soft cashmere blend...",
220+
"url": "https://example.com/product2",
221+
"thumbnail": "https://...",
222+
"source": "Retailer B"
223+
}
224+
],
225+
"count": 2
197226
}
227+
228+
# Your Agent should then analyze these products and provide reasoning:
229+
# "Here are my top recommendations:
230+
#
231+
# 1. Winter Wool Scarf ($38.99) - Score: 9/10
232+
# This is an excellent choice within your $40 budget. The 100% merino wool
233+
# provides superior warmth and quality, making it great value at this price point.
234+
#
235+
# 2. Cashmere Blend Scarf ($35.99) - Score: 8/10
236+
# A more affordable option that still offers luxury with its cashmere blend..."
198237
```
199238

200239
### Example 2: Specific Requirements
@@ -203,20 +242,54 @@ User Query → Parse Query → Search Products → Generate Recommendations →
203242
# Query
204243
"Find me wireless headphones under $100 with good noise cancellation"
205244

206-
# Response includes 5 curated recommendations with:
207-
# - Product names
208-
# - Prices
209-
# - Detailed descriptions
210-
# - Reasons for recommendation
211-
# - Purchase links
245+
# MCP Server Response (raw product data)
246+
{
247+
"query": "wireless headphones under $100 with good noise cancellation",
248+
"products": [
249+
{
250+
"name": "Sony WH-CH710N Wireless Headphones",
251+
"price": "$89.99",
252+
"description": "Active noise canceling...",
253+
"url": "https://example.com/sony-headphones",
254+
"rating": 4.4,
255+
"reviews": 8500
256+
}
257+
],
258+
"count": 1
259+
}
260+
261+
# Your Agent analyzes and recommends:
262+
# "I found the Sony WH-CH710N Wireless Headphones at $89.99 - this is an excellent
263+
# match for your requirements:
264+
# - ✅ Wireless Bluetooth connectivity
265+
# - ✅ Active noise cancellation feature
266+
# - ✅ Well under your $100 budget ($89.99)
267+
# - ✅ Bonus: 35-hour battery life
268+
#
269+
# Recommendation Score: 9/10
270+
# This product checks all your boxes and comes from Sony, a trusted brand in audio..."
212271
```
213272

214273
## Testing
215274

216-
You can test the MCP server tools using curl:
275+
### Using Python Test Script
276+
277+
Test the shopping agent MCP server:
217278

218279
```bash
219-
# Test recommend_products tool
280+
cd mcp/shopping_agent
281+
export SERPAPI_API_KEY="your-serpapi-key"
282+
python simple_test.py
283+
```
284+
285+
This will test the product search functionality and show the structured data returned by the server.
286+
287+
### Using curl
288+
289+
You can also test the MCP server tools using curl:
290+
291+
```bash
292+
# Test recommend_products tool (returns structured product data)
220293
curl -X POST http://localhost:8000/mcp/tools/recommend_products \
221294
-H "Content-Type: application/json" \
222295
-H "Accept: application/json, text/event-stream" \
@@ -230,9 +303,15 @@ curl -X POST http://localhost:8000/mcp/tools/recommend_products \
230303

231304
### API Key Issues
232305

233-
If you see "API key not configured" errors:
234-
1. Verify your API keys are set correctly
235-
2. Check that environment variables are exported in the same shell session
306+
**SerpAPI Key Issues:**
307+
If you see "SERPAPI_API_KEY not configured" errors:
308+
1. Get your key from https://serpapi.com/manage-api-key
309+
2. Export it: `export SERPAPI_API_KEY="your-key"`
310+
3. Restart the server
311+
312+
**General API Key Tips:**
313+
1. Verify your API key is set correctly
314+
2. Check that the environment variable is exported in the same shell session
236315
3. Restart the server after setting environment variables
237316

238317
### No Results Returned
@@ -255,11 +334,12 @@ If you encounter import errors:
255334

256335
```
257336
shopping_agent/
258-
├── shopping_agent.py # Main MCP server with LangGraph agent
259-
├── pyproject.toml # Dependencies and project metadata
260-
├── README.md # This file
261-
├── Dockerfile # Container configuration
262-
└── __init__.py # Package initialization
337+
├── shopping_agent.py # Main MCP server with SerpAPI integration
338+
├── simple_test.py # Test script for product search
339+
├── pyproject.toml # Dependencies and project metadata
340+
├── README.md # This file
341+
├── Dockerfile # Container configuration
342+
└── __init__.py # Package initialization
263343
```
264344

265345
### Contributing

0 commit comments

Comments
 (0)