get_product_by_sku
Retrieve detailed product information from a Magento 2 store using the product's SKU identifier for inventory management, order processing, or catalog updates.
Instructions
Get detailed information about a product by its SKU
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sku | Yes | The SKU (Stock Keeping Unit) of the product |
Implementation Reference
- mcp-server.js:389-413 (handler)The handler function fetches the product data from Magento API using the provided SKU, formats it with formatProduct helper, and returns the JSON stringified response or an error message.async ({ sku }) => { try { const productData = await callMagentoApi(`/products/${sku}`); const formattedProduct = formatProduct(productData); return { content: [ { type: "text", text: JSON.stringify(formattedProduct, null, 2) } ] }; } catch (error) { return { content: [ { type: "text", text: `Error fetching product: ${error.message}` } ], isError: true }; } }
- mcp-server.js:386-388 (schema)Zod schema defining the input parameter 'sku' as a required string.{ sku: z.string().describe("The SKU (Stock Keeping Unit) of the product") },
- mcp-server.js:383-414 (registration)Registration of the 'get_product_by_sku' tool using McpServer's tool method, specifying name, description, input schema, and inline handler function.server.tool( "get_product_by_sku", "Get detailed information about a product by its SKU", { sku: z.string().describe("The SKU (Stock Keeping Unit) of the product") }, async ({ sku }) => { try { const productData = await callMagentoApi(`/products/${sku}`); const formattedProduct = formatProduct(productData); return { content: [ { type: "text", text: JSON.stringify(formattedProduct, null, 2) } ] }; } catch (error) { return { content: [ { type: "text", text: `Error fetching product: ${error.message}` } ], isError: true }; } } );
- mcp-server.js:337-361 (helper)Supporting helper function that formats raw product data from Magento API into a clean, readable object, extracting custom attributes.function formatProduct(product) { if (!product) return "Product not found"; // Extract custom attributes into a more readable format const customAttributes = {}; if (product.custom_attributes && Array.isArray(product.custom_attributes)) { product.custom_attributes.forEach(attr => { customAttributes[attr.attribute_code] = attr.value; }); } return { id: product.id, sku: product.sku, name: product.name, price: product.price, status: product.status, visibility: product.visibility, type_id: product.type_id, created_at: product.created_at, updated_at: product.updated_at, extension_attributes: product.extension_attributes, custom_attributes: customAttributes }; }
- mcp-server.js:309-334 (helper)Core utility helper for making authenticated API requests to the Magento 2 REST API using axios, handling errors and SSL bypass for development.async function callMagentoApi(endpoint, method = 'GET', data = null) { try { const url = `${MAGENTO_BASE_URL}${endpoint}`; const headers = { 'Authorization': `Bearer ${MAGENTO_API_TOKEN}`, 'Content-Type': 'application/json' }; const config = { method, url, headers, data: data ? JSON.stringify(data) : undefined, // Bypass SSL certificate verification for development httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false }) }; const response = await axios(config); return response.data; } catch (error) { console.error('Magento API Error:', error.response?.data || error.message); throw error; } }