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
| Name | Required | Description | Default |
|---|---|---|---|
| sku | Yes | The SKU (Stock Keeping Unit) of the product |
Implementation Reference
- mcp-server.js:625-668 (handler)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 }; } }
- mcp-server.js:623-624 (schema)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 }; } } );