SimpleSQL MCP 服务器
一个模型上下文协议(MCP)服务器,为各种数据库系统提供SQL数据库操作。该服务器使AI助手能够在多个数据库后端执行增删改查(CRUD)操作、创建表以及填充数据。
特点/特性
- 多数据库支持MySQL、PostgreSQL、SQLite 及更多
- CRUD操作在任何表中创建、读取、更新、删除数据
- 表创建动态创建具有自定义模式的表
- 数据播种生成并插入测试用的虚拟数据
- 连接测试用于验证数据库连接的ping工具
支持的数据库
这个MCP服务器通过Knex.js支持以下数据库:
- MySQL (通过
mysql2驾驶员) - PostgreSQL(译文:波斯特格瑞斯) (通过
pg司机) - SQLite (通过
sqlite3并且better-sqlite3(驱动程序) - MySQL (通过遗留方式
mysql司机)
安装
- 克隆仓库:
git clone
cd SimpleSQL- 安装依赖项:
npm install- 构建项目:
npm run build配置
环境变量
# Database Configuration
DB_TYPE=mysql # Database type: mysql, mysql2, pg, sqlite3, better-sqlite3
DB_HOST=localhost # Database host (not needed for SQLite)
DB_PORT=3306 # Database port (not needed for SQLite)
DB_USER=root # Database username (not needed for SQLite)
DB_PASSWORD= # Database password (not needed for SQLite)
DB_NAME=simple_sql_db # Database name / SQLite file path
SSL=false # Enable SSL connection (true/false)数据库特定配置示例
MySQL 配置
DB_TYPE=mysql2
DB_HOST=localhost
DB_PORT=3306
DB_USER=your_username
DB_PASSWORD=your_password
DB_NAME=your_database
SSL=falsePostgreSQL 配置
DB_TYPE=pg
DB_HOST=localhost
DB_PORT=5432
DB_USER=your_username
DB_PASSWORD=your_password
DB_NAME=your_database
SSL=falseSQLite 配置
DB_TYPE=sqlite3
DB_NAME=./database.sqlite
# Note: HOST, PORT, USER, PASSWORD, and SSL are not needed for SQLite更优的 SQLite3 配置(推荐用于 SQLite)
DB_TYPE=better-sqlite3
DB_NAME=./database.sqliteMCP服务器设置
与Claude Desktop配合使用
将此服务器添加到您的Claude Desktop配置文件中:
Windows: %APPDATA%\Claude\claude_desktop_config.json macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"simplesql": {
"command": "node",
"args": ["path/to/SimpleSQL/build/index.js"],
"env": {
"DB_TYPE": "mysql2",
"DB_HOST": "localhost",
"DB_PORT": "3306",
"DB_USER": "your_username",
"DB_PASSWORD": "your_password",
"DB_NAME": "your_database",
"SSL": "false"
}
}
}
}作为命令行界面(CLI)工具使用
你也可以将此用作命令行工具:
# Install globally
npm install -g .
# Or run directly
npx simplesql可用工具
1. CRUD 操作(crud_tool)
对数据库表执行创建、读取、更新、删除操作。
参数:
action(字符串):操作类型 - “创建”,“读取”,“更新”,“删除”table(字符串):目标表名data(对象,可选):用于创建/更新操作的数据filter(对象,可选):用于读取/更新/删除操作的过滤条件
示例:
// Create a record
{
"action": "create",
"table": "users",
"data": {"name": "John Doe", "email": "john@example.com"}
}
// Read records
{
"action": "read",
"table": "users",
"filter": {"name": "John Doe"}
}
// Update records
{
"action": "update",
"table": "users",
"data": {"email": "newemail@example.com"},
"filter": {"id": 1}
}
// Delete records
{
"action": "delete",
"table": "users",
"filter": {"id": 1}
}2. 表创建(create_table)
使用自定义模式创建新的数据库表。
参数:
table(字符串):要创建的表的名称columns(对象):包含名称-类型对的列定义
示例:
{
"table": "products",
"columns": {
"id": "INTEGER PRIMARY KEY AUTO_INCREMENT",
"name": "VARCHAR(255) NOT NULL",
"price": "DECIMAL(10,2)",
"created_at": "TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
}
}3. 数据播种(或数据初始化)seed_data)
为测试目的,在表中生成并插入虚拟数据。
参数:
table(字符串):要播种的表名count(数字,可选):要生成的记录数量(默认:10)
示例:
{
"table": "users",
"count": 50
}4. 连接测试(ping_tool)
测试数据库连接性和服务器响应速度。
参数: 无
发展
项目结构
SimpleSQL/
├── src/ # TypeScript source files
│ ├── db.ts # Database connection logic
│ ├── index.ts # MCP server setup and tool definitions
│ └── tools/ # Individual tool implementations
│ ├── crud.ts # CRUD operations
│ ├── createTable.ts # Table creation
│ └── seedData.ts # Data seeding
├── build/ # Compiled JavaScript output
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── .env # Environment variables (create this)
└── .gitignore # Git ignore rules建筑
npm run build这个将TypeScript编译为JavaScript,在 build/ 为CLI可执行文件设置目录并分配适当的权限。
依赖项
运行时依赖项:
@modelcontextprotocol/sdkMCP服务器实现knexSQL查询构建器better-sqlite3,sqlite3SQLite 驱动程序mysql,mysql2MySQL 驱动程序pgPostgreSQL 驱动程序dotenv环境变量加载zod模式验证@faker-js/faker生成虚拟数据
故障排除
常见问题
- 数据库连接失败
- 验证您的环境变量是否正确 - 确保数据库服务器正在运行 - 检查防火墙和网络连接
- 权限被拒绝
- 对于SQLite:确保对数据库文件所在位置有写权限 - 对于MySQL/PostgreSQL:验证用户是否具有所需的权限
- 模块未找到
- 跑 npm install 安装依赖项 - 确保你已经使用(相应的工具或环境)构建了该项目 npm run build
测试连接
使用ping工具测试您的数据库连接:
# The ping_tool will return "Pong!" if connection is successful
# or an error message if there are connection issues许可证
麻省理工学院许可证(或简称MIT许可证)
贡献
- 为仓库创建分支
- 创建一个特性分支
- 做出你的更改
- 如适用,请添加测试
- 提交拉取请求
