GetProducts
Retrieve and filter product lists by fields like id, title, or handle. Sort, paginate, and query products for precise results using Medusa MCP Server.
Instructions
Retrieve a list of products. The products can be filtered by fields such as id. The products can also be sorted or paginated.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| $and | No | ||
| $or | No | ||
| cart_id | No | ||
| category_id | No | ||
| collection_id | No | ||
| country_code | No | ||
| created_at | No | ||
| fields | No | ||
| handle | No | ||
| id | No | ||
| is_giftcard | No | ||
| limit | No | ||
| offset | No | ||
| order | No | ||
| province | No | ||
| q | No | ||
| region_id | No | ||
| sales_channel_id | No | ||
| tag_id | No | ||
| title | No | ||
| type_id | No | ||
| updated_at | No | ||
| variants | No |
Implementation Reference
- src/services/medusa-store.ts:83-127 (handler)The handler function that executes the logic for the 'GetProducts' tool (and all store tools). It constructs query/body from input and calls the Medusa SDK client.fetch for the corresponding endpoint (e.g., /store/products).handler: async ( input: InferToolHandlerInput<any, ZodTypeAny> ): Promise<any> => { const query = new URLSearchParams(input); const body = Object.entries(input).reduce( (acc, [key, value]) => { if ( parameters.find( (p) => p.name === key && p.in === "body" ) ) { acc[key] = value; } return acc; }, {} as Record<string, any> ); if (method === "get") { console.error( `Fetching ${refPath} with GET ${query.toString()}` ); const response = await this.sdk.client.fetch(refPath, { method: method, headers: { "Content-Type": "application/json", "Accept": "application/json", "Authorization": `Bearer ${process.env.PUBLISHABLE_KEY}` }, query: query }); return response; } else { const response = await this.sdk.client.fetch(refPath, { method: method, headers: { "Content-Type": "application/json", "Accept": "application/json", "Authorization": `Bearer ${process.env.PUBLISHABLE_KEY}` }, body: JSON.stringify(body) }); return response; } } };
- src/services/medusa-store.ts:54-81 (schema)Dynamic generation of Zod input schema for 'GetProducts' (and other store tools) based on OAS parameters from store.json.inputSchema: { ...parameters .filter((p) => p.in != "header") .reduce((acc, param) => { switch (param.schema.type) { case "string": acc[param.name] = z.string().optional(); break; case "number": acc[param.name] = z.number().optional(); break; case "boolean": acc[param.name] = z.boolean().optional(); break; case "array": acc[param.name] = z .array(z.string()) .optional(); break; case "object": acc[param.name] = z.object({}).optional(); break; default: acc[param.name] = z.string().optional(); } return acc; }, {} as any) },
- src/index.ts:35-42 (registration)Registers the 'GetProducts' tool (among others from store and admin services) with the MCP server.tools.forEach((tool) => { server.tool( tool.name, tool.description, tool.inputSchema, tool.handler ); });
- src/services/medusa-store.ts:131-137 (helper)Generates tool definitions for all store API endpoints from OAS spec, including 'GetProducts'.defineTools(store = storeJson): any[] { const paths = Object.entries(store.paths) as [string, SdkRequestType][]; const tools = paths.map(([path, refFunction]) => this.wrapPath(path, refFunction) ); return tools; }
- src/utils/define-tools.ts:16-60 (helper)Utility function that wraps the tool handler to conform to MCP protocol response format, used for 'GetProducts' and all tools.export const defineTool = ( cb: (zod: typeof z) => ToolDefinition<any, ZodAny, any> ) => { const tool = cb(z); const wrappedHandler = async ( input: InferToolHandlerInput<Zod.ZodAny, Zod.ZodAny>, _: RequestHandlerExtra ): Promise<{ content: CallToolResult["content"]; isError?: boolean; statusCode?: number; }> => { try { const result = await tool.handler(input); return { content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; } catch (error) { return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }` } ], isError: true }; } }; return { ...tool, handler: wrappedHandler }; };