import json
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("json")

@mcp.tool()
def format_json(text: str, indent: int = 2, sort_keys: bool = False, compress: bool = False) -> str:
    """美化或压缩校验 JSON 字符串。"""
    try:
        data = json.loads(text)
    except json.JSONDecodeError as e:
        return f"JSON 解析失败: {e}"
        
    if compress:
        return json.dumps(data, separators=(',', ':'), sort_keys=sort_keys, ensure_ascii=False)
    else:
        return json.dumps(data, indent=indent, sort_keys=sort_keys, ensure_ascii=False)

def main():
    mcp.run()

if __name__ == "__main__":
    main()
