All files / src/tools orders.ts

86.66% Statements 26/30
62.16% Branches 23/37
90.9% Functions 10/11
86.66% Lines 26/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191              2x 3x     2x 3x     2x 3x     2x 1x   2x 13x                                                                         5x 5x 4x   5x 2x     2x       2x         2x                   1x         13x                         2x 2x   1x                 1x                                   1x         13x                                                                                                    
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import * as z from "zod/v4";
import type { BolClient } from "../bol-client.js";
import type { OrderItem } from "../types.js";
import { toTextResult, toErrorResult } from "../tool-result.js";
 
/** Helper to extract EAN from v10 nested structure or v9 flat field */
const getEan = (item: OrderItem): string =>
  item.product?.ean ?? item.ean ?? "unknown";
 
/** Helper to extract title from v10 nested structure or v9 flat field */
const getTitle = (item: OrderItem): string =>
  item.product?.title ?? item.title ?? "unknown";
 
/** Helper to extract unit price from v10 or v9 field */
const getUnitPrice = (item: OrderItem): number | undefined =>
  item.unitPrice ?? item.offerPrice;
 
/** Helper to extract fulfilment method from v10 nested or v9 flat */
const getFulfilmentMethod = (item: OrderItem): string =>
  item.fulfilment?.method ?? item.fulfilmentMethod ?? "unknown";
 
export const registerOrderTools = (server: McpServer, client: BolClient): void => {
  server.registerTool(
    "list_orders",
    {
      title: "List Orders",
      description:
        "List recent orders from bol.com. Returns orders with their items, shipping details, and status. " +
        "Use the fulfilmentMethod filter to show only FBR (fulfilled by retailer), FBB (fulfilled by bol.com), or ALL orders. " +
        "Use the status filter to show OPEN (awaiting shipment/cancellation), SHIPPED, or ALL orders.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        page: z.number().int().min(1).default(1).describe("Page number (1-based)."),
        fulfilmentMethod: z
          .enum(["FBR", "FBB", "ALL"])
          .optional()
          .describe("Filter by fulfilment method: FBR (fulfilled by retailer), FBB (fulfilled by bol.com), or ALL."),
        status: z
          .enum(["OPEN", "SHIPPED", "ALL"])
          .optional()
          .describe("Filter by order status: OPEN (needs handling), SHIPPED (shipped), or ALL."),
        changeIntervalMinute: z
          .number()
          .int()
          .max(60)
          .optional()
          .describe("Filter order items by most recent change within this number of minutes."),
        latestChangeDate: z
          .string()
          .optional()
          .describe("Filter on the date of latest change to an order item (up to 3 months history)."),
        vvbOnly: z
          .boolean()
          .optional()
          .describe("Filter to include only VVB orders."),
      }),
    },
    async ({ page, fulfilmentMethod, status, changeIntervalMinute, latestChangeDate, vvbOnly }) => {
      try {
        const response = await client.getOrders(page, fulfilmentMethod, status, changeIntervalMinute, latestChangeDate, vvbOnly);
        const orders = response.orders ?? [];
 
        if (orders.length === 0) {
          return toTextResult("No orders found.");
        }
 
        return toTextResult(
          [
            `Orders (page ${page}): ${orders.length} results`,
            ...orders.map((o) =>
              [
                `  - Order ${o.orderId}`,
                o.orderPlacedDateTime ? `    Placed: ${o.orderPlacedDateTime}` : null,
                `    Items: ${o.orderItems.length}`,
                ...o.orderItems.map((item) =>
                  `      ${getEan(item)} - ${getTitle(item)} x${item.quantity} @ ${getUnitPrice(item) ?? "N/A"}`,
                ),
              ]
                .filter(Boolean)
                .join("\n"),
            ),
          ].join("\n"),
          { orders } as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_order",
    {
      title: "Get Order Details",
      description:
        "Get detailed information about a specific order including order items, shipping details, billing details, and fulfilment status.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        orderId: z.string().min(1).describe("The bol.com order ID."),
      }),
    },
    async ({ orderId }) => {
      try {
        const order = await client.getOrder(orderId);
 
        return toTextResult(
          [
            `Order: ${order.orderId}`,
            order.orderPlacedDateTime ? `Placed: ${order.orderPlacedDateTime}` : null,
            order.shipmentDetails?.firstName
              ? `Ship to: ${order.shipmentDetails.firstName} ${order.shipmentDetails.surname ?? ""}, ${order.shipmentDetails.city ?? ""}`
              : null,
            `Items (${order.orderItems.length}):`,
            ...order.orderItems.map((item) =>
              [
                `  - ${item.orderItemId}: ${getEan(item)}`,
                `    ${getTitle(item)} x${item.quantity} @ ${getUnitPrice(item) ?? "N/A"}`,
                `    Fulfilment: ${getFulfilmentMethod(item)}`,
                item.quantityShipped !== undefined ? `    Shipped: ${item.quantityShipped}` : null,
                item.quantityCancelled !== undefined ? `    Cancelled: ${item.quantityCancelled}` : null,
                item.fulfilment?.latestDeliveryDate ? `    Latest delivery: ${item.fulfilment.latestDeliveryDate}` : null,
                item.fulfilment?.expiryDate ? `    Expiry: ${item.fulfilment.expiryDate}` : null,
              ]
                .filter(Boolean)
                .join("\n"),
            ),
          ]
            .filter(Boolean)
            .join("\n"),
          order as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "cancel_order_item",
    {
      title: "Cancel Order Item",
      description:
        "Cancel an order item by order item ID. Can be used to confirm a customer cancellation request or to cancel an item you cannot fulfil. " +
        "Returns a process status — the cancellation is processed asynchronously. " +
        "Always review the order with get_order before cancelling.",
      annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true },
 
      inputSchema: z.object({
        orderItemId: z.string().min(1).describe("The order item ID to cancel."),
        reasonCode: z
          .enum([
            "OUT_OF_STOCK",
            "REQUESTED_BY_CUSTOMER",
            "BAD_CONDITION",
            "HIGHER_SHIPCOST",
            "INCORRECT_PRICE",
            "NOT_AVAIL_IN_TIME",
            "NO_BOL_GUARANTEE",
            "ORDERED_TWICE",
            "RETAIN_ITEM",
            "TECH_ISSUE",
            "UNFINDABLE_ITEM",
            "OTHER",
          ])
          .describe("The reason for cancellation."),
      }),
    },
    async ({ orderItemId, reasonCode }) => {
      try {
        const result = await client.cancelOrderItems({
          orderItems: [{ orderItemId, reasonCode }],
        });
 
        return toTextResult(
          [
            `Cancellation initiated for order item ${orderItemId}`,
            `Reason: ${reasonCode}`,
            `Process status: ${result.processStatusId} (${result.status})`,
          ].join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
};