sys_json_valid
Validate JSON strings for correctness and return formatted results, with optional pretty-printing.
Instructions
验证 JSON 字符串是否合法,并返回格式化后的结果。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | 要验证的 JSON 字符串 | |
| pretty | No | 验证通过后是否返回格式化 JSON(默认 true) |
Implementation Reference
- src/onion_mcp_server/tools/system.py:149-167 (registration)Tool schema registration for sys_json_valid within SYSTEM_TOOLS list. Defines name, description, and inputSchema (required 'text' string, optional 'pretty' boolean)
types.Tool( name="sys_json_valid", description="验证 JSON 字符串是否合法,并返回格式化后的结果。", inputSchema={ "type": "object", "properties": { "text": { "type": "string", "description": "要验证的 JSON 字符串", }, "pretty": { "type": "boolean", "description": "验证通过后是否返回格式化 JSON(默认 true)", "default": True, }, }, "required": ["text"], }, ), - Handler function that validates JSON strings. Uses json.loads() and returns detailed error position (line/col/context) on failure, or formatted JSON output on success.
def _sys_json_valid(args: dict) -> list[types.TextContent]: text = args["text"] pretty = bool(args.get("pretty", True)) try: parsed = json.loads(text) except json.JSONDecodeError as e: return [types.TextContent(type="text", text=( f"❌ JSON 格式错误\n\n" f"错误位置: 第 {e.lineno} 行,第 {e.colno} 列\n" f"错误信息: {e.msg}\n" f"上下文: ...{text[max(0,e.pos-20):e.pos+20]}..." ))] if pretty: formatted = json.dumps(parsed, ensure_ascii=False, indent=2) return [types.TextContent(type="text", text=( f"✅ JSON 合法\n\n```json\n{formatted}\n```" ))] type_name = type(parsed).__name__ return [types.TextContent(type="text", text=( f"✅ JSON 合法 类型: {type_name}" ))] - src/onion_mcp_server/tools/system.py:176-187 (registration)Routing map inside handle_system that dispatches 'sys_json_valid' to the _sys_json_valid handler function.
handlers = { "sys_time": _sys_time, "sys_uuid": _sys_uuid, "sys_hash": _sys_hash, "sys_base64": _sys_base64, "sys_url_encode": _sys_url_encode, "sys_json_valid": _sys_json_valid, } fn = handlers.get(name) if fn is None: raise ValueError(f"未知 system 工具: {name}") return fn(arguments) - src/onion_mcp_server/server.py:60-61 (registration)Top-level routing in server.py: maps all SYSTEM_TOOLS (including sys_json_valid) to the handle_system dispatcher.
for _t in SYSTEM_TOOLS: _HANDLERS[_t.name] = handle_system