get-products-by-ids
Retrieve specific Shopify products using their unique IDs to access detailed information for inventory management or order processing.
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:167-191 (registration)Registers the 'get-products-by-ids' tool with MCP server, defines input schema and inline handler that instantiates ShopifyClient, calls loadProductsByIds, formats output using formatProduct, and 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 handler implementation in ShopifyClient: executes GraphQL query using 'nodes(ids)' to fetch products by GID, filters results, returns products and shop currency.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:170-174 (schema)Zod input schema for the tool: requires array of product ID strings.{ productIds: z .array(z.string()) .describe("Array of product IDs to retrieve"), },
- TypeScript output type definition for loadProductsByIds response.export type LoadProductsByIdsResponse = { currencyCode: string; products: ProductNode[]; };
- src/index.ts:33-50 (helper)Helper function to format product data into readable text string used in tool response.function formatProduct(product: ProductNode): string { return ` Product: ${product.title} id: ${product.id} 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(", ")} `; }