All files / src server.ts

100% Statements 25/25
90% Branches 9/10
100% Functions 3/3
100% Lines 23/23

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              1x 1x       1x   1x 7x   10x 5x   5x 10x 7x       5x         1x           1x       8x         8x 8x   8x 18x   18x 18x 18x 18x         8x    
import { createRequire } from "node:module";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { KvkClient } from "./kvk-client.js";
import { registerSearchTools } from "./tools/search.js";
import { registerProfileTools } from "./tools/profiles.js";
import { registerMutationTools } from "./tools/mutations.js";
 
const require = createRequire(import.meta.url);
const { version } = require("../package.json") as { version: string };
 
export type Toolset = "search" | "profiles" | "mutations";
 
const ALL_TOOLSETS: Toolset[] = ["search", "profiles", "mutations"];
 
export const parseToolsets = (env?: string): Set<Toolset> => {
  if (!env) return new Set(ALL_TOOLSETS);
 
  const requested = env.split(",").map((s) => s.trim().toLowerCase());
  const valid = new Set<Toolset>();
 
  for (const name of requested) {
    if (ALL_TOOLSETS.includes(name as Toolset)) {
      valid.add(name as Toolset);
    }
  }
 
  return valid.size > 0 ? valid : new Set(ALL_TOOLSETS);
};
 
type ToolRegisterer = (server: McpServer, client: KvkClient) => void;
 
const toolsetRegistry: Record<Toolset, ToolRegisterer[]> = {
  search: [registerSearchTools],
  profiles: [registerProfileTools],
  mutations: [registerMutationTools],
};
 
export const createServer = (
  client: KvkClient,
  toolsets?: Set<Toolset>,
): McpServer => {
  const server = new McpServer({
    name: "kvk-mcp",
    version,
  });
 
  const enabled = toolsets ?? new Set(ALL_TOOLSETS);
  const registered = new Set<ToolRegisterer>();
 
  for (const toolset of enabled) {
    const registerers = toolsetRegistry[toolset];
 
    for (const register of registerers) {
      Eif (!registered.has(register)) {
        registered.add(register);
        register(server, client);
      }
    }
  }
 
  return server;
};