PostCartsIdShippingMethods
Add a preferred shipping method to a cart for efficient order processing. This API route is triggered when a customer selects their shipping option in Medusa MCP Server.
Instructions
Add a shipping method to a cart. Use this API route when the customer chooses their preferred shipping option.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fields | No | ||
| id | No |
Implementation Reference
- src/services/medusa-store.ts:83-126 (handler)The handler logic for the PostCartsIdShippingMethods tool (shared with all store API tools). It constructs query parameters and body from input based on OAS parameters and calls the Medusa SDK's fetch method for the corresponding path.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 input schema generation for PostCartsIdShippingMethods based on the OAS parameters for the POST /carts/{id}/shipping-methods 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/services/medusa-store.ts:131-137 (registration)Registers all store API tools, including PostCartsIdShippingMethods, by mapping over OAS paths and wrapping each into a tool with operationId as name.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/index.ts:14-17 (registration)Retrieves and combines tools from store and admin services, including the PostCartsIdShippingMethods tool from store service.tools = [ ...medusaStoreService.defineTools(), ...medusaAdminService.defineTools() ];
- src/index.ts:35-42 (registration)Registers each tool, including PostCartsIdShippingMethods, with the MCP server.tools.forEach((tool) => { server.tool( tool.name, tool.description, tool.inputSchema, tool.handler ); });