All files / src/tools offers.ts

71.18% Statements 42/59
52.5% Branches 21/40
61.53% Functions 8/13
70.68% Lines 41/58

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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391          2x         2x       2x         2x                     2x                       2x 19x                         2x 2x   1x   2x                                     1x         19x                                           3x 3x                       2x                       1x         19x                                       2x 2x               1x               1x         19x                           2x 2x   1x               1x         19x                             2x 2x   1x               1x         19x                               2x 2x   1x               1x         19x                                                                   19x                                                   19x                                                               19x                                                    
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import * as z from "zod/v4";
import type { BolClient } from "../bol-client.js";
import { toTextResult, toErrorResult } from "../tool-result.js";
 
const BundlePriceSchema = z.object({
  quantity: z.number().int().min(1).max(24).describe("Minimum quantity for this price tier."),
  unitPrice: z.number().min(1).max(9999).describe("Unit price for this quantity tier."),
});
 
const PricingSchema = z.object({
  bundlePrices: z.array(BundlePriceSchema).min(1).max(4).describe("Price tiers (max 4). At minimum one entry with quantity 1."),
});
 
const StockSchema = z.object({
  amount: z.number().int().min(0).max(999).describe("Stock amount."),
  managedByRetailer: z.boolean().describe("Whether stock is managed by the retailer."),
});
 
const ConditionSchema = z.object({
  name: z
    .enum(["NEW", "AS_NEW", "GOOD", "REASONABLE", "MODERATE"])
    .describe("Condition of the product."),
  category: z
    .enum(["NEW", "SECONDHAND"])
    .optional()
    .describe("Condition category."),
  comment: z.string().max(2000).optional().describe("Comment about the condition. Only allowed if name is not NEW."),
});
 
const FulfilmentSchema = z.object({
  method: z.enum(["FBR", "FBB"]).describe("FBR (fulfilled by retailer) or FBB (fulfilled by bol.com)."),
  deliveryCode: z
    .enum([
      "24uurs-23", "24uurs-22", "24uurs-21", "24uurs-20", "24uurs-19", "24uurs-18",
      "24uurs-17", "24uurs-16", "24uurs-15", "24uurs-14", "24uurs-13", "24uurs-12",
      "1-2d", "2-3d", "3-5d", "4-8d", "1-8d", "MijnLeverbelofte", "VVB",
    ])
    .optional()
    .describe("Delivery promise code. Only used with FBR."),
});
 
