find_part_in_sets
Locate LEGO sets containing a specific brick part number, with optional color filtering. Helps builders identify sources for particular pieces, displaying results by quantity.
Instructions
Find which LEGO sets contain a specific part, optionally in a specific colour. Use this when a builder wants to know where to source a particular brick. Results sorted by quantity (most pieces first).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| part_num | Yes | Part number to search for | |
| color | No | Optional colour filter (name or ID) | |
| limit | No | Max results (default 25, max 50) |
Implementation Reference
- src/tools/find-part-in-sets.ts:32-69 (handler)The handler function that executes the SQL query to find sets containing a specific part.
export function handler(db: Database.Database, params: FindPartInSetsParams): FindPartInSetsResult { const limit = params.limit ?? 25; const conditions: string[] = ['ip.part_num = ?']; const bindings: unknown[] = [params.part_num]; if (params.color) { const colorId = parseInt(params.color, 10); if (!isNaN(colorId)) { conditions.push('c.id = ?'); bindings.push(colorId); } else { conditions.push('LOWER(c.name) = LOWER(?)'); bindings.push(params.color); } } const sql = ` SELECT s.set_num, s.name as set_name, s.year, c.name as color_name, SUM(ip.quantity) as quantity FROM inventory_parts ip JOIN inventories inv ON ip.inventory_id = inv.id JOIN sets s ON inv.set_num = s.set_num JOIN colors c ON ip.color_id = c.id WHERE ${conditions.join(' AND ')} GROUP BY s.set_num, c.name ORDER BY quantity DESC LIMIT ? `; bindings.push(limit); const rows = db.prepare(sql).all(...bindings) as PartInSet[]; return { part_num: params.part_num, sets: rows, total: rows.length, }; } - src/tools/find-part-in-sets.ts:6-12 (schema)The input validation schema for the find_part_in_sets tool using Zod.
export const FindPartInSetsInput = z.object({ part_num: z.string().describe('Part number to search for'), color: z.string().optional().describe('Optional colour filter (name or ID)'), limit: z.number().min(1).max(50).optional().describe('Max results (default 25, max 50)'), }); export type FindPartInSetsParams = z.infer<typeof FindPartInSetsInput>; - src/server.ts:131-143 (registration)Registration of the find_part_in_sets tool in the MCP server instance.
server.tool( 'find_part_in_sets', 'Find which LEGO sets contain a specific part, optionally in a specific colour. Use this when a builder wants to know where to source a particular brick. Results sorted by quantity (most pieces first).', FindPartInSetsInput.shape, async (params) => { try { const result = findPartInSetsHandler(db, params); return { content: [{ type: 'text' as const, text: formatFindPartInSets(result) }] }; } catch (err) { return { content: [{ type: 'text' as const, text: `Error finding part in sets: ${err instanceof Error ? err.message : String(err)}` }], isError: true }; } }, );