Add Item to List
add_item_to_listAdd items to shopping lists while checking for duplicates to maintain organized and efficient list management.
Instructions
Add a freeform item to a shopping list. Checks for duplicates before adding.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| item_name | Yes | Name of the item to add | |
| list_name | Yes | Name of the list to add to | |
| quantity | No | Quantity string, e.g. "2 lbs" or "3" |
Implementation Reference
- src/tools/lists.ts:79-131 (handler)The 'add_item_to_list' tool is registered and implemented within 'src/tools/lists.ts'. The handler takes 'item_name', 'list_name', and optional 'quantity', validates the list, checks for existing items, and adds a new item if not found.
server.registerTool( 'add_item_to_list', { title: 'Add Item to List', description: 'Add a freeform item to a shopping list. Checks for duplicates before adding.', inputSchema: z.object({ item_name: z.string().describe('Name of the item to add'), list_name: z.string().describe('Name of the list to add to'), quantity: z.string().optional().describe('Quantity string, e.g. "2 lbs" or "3"'), }), }, async ({ item_name, list_name, quantity }) => { try { const client = AnyListClient.getInstance(); await client.getLists(); const list = client.getListByName(list_name); if (!list) { return { content: [{ type: 'text', text: `List not found: "${list_name}"` }], isError: true, }; } // Check for existing item const existing = list.getItemByName(item_name); if (existing) { if (existing.checked) { existing.checked = false; await existing.save(); return { content: [{ type: 'text', text: `"${item_name}" was already on "${list_name}" (checked off) — unchecked it` }], }; } return { content: [{ type: 'text', text: `"${item_name}" is already on "${list_name}"` }], }; } const item = client.createItem({ name: item_name, quantity }); await list.addItem(item); return { content: [{ type: 'text', text: `Added "${item_name}"${quantity ? ` (${quantity})` : ''} to "${list_name}"` }], }; } catch (error) { return { content: [{ type: 'text', text: `Error adding item: ${error instanceof Error ? error.message : String(error)}` }], isError: true, }; } }, );