All files / src/tools mutations.ts

100% Statements 35/35
81.57% Branches 31/38
100% Functions 5/5
100% Lines 35/35

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            2x 2x                     2x 1x     2x     2x 16x                       4x 4x 3x   4x 2x     1x   4x 2x         2x     1x         1x         16x                                   3x 3x               2x   3x 1x     1x         3x 1x           1x     1x                   1x         16x                           3x 3x   2x         1x          
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import * as z from "zod/v4";
import type { KvkClient } from "../kvk-client.js";
import type { Signaal } from "../types.js";
import { toTextResult, toErrorResult } from "../tool-result.js";
 
const formatSignaal = (s: Signaal): string => {
  const lines = [
    `Signal ID: ${s.signaalId}`,
    `Subscription ID: ${s.abonnementId}`,
    s.kvkNummer ? `KVK number: ${s.kvkNummer}` : null,
    s.vestigingsnummer ? `Vestigingsnummer: ${s.vestigingsnummer}` : null,
    s.type ? `Type: ${s.type}` : null,
    s.registratietijdstip ? `Registration time: ${s.registratietijdstip}` : null,
    s.signaalTijdstip ? `Signal time: ${s.signaalTijdstip}` : null,
    s.omschrijving ? `Description: ${s.omschrijving}` : null,
  ].filter(Boolean);
 
  if (s.details && Object.keys(s.details).length > 0) {
    lines.push(`Details: ${JSON.stringify(s.details)}`);
  }
 
  return lines.join("\n");
};
 
export const registerMutationTools = (server: McpServer, client: KvkClient): void => {
  server.registerTool(
    "list_subscriptions",
    {
      title: "List Mutation Subscriptions",
      description:
        "List all mutation subscriptions (abonnementen) for the KVK Mutatieservice. " +
        "Returns all active subscriptions with their IDs and descriptions.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({}),
    },
    async () => {
      try {
        const response = await client.listAbonnementen();
        const abonnementen = response.abonnementen ?? [];
 
        if (abonnementen.length === 0) {
          return toTextResult("No subscriptions found.");
        }
 
        const lines = [`Found ${abonnementen.length} subscription${abonnementen.length !== 1 ? "s" : ""}:`, ""];
 
        for (const ab of abonnementen) {
          const parts = [
            `ID: ${ab.abonnementId}`,
            ab.naam ? `Name: ${ab.naam}` : null,
            ab.beschrijving ? `Description: ${ab.beschrijving}` : null,
          ].filter(Boolean);
          lines.push(`  - ${parts.join(", ")}`);
        }
 
        return toTextResult(
          lines.join("\n"),
          { abonnementen } as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "list_signals",
    {
      title: "List Mutation Signals",
      description:
        "List mutation signals (signalen) for a specific subscription. " +
        "Returns a paged list of signals indicating changes to companies or locations.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        abonnementId: z.string().describe("The subscription ID to list signals for."),
        vanaf: z.string().optional().describe("Start datetime (ISO 8601) to filter signals from."),
        tot: z.string().optional().describe("End datetime (ISO 8601) to filter signals until."),
        pagina: z.number().int().min(1).default(1).describe("Page number (starts at 1)."),
        aantal: z.number().int().min(10).max(500).default(100).describe("Number of results per page (10-500)."),
      }),
    },
    async ({ abonnementId, vanaf, tot, pagina, aantal }) => {
      try {
        const response = await client.listSignalen({
          abonnementId,
          vanaf,
          tot,
          pagina,
          aantal,
        });
 
        const signalen = response.signalen ?? [];
 
        if (signalen.length === 0) {
          return toTextResult("No signals found for this subscription.");
        }
 
        const lines = [
          `Found ${response.totaal} signal${response.totaal !== 1 ? "s" : ""} (page ${response.pagina}, ${response.aantal} per page)`,
          "",
        ];
 
        for (const s of signalen) {
          const parts = [
            `ID: ${s.signaalId}`,
            s.kvkNummer ? `KVK: ${s.kvkNummer}` : null,
            s.type ? `Type: ${s.type}` : null,
            s.omschrijving ?? null,
          ].filter(Boolean);
          lines.push(`  - ${parts.join(", ")}`);
        }
 
        return toTextResult(
          lines.join("\n"),
          {
            totaal: response.totaal,
            pagina: response.pagina,
            aantal: response.aantal,
            signalen,
          } as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_signal",
    {
      title: "Get Signal Details",
      description:
        "Get the full details of a specific mutation signal (signaal) by subscription ID and signal ID.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        abonnementId: z.string().describe("The subscription ID."),
        signaalId: z.string().describe("The signal ID."),
      }),
    },
    async ({ abonnementId, signaalId }) => {
      try {
        const signaal = await client.getSignaal(abonnementId, signaalId);
 
        return toTextResult(
          formatSignaal(signaal),
          signaal as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
};