comparePrice.ts•4.09 kB
/**
* MCP Tool: Compare Prices
* Compares prices for a product across all supermarkets and finds the best deal
*/
import type { SuperPrecioApiClient } from '../client/superPrecioApi.js';
import type { ProductComparison } from '../types/index.js';
export const comparePriceTool = {
name: 'compare_prices',
description: `Compare prices for a product across all supermarkets and find the best deal.
This tool analyzes prices and provides:
- Lowest price and which supermarket has it
- Highest price for comparison
- Average price across all stores
- Potential savings
- Price differences between stores
Perfect for making informed buying decisions and finding the best deals.`,
inputSchema: {
type: 'object',
properties: {
productName: {
type: 'string',
description: 'Product name to compare (e.g., "arroz tio pelon")',
},
maxResults: {
type: 'number',
description: 'Number of results to analyze per store (default: 5)',
minimum: 1,
maximum: 20,
default: 5,
},
},
required: ['productName'],
},
};
export async function executeComparePrice(
client: SuperPrecioApiClient,
args: { productName: string; maxResults?: number }
) {
const { productName, maxResults = 5 } = args;
const response = await client.searchProducts({
search: productName,
maxResults,
order: 'OrderByPriceASC', // Sort by price ascending to find best deals
});
// Collect all products with prices
const allProducts: Array<{
market: string;
logo: string;
price: number;
name: string;
link: string;
img: string;
}> = [];
response.allData.forEach((marketProducts, idx) => {
const market = response.markets[idx];
if (marketProducts && marketProducts.length > 0) {
marketProducts.forEach((product) => {
allProducts.push({
market: market.name,
logo: market.logo,
price: product.price,
name: product.desc,
link: product.link,
img: product.img,
});
});
}
});
if (allProducts.length === 0) {
return {
content: [
{
type: 'text',
text: `No products found for "${productName}". Try a different search term.`,
},
],
};
}
// Find lowest and highest prices
const sortedByPrice = [...allProducts].sort((a, b) => a.price - b.price);
const lowest = sortedByPrice[0];
const highest = sortedByPrice[sortedByPrice.length - 1];
// Calculate average
const averagePrice =
allProducts.reduce((sum, p) => sum + p.price, 0) / allProducts.length;
// Calculate savings
const savings = highest.price - lowest.price;
const savingsPercent = ((savings / highest.price) * 100).toFixed(1);
const comparison: ProductComparison = {
productName,
markets: allProducts.map((p) => ({
market: p.market,
logo: p.logo,
price: p.price,
link: p.link,
img: p.img,
})),
lowestPrice: {
market: lowest.market,
price: lowest.price,
savings: savings,
},
highestPrice: {
market: highest.market,
price: highest.price,
},
averagePrice: parseFloat(averagePrice.toFixed(2)),
};
const summary = `
Price Comparison for "${productName}"
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🏆 BEST DEAL: ${lowest.market}
$${lowest.price.toLocaleString('es-AR')}
💰 POTENTIAL SAVINGS: $${savings.toLocaleString('es-AR')} (${savingsPercent}%)
vs. highest price at ${highest.market}
📊 AVERAGE PRICE: $${averagePrice.toLocaleString('es-AR')}
📍 FOUND IN ${response.columns} SUPERMARKETS
Price Breakdown:
${sortedByPrice
.map(
(p, i) =>
`${i + 1}. ${p.market}: $${p.price.toLocaleString('es-AR')} ${i === 0 ? '🏆 BEST' : ''}`
)
.join('\n')}
`;
return {
content: [
{
type: 'text',
text: summary,
},
{
type: 'text',
text: JSON.stringify(comparison, null, 2),
},
],
};
}