PostCartsIdLineItemsLine_id
Modify specific details of a cart line item using its ID and line item ID within the Medusa MCP Server environment.
Instructions
Update a line item's details in the cart.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fields | No | ||
| id | No | ||
| line_id | No |
Implementation Reference
- src/services/medusa-store.ts:83-127 (handler)The core handler logic for the PostCartsIdLineItemsLine_id tool. It processes input into query params and body (for POST), logs the request, and fetches the Medusa store API endpoint using the SDK, returning the response. The specific path and parameters are derived from store.json for this operationId.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-82 (schema)Generates the Zod input schema for the tool by mapping OpenAPI parameter schemas (excluding headers) to optional Zod types (string, number, boolean, array, object). Used for validating inputs to PostCartsIdLineItemsLine_id.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 dynamically generated tool 'PostCartsIdLineItemsLine_id' (from medusaStoreService.defineTools()) along with others to the MCP server.tools.forEach((tool) => { server.tool( tool.name, tool.description, tool.inputSchema, tool.handler ); });
- src/services/medusa-store.ts:131-137 (registration)Generates tool definitions for all store API paths from store.json OpenAPI spec, including the one with operationId 'PostCartsIdLineItemsLine_id', by calling wrapPath for each.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 that wraps the raw tool handler (from wrapPath) into the MCP protocol format, handling errors and formatting output as JSON text content.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 }; };