Skip to main content
Glama
boldcommerce

Magento 2 MCP Server

by boldcommerce

get_product_attributes

Retrieve all product attributes from a Magento 2 store using the product SKU to access detailed specifications and properties.

Instructions

Get all attributes for a product by SKU

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
skuYesThe SKU (Stock Keeping Unit) of the product

Implementation Reference

  • The handler function that implements the core logic of the get_product_attributes tool. It fetches the product data from the Magento API using the provided SKU, extracts both base attributes and custom attributes, formats them into a structured object, and returns the JSON stringified response or an error message.
    async ({ sku }) => {
      try {
        const productData = await callMagentoApi(`/products/${sku}`);
        
        // Extract and format attributes
        const attributes = {
          base_attributes: {
            id: productData.id,
            sku: productData.sku,
            name: productData.name,
            price: productData.price,
            status: productData.status,
            visibility: productData.visibility,
            type_id: productData.type_id
          },
          custom_attributes: {}
        };
        
        if (productData.custom_attributes && Array.isArray(productData.custom_attributes)) {
          productData.custom_attributes.forEach(attr => {
            attributes.custom_attributes[attr.attribute_code] = attr.value;
          });
        }
        
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(attributes, null, 2)
            }
          ]
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: `Error fetching product attributes: ${error.message}`
            }
          ],
          isError: true
        };
      }
    }
  • The input schema defined using Zod for the get_product_attributes tool, specifying that it requires a 'sku' parameter as a string.
      sku: z.string().describe("The SKU (Stock Keeping Unit) of the product")
    },
  • mcp-server.js:619-669 (registration)
    The registration of the get_product_attributes tool with the MCP server, including the tool name, description, input schema, and handler function reference.
    server.tool(
      "get_product_attributes",
      "Get all attributes for a product by SKU",
      {
        sku: z.string().describe("The SKU (Stock Keeping Unit) of the product")
      },
      async ({ sku }) => {
        try {
          const productData = await callMagentoApi(`/products/${sku}`);
          
          // Extract and format attributes
          const attributes = {
            base_attributes: {
              id: productData.id,
              sku: productData.sku,
              name: productData.name,
              price: productData.price,
              status: productData.status,
              visibility: productData.visibility,
              type_id: productData.type_id
            },
            custom_attributes: {}
          };
          
          if (productData.custom_attributes && Array.isArray(productData.custom_attributes)) {
            productData.custom_attributes.forEach(attr => {
              attributes.custom_attributes[attr.attribute_code] = attr.value;
            });
          }
          
          return {
            content: [
              {
                type: "text",
                text: JSON.stringify(attributes, null, 2)
              }
            ]
          };
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Error fetching product attributes: ${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