update_product_attribute
Modify product attributes like name, price, or description by SKU to maintain accurate catalog information in Magento 2 stores.
Instructions
Update a specific attribute of a product by SKU
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sku | Yes | The SKU (Stock Keeping Unit) of the product | |
| attribute_code | Yes | The code of the attribute to update (e.g., name, price, description, status, etc.) | |
| value | No | The new value for the attribute |
Implementation Reference
- mcp-server.js:785-847 (handler)The handler function that implements the core logic for updating a product attribute. It validates the product exists, handles both custom and standard attributes, makes a PUT request to the Magento /products/{sku} endpoint, and returns formatted success/error response.async ({ sku, attribute_code, value }) => { try { // First, check if the product exists const productData = await callMagentoApi(`/products/${sku}`).catch(() => null); if (!productData) { return { content: [ { type: "text", text: `Product with SKU '${sku}' not found` } ], isError: true }; } // Prepare the update data with the correct structure // Magento 2 API requires a "product" wrapper object let updateData = { product: {} }; // Determine if this is a standard attribute or custom attribute const isCustomAttribute = productData.custom_attributes && productData.custom_attributes.some(attr => attr.attribute_code === attribute_code); if (isCustomAttribute) { // For custom attributes, we need to use the custom_attributes array updateData.product.custom_attributes = [ { attribute_code, value } ]; } else { // For standard attributes, we set them directly on the product object updateData.product[attribute_code] = value; } // Make the API call to update the product const result = await callMagentoApi(`/products/${sku}`, 'PUT', updateData); return { content: [ { type: "text", text: `Successfully updated '${attribute_code}' for product with SKU '${sku}'. Updated product: ${JSON.stringify(formatProduct(result), null, 2)}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error updating product attribute: ${error.response?.data?.message || error.message}` } ], isError: true }; } }
- mcp-server.js:780-784 (schema)Zod schema defining the input parameters for the tool: sku (string), attribute_code (string), and value (any type).{ sku: z.string().describe("The SKU (Stock Keeping Unit) of the product"), attribute_code: z.string().describe("The code of the attribute to update (e.g., name, price, description, status, etc.)"), value: z.any().describe("The new value for the attribute") },
- mcp-server.js:777-848 (registration)Registers the 'update_product_attribute' tool with the MCP server, including name, description, input schema, and handler function.server.tool( "update_product_attribute", "Update a specific attribute of a product by SKU", { sku: z.string().describe("The SKU (Stock Keeping Unit) of the product"), attribute_code: z.string().describe("The code of the attribute to update (e.g., name, price, description, status, etc.)"), value: z.any().describe("The new value for the attribute") }, async ({ sku, attribute_code, value }) => { try { // First, check if the product exists const productData = await callMagentoApi(`/products/${sku}`).catch(() => null); if (!productData) { return { content: [ { type: "text", text: `Product with SKU '${sku}' not found` } ], isError: true }; } // Prepare the update data with the correct structure // Magento 2 API requires a "product" wrapper object let updateData = { product: {} }; // Determine if this is a standard attribute or custom attribute const isCustomAttribute = productData.custom_attributes && productData.custom_attributes.some(attr => attr.attribute_code === attribute_code); if (isCustomAttribute) { // For custom attributes, we need to use the custom_attributes array updateData.product.custom_attributes = [ { attribute_code, value } ]; } else { // For standard attributes, we set them directly on the product object updateData.product[attribute_code] = value; } // Make the API call to update the product const result = await callMagentoApi(`/products/${sku}`, 'PUT', updateData); return { content: [ { type: "text", text: `Successfully updated '${attribute_code}' for product with SKU '${sku}'. Updated product: ${JSON.stringify(formatProduct(result), null, 2)}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error updating product attribute: ${error.response?.data?.message || error.message}` } ], isError: true }; } } );