All files / src/tools sellerhub.ts

100% Statements 58/58
100% Branches 44/44
100% Functions 15/15
100% Lines 57/57

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            3x 9x   3x 6x   3x         3x 44x                             3x 3x 2x 1x     3x           1x 1x     1x         1x         44x                                           4x 4x 4x                 3x 4x         1x         44x                       4x 4x 4x 3x                           1x         44x                                       3x 3x 3x               2x         1x         44x                           2x 2x 2x 1x   1x         44x                                 4x 4x 4x         3x                   1x         44x                       3x 3x 2x   2x 1x     1x     1x   2x           1x          
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import * as z from "zod/v4";
import type { SpaceshipClient } from "../spaceship-client.js";
import { normalizeDomain } from "../dns-utils.js";
import { toTextResult, toErrorResult } from "../tool-result.js";
 
const formatPrice = (price?: { amount: string; currency: string }): string =>
  price ? `${price.currency} ${price.amount}` : "not set";
 
const formatDomain = (d: { name: string; status?: string; binPrice?: { amount: string; currency: string }; minPrice?: { amount: string; currency: string } }): string =>
  `${d.name}${d.status ? ` [${d.status}]` : ""}${d.binPrice ? ` BIN: ${formatPrice(d.binPrice)}` : ""}${d.minPrice ? ` min: ${formatPrice(d.minPrice)}` : ""}`;
 
const SellerHubPriceSchema = z.object({
  amount: z.string().describe('Price amount as a string (e.g. "9999")'),
  currency: z.string().describe('Currency code (e.g. "USD")'),
});
 
