get-variants-by-ids
Retrieve specific product variants from Shopify using their unique IDs to access detailed variant information.
Instructions
Get product variants by their IDs
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| variantIds | Yes | Array of variant IDs to retrieve |
Implementation Reference
- src/index.ts:220-242 (registration)Registration of the 'get-variants-by-ids' MCP tool, including description, Zod input schema for variantIds array, and inline handler that uses ShopifyClient to load variants by IDs and returns JSON-formatted response.server.tool( "get-variants-by-ids", "Get product variants by their IDs", { variantIds: z .array(z.string()) .describe("Array of variant IDs to retrieve"), }, async ({ variantIds }) => { const client = new ShopifyClient(); try { const variants = await client.loadVariantsByIds( SHOPIFY_ACCESS_TOKEN, MYSHOPIFY_DOMAIN, variantIds ); return { content: [{ type: "text", text: JSON.stringify(variants, null, 2) }], }; } catch (error) { return handleError("Failed to retrieve variants", error); } }
- Core handler implementation in ShopifyClient that performs GraphQL query using nodes(ids:) to fetch product variants by their GIDs, includes associated product details and images, filters for ProductVariant type, returns variants array with currencyCode.async loadVariantsByIds( accessToken: string, shop: string, variantIds: string[] ): Promise<LoadVariantsByIdResponse> { const myshopifyDomain = await this.getMyShopifyDomain(accessToken, shop); const graphqlQuery = gql` { shop { currencyCode } nodes(ids: ${JSON.stringify(variantIds)}) { __typename ... on ProductVariant { ${productVariantsFragment} product { id title description images(first: 20) { edges { node { ${productImagesFragment} } } } } } } } `; const res = await this.shopifyGraphqlRequest<{ data: { shop: { currencyCode: string; }; nodes: Array< | ({ __typename: string; } & ProductVariantWithProductDetails) | null >; }; }>({ url: `https://${myshopifyDomain}/admin/api/${this.SHOPIFY_API_VERSION}/graphql.json`, accessToken, query: graphqlQuery, }); const variants = res.data.data.nodes.filter( ( node ): node is { __typename: string; } & ProductVariantWithProductDetails => node?.__typename === "ProductVariant" ); const currencyCode = res.data.data.shop.currencyCode; return { variants, currencyCode }; }
- TypeScript interface definition in ShopifyClientPort for the loadVariantsByIds method, specifying input parameters and LoadVariantsByIdResponse output.loadVariantsByIds( accessToken: string, shop: string, variantIds: string[] ): Promise<LoadVariantsByIdResponse>;
- Type definition for the response of loadVariantsByIds, including currencyCode and array of ProductVariantWithProductDetails (variant plus product info).export type LoadVariantsByIdResponse = { currencyCode: string; variants: ProductVariantWithProductDetails[]; };