Skip to main content
Glama

get-product-by-id

Retrieve detailed product information from a Shopify store by specifying the product ID, enabling data access for inventory management or integration workflows.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
productIdYes

Implementation Reference

  • The async execute function that performs the GraphQL query to fetch product details by ID, formats the response with images, variants, collections, and handles errors.
    execute: async (input: GetProductByIdInput) => {
      try {
        const { productId } = input;
    
        const query = gql`
          query GetProductById($id: ID!) {
            product(id: $id) {
              id
              title
              description
              descriptionHtml
              handle
              status
              createdAt
              updatedAt
              totalInventory
              priceRangeV2 {
                minVariantPrice {
                  amount
                  currencyCode
                }
                maxVariantPrice {
                  amount
                  currencyCode
                }
              }
              images(first: 5) {
                edges {
                  node {
                    id
                    url
                    altText
                    width
                    height
                  }
                }
              }
              variants(first: 20) {
                edges {
                  node {
                    id
                    title
                    price
                    inventoryQuantity
                    sku
                    selectedOptions {
                      name
                      value
                    }
                  }
                }
              }
              collections(first: 5) {
                edges {
                  node {
                    id
                    title
                  }
                }
              }
              tags
              vendor
            }
          }
        `;
    
        const variables = {
          id: productId
        };
    
        const data = (await shopifyClient.request(query, variables)) as {
          product: any;
        };
    
        if (!data.product) {
          throw new Error(`Product with ID ${productId} not found`);
        }
    
        // Format product data
        const product = data.product;
    
        // Format variants
        const variants = product.variants.edges.map((variantEdge: any) => ({
          id: variantEdge.node.id,
          title: variantEdge.node.title,
          price: variantEdge.node.price,
          inventoryQuantity: variantEdge.node.inventoryQuantity,
          sku: variantEdge.node.sku,
          options: variantEdge.node.selectedOptions
        }));
    
        // Format images
        const images = product.images.edges.map((imageEdge: any) => ({
          id: imageEdge.node.id,
          url: imageEdge.node.url,
          altText: imageEdge.node.altText,
          width: imageEdge.node.width,
          height: imageEdge.node.height
        }));
    
        // Format collections
        const collections = product.collections.edges.map(
          (collectionEdge: any) => ({
            id: collectionEdge.node.id,
            title: collectionEdge.node.title
          })
        );
    
        /**
         * @typedef {Object} FormattedProduct
         * @property {string} id - The unique GID of the product
         * @property {string} title - The product title, optimized for SEO
         * @property {string} description - The plain text description of the product
         * @property {string} descriptionHtml - The HTML formatted description of the product
         * @property {string} handle - The URL-friendly handle/slug of the product
         * @property {string} status - Current status of the product (e.g., ACTIVE, ARCHIVED)
         * @property {string} createdAt - Product creation timestamp
         * @property {string} updatedAt - Last update timestamp
         * @property {number} totalInventory - Total available inventory across all variants
         */
        const formattedProduct = {
          id: product.id,
          title: product.title,
          description: product.description,
          descriptionHtml: product.descriptionHtml,
          handle: product.handle,
          status: product.status,
          createdAt: product.createdAt,
          updatedAt: product.updatedAt,
          totalInventory: product.totalInventory,
          priceRange: {
            minPrice: {
              amount: product.priceRangeV2.minVariantPrice.amount,
              currencyCode: product.priceRangeV2.minVariantPrice.currencyCode
            },
            maxPrice: {
              amount: product.priceRangeV2.maxVariantPrice.amount,
              currencyCode: product.priceRangeV2.maxVariantPrice.currencyCode
            }
          },
          images,
          variants,
          collections,
          tags: product.tags,
          vendor: product.vendor
        };
    
        return { product: formattedProduct };
      } catch (error) {
        console.error("Error fetching product by ID:", error);
        throw new Error(
          `Failed to fetch product: ${
            error instanceof Error ? error.message : String(error)
          }`
        );
      }
    }
  • Zod schema defining the input for the get-product-by-id tool: productId as string.
    const GetProductByIdInputSchema = z.object({
      productId: z.string().min(1).describe("The GID of the product to fetch (e.g., \"gid://shopify/Product/1234567890\")")
    });
  • src/index.ts:108-119 (registration)
    MCP server registration of the 'get-product-by-id' tool, providing inline input schema and delegating to the imported handler's execute method.
    server.tool(
      "get-product-by-id",
      {
        productId: z.string().min(1)
      },
      async (args) => {
        const result = await getProductById.execute(args);
        return {
          content: [{ type: "text", text: JSON.stringify(result) }]
        };
      }
    );
  • src/index.ts:11-11 (registration)
    Import of the getProductById tool handler from its module.
    import { getProductById } from "./tools/getProductById.js";
  • src/index.ts:70-70 (registration)
    Initialization of the tool with the Shopify GraphQL client.
    getProductById.initialize(shopifyClient);
Behavior1/5

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

Tool has no description.

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

Conciseness1/5

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

Tool has no description.

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

Completeness1/5

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

Tool has no description.

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

Parameters1/5

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

Tool has no description.

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

Purpose1/5

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

Tool has no description.

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

Usage Guidelines1/5

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

Tool has no description.

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/luckyfarnon/Shopify-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server