export const registerOfferTools = (server: McpServer, client: BolClient): void => {
  server.registerTool(
    "get_offer",
    {
      title: "Get Offer Details",
      description:
        "Get detailed information about a specific offer including EAN, pricing, stock, fulfilment method, and condition.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        offerId: z.string().min(1).describe("The bol.com offer ID."),
      }),
    },
    async ({ offerId }) => {
      try {
        const offer = await client.getOffer(offerId);
 
        const mainPrice = offer.pricing?.bundlePrices?.find((p) => p.quantity === 1);
 
        return toTextResult(
          [
            `Offer: ${offer.offerId}`,
            `EAN: ${offer.ean}`,
            offer.reference ? `Reference: ${offer.reference}` : null,
            offer.condition ? `Condition: ${offer.condition.name}${offer.condition.category ? ` (${offer.condition.category})` : ""}` : null,
            mainPrice ? `Price: ${mainPrice.unitPrice}` : null,
            `Stock: ${offer.stock.amount} (managed by retailer: ${offer.stock.managedByRetailer})`,
            `Fulfilment: ${offer.fulfilment.method}${offer.fulfilment.deliveryCode ? ` (${offer.fulfilment.deliveryCode})` : ""}`,
            `On hold: ${offer.onHoldByRetailer}`,
            offer.notPublishableReasons && offer.notPublishableReasons.length > 0
              ? `Not publishable: ${offer.notPublishableReasons.map((r) => r.description).join(", ")}`
              : null,
          ]
            .filter(Boolean)
            .join("\n"),
          offer as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "create_offer",
    {
      title: "Create Offer",
      description:
        "Create a new offer on bol.com. Requires EAN, condition, pricing (at least one bundle price with quantity 1), stock, and fulfilment method. " +
        "Returns a process status — the offer is created asynchronously. Use get_process_status to check completion.",
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true },
 
      inputSchema: z.object({
        ean: z.string().min(8).max(13).describe("EAN (European Article Number) barcode of the product."),
        condition: ConditionSchema.describe("Product condition."),
        reference: z.string().max(100).optional().describe("Your internal reference for this offer (max 100 chars)."),
        onHoldByRetailer: z.boolean().optional().describe("Put the offer on hold (not visible on bol.com)."),
        unknownProductTitle: z.string().max(500).optional().describe("Title for products not yet known by bol.com."),
        economicOperatorId: z.string().optional().describe("Identifier referring to the Economic Operator entity for EU compliance."),
        pricing: PricingSchema.describe("Pricing with bundle prices."),
        stock: StockSchema.describe("Stock information."),
        fulfilment: FulfilmentSchema.describe("Fulfilment method and delivery code."),
      }),
    },
    async ({ ean, condition, reference, onHoldByRetailer, unknownProductTitle, economicOperatorId, pricing, stock, fulfilment }) => {
      try {
        const result = await client.createOffer({
          ean,
          condition,
          ...(reference !== undefined ? { reference } : {}),
          ...(onHoldByRetailer !== undefined ? { onHoldByRetailer } : {}),
          ...(unknownProductTitle !== undefined ? { unknownProductTitle } : {}),
          ...(economicOperatorId !== undefined ? { economicOperatorId } : {}),
          pricing,
          stock,
          fulfilment,
        });
 
        return toTextResult(
          [
            `Offer creation initiated for EAN ${ean}`,
            `Process status: ${result.processStatusId} (${result.status})`,
            result.entityId ? `Entity ID: ${result.entityId}` : null,
            "Use get_process_status to check when the offer is created.",
          ]
            .filter(Boolean)
            .join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "update_offer",
    {
      title: "Update Offer",
      description:
        "Update an existing offer's reference, on-hold status, product title, or fulfilment settings. " +
        "To update pricing or stock, use update_offer_price or update_offer_stock instead. " +
        "Returns a process status — changes are applied asynchronously.",
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        offerId: z.string().min(1).describe("The bol.com offer ID to update."),
        reference: z.string().max(100).optional().describe("Your internal reference for this offer (max 100 chars)."),
        onHoldByRetailer: z.boolean().optional().describe("Put the offer on hold (not visible on bol.com)."),
        unknownProductTitle: z.string().max(500).optional().describe("Title for products not yet known by bol.com."),
        economicOperatorId: z.string().optional().describe("Identifier referring to the Economic Operator entity for EU compliance."),
        fulfilment: FulfilmentSchema.describe("Fulfilment method and delivery code."),
      }),
    },
    async ({ offerId, reference, onHoldByRetailer, unknownProductTitle, economicOperatorId, fulfilment }) => {
      try {
        const result = await client.updateOffer(offerId, {
          ...(reference !== undefined ? { reference } : {}),
          ...(onHoldByRetailer !== undefined ? { onHoldByRetailer } : {}),
          ...(unknownProductTitle !== undefined ? { unknownProductTitle } : {}),
          ...(economicOperatorId !== undefined ? { economicOperatorId } : {}),
          fulfilment,
        });
 
        return toTextResult(
          [
            `Offer update initiated for ${offerId}`,
            `Process status: ${result.processStatusId} (${result.status})`,
          ].join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "delete_offer",
    {
      title: "Delete Offer",
      description:
        "Delete an offer from bol.com. This permanently removes the offer — it cannot be undone. " +
        "Always confirm with the user before calling this tool.",
      annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true },
 
      inputSchema: z.object({
        offerId: z.string().min(1).describe("The bol.com offer ID to delete."),
      }),
    },
    async ({ offerId }) => {
      try {
        const result = await client.deleteOffer(offerId);
 
        return toTextResult(
          [
            `Offer deletion initiated for ${offerId}`,
            `Process status: ${result.processStatusId} (${result.status})`,
          ].join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "update_offer_price",
    {
      title: "Update Offer Price",
      description:
        "Update the pricing for an existing offer. Provide bundle prices with at least one entry for quantity 1. " +
        "Returns a process status — changes are applied asynchronously.",
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        offerId: z.string().min(1).describe("The bol.com offer ID."),
        pricing: PricingSchema.describe("New pricing with bundle prices."),
      }),
    },
    async ({ offerId, pricing }) => {
      try {
        const result = await client.updateOfferPrice(offerId, { pricing });
 
        return toTextResult(
          [
            `Price update initiated for offer ${offerId}`,
            `Process status: ${result.processStatusId} (${result.status})`,
          ].join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "update_offer_stock",
    {
      title: "Update Offer Stock",
      description:
        "Update the stock level for an existing offer. " +
        "Returns a process status — changes are applied asynchronously.",
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        offerId: z.string().min(1).describe("The bol.com offer ID."),
        amount: z.number().int().min(0).max(999).describe("New stock amount."),
        managedByRetailer: z.boolean().describe("Whether stock is managed by the retailer."),
      }),
    },
    async ({ offerId, amount, managedByRetailer }) => {
      try {
        const result = await client.updateOfferStock(offerId, { amount, managedByRetailer });
 
        return toTextResult(
          [
            `Stock update initiated for offer ${offerId}: ${amount} units`,
            `Process status: ${result.processStatusId} (${result.status})`,
          ].join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "request_offer_export",
    {
      title: "Request Offer Export",
      description:
        "Request an offer export file containing all offers. " +
        "Returns a process status — use get_process_status to get the report ID, then retrieve with get_offer_export.",
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true },
 
      inputSchema: z.object({
        format: z.enum(["CSV"]).default("CSV").describe("The file format for the export."),
      }),
    },
    async ({ format }) => {
      try {
        const result = await client.requestOfferExport({ format });
 
        return toTextResult(
          [
            `Offer export requested (${format})`,
            `Process status: ${result.processStatusId} (${result.status})`,
            result.entityId ? `Report ID: ${result.entityId}` : null,
            "Use get_process_status to check completion, then get_offer_export to retrieve.",
          ]
            .filter(Boolean)
            .join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_offer_export",
    {
      title: "Get Offer Export",
      description:
        "Retrieve an offer export file by report ID. The report ID is obtained from the process status after requesting an export.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        reportId: z.string().min(1).describe("The report ID from the offer export request."),
      }),
    },
    async ({ reportId }) => {
      try {
        const data = await client.getOfferExport(reportId);
 
        return toTextResult(
          typeof data === "string" ? `Offer export retrieved (${reportId}).` : JSON.stringify(data),
          { reportId, data } as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "request_unpublished_offer_report",
    {
      title: "Request Unpublished Offer Report",
      description:
        "Request a report of all unpublished offers and reasons. " +
        "Returns a process status — use get_process_status to get the report ID, then retrieve with get_unpublished_offer_report.",
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true },
 
      inputSchema: z.object({}),
    },
    async () => {
      try {
        const result = await client.requestUnpublishedOfferReport();
 
        return toTextResult(
          [
            `Unpublished offer report requested`,
            `Process status: ${result.processStatusId} (${result.status})`,
            result.entityId ? `Report ID: ${result.entityId}` : null,
            "Use get_process_status to check completion, then get_unpublished_offer_report to retrieve.",
          ]
            .filter(Boolean)
            .join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_unpublished_offer_report",
    {
      title: "Get Unpublished Offer Report",
      description:
        "Retrieve an unpublished offer report by report ID. Contains all unpublished offers and reasons.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        reportId: z.string().min(1).describe("The report ID from the unpublished offer report request."),
      }),
    },
    async ({ reportId }) => {
      try {
        const data = await client.getUnpublishedOfferReport(reportId);
 
        return toTextResult(
          typeof data === "string" ? `Unpublished offer report retrieved (${reportId}).` : JSON.stringify(data),
          { reportId, data } as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
};