json_format MCP tool: validate and format JSON in an AI agent
· 6 min read
Get the devtoolbox.cc MCP serverOpen the JSON Editor tool
Paste a broken JSON file into a chat and ask what’s wrong with it, and the model will usually find the problem. Ask it to return the fixed, pretty-printed file and something else happens: it retypes the whole document token by token. On a 300-line file that’s slow, it fills the conversation, and every retyped value is a chance for a small change you won’t spot. An ID of 9007199254740993 that comes back as 9007199254740992 looks identical at a glance.
The devtoolbox.cc MCP server has a tool for this, json_format. It validates, pretty-prints, minifies and sorts JSON with a real parser, on your computer, and the assistant only reads the result. It’s the same code as the JSON formatter on this site, packaged for AI assistants; installation is described on the MCP page.
What json_format does
The tool has three modes:
format(the default) pretty-prints the JSON with 2 or 4 spaces or tabs, and can sort object keys alphabetically at every level.minifyremoves all whitespace, for a payload you want to paste into a config value or a URL.validateonly checks the document. It reports the line and column of the first syntax error, or says the JSON is valid and lists any duplicate keys.
Numbers are kept exactly as they were written. That covers integers beyond 2^53, which JavaScript’s JSON.parse silently rounds, long decimals, and spellings such as 1.10 or 1e3. The tool never changes a value; only whitespace and, if you ask, key order.
Arguments
| Argument | Type | Meaning |
|---|---|---|
json |
string | The JSON text. Use this or path. |
path |
string | A local file to read instead, preferably an absolute path. |
mode |
"format", "minify" or "validate" |
Default "format". |
indent |
"2", "4" or "tab" |
Indentation for format. Default "2". |
sort_keys |
boolean | Sort keys alphabetically, recursively. Default false. |
output_path |
string | Write the result to this file instead of returning it. |
overwrite |
boolean | Allow output_path to replace an existing file. Default false. |
You rarely type these yourself. A request like “pretty-print config.json with sorted keys and save it as config.sorted.json” becomes a call with path, sort_keys and output_path filled in.
Examples
The examples below were run against the real tool, and the outputs are copied from it. They use this file:
{"id":9007199254740993,"status":"paid","customer":{"name":"Anna","email":"anna@example.com"},"total":12.50,"items":[{"sku":"ESP","qty":2}]}
Pretty-printing with sorted keys keeps the big ID and the 12.50 exactly as written:
{ "path": "/home/dev/project/order.json", "sort_keys": true }
{
"customer": {
"email": "anna@example.com",
"name": "Anna"
},
"id": 9007199254740993,
"items": [
{
"qty": 2,
"sku": "ESP"
}
],
"status": "paid",
"total": 12.50
}
The same file as a single line:
{ "path": "/home/dev/project/order.json", "mode": "minify" }
{"id":9007199254740993,"status":"paid","customer":{"name":"Anna","email":"anna@example.com"},"total":12.50,"items":[{"sku":"ESP","qty":2}]}
When the JSON is broken, the tool returns an error with an exact position instead of a guess. This one has a trailing comma after the last item, which JSON doesn’t allow:
{
"name": "api",
"ports": [80, 443],
}
{ "path": "/home/dev/project/broken.json", "mode": "validate" }
Invalid JSON at line 4, column 1: Unexpected character "}", expected a double-quoted property name
Line 4, column 1 is the closing brace: the parser expected another property name after the comma. Duplicate keys are legal JSON but usually a mistake, because only the last value survives. validate lists them:
{ "json": "{\"timeout\": 30, \"retries\": 3, \"timeout\": 5}", "mode": "validate" }
Valid JSON (object).
Duplicate key: $['timeout'] (the last value wins)
Large files stay out of the conversation
Results longer than 200,000 characters are never pushed into the chat. The tool asks for an output_path instead, and with one it writes the file and only reports the size:
{
"path": "/home/dev/project/order.json",
"indent": "4",
"output_path": "/home/dev/project/order.pretty.json"
}
Wrote 246 bytes to /home/dev/project/order.pretty.json
Running the same call again fails instead of replacing the file you just wrote. To overwrite on purpose, add "overwrite": true:
{ "path": "/home/dev/project/order.json", "output_path": "/home/dev/project/order.pretty.json" }
Cannot write /home/dev/project/order.pretty.json: EEXIST: file already exists, open '/home/dev/project/order.pretty.json' (set "overwrite": true to replace it)
This matters for agents that work through a list of files. A mistaken output_path can’t destroy anything unless the call says so explicitly.
Encodings
Files saved by some Windows editors start with a UTF-8 byte order mark, an invisible character that makes many parsers fail on line 1. The tool skips it when reading a file, so such files validate like any other. Files in other encodings, such as UTF-16 or Latin-1, are rejected with a message saying the file isn’t UTF-8, rather than parsed into garbled strings.
Things to ask your assistant
Once the server is installed, plain requests are enough:
- “Is
package-lock.jsonvalid JSON? If not, where’s the error?” - “Format every JSON file in
fixtures/with 2 spaces and sorted keys, writing.formatted.jsoncopies next to them.” - “Minify
payload.jsonso I can put it in an environment variable.” - “Check
settings.jsonfor duplicate keys.”
For a quick look at a single document you don’t need an assistant at all. The JSON editor on this site does the same formatting in the browser, with a tree view and search, and nothing leaves your machine.
Why a tool and not the model
Formatting looks like text manipulation, which language models are good at, but JSON formatting has strict requirements that sampling doesn’t guarantee. A real parser rejects invalid input instead of quietly “fixing” it, never drops or reorders array items, and never changes 0.1 to .1 or 1e3 to 1000. On top of that, the model doesn’t have to read or write the whole file: a 2 MB export costs one short tool result instead of a few hundred thousand tokens in each direction.
The json_format tool runs locally inside the devtoolbox.cc MCP server, which makes no network requests. Your configuration files, API responses and customer exports are never uploaded anywhere. To add it to Claude Desktop, Claude Code, Cursor or VS Code, follow the steps on the MCP install page.