Skip to main content
Glama

Inventory Forecast

inventory_forecast
Read-onlyIdempotent

Use moving-average sales velocity to forecast stock depletion dates, providing reorder points, safety stock levels, and recommended reorder quantities to prevent stockouts.

Instructions

Predict stock depletion dates using moving-average sales velocity. Returns reorder points, safety stock levels, and suggested reorder quantities for each product.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
store_idYesUUID of a connected store (returned by store_connect with action="connect" or visible in store_connect with action="list" / the store_overview resource)
product_idNoRestrict the forecast to a single product by its external_id (Shopify product ID or WooCommerce product slug). Omit to forecast every active product in the store.

Implementation Reference

  • src/index.ts:146-166 (registration)
    Registration of the 'inventory_forecast' tool on the MCP server using server.registerTool(), with inputSchema (store_id UUID required, product_id optional), description, and annotations.
    // ── Tool: inventory_forecast ──────────────────────────────────────
    server.registerTool(
      'inventory_forecast',
      {
        title: 'Inventory Forecast',
        description: 'Predict stock depletion dates using moving-average sales velocity. Returns reorder points, safety stock levels, and suggested reorder quantities for each product.',
        inputSchema: z.object({
          store_id: z.string().uuid().describe('UUID of a connected store (returned by store_connect with action="connect" or visible in store_connect with action="list" / the store_overview resource)'),
          product_id: z.string().optional().describe('Restrict the forecast to a single product by its external_id (Shopify product ID or WooCommerce product slug). Omit to forecast every active product in the store.'),
        }),
        annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
      },
      async ({ store_id, product_id }) => {
        try {
          const reject = await ensureProOrReject(LICENSE_CONFIG, 'inventory_forecast');
          if (reject) return reject;
          const result = await getInventoryForecast(store_id, product_id);
          return { content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }] };
        } catch (e) { return handleToolError(e); }
      }
    );
  • Handler function getInventoryForecast that validates the store, checks for existence, retrieves orders, and delegates to forecastProduct (single product) or forecastAll (all active products).
    export async function getInventoryForecast(storeId: string, productId?: string): Promise<ForecastResult[]> {
      validateUUID(storeId, 'store');
      const store = await storage.getStoreById(storeId);
      if (!store) throw new NotFoundError('Store', storeId);
    
      const orders = await storage.getOrders(storeId);
    
      if (productId) {
        const product = await storage.getProductById(productId);
        if (!product) throw new NotFoundError('Product', productId);
        return [forecastProduct(product, orders)];
      }
    
      const products = await storage.getProducts(storeId);
      return forecastAll(products, orders);
    }
  • Helper function forecastProduct that computes moving-average sales velocity, safety stock (1.65*σ*√leadTime), reorder point, days of stock, depletion date, suggested reorder qty, and risk level for a single product.
    export function forecastProduct(product: Product, orders: Order[], daysBack = 30): ForecastResult {
      const dailySales = computeDailySales(product.id, orders, daysBack);
      const quantities = dailySales.map((d) => d.quantity);
    
      const avgDaily = movingAverage(quantities, daysBack);
      const salesStdDev = stdDev(quantities);
    
      // Safety stock: 1.65 × σ × √leadTime (95% service level, 7-day lead time)
      const leadTimeDays = 7;
      const safetyStock = Math.ceil(1.65 * salesStdDev * Math.sqrt(leadTimeDays));
    
      // Reorder point: (avg daily sales × lead time) + safety stock
      const reorderPoint = Math.ceil(avgDaily * leadTimeDays + safetyStock);
    
      // Days of stock remaining
      const currentStock = product.inventory_quantity;
      const daysOfStock = avgDaily > 0 ? currentStock / avgDaily : null;
    
      // Depletion date
      let depletionDate: string | null = null;
      if (daysOfStock !== null && Number.isFinite(daysOfStock)) {
        depletionDate = new Date(Date.now() + daysOfStock * MS_PER_DAY).toISOString().slice(0, 10);
      }
    
      // Suggested reorder quantity: 30 days of avg sales + safety stock - current stock
      const targetStock = Math.ceil(avgDaily * 30 + safetyStock);
      const suggestedReorder = Math.max(0, targetStock - currentStock);
    
      // Risk level
      let riskLevel: 'low' | 'medium' | 'high' | 'critical';
      let detail: string;
    
      if (currentStock <= 0) {
        riskLevel = 'critical';
        detail = 'Out of stock — immediate restock needed';
      } else if (daysOfStock !== null && daysOfStock <= 3) {
        riskLevel = 'critical';
        detail = `Only ${Math.round(daysOfStock)} day(s) of stock remaining`;
      } else if (daysOfStock !== null && daysOfStock <= 7) {
        riskLevel = 'high';
        detail = `${Math.round(daysOfStock)} days of stock — below lead time threshold`;
      } else if (currentStock <= reorderPoint) {
        riskLevel = 'medium';
        detail = `Stock at or below reorder point (${reorderPoint} units)`;
      } else if (daysOfStock !== null && daysOfStock <= 14) {
        riskLevel = 'medium';
        detail = `${Math.round(daysOfStock)} days of stock — approaching reorder point`;
      } else {
        riskLevel = 'low';
        detail = daysOfStock !== null
          ? `${Math.round(daysOfStock)} days of stock remaining`
          : 'No recent sales data — unable to forecast depletion';
      }
    
      return {
        product_id: product.id,
        product_title: product.title,
        sku: product.sku,
        current_stock: currentStock,
        avg_daily_sales: Math.round(avgDaily * 100) / 100,
        days_of_stock: daysOfStock !== null ? Math.round(daysOfStock) : null,
        depletion_date: depletionDate,
        reorder_point: reorderPoint,
        suggested_reorder_qty: suggestedReorder,
        safety_stock: safetyStock,
        risk_level: riskLevel,
        detail,
      };
    }
    
    /**
     * Forecast inventory for all products in a store.
     */
    export function forecastAll(products: Product[], orders: Order[], daysBack = 30): ForecastResult[] {
      return products
        .filter((p) => p.status === 'active')
        .map((p) => forecastProduct(p, orders, daysBack))
        .sort((a, b) => {
          const riskOrder = { critical: 0, high: 1, medium: 2, low: 3 };
          return riskOrder[a.risk_level] - riskOrder[b.risk_level];
        });
    }
  • Helper function forecastAll that filters active products, calls forecastProduct for each, and sorts by risk level (critical first).
    export function forecastAll(products: Product[], orders: Order[], daysBack = 30): ForecastResult[] {
      return products
        .filter((p) => p.status === 'active')
        .map((p) => forecastProduct(p, orders, daysBack))
        .sort((a, b) => {
          const riskOrder = { critical: 0, high: 1, medium: 2, low: 3 };
          return riskOrder[a.risk_level] - riskOrder[b.risk_level];
        });
    }
  • ForecastResultSchema Zod schema defining the forecast output shape: product_id, product_title, sku, current_stock, avg_daily_sales, days_of_stock, depletion_date, reorder_point, suggested_reorder_qty, safety_stock, risk_level, and detail.
    // ── Inventory Forecast ────────────────────────────────────────────
    export const ForecastResultSchema = z.object({
      product_id: z.string(),
      product_title: z.string(),
      sku: z.string().nullable(),
      current_stock: z.number().int(),
      avg_daily_sales: z.number(),
      days_of_stock: z.number().nullable(),
      depletion_date: z.string().nullable(),
      reorder_point: z.number().int(),
      suggested_reorder_qty: z.number().int(),
      safety_stock: z.number().int(),
      risk_level: z.enum(['low', 'medium', 'high', 'critical']),
      detail: z.string(),
    });
    export type ForecastResult = z.infer<typeof ForecastResultSchema>;
Behavior4/5

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

Annotations declare the tool as read-only, idempotent, and non-destructive. The description adds behavioral context beyond annotations by specifying the use of moving-average sales velocity and listing the specific outputs, which aligns with the read-only nature.

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 two concise sentences that are front-loaded with the core action and outputs. Every word adds value with no redundancy.

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

Completeness4/5

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

For a forecasting tool with no output schema, the description explains the return values well. However, it could be more complete by mentioning the time horizon or data assumptions. Given the sibling context (inventory_status for current state), it is adequately informative.

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?

With 100% schema description coverage, the baseline is 3. The description does not add semantic meaning beyond what is in the schema; it only repeats that store_id is required and product_id optional. No additional parameter guidance is provided.

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

Purpose5/5

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

The description clearly states the tool predicts stock depletion dates using moving-average sales velocity and specifies the returned metrics (reorder points, safety stock, suggested reorder quantities). It is distinct from siblings like inventory_status which likely shows current inventory.

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 the tool is for forecasting but provides no explicit guidance on when to use it over siblings like inventory_status or pricing_analyze. The required store_id parameter suggests a prerequisite store connection, but no alternative tools 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/enzoemir1/shopops-mcp'

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