mattrguerra/meridian-orders-mcpmainPrivate on GitHub, published here in full

getSplitShipments.ts

Language TypeScript1.9 KB48 lines

mcp-server/src/tools/getSplitShipments.ts

  1. import { z } from "zod";
  2. import type { ToolModule } from "./types.js";
  3. import { plinthRequest } from "../lib/http.js";
  4. import { PlinthApiError } from "../lib/errors.js";
  5. /**
  6. * get_split_shipments: per-order multi-address shipment breakdown (Plinth
  7. * method `orders.splitShipments`).
  8. *
  9. * Wire notes:
  10. * - The ONLY method whose top level is a bare JSON ARRAY (one entry per
  11. * destination).
  12. * - {"notice":"No match"} for a valid single-address order means "not a
  13. * multi-address order": treat it as an empty result, not a hard error
  14. * (PlinthApiError.raw lets you distinguish).
  15. * - `split_lines` can be an array OR an ordinal-keyed object within one
  16. * response (lib/collections.ts).
  17. * - Destination count is not guaranteed to equal the shipment-entry count on
  18. * orders.fetch for the same order.
  19. */
  20. export const getSplitShipments: ToolModule = {
  21. name: "get_split_shipments",
  22. config: {
  23. title: "Get multi-address shipments",
  24. description:
  25. "Per-order multi-address shipment breakdown, keyed on order_ref. Read-only. Returns 'not multi-address' for single-address orders.",
  26. inputSchema: {
  27. orderId: z.number().int().positive().describe("Raw integer order_ref."),
  28. },
  29. },
  30. async handler(args, ctx) {
  31. const { orderId } = args as { orderId: number };
  32. try {
  33. const body = await plinthRequest(ctx, "orders.splitShipments", { order_ref: orderId });
  34. return { content: [{ type: "text", text: JSON.stringify(body, null, 2) }] };
  35. } catch (err) {
  36. // "No match" here means "not a multi-address order", not a failure.
  37. if (err instanceof PlinthApiError && /no match/i.test(err.message)) {
  38. return {
  39. content: [
  40. { type: "text", text: `Order ${orderId} is not a multi-address order (single-address or no split shipment).` },
  41. ],
  42. };
  43. }
  44. throw err;
  45. }
  46. },
  47. };