Skip to main content
Glama
boldcommerce

Magento 2 MCP Server

by boldcommerce

get_product_by_id

Retrieve detailed product information from a Magento 2 store by specifying the product ID to access inventory data, pricing, and specifications.

Instructions

Get detailed information about a product by its ID

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
idYesThe ID of the product

Implementation Reference

  • mcp-server.js:671-725 (registration)
    Registration of the 'get_product_by_id' tool using server.tool(), including description, input schema, and handler function.
    // Tool: Get product by ID
    server.tool(
      "get_product_by_id",
      "Get detailed information about a product by its ID",
      {
        id: z.number().describe("The ID of the product")
      },
      async ({ id }) => {
        try {
          // First we need to search for the product by ID to get its SKU
          const searchCriteria = `searchCriteria[filter_groups][0][filters][0][field]=entity_id&` +
                                `searchCriteria[filter_groups][0][filters][0][value]=${id}&` +
                                `searchCriteria[filter_groups][0][filters][0][condition_type]=eq`;
          
          const searchResults = await callMagentoApi(`/products?${searchCriteria}`);
          
          if (!searchResults.items || searchResults.items.length === 0) {
            return {
              content: [
                {
                  type: "text",
                  text: `No product found with ID: ${id}`
                }
              ]
            };
          }
          
          // Get the SKU from the search results
          const sku = searchResults.items[0].sku;
          
          // Now get the full product details using the SKU
          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
          };
        }
      }
    );
  • The handler function executes the tool logic: searches products by entity_id to retrieve the SKU, fetches full product details by SKU using Magento API, formats the product data, and returns it as JSON text content.
    async ({ id }) => {
      try {
        // First we need to search for the product by ID to get its SKU
        const searchCriteria = `searchCriteria[filter_groups][0][filters][0][field]=entity_id&` +
                              `searchCriteria[filter_groups][0][filters][0][value]=${id}&` +
                              `searchCriteria[filter_groups][0][filters][0][condition_type]=eq`;
        
        const searchResults = await callMagentoApi(`/products?${searchCriteria}`);
        
        if (!searchResults.items || searchResults.items.length === 0) {
          return {
            content: [
              {
                type: "text",
                text: `No product found with ID: ${id}`
              }
            ]
          };
        }
        
        // Get the SKU from the search results
        const sku = searchResults.items[0].sku;
        
        // Now get the full product details using the SKU
        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
        };
      }
  • Input schema using Zod: requires a numeric 'id' parameter for the product ID.
    {
      id: z.number().describe("The ID of the product")
    },
  • Helper function to format product data from Magento API response into a readable structure with flattened 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
      };
    }

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