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
      };
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It states it's a read operation ('Get'), but doesn't disclose behavioral traits such as error handling (e.g., what happens if the ID doesn't exist), authentication needs, rate limits, or response format. For a tool with no annotation coverage, this is a significant gap in transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core purpose without unnecessary words. Every part ('Get detailed information', 'about a product', 'by its ID') contributes essential information, making it appropriately sized and structured.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no annotations, no output schema, and a simple input schema, the description is incomplete. It doesn't explain what 'detailed information' includes in the response, error conditions, or other behavioral aspects. For a tool that likely returns complex product data, more context is needed to guide the agent effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, with the parameter 'id' fully documented in the schema. The description adds no additional meaning beyond what the schema provides (e.g., no examples or constraints on ID format). Baseline 3 is appropriate as the schema handles parameter documentation adequately.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Get detailed information') and target resource ('about a product'), with the specific mechanism 'by its ID'. It distinguishes from siblings like 'get_product_by_sku' (which uses SKU) and 'search_products' (which is broader). However, it doesn't specify what 'detailed information' includes, which could be more precise.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage when you have a product ID and need detailed info, distinguishing it from siblings that use other identifiers (e.g., SKU) or perform searches. However, it lacks explicit guidance on when to use this versus alternatives like 'get_product_attributes' or 'get_related_products', and no exclusions or prerequisites are mentioned.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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