searchByCode.ts•1.95 kB
/**
* MCP Tool: Search by Code
* Searches for products by EAN/barcode
*/
import type { SuperPrecioApiClient } from '../client/superPrecioApi.js';
export const searchByCodeTool = {
name: 'search_by_code',
description: `Search for a specific product by its EAN/barcode across all supermarkets.
This tool is perfect for:
- Finding exact product matches
- Scanning barcodes
- Price checking specific items
- Verifying product availability
The barcode/EAN search will find the exact same product across different stores,
making it ideal for precise price comparisons.`,
inputSchema: {
type: 'object',
properties: {
code: {
type: 'string',
description: 'Product EAN/barcode (numeric code, usually 13 digits)',
},
},
required: ['code'],
},
};
export async function executeSearchByCode(
client: SuperPrecioApiClient,
args: { code: string }
) {
const { code } = args;
const response = await client.searchByCode(code);
// Format response
const results = {
summary: {
barcode: code,
totalSupermarkets: response.columns,
foundIn: response.allData.length,
},
availability: response.allData.map((marketProducts, idx) => {
const market = response.markets[idx];
if (!marketProducts || marketProducts.length === 0) {
return {
supermarket: market ? market.name : 'Unknown',
available: false,
};
}
const product = marketProducts[0];
return {
supermarket: market ? market.name : 'Unknown',
logo: market ? market.logo : '',
available: true,
product: {
name: product.desc,
price: product.price,
image: product.img,
link: product.link,
code: product.code,
},
};
}),
};
return {
content: [
{
type: 'text',
text: JSON.stringify(results, null, 2),
},
],
};
}