Skip to main content
Glama
smithery-ai

Shopify Update MCP Server

by smithery-ai

create-draft-order

Create draft orders in Shopify by specifying line items, customer email, and shipping address details to prepare orders for review and processing.

Instructions

Create a draft order

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
lineItemsYesLine items to add to the order
emailYesCustomer email
shippingAddressYesShipping address details
noteNoOptional note for the order

Implementation Reference

  • src/index.ts:432-482 (registration)
    MCP tool registration including input schema (Zod) and handler function that delegates to ShopifyClient.createDraftOrder
    server.tool(
      "create-draft-order",
      "Create a draft order",
      {
        lineItems: z
          .array(
            z.object({
              variantId: z.string(),
              quantity: z.number(),
            })
          )
          .describe("Line items to add to the order"),
        email: z.string().email().describe("Customer email"),
        shippingAddress: z
          .object({
            address1: z.string(),
            city: z.string(),
            province: z.string(),
            country: z.string(),
            zip: z.string(),
            firstName: z.string(),
            lastName: z.string(),
            countryCode: z.string(),
          })
          .describe("Shipping address details"),
        note: z.string().optional().describe("Optional note for the order"),
      },
      async ({ lineItems, email, shippingAddress, note }) => {
        const client = new ShopifyClient();
        try {
          const draftOrderData: CreateDraftOrderPayload = {
            lineItems,
            email,
            shippingAddress,
            billingAddress: shippingAddress, // Using same address for billing
            tags: "draft",
            note: note || "",
          };
          const draftOrder = await client.createDraftOrder(
            SHOPIFY_ACCESS_TOKEN,
            MYSHOPIFY_DOMAIN,
            draftOrderData
          );
          return {
            content: [{ type: "text", text: JSON.stringify(draftOrder, null, 2) }],
          };
        } catch (error) {
          return handleError("Failed to create draft order", error);
        }
      }
    );
  • Core handler implementation performing GraphQL mutation to create draft order in Shopify
    async createDraftOrder(
      accessToken: string,
      myshopifyDomain: string,
      draftOrderData: CreateDraftOrderPayload
    ): Promise<DraftOrderResponse> {
      const graphqlQuery = gql`
        mutation draftOrderCreate($input: DraftOrderInput!) {
          draftOrderCreate(input: $input) {
            draftOrder {
              id
              name
            }
            userErrors {
              field
              message
            }
          }
        }
      `;
    
      const res = await this.shopifyGraphqlRequest<{
        data: {
          draftOrderCreate: {
            draftOrder: {
              id: string;
              name: string;
            };
            userErrors: Array<{
              field: string[];
              message: string;
            }>;
          };
        };
      }>({
        url: `https://${myshopifyDomain}/admin/api/${this.SHOPIFY_API_VERSION}/graphql.json`,
        accessToken,
        query: graphqlQuery,
        variables: {
          input: draftOrderData,
        },
      });
    
      const draftOrder = res.data.data.draftOrderCreate.draftOrder;
      const userErrors = res.data.data.draftOrderCreate.userErrors;
    
      if (userErrors.length > 0) {
        throw getGraphqlShopifyUserError(userErrors, {
          myshopifyDomain,
          draftOrderData,
        });
      }
    
      return {
        draftOrderId: draftOrder.id,
        draftOrderName: draftOrder.name,
      };
    }
  • TypeScript type definition for the draft order payload used in the tool
    export type CreateDraftOrderPayload = {
      lineItems: Array<{
        variantId: string;
        quantity: number;
        appliedDiscount?: {
          title: string;
          value: number;
          valueType: "FIXED_AMOUNT" | "PERCENTAGE";
        };
      }>;
      shippingAddress: {
        address1: string;
        address2?: string;
        countryCode: string;
        firstName: string;
        lastName: string;
        zip: string;
        city: string;
        country: string;
        province?: string;
        provinceCode?: string;
        phone?: string;
      };
      billingAddress: {
        address1: string;
        address2?: string;
        countryCode: string;
        firstName: string;
        lastName: string;
        zip: string;
        city: string;
        country: string;
        province?: string;
        provinceCode?: string;
        phone?: string;
      };
      email: string;
      tags: string;
      note: string;
    };
  • Interface definition for the createDraftOrder method in ShopifyClientPort
    createDraftOrder(
      accessToken: string,
      shop: string,
      draftOrderData: CreateDraftOrderPayload,
      idempotencyKey: string
    ): Promise<DraftOrderResponse>;

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/smithery-ai/shopify-mcp-server-main-1'

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