get-variants-by-ids
Retrieve specific product variants from a Shopify store using their unique IDs. This tool fetches variant details for inventory management, order processing, or data analysis.
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:192-215 (registration)Registers the 'get-variants-by-ids' MCP tool, defines input schema (array of variantIds), and provides the handler function that instantiates ShopifyClient and calls loadVariantsByIds to fetch and return variant data as JSON.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 logic: Executes GraphQL query to fetch product variants by IDs using nodes(ids: [...]), includes product details and images, filters valid ProductVariant nodes, returns variants with shop 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 for loadVariantsByIds method in ShopifyClientPort.loadVariantsByIds( accessToken: string, shop: string, variantIds: string[] ): Promise<LoadVariantsByIdResponse>;
- Type definition for the response of loadVariantsByIds, including currency and array of variants with product details.export type LoadVariantsByIdResponse = { currencyCode: string; variants: ProductVariantWithProductDetails[]; };
- Extended type for product variant including associated product details (title, description, images).export type ProductVariantWithProductDetails = ProductVariant & { product: { id: string; title: string; description: string; images: { edges: { node: ProductImage; }[]; }; };