Skip to main content
Glama
boldcommerce

Magento 2 MCP Server

by boldcommerce

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
NameRequiredDescriptionDefault
skuYesThe SKU (Stock Keeping Unit) of the product
attribute_codeYesThe code of the attribute to update (e.g., name, price, description, status, etc.)
valueNoThe new value for the attribute

Implementation Reference

  • 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
        };
      }
    }
  • 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
          };
        }
      }
    );

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/boldcommerce/magento2-mcp'

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