JSON操作服务器

一个实现Model Context Protocol (MCP)的Node.js服务器,提供JSON文件查询、模式生成和验证功能,支持S3同步和多种传输协议。

  • by berrydev-ai
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 berrydev-ai/json-mcp-server

使用说明

JSON MCP服务器

实现JSON操作的模型上下文协议(MCP)的Node.js服务器。

特性

  • 使用具有复杂过滤器和转换的jq表示法查询JSON文件
  • 从现有JSON数据自动生成JSON模式
  • 验证JSON模式以确保其格式正确
  • S3同步支持远程JSON文件同步
  • 支持stdio和HTTP传输协议

备注:服务器需要 jq 二进制文件将安装在您的系统上,并且只允许对具有绝对路径的文件进行操作。

先决条件

🚨 必需:在您的系统上安装jq二进制文件

macOS:

brew install jq

Linux:

# Ubuntu/Debian
sudo apt-get install jq

# CentOS/RHEL/Fedora
sudo yum install jq  # or sudo dnf install jq

# Arch Linux
sudo pacman -S jq

窗户:

Winget(推荐)


# Winget (recommended)
winget install --id=jqlang.jq

# Chocolatey
choco install jq

# Scoop
scoop instal jq

验证安装:

jq --version
# Should output something like: jq-1.6

API

资源

  • file://json:JSON文件操作接口

工具

  • query_json

    • 使用复杂的过滤器和转换对JSON文件执行jq查询
    • 输入:
      • filePath (字符串,如果设置了默认值,则可选):JSON文件的绝对路径
      • query (string):jq查询表达式
    • 示例查询:
      • "." -返回整个JSON(不推荐)
      • ".users" -获取用户数组
      • ".users[0].name" -获取第一个用户名
      • ".users[] | select(.active == true)" -筛选活跃用户
      • ".[].price | add" -将所有价格相加
  • generate-json_schema

    • 使用genson-js从现有JSON数据自动生成JSON模式
    • 输入: filePath (字符串,如果设置了默认值,则可选):JSON文件的绝对路径
    • 返回:描述数据结构的完整JSON模式
  • validate_json_schema

    • 使用AJV验证JSON模式的格式是否正确
    • 输入:
      • schema (object):要验证的JSON模式对象,OR
      • schemaFilePath (string):包含JSON模式的文件的路径
    • 返回:带有详细反馈的验证结果

使用Claude Desktop

将此添加到您的 claude_desktop_config.json:

NPX(推荐)

标准传输(默认):

{
  "mcpServers": {
    "json-mcp-server": {
      "command": "npx",
      "args": [
        "-y",
        "@berrydev-ai/json-mcp-server",
        "--verbose=true",
        "--file-path=/absolute/path/to/your/data.json"
      ]
    }
  }
}

HTTP传输:

{
  "mcpServers": {
    "json-mcp-server": {
      "command": "npx",
      "args": [
        "-y",
        "@berrydev-ai/json-mcp-server",
        "--transport=http",
        "--port=3000",
        "--verbose=true",
        "--file-path=/absolute/path/to/your/data.json"
      ]
    }
  }
}

地方发展

标准运输:

{
  "mcpServers": {
    "json-mcp-server": {
      "command": "node",
      "args": [
        "/path/to/json-mcp-server/index.js",
        "--verbose=true",
        "--file-path=/absolute/path/to/your/data.json"
      ]
    }
  }
}

HTTP传输:

{
  "mcpServers": {
    "json-mcp-server": {
      "command": "node",
      "args": [
        "/path/to/json-mcp-server/index.js",
        "--transport=http",
        "--port=3000",
        "--host=localhost",
        "--verbose=true",
        "--file-path=/absolute/path/to/your/data.json"
      ]
    }
  }
}

使用S3同步(可选)

{
  "mcpServers": {
    "json-mcp-server": {
      "command": "npx",
      "args": [
        "-y",
        "@berrydev-ai/json-mcp-server",
        "--s3-uri=s3://your-bucket/data.json",
        "--file-path=/absolute/path/to/local-data.json",
        "--aws-region=us-east-1",
        "--verbose=true"
      ],
      "env": {
        "AWS_ACCESS_KEY_ID": "your-access-key-id",
        "AWS_SECRET_ACCESS_KEY": "your-secret-access-key"
      }
    }
  }
}

命令行用法

在本地启动服务器(stdio传输-默认):

npm start                    # Basic start with stdio transport
npm run dev                  # Start with verbose logging and stdio transport
node index.js --verbose=true --file-path=/absolute/path/to/data.json

使用HTTP传输启动服务器:

npm run start:http           # Start HTTP server with verbose logging
npm run dev:http             # Start HTTP server on port 3000 with verbose logging
node index.js --transport=http --verbose=true --port=3000 --host=localhost

运输选项:

--transport=stdio            # Default: stdin/stdout communication
--transport=http             # HTTP server with session management
--port=3000                  # HTTP server port (default: 3000)
--host=localhost             # HTTP server host (default: localhost)
--cors-origin=*              # CORS allowed origins (default: *)

测试:

npm test                     # Run test setup (creates test files and validates jq)
node test.js                 # Same as above

MCP检查员(用于调试):

npm run inspect:local       # Test local development version
npm run inspect:published   # Test published npm package

构建

NPM安装:

npm install -g @berrydev-ai/json-mcp-server

地方发展:

git clone https://github.com/berrydev-ai/json-mcp-server.git
cd json-mcp-server
npm install

依赖项

  • @modelcontextprotocol/sdk:MCP协议实施
  • node-jq:用于jq二进制文件的Node.js包装器(需要安装系统jq)
  • genson-js:JSON模式生成
  • ajv:JSON模式验证
  • commander:命令行参数解析
  • which:二进制路径检测实用程序
  • @aws-sdk/client-s3:用于可选文件同步的AWS S3客户端
  • express:用于HTTP传输的HTTP服务器框架
  • cors:跨源资源共享中间件

JSON数据示例

创建测试文件 test-data.json:

{
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "email": "john@example.com",
      "active": true,
      "roles": ["user", "admin"]
    },
    {
      "id": 2,
      "name": "Jane Smith",
      "email": "jane@example.com",
      "active": false,
      "roles": ["user"]
    }
  ],
  "metadata": {
    "version": "1.0",
    "created_at": "2024-01-01T00:00:00Z"
  }
}

许可证

此MCP服务器根据ISC许可证获得许可。这意味着您可以根据ISC许可证的条款和条件自由使用、修改和分发软件。有关更多详细信息,请参阅项目存储库中的LICENSE文件。