Skip to main content
Glama

Inventory Forecast

inventory_forecast
Read-onlyIdempotent

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

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

  • Main handler function for the inventory_forecast tool. Validates the store UUID, loads store/orders/products, and delegates to forecastProduct (single product) or forecastAll (all products) from the forecasting service.
    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);
    }
  • src/index.ts:146-166 (registration)
    Registration of the 'inventory_forecast' tool on the MCP server. Defines input schema (store_id required, product_id optional), title, description, annotations, and the async handler that checks license (ensureProOrReject) before calling getInventoryForecast.
    // ── 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); }
      }
    );
  • Type definition (ForecastResult) used by the inventory_forecast tool. Defines all output fields: 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>;
  • Core forecasting logic for a single product. Computes daily sales from order history, calculates moving average, standard deviation, safety stock (1.65σ × √7 for 95% service level), reorder point, depletion date, suggested reorder quantity, and assigns a risk level (critical/high/medium/low). This is the algorithm behind the forecast.
    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,
      };
    }
  • Batch forecast for all active products in a store. Filters active products, runs forecastProduct on each, then sorts by risk priority (critical → high → medium → low).
    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];
        });
    }
Behavior4/5

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

Annotations already declare the tool read-only and idempotent. The description adds that it uses moving-average sales velocity and returns specific outputs (reorder points, safety stock, suggested reorder quantities), providing useful behavioral context beyond annotations.

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 sentences, front-loading the purpose and method, followed by return values. No extraneous information.

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?

Given the annotations and full schema coverage, the description adequately covers purpose and output. It lacks details on the moving-average window or data recency, but the tool is straightforward.

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?

Schema coverage is 100%, so the description adds no new meaning to the parameters beyond what the schema already provides.

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, distinguishing it from sibling tools like inventory_status or order_anomalies.

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 does not explicitly state when to use this tool versus alternatives, nor does it provide exclusions or prerequisites.

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