getBestDeals.ts•3.21 kB
/**
* MCP Tool: Get Best Deals
* Find the best deals and discounts across all supermarkets
*/
import type { SuperPrecioApiClient } from '../client/superPrecioApi.js';
export const getBestDealsTool = {
name: 'get_best_deals',
description: `Find the best deals and discounts currently available in supermarkets.
This tool helps you discover:
- Products on sale
- Best price-to-value items
- Popular deals
- Discount opportunities
Great for budget shopping and finding savings across all stores.`,
inputSchema: {
type: 'object',
properties: {
category: {
type: 'string',
description: 'Product category or type to find deals for (e.g., "lácteos", "cereales", "bebidas")',
},
maxResults: {
type: 'number',
description: 'Number of deals per supermarket (default: 10)',
minimum: 1,
maximum: 30,
default: 10,
},
},
required: ['category'],
},
};
export async function executeGetBestDeals(
client: SuperPrecioApiClient,
args: { category: string; maxResults?: number }
) {
const { category, maxResults = 10 } = args;
// Search with TopSale sorting to get best deals
const response = await client.searchProducts({
search: category,
maxResults,
order: 'OrderByTopSaleDESC',
});
const deals: Array<{
supermarket: string;
logo: string;
product: string;
price: number;
discount?: number;
link: string;
image: string;
}> = [];
response.allData.forEach((marketProducts, idx) => {
const market = response.markets[idx];
if (marketProducts && marketProducts.length > 0) {
marketProducts.forEach((product) => {
const discount = product.listPrice
? ((product.listPrice - product.price) / product.listPrice) * 100
: undefined;
deals.push({
supermarket: market.name,
logo: market.logo,
product: product.desc,
price: product.price,
discount: discount ? parseFloat(discount.toFixed(1)) : undefined,
link: product.link,
image: product.img,
});
});
}
});
// Sort by discount percentage if available
const dealsWithDiscount = deals.filter((d) => d.discount !== undefined);
const dealsWithoutDiscount = deals.filter((d) => d.discount === undefined);
const sortedDeals = [
...dealsWithDiscount.sort((a, b) => (b.discount || 0) - (a.discount || 0)),
...dealsWithoutDiscount,
];
const summary = `
Best Deals in "${category}"
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Found ${deals.length} products across ${response.columns} supermarkets
${sortedDeals
.slice(0, 15)
.map(
(deal, i) =>
`${i + 1}. ${deal.product}
🏪 ${deal.supermarket}
💵 $${deal.price.toLocaleString('es-AR')}${deal.discount ? ` (${deal.discount}% OFF)` : ''}
🔗 ${deal.link}`
)
.join('\n\n')}
`;
return {
content: [
{
type: 'text',
text: summary,
},
{
type: 'text',
text: JSON.stringify({ category, totalDeals: deals.length, deals: sortedDeals }, null, 2),
},
],
};
}