JSON验证修复工具

一个用于验证JSON数据是否符合JSON Schema规范并提供修复功能的服务器工具。

  • by BUZDOLAPCI
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 BUZDOLAPCI/json-validate

使用说明

json验证

MCP服务器,用于根据JSON模式验证JSON并修复格式错误/无效的JSON。

特性

  • validate_json:根据JSON模式验证JSON实例(支持draft-07)
  • 解释验证:获取人类可读的解释,并修复验证错误的建议
  • 维修_儿子:尝试修复无效的JSON以匹配模式(保守方法)

安装

npm install
npm run build

用法

STDIO传输(默认)

npm start
# or
node dist/index.js

HTTP传输

npm start -- --transport http --port 3000
# or
TRANSPORT=http PORT=3000 node dist/index.js

Claude桌面配置

添加到您的Claude桌面配置(~/.config/claude/claude_desktop_config.json):

{
  "mcpServers": {
    "json-validate": {
      "command": "node",
      "args": ["/path/to/json-validate/dist/index.js"]
    }
  }
}

工具

validate_json

根据JSON模式验证JSON实例。

输入:

{
  "schema": {
    "type": "object",
    "properties": {
      "name": { "type": "string" },
      "age": { "type": "integer", "minimum": 0 }
    },
    "required": ["name"]
  },
  "instance": {
    "name": "John",
    "age": 30
  }
}

输出(成功):

{
  "ok": true,
  "data": {
    "valid": true,
    "errors": []
  },
  "meta": {
    "source": "json-validate",
    "retrieved_at": "2024-01-15T10:30:00.000Z",
    "pagination": { "next_cursor": null },
    "warnings": []
  }
}

输出(验证错误):

{
  "ok": true,
  "data": {
    "valid": false,
    "errors": [
      {
        "path": "/age",
        "keyword": "minimum",
        "message": "must be >= 0",
        "params": { "limit": 0 },
        "schemaPath": "#/properties/age/minimum"
      }
    ]
  },
  "meta": {
    "source": "json-validate",
    "retrieved_at": "2024-01-15T10:30:00.000Z",
    "pagination": { "next_cursor": null },
    "warnings": []
  }
}

解释验证

获取人类可读的验证错误解释。

输入:

{
  "errors": [
    {
      "path": "",
      "keyword": "required",
      "message": "must have required property 'name'",
      "params": { "missingProperty": "name" },
      "schemaPath": "#/required"
    }
  ]
}

输出:

{
  "ok": true,
  "data": {
    "explanations": [
      {
        "path": "",
        "error": "must have required property 'name'",
        "explanation": "The required property \"name\" is missing at root.",
        "suggestion": "Add the missing property \"name\" with an appropriate value."
      }
    ]
  },
  "meta": {
    "source": "json-validate",
    "retrieved_at": "2024-01-15T10:30:00.000Z",
    "pagination": { "next_cursor": null },
    "warnings": []
  }
}

维修_儿子

尝试修复无效的JSON以匹配架构。

特征:

  • 解析格式错误的JSON(尾随逗号、单引号、无引号键)
  • 应用架构默认值
  • 在以下情况下删除其他属性 additionalProperties: false
  • 安全时的强制类型(例如。, "123" 到 123 对于数字模式)
  • 保守:除非模式要求默认值,否则从不发明未知字段

输入:

{
  "schema": {
    "type": "object",
    "properties": {
      "name": { "type": "string" },
      "age": { "type": "integer" },
      "active": { "type": "boolean", "default": true }
    },
    "additionalProperties": false
  },
  "instance_or_text": "{'name': 'John', 'age': '30', 'extra': 'field',}"
}

输出:

{
  "ok": true,
  "data": {
    "repaired": {
      "name": "John",
      "age": 30,
      "active": true
    },
    "changes": [
      {
        "path": "/extra",
        "action": "removed",
        "from": "field",
        "reason": "Property not allowed by schema (additionalProperties: false)"
      },
      {
        "path": "/age",
        "action": "coerced",
        "from": "30",
        "to": 30,
        "reason": "Coerced string to integer"
      },
      {
        "path": "/active",
        "action": "defaulted",
        "to": true,
        "reason": "Applied schema default value"
      }
    ],
    "parseErrors": [
      "Standard JSON parse failed, attempting repairs...",
      "JSON repaired successfully with syntax fixes"
    ]
  },
  "meta": {
    "source": "json-validate",
    "retrieved_at": "2024-01-15T10:30:00.000Z",
    "pagination": { "next_cursor": null },
    "warnings": []
  }
}

CLI选项

Usage: json-validate [options]

Options:
  -t, --transport <type>  Transport type: 'stdio' or 'http' (default: stdio)
  -p, --port <number>     HTTP server port (default: 3000)
  -H, --host <address>    HTTP server host (default: 127.0.0.1)
  -l, --log-level <level> Log level: 'debug', 'info', 'warn', 'error' (default: info)
  -h, --help              Show this help message

环境变量

变量 描述 默认值
TRANSPORT 运输类型: stdio 或 http stdio
PORT HTTP服务器端口 3000
HOST HTTP服务器主机 127.0.0.1
LOG_LEVEL 日志级别: debug, info, warn, error info

发展

# Install dependencies
npm install

# Run in development mode
npm run dev

# Run tests
npm test

# Run tests in watch mode
npm run test:watch

# Type check
npm run typecheck

# Build
npm run build

响应信封

所有工具均以标准信封格式返回响应:

成功:

{
  "ok": true,
  "data": { ... },
  "meta": {
    "source": "json-validate",
    "retrieved_at": "ISO-8601 timestamp",
    "pagination": { "next_cursor": null },
    "warnings": []
  }
}

错误:

{
  "ok": false,
  "error": {
    "code": "INVALID_INPUT | PARSE_ERROR | INTERNAL_ERROR",
    "message": "Human readable message",
    "details": { ... }
  },
  "meta": {
    "retrieved_at": "ISO-8601 timestamp"
  }
}

许可证

麻省理工学院