getAllergenCheck
Check if a food product contains a specific allergen by entering its name or barcode. Supports common allergens like gluten, milk, eggs, nuts, peanuts, soy, fish, and shellfish.
Instructions
Check if a product contains a specific allergen (gluten, milk, eggs, nuts, peanuts, soy, fish, shellfish, etc.)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| nameOrBarcode | Yes | Product name or barcode | |
| allergen | Yes | Allergen to check for (e.g., "gluten", "milk", "eggs", "nuts", "peanuts", "soy", "fish", "shellfish") |
Implementation Reference
- src/tools/helpers.ts:294-327 (handler)Core handler function that checks if a product contains a specific allergen by scanning allergens_tags, allergens_hierarchy, and traces_tags fields.
export function checkAllergen(product: any, allergen: string): AllergenResult { const allergensTags = product.allergens_tags || []; const allergensHierarchy = product.allergens_hierarchy || []; const tracesTags = product.traces_tags || []; const allergenLower = allergen.toLowerCase(); const allAllergenTags = [...new Set([...allergensTags, ...allergensHierarchy])]; const allergenFound = allAllergenTags.some(tag => tag.toLowerCase().includes(allergenLower) ); const inTraces = tracesTags.some((tag: string) => tag.toLowerCase().includes(allergenLower) ); const allAllergens = allAllergenTags.map((tag: string) => tag.replace('en:', '').replace(/-/g, ' ') ); const traces = tracesTags.map((tag: string) => tag.replace('en:', '').replace(/-/g, ' ') ); return { barcode: product.barcode || product.code, productName: product.name || product.product_name || 'Unknown', allergenFound: allergenFound || inTraces, allergenChecked: allergen, allAllergens, allergensTags: allAllergenTags, traces }; } - src/tools/types.ts:55-63 (schema)Type definition for the allergen check result returned by checkAllergen.
export interface AllergenResult { barcode: string; productName: string; allergenFound: boolean; allergenChecked: string; allAllergens: string[]; allergensTags: string[]; traces: string[]; } - src/tools/nutrition-tools.ts:77-105 (registration)Registration of the getAllergenCheck tool on the MCP server with its description, input schema, and handler callback that calls checkAllergen.
server.registerTool('getAllergenCheck', { description: 'Check if a product contains a specific allergen (gluten, milk, eggs, nuts, peanuts, soy, fish, shellfish, etc.)', inputSchema: allergenCheckSchema }, async ({ nameOrBarcode, allergen }) => { try { const product = await findProduct(nameOrBarcode); if (!product) { return { content: [{ type: 'text' as const, text: `Product "${nameOrBarcode}" not found.` }], isError: true }; } const result = checkAllergen(product, allergen); let message = result.allergenFound ? `WARNING: ${result.allergenChecked.toUpperCase()} found in this product!` : `${result.allergenChecked.toUpperCase()} not detected in this product.`; if (result.traces.length > 0) { message += `\n\nNote: May contain traces of: ${result.traces.join(', ')}`; } 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:18-21 (schema)Zod input schema for the getAllergenCheck tool specifying nameOrBarcode and allergen string parameters.
const allergenCheckSchema = { nameOrBarcode: z.string().describe('Product name or barcode'), allergen: z.string().describe('Allergen to check for (e.g., "gluten", "milk", "eggs", "nuts", "peanuts", "soy", "fish", "shellfish")') };