Skip to main content
Glama
boldcommerce

Magento 2 MCP Server

by boldcommerce

search_products

Search for products in a Magento 2 store using search criteria like product name or description, with pagination controls for results.

Instructions

Search for products using Magento search criteria

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesSearch query (product name, description, etc.)
page_sizeNoNumber of results per page (default: 10)
current_pageNoPage number (default: 1)

Implementation Reference

  • The main handler function for the search_products tool that constructs Magento API search criteria for product name matching, fetches results, formats them, and handles errors.
    async ({ query, page_size = 10, current_page = 1 }) => { try { // Build search criteria for a simple name search const searchCriteria = `searchCriteria[filter_groups][0][filters][0][field]=name&` + `searchCriteria[filter_groups][0][filters][0][value]=%25${encodeURIComponent(query)}%25&` + `searchCriteria[filter_groups][0][filters][0][condition_type]=like&` + `searchCriteria[pageSize]=${page_size}&` + `searchCriteria[currentPage]=${current_page}`; const productData = await callMagentoApi(`/products?${searchCriteria}`); const formattedResults = formatSearchResults(productData); return { content: [ { type: "text", text: JSON.stringify(formattedResults, null, 2) } ] }; } catch (error) { return { content: [ { type: "text", text: `Error searching products: ${error.message}` } ], isError: true }; } }
  • Zod schema defining input parameters: query (required string), page_size and current_page (optional numbers).
    { query: z.string().describe("Search query (product name, description, etc.)"), page_size: z.number().optional().describe("Number of results per page (default: 10)"), current_page: z.number().optional().describe("Page number (default: 1)") },
  • mcp-server.js:417-457 (registration)
    MCP server.tool registration call that defines the tool name, description, input schema, and handler function.
    server.tool( "search_products", "Search for products using Magento search criteria", { query: z.string().describe("Search query (product name, description, etc.)"), page_size: z.number().optional().describe("Number of results per page (default: 10)"), current_page: z.number().optional().describe("Page number (default: 1)") }, async ({ query, page_size = 10, current_page = 1 }) => { try { // Build search criteria for a simple name search const searchCriteria = `searchCriteria[filter_groups][0][filters][0][field]=name&` + `searchCriteria[filter_groups][0][filters][0][value]=%25${encodeURIComponent(query)}%25&` + `searchCriteria[filter_groups][0][filters][0][condition_type]=like&` + `searchCriteria[pageSize]=${page_size}&` + `searchCriteria[currentPage]=${current_page}`; const productData = await callMagentoApi(`/products?${searchCriteria}`); const formattedResults = formatSearchResults(productData); return { content: [ { type: "text", text: JSON.stringify(formattedResults, null, 2) } ] }; } catch (error) { return { content: [ { type: "text", text: `Error searching products: ${error.message}` } ], isError: true }; } } );
  • Helper function used by the handler to format raw Magento product search results into a simplified structure with essential fields.
    function formatSearchResults(results) { if (!results || !results.items || !Array.isArray(results.items)) { return "No products found"; } return { total_count: results.total_count, items: results.items.map(item => ({ id: item.id, sku: item.sku, name: item.name, price: item.price, status: item.status, type_id: item.type_id })) }; }

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