Skip to main content
Glama

PostCartsIdLineItemsLine_id

Modify cart line item details such as quantity or custom fields to update shopping cart contents during checkout.

Instructions

Update a line item's details in the cart.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
idNo
line_idNo
fieldsNo

Implementation Reference

  • The handler function that executes the logic for the PostCartsIdLineItemsLine_id tool and all other dynamically generated store API tools. It constructs query/body from input and calls the Medusa SDK fetch 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;
        }
    }
  • Generates the input schema (Zod) for the tool based on OpenAPI parameters from store.json, excluding headers.
    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)
    },
  • The defineTools method that iterates over OpenAPI paths in store.json, wraps each into a tool (including PostCartsIdLineItemsLine_id if defined there), using 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:35-42 (registration)
    Registers all tools from store and admin services with the MCP server, including PostCartsIdLineItemsLine_id.
    tools.forEach((tool) => {
        server.tool(
            tool.name,
            tool.description,
            tool.inputSchema,
            tool.handler
        );
    });
  • Utility that wraps the raw handler to conform to MCP protocol response format with JSON output or error text.
    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
        };
    };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/SGFGOV/medusa-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server