All files / src/tools profiles.ts

89.07% Statements 106/119
85.48% Branches 106/124
100% Functions 10/10
88.98% Lines 105/118

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            2x 5x   3x             5x 5x   5x     2x 3x   2x 3x   2x 29x                             3x 3x   2x                 3x 1x 1x 2x       2x 1x 1x 2x       2x         1x         29x                             6x 6x   5x               6x 4x 4x 4x 4x       5x 1x 1x 1x       5x 1x 1x 1x       5x 1x 1x 1x       5x         1x         29x                           7x 7x   6x             7x 3x 3x 3x 3x 2x 2x   2x 1x 1x 2x     1x 1x       3x     6x         1x         29x                             3x 3x   2x             3x 1x 1x 1x 1x       2x 1x 1x 1x       2x         1x         29x                             2x 2x   1x               2x               1x             1x             1x             1x         1x         29x                           2x 2x   1x             1x 1x 1x 3x   3x 3x       1x         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 { Adres, SbiActiviteit, NaamgevingVestiging, VestigingList } from "../types.js";
import { toTextResult, toErrorResult } from "../tool-result.js";
 
const formatAdres = (adres: Adres): string => {
  if (adres.volledigAdres) return adres.volledigAdres;
 
  const parts = [
    adres.straatnaam,
    adres.huisnummer !== undefined ? String(adres.huisnummer) : null,
    adres.huisletter,
    adres.huisnummerToevoeging,
  ].filter(Boolean).join(" ");
 
  const line2 = [adres.postcode, adres.plaats].filter(Boolean).join(" ");
  const line3 = adres.land && adres.land !== "Nederland" ? adres.land : null;
 
  return [parts, line2, line3].filter(Boolean).join(", ");
};
 
const formatActiviteit = (act: SbiActiviteit): string =>
  `${act.sbiCode} - ${act.sbiOmschrijving}${act.indHoofdactiviteit === "Ja" ? " (hoofdactiviteit)" : ""}`;
 
const isCommercieleVestiging = (v: NaamgevingVestiging): v is NaamgevingVestiging & { eersteHandelsnaam?: string; handelsnamen?: Array<{ naam: string; volgorde: number }> } =>
  "eersteHandelsnaam" in v || "handelsnamen" in v;
 
