UNPKG

it-tools-mcp

Version:

MCP-compliant server access to over 100 IT tools and utilities commonly used by developers, system administrators, and IT professionals.

80 lines (75 loc) 2.77 kB
import { z } from "zod"; export function registerEnergyConverter(server) { server.registerTool("convert_energy", { inputSchema: { value: z.number().describe("Energy value to convert"), fromUnit: z.enum([ "joule", "kilojoule", "calorie", "kilocalorie", "btu", "watt-hour", "kilowatt-hour", "electronvolt", "foot-pound" ]).describe("Source energy unit"), toUnit: z.enum([ "joule", "kilojoule", "calorie", "kilocalorie", "btu", "watt-hour", "kilowatt-hour", "electronvolt", "foot-pound" ]).describe("Target energy unit") }, // VS Code compliance annotations annotations: { title: "Convert Energy", readOnlyHint: false } }, async ({ value, fromUnit, toUnit }) => { try { // Conversion factors to joules const toJoules = { "joule": 1, "kilojoule": 1000, "calorie": 4.184, "kilocalorie": 4184, "btu": 1055.06, "watt-hour": 3600, "kilowatt-hour": 3600000, "electronvolt": 1.602176634e-19, "foot-pound": 1.355818 }; // Convert to joules first const joules = value * toJoules[fromUnit]; // Convert from joules to target unit const result = joules / toJoules[toUnit]; const units = { "joule": "J", "kilojoule": "kJ", "calorie": "cal", "kilocalorie": "kcal", "btu": "BTU", "watt-hour": "Wh", "kilowatt-hour": "kWh", "electronvolt": "eV", "foot-pound": "ft·lb" }; return { content: [{ type: "text", text: `Energy Conversion Results: ${value} ${units[fromUnit]} = ${result.toExponential(6)} ${units[toUnit]} Formatted Results: • Scientific: ${result.toExponential(6)} ${units[toUnit]} • Fixed: ${result.toFixed(6)} ${units[toUnit]} • Compact: ${result.toPrecision(6)} ${units[toUnit]} Intermediate (Joules): ${joules.toExponential(6)} J Common Energy Equivalents: • 1 kWh = 3.6 MJ = 860 kcal • 1 BTU = 1055 J = 252 cal • 1 eV = 1.602 × 10⁻¹⁹ J` }] }; } catch (error) { return { content: [{ type: "text", text: `Error converting energy: ${error instanceof Error ? error.message : 'Unknown error'}` }] }; } }); }