json_format
Format JSON strings with customizable indentation or minify them for cleaner, more readable code.
Instructions
Format (pretty-print) a JSON string with configurable indentation. Can also minify JSON.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | The JSON string to format | |
| indent | No | Number of spaces for indentation (0 = minify, default: 2) |
Implementation Reference
- src/tools/formatters.ts:19-38 (handler)The handler function for 'json_format' which takes the input JSON string and an optional indentation level, parses the input, and returns the formatted (or minified) JSON string.
async ({ input, indent }) => { try { const parsed = JSON.parse(input); const formatted = indent === 0 ? JSON.stringify(parsed) : JSON.stringify(parsed, null, indent); return { content: [{ type: "text" as const, text: formatted }] }; } catch (e) { return { content: [ { type: "text" as const, text: `Error: Invalid JSON — ${e instanceof Error ? e.message : String(e)}`, }, ], isError: true, }; } } - src/tools/formatters.ts:9-18 (schema)Input schema definition using Zod for 'json_format', defining the required input string and optional integer indent level.
{ input: z.string().describe("The JSON string to format"), indent: z .number() .int() .min(0) .max(8) .default(2) .describe("Number of spaces for indentation (0 = minify, default: 2)"), }, - src/tools/formatters.ts:6-39 (registration)Registration of the 'json_format' tool in the MCP server using server.tool.
server.tool( "json_format", "Format (pretty-print) a JSON string with configurable indentation. Can also minify JSON.", { input: z.string().describe("The JSON string to format"), indent: z .number() .int() .min(0) .max(8) .default(2) .describe("Number of spaces for indentation (0 = minify, default: 2)"), }, async ({ input, indent }) => { try { const parsed = JSON.parse(input); const formatted = indent === 0 ? JSON.stringify(parsed) : JSON.stringify(parsed, null, indent); return { content: [{ type: "text" as const, text: formatted }] }; } catch (e) { return { content: [ { type: "text" as const, text: `Error: Invalid JSON — ${e instanceof Error ? e.message : String(e)}`, }, ], isError: true, }; } } );