Skip to main content
Glama

add_to_cart

Add products to your Costco shopping cart using product URLs or item numbers, with quantity control for batch additions.

Instructions

Add a Costco product to the cart

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlNoCostco product URL
item_numberNoCostco item number
quantityNoQuantity to add (default: 1)

Implementation Reference

  • The handleAddToCart function implements the add_to_cart tool logic. It checks if the user is logged in, validates that either url or item_number is provided, navigates to the product page, adjusts quantity if needed, clicks the Add to Cart button, and returns confirmation with cart details.
    async function handleAddToCart(
      url?: string,
      itemNumber?: string,
      quantity = 1
    ) {
      if (!isLoggedIn()) {
        return err("Not logged in. Use the `login` tool first.");
      }
      if (!url && !itemNumber) {
        return err("Provide either url or item_number");
      }
    
      return withPage(async (page: Page) => {
        const productUrl = url ?? `https://www.costco.com/p.${itemNumber}.product.html`;
    
        await page.goto(productUrl, {
          waitUntil: "domcontentloaded",
          timeout: 30000,
        });
        await page.waitForTimeout(2000);
    
        // Adjust quantity if > 1
        if (quantity > 1) {
          try {
            const qtyInput = await page.$(
              '[automation-id="product-detail-quantity-input"], input[id*="qty"], input[name*="qty"], input[class*="qty"]'
            );
            if (qtyInput) {
              await qtyInput.fill(String(quantity));
            }
          } catch {
            // Ignore quantity adjustment errors
          }
        }
    
        // Click Add to Cart
        const addBtn = await page.waitForSelector(
          '[automation-id="add-to-cart-btn"], button[id*="add-to-cart"], button[class*="add-to-cart"], [class*="addToCart"]',
          { timeout: 10000 }
        );
    
        const btnText = await addBtn.textContent();
        if (
          btnText?.toLowerCase().includes("out of stock") ||
          btnText?.toLowerCase().includes("unavailable") ||
          btnText?.toLowerCase().includes("sold out")
        ) {
          return err("Item is out of stock or unavailable");
        }
    
        await addBtn.click();
        await page.waitForTimeout(3000);
    
        // Confirm added — look for cart confirmation modal or count change
        const confirmation = await page.$(
          '[automation-id="cart-count"], .cart-count, [class*="cartCount"]'
        );
        const count = confirmation ? await confirmation.textContent() : null;
    
        // Extract item number from URL
        const itemMatch = page.url().match(/\.(\d+)\.product/);
        const resolvedItemNumber = itemMatch ? itemMatch[1] : (itemNumber ?? "unknown");
    
        return ok(
          `Successfully added to cart.\n` +
          `Quantity: ${quantity}\n` +
          `Item #: ${resolvedItemNumber}\n` +
          `Cart count: ${count?.trim() ?? "updated"}\n` +
          `Product URL: ${productUrl}`
        );
      });
    }
  • Schema definition for the add_to_cart tool in the ListToolsRequestSchema handler. Defines input parameters: url (product URL), item_number (Costco item number), and quantity (default: 1).
      name: "add_to_cart",
      description: "Add a Costco product to the cart",
      inputSchema: {
        type: "object",
        properties: {
          url: { type: "string", description: "Costco product URL" },
          item_number: { type: "string", description: "Costco item number" },
          quantity: {
            type: "number",
            description: "Quantity to add (default: 1)",
          },
        },
      },
    },
  • src/index.ts:312-317 (registration)
    Registration of the add_to_cart tool in the CallToolRequestSchema handler switch statement. Routes tool calls to the handleAddToCart function with url, item_number, and quantity parameters.
    case "add_to_cart":
      return await handleAddToCart(
        a.url as string | undefined,
        a.item_number as string | undefined,
        (a.quantity as number | undefined) ?? 1
      );
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure but only states the basic action. It doesn't mention whether this requires authentication, affects existing cart items, has rate limits, or what happens on success/failure. For a mutation tool with zero annotation coverage, this is insufficient.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without any wasted words. It's appropriately sized and front-loaded, making it easy to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given this is a mutation tool with no annotations and no output schema, the description is incomplete. It doesn't explain behavioral aspects like authentication needs, error handling, or what the tool returns, leaving significant gaps for an AI agent to operate effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the input schema fully documents the three parameters (url, item_number, quantity). The description adds no additional parameter semantics beyond what's in the schema, but since the schema is comprehensive, the baseline score of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Add') and resource ('Costco product to the cart'), making the tool's purpose immediately understandable. However, it doesn't explicitly differentiate from sibling tools like 'update_cart' or 'view_cart', which would require more specific context about when to use each.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'update_cart' or prerequisites such as needing to be logged in. It lacks context about typical scenarios or exclusions, leaving the agent to infer usage from the tool name alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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-costco'

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