All files / src/tools dns-records.ts

100% Statements 71/71
100% Branches 52/52
100% Functions 5/5
100% Lines 47/47

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              3x 30x                                             4x 4x   4x                   4x   4x                           1x         30x                                                                                                 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x     3x   2x       1x         30x                                             2x 2x 2x   1x       1x          
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import * as z from "zod/v4";
import type { SpaceshipClient } from "../spaceship-client.js";
import type { DnsRecord } from "../types.js";
import { normalizeDomain, summarizeByType, extractComparableFields } from "../dns-utils.js";
import { toTextResult, toErrorResult } from "../tool-result.js";
 
export const registerDnsRecordTools = (server: McpServer, client: SpaceshipClient): void => {
  server.registerTool(
    "list_dns_records",
    {
      title: "List DNS Records",
      description:
        "List all DNS records for a domain. Uses pagination and can fetch all pages automatically.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        domain: z.string().min(4).max(255).describe("Domain name, e.g. example.com"),
        fetchAll: z.boolean().default(true).describe("Fetch all pages (recommended)."),
        take: z
          .number()
          .int()
          .min(1)
          .max(500)
          .default(500)
          .describe("Items per page when fetchAll=false."),
        skip: z.number().int().min(0).default(0).describe("Offset when fetchAll=false."),
        orderBy: z.enum(["type", "-type", "name", "-name"]).optional(),
      }),
    },
    async ({ domain, fetchAll, take, skip, orderBy }) => {
      try {
        const normalizedDomain = normalizeDomain(domain);
 
        const records = fetchAll
          ? await client.listAllDnsRecords(normalizedDomain, orderBy)
          : (
              await client.listDnsRecords(normalizedDomain, {
                take,
                skip,
                ...(orderBy ? { orderBy } : {}),
              })
            ).items;
 
        const summary = summarizeByType(records);
 
        return toTextResult(
          [
            `Domain: ${normalizedDomain}`,
            `Records returned: ${records.length}`,
            `By type: ${JSON.stringify(summary)}`,
          ].join("\n"),
          {
            domain: normalizedDomain,
            count: records.length,
            byType: summary,
            items: records.map(extractComparableFields),
          },
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "save_dns_records",
    {
      title: "Save DNS Records",
      description:
        "Save (upsert) DNS records for a domain using PUT with force overwrite. " +
        "WARNING: All existing records matching the same name AND type combination will be completely replaced — not merged. Other record name/type combinations are preserved. " +
        "Use this for bulk operations with mixed record types. For single records, prefer the type-specific tools (create_a_record, create_cname_record, etc.). " +
        "Always confirm with the user before calling this tool and use list_dns_records first to check existing records.",
      annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        domain: z.string().min(4).max(255).describe("The domain name"),
        records: z
          .array(
            z.object({
              name: z.string().describe("Record name (subdomain, use @ for root)"),
              type: z.string().describe("Record type (A, AAAA, ALIAS, CAA, CNAME, HTTPS, MX, NS, PTR, SRV, SVCB, TLSA, TXT)"),
              ttl: z.number().int().min(60).max(86400).default(3600).describe("TTL in seconds"),
              value: z.string().optional().describe("Generic value (TXT, CAA value, or fallback)"),
              address: z.string().optional().describe("IPv4/IPv6 address (A, AAAA)"),
              aliasName: z.string().optional().describe("Alias target (ALIAS)"),
              flag: z.number().int().optional().describe("Flag 0 or 128 (CAA)"),
              tag: z.string().optional().describe('Tag e.g. "issue", "issuewild", "iodef" (CAA)'),
              cname: z.string().optional().describe("Canonical name (CNAME)"),
              svcPriority: z.number().int().optional().describe("Service priority 0-65535 (HTTPS, SVCB)"),
              targetName: z.string().optional().describe("Target name (HTTPS, SVCB)"),
              svcParams: z.string().optional().describe("SvcParams string (HTTPS, SVCB)"),
              exchange: z.string().optional().describe("Mail server hostname (MX)"),
              preference: z.number().int().optional().describe("MX preference 0-65535"),
              nameserver: z.string().optional().describe("Nameserver hostname (NS)"),
              pointer: z.string().optional().describe("Pointer target (PTR)"),
              service: z.string().optional().describe("Service label e.g. _sip (SRV)"),
              protocol: z.string().optional().describe("Protocol label e.g. _tcp (SRV, TLSA)"),
              priority: z.number().int().optional().describe("Priority 0-65535 (SRV)"),
              weight: z.number().int().optional().describe("Weight 0-65535 (SRV)"),
              port: z.union([z.number().int(), z.string()]).optional().describe("Port number (SRV) or port string like _443 (HTTPS, SVCB, TLSA)"),
              target: z.string().optional().describe("Target hostname (SRV)"),
              scheme: z.string().optional().describe("Scheme e.g. _https, _tcp (HTTPS, SVCB, TLSA)"),
              usage: z.number().int().optional().describe("Usage 0-255 (TLSA)"),
              selector: z.number().int().optional().describe("Selector 0-255 (TLSA)"),
              matching: z.number().int().optional().describe("Matching type 0-255 (TLSA)"),
              associationData: z.string().optional().describe("Certificate association hex data (TLSA)"),
            }),
          )
          .min(1),
      }),
    },
    async ({ domain, records }) => {
      try {
        const normalizedDomain = normalizeDomain(domain);
        const dnsRecords: DnsRecord[] = records.map((r) => {
          const record: DnsRecord = { name: r.name, type: r.type, ttl: r.ttl };
          if (r.value !== undefined) record.value = r.value;
          if (r.address !== undefined) record.address = r.address;
          if (r.aliasName !== undefined) record.aliasName = r.aliasName;
          if (r.flag !== undefined) record.flag = r.flag;
          if (r.tag !== undefined) record.tag = r.tag;
          if (r.cname !== undefined) record.cname = r.cname;
          if (r.svcPriority !== undefined) record.svcPriority = r.svcPriority;
          if (r.targetName !== undefined) record.targetName = r.targetName;
          if (r.svcParams !== undefined) record.svcParams = r.svcParams;
          if (r.exchange !== undefined) record.exchange = r.exchange;
          if (r.preference !== undefined) record.preference = r.preference;
          if (r.nameserver !== undefined) record.nameserver = r.nameserver;
          if (r.pointer !== undefined) record.pointer = r.pointer;
          if (r.service !== undefined) record.service = r.service;
          if (r.protocol !== undefined) record.protocol = r.protocol;
          if (r.priority !== undefined) record.priority = r.priority;
          if (r.weight !== undefined) record.weight = r.weight;
          if (r.port !== undefined) record.port = r.port;
          if (r.target !== undefined) record.target = r.target;
          if (r.scheme !== undefined) record.scheme = r.scheme;
          if (r.usage !== undefined) record.usage = r.usage;
          if (r.selector !== undefined) record.selector = r.selector;
          if (r.matching !== undefined) record.matching = r.matching;
          if (r.associationData !== undefined) record.associationData = r.associationData;
          return record;
        });
 
        await client.saveDnsRecords(normalizedDomain, dnsRecords);
 
        return toTextResult(
          `Successfully created ${records.length} DNS record(s) for ${normalizedDomain}`,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "delete_dns_records",
    {
      title: "Delete DNS Records",
      description:
        "Delete specific DNS records from a domain by name and type. Only records matching both the name AND type will be removed. Other records are not affected. " +
        "WARNING: This permanently removes DNS records, which can break services relying on them. " +
        "Always confirm with the user before calling this tool and use list_dns_records first to verify which records will be deleted.",
      annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        domain: z.string().min(4).max(255).describe("The domain name"),
        records: z
          .array(
            z.object({
              name: z.string().describe("Record name (subdomain)"),
              type: z.string().describe("Record type (A, AAAA, CNAME, MX, TXT, SRV, etc.)"),
            }),
          )
          .min(1),
      }),
    },
    async ({ domain, records }) => {
      try {
        const normalizedDomain = normalizeDomain(domain);
        await client.deleteDnsRecords(normalizedDomain, records);
 
        return toTextResult(
          `Successfully deleted ${records.length} DNS record(s) from ${normalizedDomain}`,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
};