withdrawItem
Withdraw specified items from an open container in Minecraft. Define the item name and optional amount to manage inventory via the MCP server connection.
Instructions
Take items from an open container
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | No | Amount of items to withdraw | |
| itemName | Yes | Name of the item to withdraw |
Implementation Reference
- Handler function that locates matching items in the open container and withdraws the specified amount using container.withdraw(). Handles multiple slots if needed.async ({ itemName, amount }) => { if (!botState.isConnected || !botState.bot) { return createNotConnectedResponse() } try { // Check if a container is open if (!botState.currentContainer) { return createSuccessResponse( 'No container is currently open. Use openContainer first.' ) } // Find the item in the container const container = botState.currentContainer const matchingItems = container.slots .filter((item): item is NonNullable<typeof item> => Boolean( item !== null && item.name && item.name.toLowerCase() === itemName.toLowerCase() ) ) .map((item, slotIndex) => ({ item, slotIndex })) if (matchingItems.length === 0) { return createSuccessResponse( `Item "${itemName}" not found in the container.` ) } // Calculate how many items to withdraw let remainingAmount = amount let withdrawnAmount = 0 // Withdraw items from each matching slot until we have enough for (const { item, slotIndex } of matchingItems) { if (remainingAmount <= 0) break const amountFromThisSlot = Math.min(remainingAmount, item.count) // In mineflayer, withdraw takes itemType, metadata, count await container.withdraw(item.type, null, amountFromThisSlot) remainingAmount -= amountFromThisSlot withdrawnAmount += amountFromThisSlot } return createSuccessResponse( `Withdrew ${withdrawnAmount} x ${itemName} from the container.` ) } catch (error) { return createErrorResponse(error) }
- Input schema using Zod for the withdrawItem tool parameters: itemName (string) and optional amount (number, default 1).{ itemName: z.string().describe('Name of the item to withdraw'), amount: z .number() .optional() .default(1) .describe('Amount of items to withdraw'), },
- src/tools/containerInteraction.ts:93-158 (registration)Direct registration of the withdrawItem tool via server.tool() call within registerContainerInteractionTools().server.tool( 'withdrawItem', 'Take items from an open container', { itemName: z.string().describe('Name of the item to withdraw'), amount: z .number() .optional() .default(1) .describe('Amount of items to withdraw'), }, async ({ itemName, amount }) => { if (!botState.isConnected || !botState.bot) { return createNotConnectedResponse() } try { // Check if a container is open if (!botState.currentContainer) { return createSuccessResponse( 'No container is currently open. Use openContainer first.' ) } // Find the item in the container const container = botState.currentContainer const matchingItems = container.slots .filter((item): item is NonNullable<typeof item> => Boolean( item !== null && item.name && item.name.toLowerCase() === itemName.toLowerCase() ) ) .map((item, slotIndex) => ({ item, slotIndex })) if (matchingItems.length === 0) { return createSuccessResponse( `Item "${itemName}" not found in the container.` ) } // Calculate how many items to withdraw let remainingAmount = amount let withdrawnAmount = 0 // Withdraw items from each matching slot until we have enough for (const { item, slotIndex } of matchingItems) { if (remainingAmount <= 0) break const amountFromThisSlot = Math.min(remainingAmount, item.count) // In mineflayer, withdraw takes itemType, metadata, count await container.withdraw(item.type, null, amountFromThisSlot) remainingAmount -= amountFromThisSlot withdrawnAmount += amountFromThisSlot } return createSuccessResponse( `Withdrew ${withdrawnAmount} x ${itemName} from the container.` ) } catch (error) { return createErrorResponse(error) } } )
- src/tools/index.ts:38-38 (registration)Invocation of registerContainerInteractionTools() which registers the withdrawItem tool among others.registerContainerInteractionTools()