get_products
Retrieve a list of products with optional filters for ID, title, category, brand, price, and rating. Supports pagination to manage large datasets efficiently.
Instructions
Get a list of products with optional filtering and pagination.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| brand | No | Filter products by brand | |
| category | No | Filter products by category | |
| id | No | Filter products by ID | |
| limit | No | Maximum number of products to return | |
| price | No | Filter products by price | |
| q | No | Filter products by title | |
| rating | No | Filter products by rating | |
| skip | No | Number of products to skip |
Implementation Reference
- src/tools/products.ts:25-71 (handler)The async handler function that implements the get_products tool logic: fetches products from dummyjson.com API using provided parameters, handles search and filters, formats response as MCP text content.handler: async (params: types.ProductParams): Promise<types.McpResponse> => { try { const { q, ...rest } = params; let result: types.ProductsResponse; let response; if (q) { response = await fetch(`https://dummyjson.com/products/search?q=${q}`, { method: "GET", }); } else { const urlParams = new URLSearchParams(); Object.entries(rest).forEach(([key, value]) => { if (value !== undefined) { urlParams.set(key, value.toString()); } }); response = await fetch( `https://dummyjson.com/products?${urlParams.toString()}`, { method: "GET", } ); } result = await response.json(); if (!result.products) { throw new Error("No results returned from API"); } const content: types.McpTextContent = { type: "text", text: `Products Results:\n\n${JSON.stringify( result.products, null, 2 )}`, }; return { content: [content], }; } catch (error) { console.error(error); throw new Error(`Failed to fetch products: ${error.message} ${error}`); } },
- src/tools/products.ts:7-24 (schema)Zod schema defining the input parameters for the get_products tool, including optional filters and pagination.parameters: { id: z.string().optional().describe("Filter products by ID"), q: z.string().optional().describe("Filter products by title"), category: z.string().optional().describe("Filter products by category"), brand: z.string().optional().describe("Filter products by brand"), price: z.number().optional().describe("Filter products by price"), rating: z.number().optional().describe("Filter products by rating"), skip: z .number() .optional() .default(0) .describe("Number of products to skip"), limit: z .number() .optional() .default(10) .describe("Maximum number of products to return"), },
- src/index.ts:15-20 (registration)Registration of the get_products tool on the MCP server using server.tool() with name, description, parameters, and handler.getProductsTool.name, getProductsTool.description, getProductsTool.parameters, getProductsTool.handler );