Skip to main content
Glama
amir-bengherbi

Shopify MCP Server

create-discount

Generate discount codes for Shopify stores by specifying title, code, value type, amount, and validity dates to apply promotions to customer orders.

Instructions

Create a basic discount code

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
titleYesTitle of the discount
codeYesDiscount code that customers will enter
valueTypeYesType of discount
valueYesDiscount value (percentage as decimal or fixed amount)
startsAtYesStart date in ISO format
endsAtNoOptional end date in ISO format
appliesOncePerCustomerYesWhether discount can be used only once per customer

Implementation Reference

  • src/index.ts:344-401 (registration)
    Registration of the MCP tool 'create-discount', including input schema with Zod and the handler function that constructs the input and delegates to ShopifyClient.createBasicDiscountCode.
    server.tool(
      "create-discount",
      "Create a basic discount code",
      {
        title: z.string().describe("Title of the discount"),
        code: z.string().describe("Discount code that customers will enter"),
        valueType: z
          .enum(["percentage", "fixed_amount"])
          .describe("Type of discount"),
        value: z
          .number()
          .describe("Discount value (percentage as decimal or fixed amount)"),
        startsAt: z.string().describe("Start date in ISO format"),
        endsAt: z.string().optional().describe("Optional end date in ISO format"),
        appliesOncePerCustomer: z
          .boolean()
          .describe("Whether discount can be used only once per customer"),
      },
      async ({
        title,
        code,
        valueType,
        value,
        startsAt,
        endsAt,
        appliesOncePerCustomer,
      }) => {
        const client = new ShopifyClient();
        try {
          const discountInput: CreateBasicDiscountCodeInput = {
            title,
            code,
            valueType,
            value,
            startsAt,
            endsAt,
            includeCollectionIds: [],
            excludeCollectionIds: [],
            appliesOncePerCustomer,
            combinesWith: {
              productDiscounts: true,
              orderDiscounts: true,
              shippingDiscounts: true,
            },
          };
          const discount = await client.createBasicDiscountCode(
            SHOPIFY_ACCESS_TOKEN,
            MYSHOPIFY_DOMAIN,
            discountInput
          );
          return {
            content: [{ type: "text", text: JSON.stringify(discount, null, 2) }],
          };
        } catch (error) {
          return handleError("Failed to create discount", error);
        }
      }
    );
  • Core handler in ShopifyClient that implements the discount creation logic: input validation, GraphQL mutation execution via shopifyGraphqlRequest, response processing with error handling.
    async createBasicDiscountCode(
      accessToken: string,
      shop: string,
      discountInput: CreateBasicDiscountCodeInput
    ): Promise<CreateBasicDiscountCodeResponse> {
      if (discountInput.valueType === "percentage") {
        if (discountInput.value < 0 || discountInput.value > 1) {
          throw new CustomError(
            "Invalid input: percentage value must be between 0 and 1",
            "InvalidInputError",
            {
              contextData: {
                discountInput,
                shop,
              },
            }
          );
        }
      }
    
      if (discountInput.valueType === "fixed_amount") {
        if (discountInput.value <= 0) {
          throw new CustomError(
            "Invalid input: fixed_amount value must be greater than 0",
            "InvalidInputError",
            {
              contextData: {
                discountInput,
                shop,
              },
            }
          );
        }
      }
    
      const myShopifyDomain = await this.getMyShopifyDomain(accessToken, shop);
    
      const isEligibleForSubscription = await this.checkSubscriptionEligibility(
        accessToken,
        myShopifyDomain
      );
    
      const graphqlQuery =
        this.graphqlQueryPreparationForCreateBasicDiscountCode();
    
      const variables = this.prepareBasicDiscountCodeVariable(
        discountInput,
        isEligibleForSubscription
      );
    
      const res = await this.shopifyGraphqlRequest<BasicDiscountCodeResponse>({
        url: `https://${myShopifyDomain}/admin/api/${this.SHOPIFY_API_VERSION}/graphql.json`,
        accessToken,
        query: graphqlQuery,
        variables,
      });
    
      const id = res.data.data.discountCodeBasicCreate.codeDiscountNode.id;
      const codeDiscount =
        res.data.data.discountCodeBasicCreate.codeDiscountNode.codeDiscount.codes
          .nodes[0];
      const userErrors = res.data.data.discountCodeBasicCreate.userErrors;
    
      if (userErrors.length > 0) {
        throw getGraphqlShopifyUserError(userErrors, {
          shop,
          discountInput,
        });
      }
    
      return {
        id,
        code: codeDiscount.code,
      };
    }
  • TypeScript type definition for the CreateBasicDiscountCodeInput used by the tool handler.
    export type CreateBasicDiscountCodeInput = {
      title: string;
      code: string;
      startsAt: ISODate;
      endsAt?: ISODate;
      valueType: string;
      value: number;
      usageLimit?: number;
      includeCollectionIds: string[];
      excludeCollectionIds: string[];
      appliesOncePerCustomer: boolean;
      combinesWith: {
        productDiscounts: boolean;
        orderDiscounts: boolean;
        shippingDiscounts: boolean;
      };
    };
  • Private helper method that returns the GraphQL mutation query string for discountCodeBasicCreate.
    private graphqlQueryPreparationForCreateBasicDiscountCode(): string {
      return gql`
        mutation discountCodeBasicCreate(
          $basicCodeDiscount: DiscountCodeBasicInput!
        ) {
          discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
            codeDiscountNode {
              id
              codeDiscount {
                ... on DiscountCodeBasic {
                  title
                  codes(first: 10) {
                    nodes {
                      code
                    }
                  }
                  startsAt
                  endsAt
                  customerSelection {
                    ... on DiscountCustomerAll {
                      allCustomers
                    }
                  }
                  customerGets {
                    appliesOnOneTimePurchase
                    appliesOnSubscription
                    value {
                      ... on DiscountPercentage {
                        percentage
                      }
                      ... on DiscountAmount {
                        amount {
                          amount
                          currencyCode
                        }
                        appliesOnEachItem
                      }
                    }
                    items {
                      ... on AllDiscountItems {
                        allItems
                      }
                    }
                  }
                  appliesOncePerCustomer
                }
              }
            }
            userErrors {
              field
              code
              message
            }
          }
        }
      `;
  • Private helper method that prepares the GraphQL variables object for the discount creation mutation.
    private prepareBasicDiscountCodeVariable(
      discountInput: CreateBasicDiscountCodeInput,
      isEligibleForSubscription: boolean
    ): any {
      return {
        basicCodeDiscount: {
          title: discountInput.title,
          code: discountInput.code,
          startsAt: discountInput.startsAt,
          endsAt: discountInput.endsAt,
          customerSelection: {
            all: true,
          },
          customerGets: {
            appliesOnOneTimePurchase: isEligibleForSubscription
              ? true
              : undefined,
            appliesOnSubscription: isEligibleForSubscription ? true : undefined,
            value: {
              percentage:
                discountInput.valueType === "percentage"
                  ? discountInput.value
                  : undefined,
              discountAmount:
                discountInput.valueType === "fixed_amount"
                  ? {
                      amount: discountInput.value,
                      appliesOnEachItem: false,
                    }
                  : undefined,
            },
            items: {
              all:
                discountInput.excludeCollectionIds.length === 0 &&
                discountInput.includeCollectionIds.length === 0,
              collections:
                discountInput.includeCollectionIds.length ||
                discountInput.excludeCollectionIds.length
                  ? {
                      add: discountInput.includeCollectionIds.map(
                        (id) => `gid://shopify/Collection/${id}`
                      ),
                      remove: discountInput.excludeCollectionIds.map(
                        (id) => `gid://shopify/Collection/${id}`
                      ),
                    }
                  : undefined,
            },
          },
          appliesOncePerCustomer: discountInput.appliesOncePerCustomer,
          recurringCycleLimit: isEligibleForSubscription
            ? discountInput.valueType === "fixed_amount"
              ? 1
              : null
            : undefined,
          usageLimit: discountInput.usageLimit,
          combinesWith: {
            productDiscounts: discountInput.combinesWith.productDiscounts,
            orderDiscounts: discountInput.combinesWith.orderDiscounts,
            shippingDiscounts: discountInput.combinesWith.shippingDiscounts,
          },
        },
      };
    }
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. It states 'Create' which implies a write/mutation operation, but doesn't mention permissions required, whether the discount is immediately active, if there are rate limits, or what happens on success/failure. For a creation tool with zero annotation coverage, this leaves significant gaps in understanding its behavior.

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 gets straight to the point with zero wasted words. It's appropriately sized for a tool with well-documented parameters in the schema. Every word earns its place by clearly stating the tool's core function.

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?

For a creation tool with no annotations and no output schema, the description is insufficient. It doesn't explain what happens after creation (e.g., returns a discount ID, activates immediately), doesn't mention error conditions or validation rules, and provides no context about the discount system's limitations. The 100% schema coverage helps with parameters but doesn't compensate for missing behavioral and outcome information.

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 schema fully documents all 7 parameters with clear descriptions. The description adds no additional parameter information beyond what's in the schema. According to scoring rules, when schema coverage is high (>80%), the baseline is 3 even with no param info in the description.

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 ('Create') and resource ('a basic discount code'), making the purpose immediately understandable. It distinguishes this from sibling tools like 'complete-draft-order' or 'get-products' by focusing on discount creation rather than order or product operations. However, it doesn't specify what 'basic' means compared to more advanced discount options that might exist.

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. There are no explicit instructions about prerequisites, constraints, or comparisons with other discount-related tools (if any existed in the sibling list). The agent must infer usage from the tool name and parameters 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/amir-bengherbi/shopify-mcp-server'

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