虚假数据生成服务

基于Faker.js的MCP服务器,提供多语言虚假数据生成功能,适用于数据库填充、API测试和开发环境。

  • by funsjanssen
  • TypeScript
  • 本地部署
  • 7 星标
  • 更新于
在 GitHub 查看 funsjanssen/faker-mcp

使用说明

Faker MCP服务器

一个模型上下文协议(MCP)服务器,使用Faker.js库提供伪造/模拟数据生成功能。为数据库种子、API测试、演示应用程序和开发环境生成真实的测试数据。

阅读更多关于为什么以及何时在我的 博客文章.

特性

  • 基础数据生成:生成包含姓名、电子邮件、地址和联系信息的真实个人和公司数据
  • 结构化数据集:为复杂的测试场景创建具有引用完整性的多实体数据集
  • 自定义图案:根据特定于域的要求,按照自定义模式(正则表达式、枚举、格式、范围)生成数据
  • 多语言环境支持:生成英语、法语、德语、西班牙语和日语的数据
  • 可复制数据:基于种子的生成,以获得一致的测试数据
  • 高性能:每秒生成1000+条记录
  • 符合MCP协议:与MCP兼容客户端无缝集成

安装

先决条件

  • 系统上已安装Node.js 18+
  • MCP兼容客户端(例如,Claude Desktop、Cline、Cursor或任何MCP客户端)

快速开始

将Faker MCP服务器添加到 mcpServers 章节:

{
  "mcpServers": {
    "faker": {
      "command": "npx",
      "args": ["faker-mcp-server"]
    }
  }
}

看 MCP客户端配置 有关各种MCP客户端的详细设置说明,请参阅第节。

可用工具

Faker MCP服务器提供了四种生成虚假数据的强大工具:

1.生成人

生成真实的个人数据,包括姓名、电子邮件、电话号码和地址。

参数:

  • count (number,可选):要生成的人员记录数(1-10000,默认值:1)
  • locale (字符串,可选):生成数据的区域设置- en, fr, de, es, ja (默认值: en)
  • seed (数字,可选):用于可再生发电的种子
  • includeAddress (boolean,可选):是否包含地址信息(默认值: true)
  • includePhone (布尔值,可选):是否包含电话号码(默认值: true)
  • includeDateOfBirth (布尔值,可选):是否包含出生日期(默认值: false)

示例用法:

Generate 10 fake person records with names, emails, and addresses

示例请求 (MCP协议):

{
  "method": "tools/call",
  "params": {
    "name": "generate-person",
    "arguments": {
      "count": 5,
      "locale": "en",
      "seed": 12345,
      "includeAddress": true,
      "includePhone": true,
      "includeDateOfBirth": false
    }
  }
}

示例输出:

[
  {
    "id": "person_12345_0",
    "firstName": "John",
    "lastName": "Doe",
    "fullName": "John Doe",
    "email": "john.doe@example.com",
    "phone": "+1-555-123-4567",
    "address": {
      "street": "123 Main St",
      "city": "Springfield",
      "state": "IL",
      "postalCode": "62701",
      "country": "United States"
    }
  }
]

2.生成公司

生成真实的公司数据,包括姓名、行业、联系信息和地址。

参数:

  • count (number,可选):要生成的公司记录数(1-10000,默认值:1)
  • locale (字符串,可选):生成数据的区域设置- en, fr, de, es, ja (默认值: en)
  • seed (数字,可选):用于可再生发电的种子
  • includeAddress (boolean,可选):是否包含地址信息(默认值: true)
  • includeWebsite (布尔值,可选):是否包含网站URL(默认值: true)
  • includeFoundedYear (boolean,可选):是否包含成立年份(默认值: false)
  • includeEmployeeCount (布尔值,可选):是否包括员工人数(默认值: false)

示例用法:

Generate 5 company records with seed 54321 for reproducibility

示例请求 (MCP协议):

