optimizeShoppingList.tsβ’3.71 kB
/**
* MCP Tool: Optimize Shopping List
* π₯ THE STAR FEATURE - Find the best supermarket for your entire shopping list
*/
import type { SuperPrecioApiClient } from '../client/superPrecioApi.js';
export const optimizeShoppingListTool = {
name: 'optimize_shopping_list',
description: `π₯ OPTIMIZE YOUR SHOPPING - Find the best supermarket for your entire shopping list!
This is the most powerful feature. It:
1. Searches for every product in your list across ALL supermarkets
2. Calculates the total cost at each supermarket
3. Shows you exactly how much you'll save by choosing the cheapest option
4. Provides a complete breakdown with prices per item
Perfect for:
- Maximizing your savings
- Planning where to shop
- Comparing complete shopping costs
- Making informed decisions
The tool searches ALL active supermarkets in Argentina and tells you
which one gives you the best total price for your entire basket.`,
inputSchema: {
type: 'object',
properties: {
listId: {
type: 'number',
description: 'ID of the shopping list to optimize',
},
},
required: ['listId'],
},
};
export async function executeOptimizeShoppingList(
client: SuperPrecioApiClient,
args: { listId: number }
) {
const response = await client.optimizeShoppingList(args.listId);
if (!response.success) {
return {
content: [
{
type: 'text',
text: `Failed to optimize shopping list: ${response.message || 'Unknown error'}`,
},
],
isError: true,
};
}
const { bestMarket, savings, allMarkets, listName, totalItems, marketsCompared } = response;
const summary = `
π OPTIMIZATION RESULTS FOR "${listName}"
βββββββββββββββββββββββββββββββββββββββββββ
π° BEST SUPERMARKET: ${bestMarket.name}
Total: $${bestMarket.total.toLocaleString('es-AR')}
Found: ${bestMarket.foundItems}/${totalItems} products
${bestMarket.missingItems > 0 ? `β οΈ Missing: ${bestMarket.missingItems} items` : 'β
All items found!'}
${savings.amount > 0 ? `
πΈ YOUR SAVINGS: $${savings.amount.toLocaleString('es-AR')} (${savings.percentage}%)
vs. shopping at ${savings.comparedTo}
` : ''}
π PRICE COMPARISON (${marketsCompared} supermarkets)
βββββββββββββββββββββββββββββββββββββββββββ
${allMarkets
.map((market: any, i: number) => {
const badge = i === 0 ? 'π BEST ' : i === allMarkets.length - 1 ? 'β WORST' : ' ';
return `${badge} ${i + 1}. ${market.name.padEnd(20)} $${market.total.toLocaleString('es-AR').padStart(10)} (${market.foundItems}/${totalItems} found)`;
})
.join('\n')}
π ITEMS AT ${bestMarket.name}:
βββββββββββββββββββββββββββββββββββββββββββ
${bestMarket.items
.map((item: any, i: number) => {
return `${i + 1}. ${item.productName}
Qty: ${item.quantity} x $${item.unitPrice.toLocaleString('es-AR')} = $${item.totalPrice.toLocaleString('es-AR')}`;
})
.join('\n\n')}
βββββββββββββββββββββββββββββββββββββββββββ
π― RECOMMENDATION: Shop at ${bestMarket.name}
π΅ TOTAL TO PAY: $${bestMarket.total.toLocaleString('es-AR')}
${savings.amount > 0 ? `π° YOU SAVE: $${savings.amount.toLocaleString('es-AR')}` : ''}
`;
return {
content: [
{
type: 'text',
text: summary,
},
{
type: 'text',
text: JSON.stringify(response, null, 2),
},
],
};
}