Skip to main content
Glama

get-products-by-collection

Retrieve products from a specific Shopify collection by providing the collection ID. Specify a limit to control the number of products returned.

Instructions

Get products from a specific collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionIdYesID of the collection to get products from
limitNoMaximum number of products to return

Implementation Reference

  • MCP tool registration, schema, and handler for 'get-products-by-collection'. The handler fetches products by collection ID using ShopifyClient, formats them with formatProduct, and returns formatted text output.
    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); } } );
  • Helper function to format a product node into a readable string, used by the tool handler.
    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(", ")} `; }
  • Core implementation of fetching products by collection ID via Shopify GraphQL API, called by the tool handler.
    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 }; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/smithery-ai/shopify-mcp-server-main-1'

If you have feedback or need assistance with the MCP directory API, please join our Discord server