{
  "method": "tools/call",
  "params": {
    "name": "generate-company",
    "arguments": {
      "count": 3,
      "locale": "en",
      "seed": 54321,
      "includeAddress": true,
      "includeWebsite": true,
      "includeFoundedYear": true,
      "includeEmployeeCount": true
    }
  }
}

示例输出:

[
  {
    "id": "company_54321_0",
    "name": "Acme Corporation",
    "industry": "Technology",
    "email": "contact@acme.example.com",
    "phone": "+1-555-111-2222",
    "website": "https://acme.example.com",
    "address": {
      "street": "100 Tech Blvd",
      "city": "San Francisco",
      "state": "CA",
      "postalCode": "94105",
      "country": "United States"
    },
    "founded": 2010,
    "employeeCount": 250
  }
]

3.生成数据集

生成具有多个实体类型和它们之间的引用完整性的结构化数据集。

参数:

  • schema (object,必填):定义实体和关系的数据集架构
    • entities (对象):实体名称到实体定义的映射
      • count (number):此实体要生成的记录数(1-10000)
      • type (string):实体类型- person, company,或 custom
      • fields (数组,可选):要包含的字段列表(默认为全部)
      • relationships (object,可选):与其他实体的外键关系
        • references (string):父实体的名称
        • type (string):关系类型- one-to-many 或 many-to-many
        • nullable (boolean,可选):外键是否可以为空(默认值: false)
  • locale (字符串,可选):生成数据的区域设置- en, fr, de, es, ja (默认值: en)
  • seed (数字,可选):用于可再生发电的种子

示例用法:

Generate a dataset with 20 users and 100 orders, where each order references a user

示例请求 (MCP协议):

{
  "method": "tools/call",
  "params": {
    "name": "generate-dataset",
    "arguments": {
      "schema": {
        "entities": {
          "users": {
            "count": 10,
            "type": "person",
            "fields": ["id", "fullName", "email", "phone"]
          },
          "orders": {
            "count": 30,
            "type": "custom",
            "fields": ["id", "userId", "productName", "price", "orderDate"],
            "relationships": {
              "userId": {
                "references": "users",
                "type": "one-to-many",
                "nullable": false
              }
            }
          }
        }
      },
      "locale": "en",
      "seed": 99999
    }
  }
}

示例输出:

{
  "users": [
    {
      "id": "user_99999_0",
      "fullName": "John Doe",
      "email": "john.doe@example.com",
      "phone": "+1-555-100-0001"
    },
    {
      "id": "user_99999_1",
      "fullName": "Jane Smith",
      "email": "jane.smith@example.com",
      "phone": "+1-555-100-0002"
    }
  ],
  "orders": [
    {
      "id": "order_99999_0",
      "userId": "user_99999_0",
      "productName": "Laptop",
      "price": 1299.99,
      "orderDate": "2024-03-15"
    },
    {
      "id": "order_99999_1",
      "userId": "user_99999_0",
      "productName": "Mouse",
      "price": 29.99,
      "orderDate": "2024-03-16"
    },
    {
      "id": "order_99999_2",
      "userId": "user_99999_1",
      "productName": "Keyboard",
      "price": 89.99,
      "orderDate": "2024-03-17"
    }
  ]
}

4.生成自定义

按照自定义模式生成数据,包括正则表达式模式、枚举、格式和范围。

参数:

  • count (number,可选):要生成的记录数(1-10000,默认值:1)
  • patterns (object,必填):字段名称到模式定义的映射
    • type (string):图案类型- regex, enum, format,或 range
    • value:模式值(取决于模式类型):
      • regex:正则表达式字符串(例如。, "PRD-[0-9]{4}-[A-Z]{2}")
      • enum:可供选择的字符串值数组(例如。, ["pending", "active", "completed"])
      • format:带有占位符的模板字符串(例如。, "REF-{{year}}-{{random:5}}")
      • range:对象 min 和 max 数值(例如。, {"min": 10, "max": 1000})
  • locale (字符串,可选):生成数据的区域设置-影响基于格式的模式(默认值: en)
  • seed (数字,可选):用于可再生发电的种子

