UNPKG

it-tools-mcp

Version:

Full MCP 2025-06-18 compliant server with 121+ IT tools, logging, ping, progress tracking, cancellation, and sampling utilities

56 lines (55 loc) 2.07 kB
import { z } from "zod"; export function registerConvertNumberBase(server) { server.registerTool("convert_number_base", { description: "Convert numbers between different bases (binary, octal, decimal, hexadecimal)", inputSchema: { number: z.string().describe("Number to convert"), fromBase: z.number().describe("Source base (2-36)"), toBase: z.number().describe("Target base (2-36)") }, // VS Code compliance annotations annotations: { title: "Convert Number Base", description: "Convert numbers between different bases (binary, octal, decimal, hexadecimal)", readOnlyHint: false } }, async ({ number, fromBase, toBase }) => { try { if (fromBase < 2 || fromBase > 36 || toBase < 2 || toBase > 36) { return { content: [ { type: "text", text: "Base must be between 2 and 36.", }, ], }; } // Parse number from source base to decimal const decimal = parseInt(number, fromBase); if (isNaN(decimal)) { throw new Error("Invalid number for the specified base"); } // Convert decimal to target base const result = decimal.toString(toBase); return { content: [ { type: "text", text: `${number} (base ${fromBase}) = ${result} (base ${toBase})\nDecimal equivalent: ${decimal}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error converting number: ${error instanceof Error ? error.message : 'Unknown error'}` } ] }; } }); }