searchByBrand
Retrieve all products from a specific brand. Provide the brand name and optionally set page and page size for paginated results.
Instructions
Find all products from a specific brand
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| brand | Yes | Brand name to search for | |
| page | No | ||
| pageSize | No |
Implementation Reference
- src/tools/helpers.ts:99-117 (handler)The core handler function `searchByBrand` that executes the logic: takes a brand name, constructs a URL using Open Food Facts facets API (`/brand/{slug}/{page}.json`), fetches results, maps them via `mapToSearchProduct`, and returns a `SearchResult`.
export async function searchByBrand(brand: string, page: number, pageSize: number): Promise<SearchResult> { const brandSlug = brand.toLowerCase().replace(/\s+/g, '-'); const url = `${BASE_URL}/brand/${brandSlug}/${page}.json`; const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to search by brand: ${response.status}`); } const data = await response.json(); return { products: (data.products || []).map(mapToSearchProduct), count: data.count || 0, page, pageSize, pageCount: Math.ceil((data.count || 0) / pageSize) }; } - src/tools/category-tools.ts:59-69 (registration)Registration of the `searchByBrand` tool on the MCP server via `server.registerTool('searchByBrand', ...)`. Defines the description and input schema (`brandSchema`), and the async handler that delegates to the `searchByBrand` helper function. This is where the tool is registered with the MCP server.
server.registerTool('searchByBrand', { description: 'Find all products from a specific brand', inputSchema: brandSchema }, async ({ brand, page, pageSize }) => { try { const results = await searchByBrand(brand, page ?? 1, pageSize ?? 10); return { content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }] }; } catch (error: any) { return { content: [{ type: 'text' as const, text: `Error: ${error.message}` }], isError: true }; } }); - src/tools/category-tools.ts:17-21 (schema)The `brandSchema` Zod schema defines the input validation for `searchByBrand`: a required `brand` string, optional `page` number (default 1), and optional `pageSize` number (default 10).
const brandSchema = { brand: z.string().describe('Brand name to search for'), page: z.number().default(1), pageSize: z.number().default(10) }; - src/tools/index.ts:174-183 (registration)The `registerCategoryTools(server)` call in the main `registerTools` function that causes `searchByBrand` (along with other category tools) to be registered on the server.
registerCategoryTools(server); registerNutritionTools(server); registerInsightsTools(server); registerPriceTools(server); logger.info("All OpenFoodFacts MCP tools registered successfully"); } - src/tools/helpers.ts:57-69 (helper)The `mapToSearchProduct` helper function used by `searchByBrand` to map raw API product data into the structured `SearchProductResult` interface.
export function mapToSearchProduct(p: any): SearchProductResult { return { id: p._id || p.code, name: p.product_name || 'Unknown', brand: p.brands || 'Unknown', barcode: p.code || '', imageUrl: p.image_url || '', nutriScore: p.nutriscore_grade || '', ecoScore: p.ecoscore_grade || '', novaGroup: p.nova_group || 0, categories: p.categories || '' }; }