示例用法:

Generate 50 product records with codes matching pattern PRD-####-XX where # is a digit and X is an uppercase letter

示例请求 (MCP协议):

{
  "method": "tools/call",
  "params": {
    "name": "generate-custom",
    "arguments": {
      "count": 5,
      "patterns": {
        "productCode": {
          "type": "regex",
          "value": "PRD-[0-9]{4}-[A-Z]{2}"
        },
        "status": {
          "type": "enum",
          "value": ["pending", "active", "completed", "cancelled"]
        },
        "price": {
          "type": "range",
          "value": { "min": 10, "max": 1000 }
        },
        "reference": {
          "type": "format",
          "value": "REF-{{year}}-{{random:5}}"
        }
      },
      "locale": "en",
      "seed": 11111
    }
  }
}

示例输出:

[
  {
    "id": "custom_11111_0",
    "productCode": "PRD-1234-AB",
    "status": "active",
    "price": 456.78,
    "reference": "REF-2024-A3B5C"
  },
  {
    "id": "custom_11111_1",
    "productCode": "PRD-5678-CD",
    "status": "pending",
    "price": 123.45,
    "reference": "REF-2024-D7E9F"
  }
]

常见用例

数据库种子

生成真实的测试数据以填充开发数据库:

Generate a dataset with 100 users, 500 orders, and 1000 order items with proper relationships, using seed 100

API集成测试

创建具有真实数据结构的测试有效载荷:

Generate 20 user registration payloads with emails, passwords, and profile information

UI演示数据

使用特定于地区的数据构建演示环境:

Generate French locale data: 50 customers with addresses and 200 orders for a demo e-commerce site

性能测试

为负载测试生成大量数据:

Generate 10000 person records for load testing my user import API

最佳实践

1.使用种子进行繁殖

当您需要跨环境的一致测试数据时,请始终指定种子:

Generate 100 users with seed 12345

2.选择合适的地点

将区域设置与您的目标市场相匹配,以获得真实的数据:

Generate 50 companies in German locale (de)

3.批量处理大型请求

对于非常大的数据集,考虑批量生成:

Generate 3000 records with seed 111 (first batch)
Generate 3000 records with seed 222 (second batch)
Generate 3000 records with seed 333 (third batch)

4.仔细定义关系

确保在生成子实体之前生成父实体:

{
  "entities": {
    "users": { "count": 10, "type": "person" },
    "orders": { 
      "count": 50, 
      "type": "custom",
      "relationships": { 
        "userId": { "references": "users", "type": "one-to-many" } 
      }
    }
  }
}

绩效预期

操作 记录 预期时间 内存使用情况
生成人 100 <100ms <5MB
生成人 1000 <1s <50MB
生成人 10000 <10s <100MB
生成公司 100 <100ms <5MB
生成数据集 共1000个 <2s <50MB
生成自定义 1000 <1s <30MB

性能可能因系统资源和模式复杂性而异。


错误处理

服务器遵循MCP标准错误响应格式。常见错误包括:

无效参数 (代码:-32602):

{
  "error": {
    "code": -32602,
    "message": "Invalid count parameter: must be between 1 and 10000",
    "data": {
      "received": 50000,
      "max": 10000
    }
  }
}

不支持的区域设置 (代码:-32001):

{
  "error": {
    "code": -32001,
    "message": "Unsupported locale: zh. Supported locales: en, fr, de, es, ja",
    "data": {
      "received": "zh",
      "supported": ["en", "fr", "de", "es", "ja"]
    }
  }
}

架构验证错误 (代码:-32602):

{
  "error": {
    "code": -32602,
    "message": "Invalid schema: circular dependency detected in relationships",
    "data": {
      "cycle": ["orders", "items", "orders"]
    }
  }
}

MCP客户端配置

克劳德桌面

Claude Desktop是一个AI助手应用程序,支持MCP服务器以实现扩展功能。

