byte_convert
Convert byte units between B, KB, MB, GB, TB, and PB using binary (1024) or SI (1000) standards for data size calculations.
Instructions
Convert between byte units (B, KB, MB, GB, TB, PB). Supports both binary (1024) and SI (1000) standards.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| value | Yes | The numeric value to convert | |
| from_unit | Yes | Source unit | |
| binary | No | Use binary (1024) instead of SI (1000) standard (default: true) |
Implementation Reference
- src/tools/converters.ts:277-314 (handler)The "byte_convert" tool is defined and implemented directly using `server.tool` inside `registerConverterTools`. The implementation calculates the base (1024 vs 1000), normalizes the input value to bytes, and converts it to all other units.
server.tool( "byte_convert", "Convert between byte units (B, KB, MB, GB, TB, PB). Supports both binary (1024) and SI (1000) standards.", { value: z.number().describe("The numeric value to convert"), from_unit: z .enum(["B", "KB", "MB", "GB", "TB", "PB"]) .describe("Source unit"), binary: z .boolean() .default(true) .describe("Use binary (1024) instead of SI (1000) standard (default: true)"), }, async ({ value, from_unit, binary }) => { const base = binary ? 1024 : 1000; const units = ["B", "KB", "MB", "GB", "TB", "PB"]; const fromIndex = units.indexOf(from_unit); const bytes = value * Math.pow(base, fromIndex); const result: Record<string, string> = { standard: binary ? "Binary (1024)" : "SI (1000)", }; for (let i = 0; i < units.length; i++) { const converted = bytes / Math.pow(base, i); result[units[i]] = converted >= 1 ? converted.toLocaleString("en-US", { maximumFractionDigits: 4 }) : converted.toExponential(4); } return { content: [ { type: "text" as const, text: JSON.stringify(result, null, 2) }, ], }; } );