checkMultipleAllergens
Check if a product contains any of multiple specified allergens by providing its name or barcode. Instantly identify potential allergens like gluten, milk, eggs in food products.
Instructions
Check if a product contains any of multiple allergens at once
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| nameOrBarcode | Yes | Product name or barcode | |
| allergens | Yes | List of allergens to check (e.g., ["gluten", "milk", "eggs"]) |
Implementation Reference
- src/tools/helpers.ts:332-365 (handler)Core handler function `checkMultipleAllergens` that checks a product against multiple allergens. It inspects product allergens_tags, allergens_hierarchy, and traces_tags to determine if each allergen is found, in traces, or absent. Returns a MultiAllergenResult with per-allergen check results and a safeToConsume flag.
export function checkMultipleAllergens(product: any, allergens: string[]): MultiAllergenResult { const allergensTags = product.allergens_tags || []; const allergensHierarchy = product.allergens_hierarchy || []; const tracesTags = product.traces_tags || []; const allAllergenTags = [...new Set([...allergensTags, ...allergensHierarchy])]; const checkResults = allergens.map(allergen => { const allergenLower = allergen.toLowerCase(); const found = allAllergenTags.some(tag => tag.toLowerCase().includes(allergenLower)); const inTraces = tracesTags.some((tag: string) => tag.toLowerCase().includes(allergenLower)); return { allergen, found, inTraces }; }); const allAllergens = allAllergenTags.map((tag: string) => tag.replace('en:', '').replace(/-/g, ' ') ); const traces = tracesTags.map((tag: string) => tag.replace('en:', '').replace(/-/g, ' ') ); const safeToConsume = !checkResults.some(r => r.found || r.inTraces); return { barcode: product.barcode || product.code, productName: product.name || product.product_name || 'Unknown', checkResults, safeToConsume, allAllergens, traces }; } - src/tools/types.ts:65-76 (schema)The `MultiAllergenResult` type interface defining the shape of the output from checkMultipleAllergens, including barcode, productName, checkResults array (with allergen, found, inTraces), safeToConsume boolean, allAllergens, and traces.
export interface MultiAllergenResult { barcode: string; productName: string; checkResults: Array<{ allergen: string; found: boolean; inTraces: boolean; }>; safeToConsume: boolean; allAllergens: string[]; traces: string[]; } - src/tools/nutrition-tools.ts:23-26 (schema)Input schema definition for the checkMultipleAllergens tool using Zod validation: requires nameOrBarcode (string) and allergens (array of strings).
const multiAllergenSchema = { nameOrBarcode: z.string().describe('Product name or barcode'), allergens: z.array(z.string()).describe('List of allergens to check (e.g., ["gluten", "milk", "eggs"])') }; - src/tools/nutrition-tools.ts:107-136 (registration)Registration of the 'checkMultipleAllergens' tool on the MCP server, including description, input schema, and the async handler that looks up the product, calls the handler function, and formats the response message.
server.registerTool('checkMultipleAllergens', { description: 'Check if a product contains any of multiple allergens at once', inputSchema: multiAllergenSchema }, async ({ nameOrBarcode, allergens }) => { try { const product = await findProduct(nameOrBarcode); if (!product) { return { content: [{ type: 'text' as const, text: `Product "${nameOrBarcode}" not found.` }], isError: true }; } const result = checkMultipleAllergens(product, allergens); let message = result.safeToConsume ? `Product appears safe - none of the checked allergens were found.` : `WARNING: Some allergens were detected!`; result.checkResults.forEach(r => { const status = r.found ? 'FOUND' : (r.inTraces ? 'TRACES' : 'NOT FOUND'); message += `\n • ${r.allergen}: ${status}`; }); return { content: [{ type: 'text' as const, text: `${message}\n\n${JSON.stringify(result, null, 2)}` }] }; } catch (error: any) { return { content: [{ type: 'text' as const, text: `Error: ${error.message}` }], isError: true }; } }); - src/tools/nutrition-tools.ts:9-12 (helper)Import of the checkMultipleAllergens function from helpers.js into the nutrition tools module.
checkMultipleAllergens } from './helpers.js'; import { logger } from '../transport/transports.js';