配置文件位置:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • 视窗: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

配置:

{
  "mcpServers": {
    "faker": {
      "command": "npx",
      "args": ["faker-mcp-server"]
    }
  }
}

替代方案(使用已安装的全局包):

{
  "mcpServers": {
    "faker": {
      "command": "faker-mcp-server",
      "args": []
    }
  }
}

配置后:重新启动克劳德桌面。您可以通过询问“有哪些MCP工具可用?”来验证连接


Cline(VS代码扩展)

Cline是一个VS Code扩展,它通过MCP支持将AI辅助直接带入您的编辑器。

设置步骤:

  1. 从VS Code Marketplace安装Cline扩展
  2. 打开VS代码设置(JSON)-按 Cmd/Ctrl + Shift + P → “首选项:打开用户设置(JSON)”
  3. 添加MCP服务器配置:
{
  "cline.mcpServers": {
    "faker": {
      "command": "npx",
      "args": ["faker-mcp-server"],
      "transport": "stdio"
    }
  }
}

备选方案(特定于工作空间的配置):

创建或编辑 .vscode/settings.json 在您的项目中:

{
  "cline.mcpServers": {
    "faker": {
      "command": "npx",
      "args": ["faker-mcp-server"],
      "transport": "stdio"
    }
  }
}

配置后:重新加载VS Code窗口或重新启动Cline扩展。


继续(VS代码扩展)

Continue是一个支持MCP的VS code开源AI代码助手。

配置文件位置: ~/.continue/config.json (或特定于工作空间 .continue/config.json)

配置:

{
  "mcpServers": [
    {
      "name": "faker",
      "command": "npx",
      "args": ["faker-mcp-server"],
      "transport": "stdio"
    }
  ]
}

配置后:重新启动Continue扩展或重新加载VS代码。


Zed编辑

Zed是一款内置AI和MCP支持的高性能代码编辑器。

配置文件位置: ~/.config/zed/settings.json

配置:

{
  "context_servers": {
    "faker-mcp-server": {
      "command": "npx",
      "args": ["faker-mcp-server"]
    }
  }
}

配置后:重新启动Zed编辑器。


MCP检查员(开发/测试)

MCP Inspector是一个官方调试工具,用于在开发过程中测试MCP服务器。

用法:

# Install MCP Inspector globally
npm install -g @modelcontextprotocol/inspector

# Run with Faker MCP Server
mcp-inspector npx faker-mcp-server

这将打开一个web界面 http://localhost:5173 您可以在哪里:

  • 发现所有可用工具
  • 具有自定义参数的测试工具调用
  • 查看请求/响应日志
  • 验证MCP协议合规性

自定义MCP客户端(通用集成)

对于上面未列出的任何MCP兼容客户端,请使用以下配置参数:

连接参数:

  • 命令: npx faker-mcp-server (或 faker-mcp-server 如果全局安装)
  • 运输: stdio (标准输入/输出)
  • 协议:MCP(模型上下文协议)
  • 环境:需要Node.js 18+

通用JSON配置:

{
  "command": "npx",
  "args": ["faker-mcp-server"],
  "transport": "stdio",
  "env": {
    "NODE_ENV": "production"
  }
}

使用绝对路径 (当npx不可用时):

{
  "command": "/usr/local/bin/faker-mcp-server",
  "args": [],
  "transport": "stdio"
}

使用自定义节点路径:

{
  "command": "/usr/local/bin/node",
  "args": ["/path/to/node_modules/.bin/faker-mcp-server"],
  "transport": "stdio",
  "env": {
    "NODE_ENV": "production",
    "NODE_OPTIONS": "--max-old-space-size=512"
  }
}

Docker容器

对于容器化环境或CI/CD管道:

Dockerfile:

FROM node:18-alpine
RUN npm install -g faker-mcp-server
CMD ["faker-mcp-server"]

构建并运行:

docker build -t faker-mcp-server .
docker run -i faker-mcp-server

