All files / src/tools delivery.ts

100% Statements 32/32
70.96% Branches 22/31
100% Functions 3/3
100% Lines 32/32

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          2x           13x                                             3x 3x                         2x       2x 1x     2x         1x         13x                                             4x 4x                         3x 4x   4x 1x     2x   2x 1x 1x 1x 1x 1x 2x 2x         2x 1x 1x 1x 1x       2x         1x          
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import * as z from "zod/v4";
import type { PostNLClient } from "../postnl-client.js";
import { toTextResult, toErrorResult } from "../tool-result.js";
 
export const registerDeliveryTools = (
  server: McpServer,
  client: PostNLClient,
  _customerCode: string,
  _customerNumber: string,
): void => {
  server.registerTool(
    "get_delivery_date",
    {
      title: "Get Delivery Date",
      description:
        "Calculate the expected delivery date for a shipment based on the shipping date, postal code, " +
        "and delivery options. Useful for showing customers when they can expect their parcel.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        shippingDate: z.string().describe("Shipping date in format dd-MM-yyyy HH:mm:ss (e.g. '29-06-2024 14:00:00')"),
        postalCode: z.string().describe("Destination postal code (Dutch format: 1234AB)"),
        countryCode: z.string().length(2).default("NL").describe("Destination country code (ISO 3166-1 alpha-2)"),
        originCountryCode: z.string().length(2).optional().describe("Origin country code (ISO 3166-1 alpha-2)"),
        shippingDuration: z.number().int().optional().describe("Number of days for shipping (default depends on destination)"),
        cutOffTime: z.string().optional().describe("Cut-off time for same-day processing (format: HH:mm:ss)"),
        city: z.string().optional().describe("Destination city"),
        houseNr: z.string().optional().describe("Destination house number"),
        houseNrExt: z.string().optional().describe("House number extension"),
        options: z.array(z.string()).optional().describe("Delivery options: Daytime, Evening, Morning, Noon, Sunday, Sameday"),
      }),
    },
    async ({ shippingDate, postalCode, countryCode, originCountryCode, shippingDuration, cutOffTime, city, houseNr, houseNrExt, options }) => {
      try {
        const result = await client.getDeliveryDate({
          shippingDate,
          postalCode,
          countryCode,
          originCountryCode,
          shippingDuration,
          cutOffTime,
          city,
          houseNr,
          houseNrExt,
          options,
        });
 
        const lines: string[] = [
          `Expected delivery date: ${result.DeliveryDate}`,
        ];
 
        if (result.Options?.string) {
          lines.push(`Options: ${result.Options.string}`);
        }
 
        return toTextResult(
          lines.join("\n"),
          result as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_delivery_options",
    {
      title: "Get Delivery Options",
      description:
        "Get available delivery time windows for a specific address and date range. " +
        "Returns available timeframes including daytime, evening, and morning delivery options.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        startDate: z.string().describe("Start date for the timeframe calculation (format: dd-MM-yyyy)"),
        endDate: z.string().describe("End date for the timeframe calculation (format: dd-MM-yyyy)"),
        postalCode: z.string().describe("Destination postal code (Dutch format: 1234AB)"),
        countryCode: z.string().length(2).default("NL").describe("Destination country code (ISO 3166-1 alpha-2)"),
        houseNumber: z.number().int().optional().describe("Destination house number"),
        houseNrExt: z.string().optional().describe("House number extension"),
        city: z.string().optional().describe("Destination city"),
        street: z.string().optional().describe("Destination street name"),
        allowSundaySorting: z.boolean().optional().describe("Allow Sunday sorting for delivery"),
        options: z.array(z.string()).optional().describe("Delivery options to query: Daytime, Evening, Morning, Noon, Sunday, Sameday"),
      }),
    },
    async ({ startDate, endDate, postalCode, countryCode, houseNumber, houseNrExt, city, street, allowSundaySorting, options }) => {
      try {
        const result = await client.getTimeframes({
          StartDate: startDate,
          EndDate: endDate,
          PostalCode: postalCode,
          CountryCode: countryCode,
          HouseNumber: houseNumber,
          HouseNrExt: houseNrExt,
          City: city,
          Street: street,
          AllowSundaySorting: allowSundaySorting,
          Options: options,
        });
 
        const timeframes = result.Timeframes?.Timeframe ?? [];
        const noTimeframes = result.ReasonNoTimeframes?.ReasonNoTimeframe ?? [];
 
        if (timeframes.length === 0 && noTimeframes.length === 0) {
          return toTextResult("No delivery timeframes available for the specified address and date range.");
        }
 
        const lines: string[] = [];
 
        if (timeframes.length > 0) {
          lines.push("Available delivery timeframes:");
          for (const tf of timeframes) {
            lines.push(`  ${tf.Date ?? "Unknown date"}:`);
            const frames = tf.Timeframes?.TimeframeTimeframe ?? [];
            for (const frame of frames) {
              const opts = frame.Options?.string?.join(", ") ?? "";
              lines.push(`    - ${frame.From ?? "?"} - ${frame.To ?? "?"}${opts ? ` (${opts})` : ""}`);
            }
          }
        }
 
        if (noTimeframes.length > 0) {
          lines.push("");
          lines.push("Dates without timeframes:");
          for (const reason of noTimeframes) {
            lines.push(`  - ${reason.Date ?? "?"}: ${reason.Description ?? reason.Code ?? "Unknown reason"}`);
          }
        }
 
        return toTextResult(
          lines.join("\n"),
          result as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
};