Skip to main content
Glama
domdomegg

openfoodfacts-mcp

get_product

Retrieve current product information from Open Food Facts using a barcode. Access up-to-date nutritional data, ingredients, and food details directly from the primary database.

Instructions

Get product information from Open Food Facts by barcode. Reads the primary database directly (no sync lag), so this is always current even when search_products returns stale results. Prefer this over search whenever you have a barcode. If this returns "product not found", the product genuinely isn't in the database — you can add it with add_or_edit_product.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
barcodeYesProduct barcode (EAN-13, UPC-A, etc.)
fieldsNoFields to return. Defaults to: product_name, brands, categories, nutriscore_grade, nova_group, ingredients_text, nutriments, serving_size, image_url, quantity, code
languageNoLanguage code for language-dependent fields (product_name, generic_name, ingredients_text). Defaults to "en". When a product has a different primary language, the unsuffixed field names return that language's data — this param ensures you get the language you want.en

Implementation Reference

  • The tool handler function for 'get_product' that fetches data from the Open Food Facts API and processes the response.
    async (args) => {
    	const fields = args.fields ?? DEFAULT_FIELDS;
    	const lang = args.language as string;
    
    	// For language-dependent fields, also request the lang-suffixed version
    	// so we can prefer the requested language regardless of the product's
    	// primary language.
    	const langFields: string[] = [];
    	for (const field of fields) {
    		if (LANGUAGE_DEPENDENT_FIELDS.includes(field)) {
    			langFields.push(`${field}_${lang}`);
    		}
    	}
    
    	const allFields = [...fields, ...langFields];
    	const params: Record<string, string> = {
    		fields: allFields.join(','),
    	};
    
    	const data = await offGet(config, `/api/v2/product/${args.barcode}.json`, params) as Record<string, unknown>;
    
    	// Prefer lang-suffixed values over unsuffixed (which map to the product's
    	// primary language and may not be what was requested).
    	const product = data.product as Record<string, unknown> | undefined;
    	if (product) {
    		for (const field of LANGUAGE_DEPENDENT_FIELDS) {
    			const langKey = `${field}_${lang}`;
    			if (product[langKey] !== undefined && product[langKey] !== null && product[langKey] !== '') {
    				product[field] = product[langKey];
    			}
    
    			// Remove the lang-suffixed field from output to keep response clean,
    			// unless the caller explicitly requested it.
    			if (!fields.includes(langKey)) {
    				// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
    				delete product[langKey];
    			}
    		}
    	}
    
    	// Restructure the flat nutriments blob into a readable nested format
    	if (product?.nutriments) {
    		product.nutriments = restructureNutriments(product.nutriments as Record<string, unknown>);
    	}
    
    	return jsonResult(data);
    },
  • The input schema validation and alias configuration for 'get_product'.
    const inputSchema = strictSchemaWithAliases(
    	{
    		barcode: z.string().describe('Product barcode (EAN-13, UPC-A, etc.)'),
    		fields: z.array(z.string()).optional().describe(`Fields to return. Defaults to: ${DEFAULT_FIELDS.join(', ')}`),
    		language: z.string().default('en').describe('Language code for language-dependent fields (product_name, generic_name, ingredients_text). Defaults to "en". When a product has a different primary language, the unsuffixed field names return that language\'s data — this param ensures you get the language you want.'),
    	},
    	{
    		code: 'barcode',
    		ean: 'barcode',
    	},
    );
  • Registration of the 'get_product' tool with the MCP server.
    export function registerGetProduct(server: McpServer, config: Config): void {
    	server.registerTool(
    		'get_product',
    		{
    			title: 'Get product',
    			description: 'Get product information from Open Food Facts by barcode. Reads the primary database directly (no sync lag), so this is always current even when search_products returns stale results. Prefer this over search whenever you have a barcode. If this returns "product not found", the product genuinely isn\'t in the database — you can add it with add_or_edit_product.',
    			inputSchema,
    			annotations: {
    				readOnlyHint: true,
    			},
    		},

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/domdomegg/openfoodfacts-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server