export const registerSellerHubTools = (server: McpServer, client: SpaceshipClient): void => {
  server.registerTool(
    "list_sellerhub_domains",
    {
      title: "List SellerHub Domains",
      description:
        "List domains listed for sale on SellerHub, Spaceship's domain marketplace for selling domains.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        fetchAll: z.boolean().default(true).describe("Fetch all pages (recommended)."),
        take: z.number().int().min(1).max(100).default(100).describe("Items per page when fetchAll=false."),
        skip: z.number().int().min(0).default(0).describe("Offset when fetchAll=false."),
      }),
    },
    async ({ fetchAll, take, skip }) => {
      try {
        if (fetchAll) {
          const domains = await client.listAllSellerHubDomains();
          return toTextResult(
            [
              `Total SellerHub domains: ${domains.length}`,
              ...domains.map((d) => `  - ${formatDomain(d)}`),
            ].join("\n"),
            { count: domains.length, domains },
          );
        }
 
        const response = await client.listSellerHubDomains({ take, skip });
        return toTextResult(
          [
            `Total: ${response.total}, showing ${response.items.length} (skip: ${skip})`,
            ...response.items.map((d) => `  - ${formatDomain(d)}`),
          ].join("\n"),
          { total: response.total, items: response.items } as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "create_sellerhub_domain",
    {
      title: "Create SellerHub Listing",
      description:
        "List a domain for sale on the SellerHub marketplace. " +
        "This makes the domain publicly visible as available for purchase. The domain starts in 'verifying' status. " +
        "You can optionally set pricing at creation time, or use update_sellerhub_domain later. " +
        "Always confirm with the user before listing — verify the domain name.",
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true },
 
      inputSchema: z.object({
        domain: z.string().min(4).max(255).describe("The domain name to list for sale."),
        displayName: z.string().optional().describe("Display name for the listing."),
        description: z.string().optional().describe("Description for the listing."),
        binPriceEnabled: z.boolean().optional().describe("Enable the buy-it-now price."),
        binPrice: SellerHubPriceSchema.optional().describe('Buy-it-now price (e.g. { amount: "9999", currency: "USD" }).'),
        minPriceEnabled: z.boolean().optional().describe("Enable the minimum offer price."),
        minPrice: SellerHubPriceSchema.optional().describe('Minimum offer price (e.g. { amount: "100", currency: "USD" }).'),
      }),
    },
    async ({ domain, displayName, description, binPriceEnabled, binPrice, minPriceEnabled, minPrice }) => {
      try {
        const normalizedDomain = normalizeDomain(domain);
        const result = await client.createSellerHubDomain({
          name: normalizedDomain,
          ...(displayName !== undefined ? { displayName } : {}),
          ...(description !== undefined ? { description } : {}),
          ...(binPriceEnabled !== undefined ? { binPriceEnabled } : {}),
          ...(binPrice !== undefined ? { binPrice } : {}),
          ...(minPriceEnabled !== undefined ? { minPriceEnabled } : {}),
          ...(minPrice !== undefined ? { minPrice } : {}),
        });
        const hasPricing = binPrice !== undefined || minPrice !== undefined;
        return toTextResult(
          `Listed ${result.name} on SellerHub [${result.status ?? "pending"}].${hasPricing ? "" : " Use update_sellerhub_domain to set pricing."}`,
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_sellerhub_domain",
    {
      title: "Get SellerHub Domain",
      description: "Get details of a SellerHub listing by domain name.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        domain: z.string().min(4).max(255).describe("The domain name of the SellerHub listing."),
      }),
    },
    async ({ domain }) => {
      try {
        const normalizedDomain = normalizeDomain(domain);
        const result = await client.getSellerHubDomain(normalizedDomain);
        return toTextResult(
          [
            `Domain: ${result.name}`,
            result.displayName ? `Display name: ${result.displayName}` : null,
            result.status ? `Status: ${result.status}` : null,
            result.description ? `Description: ${result.description}` : null,
            `BIN price: ${formatPrice(result.binPrice)} (${result.binPriceEnabled ? "enabled" : "disabled"})`,
            `Min price: ${formatPrice(result.minPrice)} (${result.minPriceEnabled ? "enabled" : "disabled"})`,
          ]
            .filter(Boolean)
            .join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "update_sellerhub_domain",
    {
      title: "Update SellerHub Listing",
      description:
        "Update settings for a SellerHub domain listing. You can update description, pricing, and price toggles. " +
        "Prices use { amount: string, currency: string } format.",
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true },
 
      inputSchema: z.object({
        domain: z.string().min(4).max(255).describe("The domain name of the SellerHub listing."),
        displayName: z.string().optional().describe("Display name for the listing."),
        description: z.string().optional().describe("Description for the listing."),
        binPriceEnabled: z.boolean().optional().describe("Enable or disable the buy-it-now price."),
        binPrice: SellerHubPriceSchema.optional().describe('Buy-it-now price (e.g. { amount: "9999", currency: "USD" }).'),
        minPriceEnabled: z.boolean().optional().describe("Enable or disable the minimum offer price."),
        minPrice: SellerHubPriceSchema.optional().describe('Minimum offer price (e.g. { amount: "100", currency: "USD" }).'),
      }),
    },
    async ({ domain, displayName, description, binPriceEnabled, binPrice, minPriceEnabled, minPrice }) => {
      try {
        const normalizedDomain = normalizeDomain(domain);
        const result = await client.updateSellerHubDomain(normalizedDomain, {
          displayName,
          description,
          binPriceEnabled,
          binPrice,
          minPriceEnabled,
          minPrice,
        });
        return toTextResult(
          `Updated SellerHub listing ${result.name}: ${formatDomain(result)}`,
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "delete_sellerhub_domain",
    {
      title: "Delete SellerHub Listing",
      description:
        "Remove a domain from the SellerHub marketplace. This deletes the listing permanently — any existing checkout links will stop working. " +
        "Always confirm with the user before calling this tool.",
      annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true },
 
      inputSchema: z.object({
        domain: z.string().min(4).max(255).describe("The domain name to remove from SellerHub."),
      }),
    },
    async ({ domain }) => {
      try {
        const normalizedDomain = normalizeDomain(domain);
        await client.deleteSellerHubDomain(normalizedDomain);
        return toTextResult(`Removed ${normalizedDomain} from SellerHub`);
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "create_checkout_link",
    {
      title: "Create Checkout Link",
      description:
        "Generate a shareable checkout/purchase link for a SellerHub domain listing. " +
        "WARNING: Anyone with this link can initiate a purchase of the domain. Only share with intended buyers. " +
        "Always confirm with the user before generating a checkout link.",
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true },
 
      inputSchema: z.object({
        domain: z.string().min(4).max(255).describe("The domain name to create a checkout link for."),
        type: z.enum(["buyNow"]).describe('Checkout link type. Currently "buyNow" is the supported type.'),
        basePrice: SellerHubPriceSchema.optional().describe('Optional base price override for the checkout link (e.g. { amount: "9999", currency: "USD" }).'),
      }),
    },
    async ({ domain, type, basePrice }) => {
      try {
        const normalizedDomain = normalizeDomain(domain);
        const result = await client.createCheckoutLink({
          type,
          domainName: normalizedDomain,
          ...(basePrice !== undefined ? { basePrice } : {}),
        });
        return toTextResult(
          [
            `Checkout link for ${normalizedDomain}: ${result.url}`,
            result.validTill ? `Valid until: ${result.validTill}` : null,
          ]
            .filter(Boolean)
            .join("\n"),
          result as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
 
  server.registerTool(
    "get_verification_records",
    {
      title: "Get Verification Records",
      description:
        "Get the DNS verification records needed to verify ownership of SellerHub domain listings. " +
        "This returns account-level verification options (not per-domain). Each option contains DNS records to add.",
      annotations: { readOnlyHint: true, openWorldHint: true },
 
      inputSchema: z.object({}),
    },
    async () => {
      try {
        const response = await client.getVerificationRecords();
        const allRecords = response.options.flatMap((opt) => opt.records);
 
        if (allRecords.length === 0) {
          return toTextResult("No verification records found.");
        }
 
        return toTextResult(
          [
            `Verification options (${response.options.length}):`,
            ...response.options.map((opt, i) => [
              `  Option ${i + 1}:`,
              ...opt.records.map((r) => `    - ${r.type} ${r.name} → ${r.value}`),
            ].join("\n")),
          ].join("\n"),
          response as unknown as Record<string, unknown>,
        );
      } catch (error) {
        return toErrorResult(error);
      }
    },
  );
};