PostCartsIdLineItems
Add product variants to a shopping cart using the Medusa MCP Server. This tool enables you to manage cart items by specifying product details and cart identifiers.
Instructions
Add a product variant as a line item in the cart.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | No | ||
| fields | No |
Implementation Reference
- src/services/medusa-store.ts:83-126 (handler)Generic handler that performs HTTP POST (or GET) requests to the Medusa store API endpoints. For PostCartsIdLineItems, it fetches the /carts/{id}/line-items path with POST method and body containing line item data.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)Dynamically generates Zod input schema for the tool based on OpenAPI parameters defined in store.json for the specific endpoint.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-41 (registration)Registers all dynamically generated tools from MedusaStoreService on the MCP server, including the PostCartsIdLineItems tool.tools.forEach((tool) => { server.tool( tool.name, tool.description, tool.inputSchema, tool.handler );
- src/services/medusa-store.ts:30-129 (helper)Helper function that wraps an OpenAPI path into a tool definition, extracting operationId as tool name (e.g., PostCartsIdLineItems), generating schema and handler for it.wrapPath(refPath: string, refFunction: SdkRequestType) { return defineTool((z): any => { let name; let description; let parameters: Parameter[] = []; let method = "get"; if ("get" in refFunction) { method = "get"; name = refFunction.get.operationId; description = refFunction.get.description; parameters = refFunction.get.parameters; } else if ("post" in refFunction) { method = "post"; name = refFunction.post.operationId; description = refFunction.post.description; parameters = refFunction.post.parameters ?? []; } if (!name) { throw new Error("No name found for the function"); } return { name: name!, description: description, 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) }, 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:131-137 (helper)Generates all store tools by iterating over OpenAPI paths in store.json and calling wrapPath for each, creating the PostCartsIdLineItems tool among others.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; }