get-products-by-collection
Retrieve products from a Shopify collection by specifying the collection ID. Define a limit to control the number of products returned. Streamlines product data access for targeted collections.
Instructions
Get products from a specific collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collectionId | Yes | ID of the collection to get products from | |
| limit | No | Maximum number of products to return |
Implementation Reference
- Core handler: Executes GraphQL query to Shopify Admin API to fetch products from the specified collection ID, handles pagination cursor, maps edges to products, and returns formatted response with currency.async loadProductsByCollectionId( accessToken: string, shop: string, collectionId: string, limit: number = 10, afterCursor?: string ): Promise<LoadProductsResponse> { const myshopifyDomain = await this.getMyShopifyDomain(accessToken, shop); const graphqlQuery = gql` { shop { currencyCode } collection(id: "gid://shopify/Collection/${collectionId}") { products( first: ${limit}${afterCursor ? `, after: "${afterCursor}"` : ""} ) { edges { node { ${productFragment} } } pageInfo { hasNextPage endCursor } } } } `; const res = await this.shopifyGraphqlRequest<{ data: { shop: { currencyCode: string; }; collection: { products: { edges: Array<{ node: ProductNode; }>; pageInfo: { hasNextPage: boolean; endCursor: string; }; }; }; }; }>({ url: `https://${myshopifyDomain}/admin/api/${this.SHOPIFY_API_VERSION}/graphql.json`, accessToken, query: graphqlQuery, }); const data = res.data.data; const edges = data.collection.products.edges; const products = edges.map((edge) => edge.node); const pageInfo = data.collection.products.pageInfo; const next = pageInfo.hasNextPage ? pageInfo.endCursor : undefined; const currencyCode = data.shop.currencyCode; return { products, next, currencyCode }; }
- src/index.ts:135-165 (registration)Registers the 'get-products-by-collection' tool with MCP server, defines input schema using Zod, instantiates ShopifyClient, calls the handler, formats output using formatProduct, and handles errors.server.tool( "get-products-by-collection", "Get products from a specific collection", { collectionId: z .string() .describe("ID of the collection to get products from"), limit: z .number() .optional() .default(10) .describe("Maximum number of products to return"), }, async ({ collectionId, limit }) => { const client = new ShopifyClient(); try { const products = await client.loadProductsByCollectionId( SHOPIFY_ACCESS_TOKEN, MYSHOPIFY_DOMAIN, collectionId, limit ); const formattedProducts = products.products.map(formatProduct); return { content: [{ type: "text", text: formattedProducts.join("\n") }], }; } catch (error) { return handleError("Failed to retrieve products from collection", error); } } );
- src/index.ts:138-147 (schema)Zod schema for tool inputs: required collectionId (string) and optional limit (number, default 10).{ collectionId: z .string() .describe("ID of the collection to get products from"), limit: z .number() .optional() .default(10) .describe("Maximum number of products to return"), },
- TypeScript interface definition in ShopifyClientPort for the loadProductsByCollectionId method, specifying parameters and return type LoadProductsResponse.loadProductsByCollectionId( accessToken: string, myshopifyDomain: string, collectionId: string, limit?: number, afterCursor?: string ): Promise<LoadProductsResponse>;
- src/index.ts:33-50 (helper)Helper function to format a ProductNode into a readable string, used in the tool response to display product details and variants.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(", ")} `; }