PostCartsIdCustomer
Assign a logged-in customer to an existing cart to transition from guest to authenticated shopping, ensuring cart continuity in the Medusa MCP Server.
Instructions
Set the customer of the cart. This is useful when you create the cart for a guest customer, then they log in with their account.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fields | No | ||
| id | No |
Implementation Reference
- src/services/medusa-store.ts:83-127 (handler)This is the handler function executed for all store API tools, including PostCartsIdCustomer. It constructs the request (query/body) from input params and calls the Medusa SDK client.fetch to the specific endpoint path with POST method.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)Dynamic generation of Zod inputSchema from OpenAPI parameters for the tool, mapping types to Zod schemas.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)Registration of all dynamically generated tools (including PostCartsIdCustomer from store service) into the MCP server.tools.forEach((tool) => { server.tool( tool.name, tool.description, tool.inputSchema, tool.handler ); });
- src/utils/define-tools.ts:16-60 (helper)Utility to define MCP tools with Zod schema and wrap the handler to format output as MCP CallToolResult, handling errors.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 }; };
- src/index.ts:14-17 (registration)Calls defineTools on MedusaStoreService to generate the array of tools including PostCartsIdCustomer, then registers them.tools = [ ...medusaStoreService.defineTools(), ...medusaAdminService.defineTools() ];