Docker Compose (用于与其他服务集成):

version: '3.8'
services:
  faker-mcp:
    image: node:18-alpine
    command: npx faker-mcp-server
    stdin_open: true
    tty: true

程序化使用(Node.js)

您还可以在Node.js应用程序中以编程方式使用MCP服务器:

import { spawn } from 'child_process';

// Start the MCP server process
const mcpServer = spawn('npx', ['faker-mcp-server'], {
  stdio: ['pipe', 'pipe', 'inherit']
});

// Send MCP request to generate person data
const request = {
  jsonrpc: '2.0',
  id: 1,
  method: 'tools/call',
  params: {
    name: 'generate-person',
    arguments: {
      count: 5,
      locale: 'en',
      seed: 12345
    }
  }
};

mcpServer.stdin.write(JSON.stringify(request) + '\n');

// Read response
mcpServer.stdout.on('data', (data) => {
  const response = JSON.parse(data.toString());
  console.log('Generated data:', response.result);
});

配置故障排除

问题:“找不到命令:faker mcp服务器”

解决方案:

  • 使用 npx faker-mcp-server 而不是 faker-mcp-server
  • 先全局安装: npm install -g faker-mcp-server
  • 使用二进制文件的绝对路径

问题:“MCP服务器连接超时”

解决方案:

  • 验证是否安装了Node.js 18+: node --version
  • 检查服务器是否手动启动: npx faker-mcp-server
  • 查看客户端日志中的特定错误消息
  • 确保没有防火墙/防病毒阻止Node.js进程

问题:“来自服务器的JSON响应无效”

解决方案:

  • 确保运输设置为 stdio (不是 http 或 sse)
  • 检查Node.js版本兼容性(需要18+)
  • 验证没有其他进程正在使用stdio流

平台特定注意事项

macOS:

  • 配置文件通常位于 ~/Library/Application Support/
  • 使用Homebrew for Node.js: brew install node@18

视窗:

  • 配置文件通常位于 %APPDATA%\ 或 %USERPROFILE%\.config\
  • 使用nodejs.org上的Node.js安装程序或 nvm-windows
  • 在JSON路径中使用正斜杠或转义反斜杠

Linux:

  • 配置文件通常位于 ~/.config/
  • 使用nvm进行Node.js版本管理
  • 确保执行权限: chmod +x /path/to/faker-mcp-server

故障排除

“找不到MCP服务器”

原因:服务器安装或配置不正确。

解决方案:

  1. 验证安装: npm list -g faker-mcp-server
  2. 检查MCP客户端配置文件中的命令是否正确
  3. 配置更改后重新启动MCP客户端

“无效的区域设置错误”

原因:请求的区域设置不受支持。

解决方案:使用支持的区域设置之一: en, fr, de, es, ja

“请求大型数据集超时”

原因:生成>5000条记录可能需要几秒钟。

解决方案:

  • 使用较小的批量
  • 耐心等待(10000条记录通常需要<10秒)
  • 如果超时持续存在,请检查内存限制

“数据集中的引用完整性错误”

原因:架构以错误的顺序或循环依赖关系定义关系。

解决方案:

  • 在子实体之前定义父实体
  • 避免循环引用
  • 生成前验证架构

发展

设置

# Clone the repository
git clone <repository-url>
cd faker-mcp

# Install dependencies
npm install

# Run tests
npm test

# Build the project
npm run build

# Run in development mode
npm run dev

脚本

  • npm run build -建设生产项目
  • npm run dev -内置手表模式进行开发
  • npm test -运行一次测试
  • npm run test:watch -在监视模式下运行测试
  • npm run test:coverage -使用覆盖率报告运行测试
  • npm run lint -读取代码
  • npm run lint:fix -梳理并修复问题
  • npm run format -使用Prettier格式化代码
  • npm run typecheck -不发射的类型检查

许可证

麻省理工学院

作者

Funs杨森

贡献

欢迎投稿!请随时提交拉取请求。