Skip to main content
Glama

add_to_cart

Add alcohol products to your Drizly shopping cart by specifying product ID, name, quantity, and price for purchase preparation.

Instructions

Add a product to the shopping cart

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
productIdYesProduct ID to add
nameYesProduct name
quantityYesQuantity to add (minimum 1)
priceYesPrice per unit
imageUrlNoProduct image URL (optional)

Implementation Reference

  • MCP tool handler for 'add_to_cart' - validates input using AddToCartSchema, creates a DrizlyCartItem object, calls the addToCart method, and returns a JSON response with success message and updated cart
    case "add_to_cart": {
      const params = AddToCartSchema.parse(args);
      const cartItem: DrizlyCartItem = {
        productId: params.productId,
        name: params.name,
        quantity: params.quantity,
        price: params.price,
        imageUrl: params.imageUrl,
      };
      const cart = drizly.addToCart(cartItem);
    
      return {
        content: [
          {
            type: "text",
            text: JSON.stringify(
              {
                message: `Added ${params.quantity}x ${params.name} to cart`,
                cart,
              },
              null,
              2
            ),
          },
        ],
      };
    }
  • Core business logic implementation - adds item to cart or increments quantity if already exists, recalculates cart totals, returns updated cart state
    addToCart(product: DrizlyCartItem): DrizlyCart {
      const existingIndex = this.cart.items.findIndex((i) => i.productId === product.productId);
    
      if (existingIndex >= 0) {
        this.cart.items[existingIndex].quantity += product.quantity;
      } else {
        this.cart.items.push({ ...product });
      }
    
      this.recalculateCart();
      return this.cart;
    }
  • Zod schema definition for validating add_to_cart tool input parameters - requires productId, name, quantity (min 1), and price; imageUrl is optional
    const AddToCartSchema = z.object({
      productId: z.string().describe("Product ID to add to cart"),
      name: z.string().describe("Product name"),
      quantity: z.number().int().min(1).describe("Quantity to add"),
      price: z.number().describe("Price per unit"),
      imageUrl: z.string().optional().describe("Product image URL"),
    });
  • TypeScript interface defining the structure of a cart item used by the addToCart implementation
    export interface DrizlyCartItem {
      productId: string;
      name: string;
      quantity: number;
      price: number;
      imageUrl?: string;
    }
  • src/index.ts:175-189 (registration)
    MCP tool registration - defines the tool name, description, and JSON Schema for input validation in the ListToolsRequestSchema handler
    {
      name: "add_to_cart",
      description: "Add a product to the shopping cart",
      inputSchema: {
        type: "object",
        properties: {
          productId: { type: "string", description: "Product ID to add" },
          name: { type: "string", description: "Product name" },
          quantity: { type: "number", description: "Quantity to add (minimum 1)" },
          price: { type: "number", description: "Price per unit" },
          imageUrl: { type: "string", description: "Product image URL (optional)" },
        },
        required: ["productId", "name", "quantity", "price"],
      },
    },

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/markswendsen-code/mcp-drizly'

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