get-products
Retrieve Shopify products with optional title filtering to manage inventory and find specific items efficiently.
Instructions
Get all products or search by title
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| searchTitle | No | Search title, if missing, will return all products | |
| limit | Yes | Maximum number of products to return |
Implementation Reference
- src/index.ts:106-133 (registration)Registration of the 'get-products' tool, including Zod input schema and inline handler function that uses ShopifyClient.loadProducts and formats the response.server.tool( "get-products", "Get all products or search by title", { searchTitle: z .string() .optional() .describe("Search title, if missing, will return all products"), limit: z.number().describe("Maximum number of products to return"), }, async ({ searchTitle, limit }) => { const client = new ShopifyClient(); try { const products = await client.loadProducts( SHOPIFY_ACCESS_TOKEN, MYSHOPIFY_DOMAIN, searchTitle ?? null, limit ); const formattedProducts = products.products.map(formatProduct); return { content: [{ type: "text", text: formattedProducts.join("\n") }], }; } catch (error) { return handleError("Failed to retrieve products data", error); } } );
- src/index.ts:33-50 (helper)Helper function formatProduct used by the get-products handler to format product data into a readable text string.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 helper method loadProducts in ShopifyClient that executes GraphQL query to fetch products from Shopify, handles searchTitle filter, pagination, and returns formatted response. Called by the tool handler.async loadProducts( accessToken: string, myshopifyDomain: string, searchTitle: string | null, limit: number = 10, afterCursor?: string ): Promise<LoadProductsResponse> { const titleFilter = searchTitle ? `title:*${searchTitle}*` : ""; const graphqlQuery = gql` { shop { currencyCode } products(first: ${limit}, query: "${titleFilter}"${ afterCursor ? `, after: "${afterCursor}"` : "" }) { edges { node { ${productFragment} } } pageInfo { hasNextPage endCursor } } } `; const res = await this.shopifyGraphqlRequest<{ data: { shop: { currencyCode: string; }; 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.products.edges; const products = edges.map((edge) => edge.node); const pageInfo = data.products.pageInfo; const next = pageInfo.hasNextPage ? pageInfo.endCursor : undefined; const currencyCode = data.shop.currencyCode; return { products, next, currencyCode }; }
- Type schema LoadProductsResponse for the return type of loadProducts.export type LoadProductsResponse = { currencyCode: string; products: ProductNode[]; next?: string; };
- Type schema ProductNode defining the structure of product data returned by Shopify queries.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; }[]; }; };