export const registerProfileTools = (server: McpServer, client: KvkClient): void => {
  server.registerTool(
    "get_company_profile",
    {
      title: "Get Company Profile",
      description:
        "Get the basic profile (basisprofiel) for a company by KVK number. " +
        "Returns company name, registration date, total employees, statutory name, trade names, and SBI activities.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        kvkNummer: z.string().regex(/^\d{8}$/).describe("KVK number (8 digits)."),
        geoData: z.boolean().optional().describe("Include BAG geo/cadastral data in address results. Default false."),
      }),
    },
    async ({ kvkNummer, geoData }) => {
      try {
        const profiel = await client.getBasisprofiel(kvkNummer, geoData);
 
        const lines = [
          `Company: ${profiel.naam}`,
          `KVK number: ${profiel.kvkNummer}`,
          profiel.statutaireNaam ? `Statutory name: ${profiel.statutaireNaam}` : null,
          profiel.formeleRegistratiedatum ? `Registered: ${profiel.formeleRegistratiedatum}` : null,
          profiel.totaalWerkzamePersonen !== undefined ? `Total employees: ${profiel.totaalWerkzamePersonen}` : null,
          profiel.indNonMailing !== undefined ? `Non-mailing: ${profiel.indNonMailing}` : null,
        ].filter(Boolean);
 
        if (profiel.handelsnamen && profiel.handelsnamen.length > 0) {
          lines.push("", "Trade names:");
          for (const hn of profiel.handelsnamen) {
            lines.push(`  ${hn.volgorde}. ${hn.naam}`);
          }
        }
 
        if (profiel.sbiActiviteiten && profiel.sbiActiviteiten.length > 0) {
          lines.push("", "SBI Activities:");
          for (const act of profiel.sbiActiviteiten) {
            lines.push(`  - ${formatActiviteit(act)}`);
          }
        }
 
        return toTextResult(
          lines.join("\n"),
          profiel as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_location_profile",
    {
      title: "Get Location Profile",
      description:
        "Get the location profile (vestigingsprofiel) by vestigingsnummer. " +
        "Returns location details including address, trade name, websites, SBI activities, and number of employees.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        vestigingsnummer: z.string().regex(/^\d{12}$/).describe("Vestigingsnummer / location number (12 digits)."),
        geoData: z.boolean().optional().describe("Include BAG geo/cadastral data in address results. Default false."),
      }),
    },
    async ({ vestigingsnummer, geoData }) => {
      try {
        const profiel = await client.getVestigingsprofiel(vestigingsnummer, geoData);
 
        const lines = [
          profiel.eersteHandelsnaam ? `Name: ${profiel.eersteHandelsnaam}` : null,
          `Vestigingsnummer: ${profiel.vestigingsnummer}`,
          `KVK number: ${profiel.kvkNummer}`,
          profiel.indHoofdvestiging !== undefined ? `Main location: ${profiel.indHoofdvestiging}` : null,
          profiel.totaalWerkzamePersonen !== undefined ? `Employees: ${profiel.totaalWerkzamePersonen}` : null,
        ].filter(Boolean);
 
        if (profiel.adressen && profiel.adressen.length > 0) {
          lines.push("", "Addresses:");
          for (const adres of profiel.adressen) {
            const typeLabel = adres.type ? `(${adres.type}) ` : "";
            lines.push(`  - ${typeLabel}${formatAdres(adres)}`);
          }
        }
 
        if (profiel.websites && profiel.websites.length > 0) {
          lines.push("", "Websites:");
          for (const site of profiel.websites) {
            lines.push(`  - ${site}`);
          }
        }
 
        if (profiel.sbiActiviteiten && profiel.sbiActiviteiten.length > 0) {
          lines.push("", "SBI Activities:");
          for (const act of profiel.sbiActiviteiten) {
            lines.push(`  - ${formatActiviteit(act)}`);
          }
        }
 
        if (profiel.handelsnamen && profiel.handelsnamen.length > 0) {
          lines.push("", "Trade names:");
          for (const hn of profiel.handelsnamen) {
            lines.push(`  ${hn.volgorde}. ${hn.naam}`);
          }
        }
 
        return toTextResult(
          lines.join("\n"),
          profiel as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_trade_names",
    {
      title: "Get Trade Names",
      description:
        "Get all trade names (handelsnamen) for a company by KVK number using the Naamgeving API. " +
        "Returns the statutory name and all registered trade names per vestiging (establishment).",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        kvkNummer: z.string().regex(/^\d{8}$/).describe("KVK number (8 digits)."),
      }),
    },
    async ({ kvkNummer }) => {
      try {
        const naamgeving = await client.getNaamgeving(kvkNummer);
 
        const lines = [
          `KVK number: ${naamgeving.kvkNummer}`,
          naamgeving.naam ? `Name: ${naamgeving.naam}` : null,
          naamgeving.statutaireNaam ? `Statutory name: ${naamgeving.statutaireNaam}` : null,
          naamgeving.ookGenoemd ? `Also known as: ${naamgeving.ookGenoemd}` : null,
        ].filter(Boolean);
 
        if (naamgeving.vestigingen && naamgeving.vestigingen.length > 0) {
          lines.push("", "Vestigingen:");
          for (const v of naamgeving.vestigingen) {
            lines.push(`  Vestigingsnummer: ${v.vestigingsnummer}`);
            if (isCommercieleVestiging(v)) {
              Eif (v.eersteHandelsnaam) {
                lines.push(`    First trade name: ${v.eersteHandelsnaam}`);
              }
              if (v.handelsnamen && v.handelsnamen.length > 0) {
                lines.push("    Trade names:");
                for (const hn of v.handelsnamen) {
                  lines.push(`      ${hn.volgorde}. ${hn.naam}`);
                }
              }
            E} else if ("naam" in v && v.naam) {
              lines.push(`    Name: ${v.naam}`);
            }
          }
        } else {
          lines.push("", "No vestigingen with trade names registered.");
        }
 
        return toTextResult(
          lines.join("\n"),
          naamgeving as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_company_owner",
    {
      title: "Get Company Owner",
      description:
        "Get the owner (eigenaar) of a company by KVK number. " +
        "Returns RSIN, legal form (rechtsvorm), addresses, and websites.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        kvkNummer: z.string().regex(/^\d{8}$/).describe("KVK number (8 digits)."),
        geoData: z.boolean().optional().describe("Include BAG geo/cadastral data in address results. Default false."),
      }),
    },
    async ({ kvkNummer, geoData }) => {
      try {
        const eigenaar = await client.getEigenaar(kvkNummer, geoData);
 
        const lines = [
          `KVK number: ${kvkNummer}`,
          eigenaar.rsin ? `RSIN: ${eigenaar.rsin}` : null,
          eigenaar.rechtsvorm ? `Legal form: ${eigenaar.rechtsvorm}` : null,
          eigenaar.uitgebreideRechtsvorm ? `Extended legal form: ${eigenaar.uitgebreideRechtsvorm}` : null,
        ].filter(Boolean);
 
        if (eigenaar.adressen && eigenaar.adressen.length > 0) {
          lines.push("", "Addresses:");
          for (const adres of eigenaar.adressen) {
            const typeLabel = adres.type ? `(${adres.type}) ` : "";
            lines.push(`  - ${typeLabel}${formatAdres(adres)}`);
          }
        }
 
        if (eigenaar.websites && eigenaar.websites.length > 0) {
          lines.push("", "Websites:");
          for (const site of eigenaar.websites) {
            lines.push(`  - ${site}`);
          }
        }
 
        return toTextResult(
          lines.join("\n"),
          eigenaar as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_main_location",
    {
      title: "Get Main Location",
      description:
        "Get the main location (hoofdvestiging) for a company by KVK number. " +
        "Returns location details including address, trade name, websites, SBI activities, and number of employees.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        kvkNummer: z.string().regex(/^\d{8}$/).describe("KVK number (8 digits)."),
        geoData: z.boolean().optional().describe("Include BAG geo/cadastral data in address results. Default false."),
      }),
    },
    async ({ kvkNummer, geoData }) => {
      try {
        const profiel = await client.getHoofdvestiging(kvkNummer, geoData);
 
        const lines = [
          profiel.eersteHandelsnaam ? `Name: ${profiel.eersteHandelsnaam}` : null,
          `Vestigingsnummer: ${profiel.vestigingsnummer}`,
          `KVK number: ${profiel.kvkNummer}`,
          profiel.indHoofdvestiging !== undefined ? `Main location: ${profiel.indHoofdvestiging}` : null,
          profiel.totaalWerkzamePersonen !== undefined ? `Employees: ${profiel.totaalWerkzamePersonen}` : null,
        ].filter(Boolean);
 
        Iif (profiel.adressen && profiel.adressen.length > 0) {
          lines.push("", "Addresses:");
          for (const adres of profiel.adressen) {
            const typeLabel = adres.type ? `(${adres.type}) ` : "";
            lines.push(`  - ${typeLabel}${formatAdres(adres)}`);
          }
        }
 
        Iif (profiel.websites && profiel.websites.length > 0) {
          lines.push("", "Websites:");
          for (const site of profiel.websites) {
            lines.push(`  - ${site}`);
          }
        }
 
        Iif (profiel.sbiActiviteiten && profiel.sbiActiviteiten.length > 0) {
          lines.push("", "SBI Activities:");
          for (const act of profiel.sbiActiviteiten) {
            lines.push(`  - ${formatActiviteit(act)}`);
          }
        }
 
        Iif (profiel.handelsnamen && profiel.handelsnamen.length > 0) {
          lines.push("", "Trade names:");
          for (const hn of profiel.handelsnamen) {
            lines.push(`  ${hn.volgorde}. ${hn.naam}`);
          }
        }
 
        return toTextResult(
          lines.join("\n"),
          profiel as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_company_locations",
    {
      title: "Get Company Locations",
      description:
        "List all locations (vestigingen) for a company by KVK number. " +
        "Returns counts and a list of all commercial and non-commercial locations.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        kvkNummer: z.string().regex(/^\d{8}$/).describe("KVK number (8 digits)."),
      }),
    },
    async ({ kvkNummer }) => {
      try {
        const result = await client.getVestigingen(kvkNummer);
 
        const lines = [
          `KVK number: ${result.kvkNummer}`,
          `Commercial locations: ${result.aantalCommercieleVestigingen}`,
          `Non-commercial locations: ${result.aantalNietCommercieleVestigingen}`,
          `Total locations: ${result.totaalAantalVestigingen}`,
        ];
 
        Eif (result.vestigingen && result.vestigingen.length > 0) {
          lines.push("", "Locations:");
          for (const v of result.vestigingen) {
            const type = v.indHoofdvestiging === "Ja" ? "hoofdvestiging" :
              v.indCommercieleVestiging === "Ja" ? "commercieel" : "niet-commercieel";
            const name = v.eersteHandelsnaam ? ` - ${v.eersteHandelsnaam}` : "";
            lines.push(`  - ${v.vestigingsnummer}${name} (${type})`);
          }
        }
 
        return toTextResult(
          lines.join("\n"),
          result as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
};