get-products-by-ids
Retrieve specific products from a Shopify store by providing their unique IDs, enabling targeted product data access for inventory management or display purposes.
Instructions
Get products by their IDs
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| productIds | Yes | Array of product IDs to retrieve |
Implementation Reference
- src/index.ts:166-190 (registration)MCP tool registration for 'get-products-by-ids', including input schema (productIds: array of strings) and handler logic that delegates to ShopifyClient.loadProductsByIds, formats output with formatProduct, handles errors.server.tool( "get-products-by-ids", "Get products by their IDs", { productIds: z .array(z.string()) .describe("Array of product IDs to retrieve"), }, async ({ productIds }) => { const client = new ShopifyClient(); try { const products = await client.loadProductsByIds( SHOPIFY_ACCESS_TOKEN, MYSHOPIFY_DOMAIN, productIds ); const formattedProducts = products.products.map(formatProduct); return { content: [{ type: "text", text: formattedProducts.join("\n") }], }; } catch (error) { return handleError("Failed to retrieve products by IDs", error); } } );
- Core implementation fetching products by IDs via Shopify GraphQL 'nodes' query with productFragment, filters valid Products, returns products array and shop currencyCode.async loadProductsByIds( accessToken: string, shop: string, productIds: string[] ): Promise<LoadProductsResponse> { const myshopifyDomain = await this.getMyShopifyDomain(accessToken, shop); const graphqlQuery = gql` { shop { currencyCode } nodes(ids: ${JSON.stringify(productIds)}) { __typename ... on Product { ${productFragment} } } } `; const res = await this.shopifyGraphqlRequest<{ data: { shop: { currencyCode: string; }; nodes: Array< | ({ __typename: string; } & ProductNode) | null >; }; }>({ url: `https://${myshopifyDomain}/admin/api/${this.SHOPIFY_API_VERSION}/graphql.json`, accessToken, query: graphqlQuery, }); const data = res.data.data; const products = data.nodes.filter( ( node ): node is { __typename: string; } & ProductNode => node?.__typename === "Product" ); const currencyCode = data.shop.currencyCode; return { products, currencyCode }; }
- src/index.ts:33-49 (helper)Helper function to format a ProductNode into a human-readable string with title, description, handle, and detailed variants.function formatProduct(product: ProductNode): string { return ` Product: ${product.title} description: ${product.description} handle: ${product.handle} variants: ${product.variants.edges .map( (variant) => `variant.title: ${variant.node.title} variant.id: ${variant.node.id} variant.price: ${variant.node.price} variant.sku: ${variant.node.sku} variant.inventoryPolicy: ${variant.node.inventoryPolicy} ` ) .join(", ")} `; }
- Type definition for ProductNode, the structure of product data returned and used in formatting.export type ProductNode = { id: string; handle: string; title: string; description: string; publishedAt: string; updatedAt: string; options: ProductOption[]; images: { edges: { node: ProductImage; }[]; }; variants: { edges: { node: ProductVariant; }[]; }; };
- src/index.ts:611-626 (helper)Helper function to handle and format errors in tool responses, used in the tool handler.function handleError( defaultMessage: string, error: unknown ): { content: { type: "text"; text: string }[]; isError: boolean; } { let errorMessage = defaultMessage; if (error instanceof CustomError) { errorMessage = `${defaultMessage}: ${error.message}`; } return { content: [{ type: "text", text: errorMessage }], isError: true, }; }