@hyqf98/easy_db_mcp_server 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -8
- package/dist/database/base.d.ts +79 -0
- package/dist/database/mysql.d.ts +11 -1
- package/dist/database/mysql.js +336 -18
- package/dist/database/postgresql.d.ts +11 -1
- package/dist/database/postgresql.js +460 -2
- package/dist/database/sqlite.d.ts +11 -1
- package/dist/database/sqlite.js +223 -0
- package/dist/index.js +241 -172
- package/dist/utils/file.d.ts +4 -0
- package/dist/utils/file.js +37 -0
- package/package.json +1 -1
- package/src/database/base.ts +100 -0
- package/src/database/mysql.ts +428 -19
- package/src/database/postgresql.ts +554 -2
- package/src/database/sqlite.ts +282 -0
- package/src/index.ts +310 -195
- package/src/utils/file.ts +46 -0
package/README.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
一个通过 npx 部署的 MCP 服务器,为 AI 助手提供统一的数据库访问接口。
|
|
4
4
|
|
|
5
|
+
## 版本
|
|
6
|
+
|
|
7
|
+
v2.0.0 - 支持 13 个数据库操作工具
|
|
8
|
+
|
|
5
9
|
## 支持的数据库
|
|
6
10
|
|
|
7
11
|
- MySQL (5.7+)
|
|
@@ -73,17 +77,26 @@ export EASYDB_DATABASE=/path/to/database.db
|
|
|
73
77
|
"EASYDB_HOST": "localhost",
|
|
74
78
|
"EASYDB_USER": "root",
|
|
75
79
|
"EASYDB_PASSWORD": "your_password",
|
|
76
|
-
"EASYDB_DATABASE": "mydb"
|
|
80
|
+
"EASYDB_DATABASE": "mydb",
|
|
81
|
+
"EASYDB_ALLOW_WRITE": "true",
|
|
82
|
+
"EASYDB_ALLOW_DDL": "true",
|
|
77
83
|
}
|
|
78
84
|
}
|
|
79
85
|
}
|
|
80
86
|
}
|
|
81
87
|
```
|
|
82
88
|
|
|
89
|
+
## Claude Code
|
|
90
|
+
|
|
91
|
+
```cmd
|
|
92
|
+
claude mcp add --scope user --transport stdio easy_db_mcp_server --env EASYDB_TYPE=mysql --env EASYDB_HOST=host --env EASYDB_USER=root --env EASYDB_PASSWORD=123456 --env EASYDB_ALLOW_WRITE=true --env EASYDB_ALLOW_DDL=true -- npx -y @hyqf98/easy_db_mcp_server
|
|
93
|
+
```
|
|
94
|
+
|
|
83
95
|
## 可用工具
|
|
84
96
|
|
|
85
|
-
###
|
|
97
|
+
### 基础操作
|
|
86
98
|
|
|
99
|
+
#### list_tables
|
|
87
100
|
列出数据库中的所有表。
|
|
88
101
|
|
|
89
102
|
**参数:**
|
|
@@ -94,8 +107,7 @@ export EASYDB_DATABASE=/path/to/database.db
|
|
|
94
107
|
请显示数据库中的所有表
|
|
95
108
|
```
|
|
96
109
|
|
|
97
|
-
|
|
98
|
-
|
|
110
|
+
#### describe_table
|
|
99
111
|
获取表的结构,包括列、类型和约束。
|
|
100
112
|
|
|
101
113
|
**参数:**
|
|
@@ -107,8 +119,7 @@ export EASYDB_DATABASE=/path/to/database.db
|
|
|
107
119
|
显示 users 表的结构
|
|
108
120
|
```
|
|
109
121
|
|
|
110
|
-
|
|
111
|
-
|
|
122
|
+
#### execute_query
|
|
112
123
|
执行 SELECT 查询(只读)。
|
|
113
124
|
|
|
114
125
|
**参数:**
|
|
@@ -120,8 +131,7 @@ export EASYDB_DATABASE=/path/to/database.db
|
|
|
120
131
|
查找过去 7 天内创建的所有用户
|
|
121
132
|
```
|
|
122
133
|
|
|
123
|
-
|
|
124
|
-
|
|
134
|
+
#### execute_sql
|
|
125
135
|
执行任意 SQL 语句(需要 `EASYDB_ALLOW_WRITE=true`)。
|
|
126
136
|
|
|
127
137
|
**参数:**
|
|
@@ -130,6 +140,177 @@ export EASYDB_DATABASE=/path/to/database.db
|
|
|
130
140
|
|
|
131
141
|
**注意:** DDL 语句(CREATE、DROP、ALTER)还需要 `EASYDB_ALLOW_DDL=true`。
|
|
132
142
|
|
|
143
|
+
### 批量操作(v2.0.0 新增)
|
|
144
|
+
|
|
145
|
+
#### execute_transaction
|
|
146
|
+
执行一组 SQL 语句作为事务,失败时自动回滚。
|
|
147
|
+
|
|
148
|
+
**参数:**
|
|
149
|
+
- `sql`(必填):SQL 语句数组
|
|
150
|
+
- `database`(可选):数据库名
|
|
151
|
+
|
|
152
|
+
**示例:**
|
|
153
|
+
```
|
|
154
|
+
在事务中插入用户和关联的配置数据
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
#### batch_insert
|
|
158
|
+
批量插入数据,支持多行一次性插入。
|
|
159
|
+
|
|
160
|
+
**参数:**
|
|
161
|
+
- `table`(必填):表名
|
|
162
|
+
- `data`(必填):数据行数组
|
|
163
|
+
- `database`(可选):数据库名
|
|
164
|
+
|
|
165
|
+
**返回:**
|
|
166
|
+
- `insertedRows`:成功插入的行数
|
|
167
|
+
- `duplicateRows`:重复的行数
|
|
168
|
+
|
|
169
|
+
**示例:**
|
|
170
|
+
```
|
|
171
|
+
批量插入 100 条用户数据
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
#### batch_update
|
|
175
|
+
批量更新数据,基于条件更新。
|
|
176
|
+
|
|
177
|
+
**参数:**
|
|
178
|
+
- `table`(必填):表名
|
|
179
|
+
- `updates`(必填):
|
|
180
|
+
- `set`:要更新的列值
|
|
181
|
+
- `where`:WHERE 条件
|
|
182
|
+
- `database`(可选):数据库名
|
|
183
|
+
|
|
184
|
+
**返回:**
|
|
185
|
+
- `affectedRows`:受影响的行数
|
|
186
|
+
|
|
187
|
+
**示例:**
|
|
188
|
+
```
|
|
189
|
+
将所有状态为 inactive 的用户更新为 active
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### 数据管理(v2.0.0 新增)
|
|
193
|
+
|
|
194
|
+
#### export_data
|
|
195
|
+
导出数据为 JSON/CSV 并保存到文件。
|
|
196
|
+
|
|
197
|
+
**参数:**
|
|
198
|
+
- `table`(必填):表名
|
|
199
|
+
- `format`(必填):导出格式(`json` 或 `csv`)
|
|
200
|
+
- `filePath`(可选):文件保存路径(默认:`~/table_name.json`)
|
|
201
|
+
- `limit`(可选):行数限制
|
|
202
|
+
- `where`(可选):WHERE 条件
|
|
203
|
+
- `database`(可选):数据库名
|
|
204
|
+
|
|
205
|
+
**返回:**
|
|
206
|
+
- `success`:是否成功
|
|
207
|
+
- `filePath`:实际保存路径
|
|
208
|
+
- `rowCount`:导出的行数
|
|
209
|
+
- `fileSize`:文件大小
|
|
210
|
+
|
|
211
|
+
**示例:**
|
|
212
|
+
```
|
|
213
|
+
导出 users 表为 JSON 文件
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
#### create_table
|
|
217
|
+
创建表。
|
|
218
|
+
|
|
219
|
+
**参数:**
|
|
220
|
+
- `table`(必填):表名
|
|
221
|
+
- `columns`(必填):列定义数组
|
|
222
|
+
- `name`:列名
|
|
223
|
+
- `type`:数据类型
|
|
224
|
+
- `nullable`:是否可空
|
|
225
|
+
- `primaryKey`:是否为主键
|
|
226
|
+
- `defaultValue`:默认值
|
|
227
|
+
- `database`(可选):数据库名
|
|
228
|
+
|
|
229
|
+
**返回:**
|
|
230
|
+
- `success`:是否成功
|
|
231
|
+
- `tableName`:表名
|
|
232
|
+
|
|
233
|
+
**示例:**
|
|
234
|
+
```
|
|
235
|
+
创建一个 products 表,包含 id、name、price 列
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### drop_table
|
|
239
|
+
删除表。
|
|
240
|
+
|
|
241
|
+
**参数:**
|
|
242
|
+
- `table`(必填):表名
|
|
243
|
+
- `ifExists`(可选):使用 IF EXISTS 避免表不存在时报错
|
|
244
|
+
- `database`(可选):数据库名
|
|
245
|
+
|
|
246
|
+
**返回:**
|
|
247
|
+
- `success`:是否成功
|
|
248
|
+
- `tableName`:表名
|
|
249
|
+
|
|
250
|
+
**示例:**
|
|
251
|
+
```
|
|
252
|
+
删除 temp_data 表
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### 数据分析(v2.0.0 新增)
|
|
256
|
+
|
|
257
|
+
#### get_table_stats
|
|
258
|
+
获取表的统计信息。
|
|
259
|
+
|
|
260
|
+
**参数:**
|
|
261
|
+
- `table`(必填):表名
|
|
262
|
+
- `database`(可选):数据库名
|
|
263
|
+
|
|
264
|
+
**返回:**
|
|
265
|
+
- `tableName`:表名
|
|
266
|
+
- `rowCount`:行数
|
|
267
|
+
- `columns`:列数
|
|
268
|
+
- `indexes`:索引列表
|
|
269
|
+
- `size`:表大小
|
|
270
|
+
|
|
271
|
+
**示例:**
|
|
272
|
+
```
|
|
273
|
+
显示 orders 表的统计信息
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
#### preview_data
|
|
277
|
+
分页预览表数据。
|
|
278
|
+
|
|
279
|
+
**参数:**
|
|
280
|
+
- `table`(必填):表名
|
|
281
|
+
- `page`(可选):页码(默认:1)
|
|
282
|
+
- `pageSize`(可选):每页行数(默认:50)
|
|
283
|
+
- `orderBy`(可选):排序列
|
|
284
|
+
- `database`(可选):数据库名
|
|
285
|
+
|
|
286
|
+
**返回:**
|
|
287
|
+
- `rows`:数据行
|
|
288
|
+
- `currentPage`:当前页码
|
|
289
|
+
- `totalPages`:总页数
|
|
290
|
+
- `totalRows`:总行数
|
|
291
|
+
|
|
292
|
+
**示例:**
|
|
293
|
+
```
|
|
294
|
+
预览 users 表的第 2 页,每页 20 条
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
#### sample_data
|
|
298
|
+
随机采样数据。
|
|
299
|
+
|
|
300
|
+
**参数:**
|
|
301
|
+
- `table`(必填):表名
|
|
302
|
+
- `count`(可选):采样数量(默认:10)
|
|
303
|
+
- `database`(可选):数据库名
|
|
304
|
+
|
|
305
|
+
**返回:**
|
|
306
|
+
- `rows`:采样的数据行
|
|
307
|
+
- `sampleCount`:实际采样数量
|
|
308
|
+
|
|
309
|
+
**示例:**
|
|
310
|
+
```
|
|
311
|
+
从 orders 表随机采样 5 条数据
|
|
312
|
+
```
|
|
313
|
+
|
|
133
314
|
## 安全性
|
|
134
315
|
|
|
135
316
|
默认情况下,服务器只允许读取操作。要启用写入权限:
|
|
@@ -161,6 +342,19 @@ npm run dev
|
|
|
161
342
|
npm start
|
|
162
343
|
```
|
|
163
344
|
|
|
345
|
+
## 版本历史
|
|
346
|
+
|
|
347
|
+
### v2.0.0
|
|
348
|
+
- 升级到 MCP SDK 1.25.3
|
|
349
|
+
- 迁移到新的 McpServer API
|
|
350
|
+
- 新增 9 个工具:execute_transaction, batch_insert, batch_update, export_data, create_table, drop_table, get_table_stats, preview_data, sample_data
|
|
351
|
+
- 新增文件工具模块
|
|
352
|
+
|
|
353
|
+
### v1.0.0
|
|
354
|
+
- 初始版本
|
|
355
|
+
- 支持 MySQL、PostgreSQL、SQLite
|
|
356
|
+
- 4 个基础工具:list_tables, describe_table, execute_query, execute_sql
|
|
357
|
+
|
|
164
358
|
## 许可证
|
|
165
359
|
|
|
166
360
|
MIT
|
package/dist/database/base.d.ts
CHANGED
|
@@ -5,14 +5,84 @@ export interface TableColumn {
|
|
|
5
5
|
defaultValue: string | null;
|
|
6
6
|
primaryKey: boolean;
|
|
7
7
|
extra?: string;
|
|
8
|
+
[key: string]: unknown;
|
|
8
9
|
}
|
|
9
10
|
export interface TableInfo {
|
|
10
11
|
name: string;
|
|
11
12
|
rowCount?: number;
|
|
13
|
+
[key: string]: unknown;
|
|
12
14
|
}
|
|
13
15
|
export interface QueryResult {
|
|
14
16
|
rows: Record<string, unknown>[];
|
|
15
17
|
rowCount: number;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
export interface BatchInsertResult {
|
|
21
|
+
insertedRows: number;
|
|
22
|
+
duplicateRows: number;
|
|
23
|
+
[key: string]: unknown;
|
|
24
|
+
}
|
|
25
|
+
export interface BatchUpdateOptions {
|
|
26
|
+
set: Record<string, unknown>;
|
|
27
|
+
where: string;
|
|
28
|
+
}
|
|
29
|
+
export interface BatchUpdateResult {
|
|
30
|
+
affectedRows: number;
|
|
31
|
+
[key: string]: unknown;
|
|
32
|
+
}
|
|
33
|
+
export interface TransactionResult {
|
|
34
|
+
success: boolean;
|
|
35
|
+
affectedRows: number;
|
|
36
|
+
results: unknown[];
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
export interface ExportOptions {
|
|
40
|
+
limit?: number;
|
|
41
|
+
where?: string;
|
|
42
|
+
}
|
|
43
|
+
export interface ExportResult {
|
|
44
|
+
success: boolean;
|
|
45
|
+
filePath: string;
|
|
46
|
+
rowCount: number;
|
|
47
|
+
fileSize: string;
|
|
48
|
+
[key: string]: unknown;
|
|
49
|
+
}
|
|
50
|
+
export interface TableColumnDef {
|
|
51
|
+
name: string;
|
|
52
|
+
type: string;
|
|
53
|
+
nullable?: boolean;
|
|
54
|
+
primaryKey?: boolean;
|
|
55
|
+
defaultValue?: unknown;
|
|
56
|
+
}
|
|
57
|
+
export interface CreateTableResult {
|
|
58
|
+
success: boolean;
|
|
59
|
+
tableName: string;
|
|
60
|
+
[key: string]: unknown;
|
|
61
|
+
}
|
|
62
|
+
export interface DropTableResult {
|
|
63
|
+
success: boolean;
|
|
64
|
+
tableName: string;
|
|
65
|
+
[key: string]: unknown;
|
|
66
|
+
}
|
|
67
|
+
export interface TableStatsResult {
|
|
68
|
+
tableName: string;
|
|
69
|
+
rowCount: number;
|
|
70
|
+
columns: number;
|
|
71
|
+
indexes: string[];
|
|
72
|
+
size: string;
|
|
73
|
+
[key: string]: unknown;
|
|
74
|
+
}
|
|
75
|
+
export interface PreviewDataResult {
|
|
76
|
+
rows: Record<string, unknown>[];
|
|
77
|
+
currentPage: number;
|
|
78
|
+
totalPages: number;
|
|
79
|
+
totalRows: number;
|
|
80
|
+
[key: string]: unknown;
|
|
81
|
+
}
|
|
82
|
+
export interface SampleDataResult {
|
|
83
|
+
rows: Record<string, unknown>[];
|
|
84
|
+
sampleCount: number;
|
|
85
|
+
[key: string]: unknown;
|
|
16
86
|
}
|
|
17
87
|
export interface DatabaseAdapter {
|
|
18
88
|
/**
|
|
@@ -41,4 +111,13 @@ export interface DatabaseAdapter {
|
|
|
41
111
|
* Close the database connection
|
|
42
112
|
*/
|
|
43
113
|
close(): Promise<void>;
|
|
114
|
+
batchInsert(table: string, data: Record<string, unknown>[], database?: string): Promise<BatchInsertResult>;
|
|
115
|
+
batchUpdate(table: string, updates: BatchUpdateOptions, database?: string): Promise<BatchUpdateResult>;
|
|
116
|
+
executeTransaction(queries: string[], database?: string): Promise<TransactionResult>;
|
|
117
|
+
exportData(table: string, format: 'json' | 'csv', filePath?: string, options?: ExportOptions, database?: string): Promise<ExportResult>;
|
|
118
|
+
createTable(table: string, columns: TableColumnDef[], database?: string): Promise<CreateTableResult>;
|
|
119
|
+
dropTable(table: string, ifExists?: boolean, database?: string): Promise<DropTableResult>;
|
|
120
|
+
getTableStats(table: string, database?: string): Promise<TableStatsResult>;
|
|
121
|
+
previewData(table: string, page?: number, pageSize?: number, orderBy?: string, database?: string): Promise<PreviewDataResult>;
|
|
122
|
+
sampleData(table: string, count?: number, database?: string): Promise<SampleDataResult>;
|
|
44
123
|
}
|
package/dist/database/mysql.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DatabaseAdapter, TableInfo, TableColumn, QueryResult } from './base.js';
|
|
1
|
+
import type { DatabaseAdapter, TableInfo, TableColumn, QueryResult, TransactionResult, BatchInsertResult, BatchUpdateResult, BatchUpdateOptions, ExportResult, ExportOptions, CreateTableResult, DropTableResult, TableColumnDef, TableStatsResult, PreviewDataResult, SampleDataResult } from './base.js';
|
|
2
2
|
import type { DatabaseConfig } from '../config.js';
|
|
3
3
|
export declare class MySQLAdapter implements DatabaseAdapter {
|
|
4
4
|
private pool?;
|
|
@@ -12,4 +12,14 @@ export declare class MySQLAdapter implements DatabaseAdapter {
|
|
|
12
12
|
affectedRows: number;
|
|
13
13
|
}>;
|
|
14
14
|
close(): Promise<void>;
|
|
15
|
+
executeTransaction(queries: string[], database?: string): Promise<TransactionResult>;
|
|
16
|
+
batchInsert(table: string, data: Record<string, unknown>[], database?: string): Promise<BatchInsertResult>;
|
|
17
|
+
batchUpdate(table: string, updates: BatchUpdateOptions, database?: string): Promise<BatchUpdateResult>;
|
|
18
|
+
private escapeValue;
|
|
19
|
+
exportData(table: string, format: 'json' | 'csv', filePath?: string, options?: ExportOptions, database?: string): Promise<ExportResult>;
|
|
20
|
+
createTable(table: string, columns: TableColumnDef[], database?: string): Promise<CreateTableResult>;
|
|
21
|
+
dropTable(table: string, ifExists?: boolean, database?: string): Promise<DropTableResult>;
|
|
22
|
+
getTableStats(table: string, database?: string): Promise<TableStatsResult>;
|
|
23
|
+
previewData(table: string, page?: number, pageSize?: number, orderBy?: string, database?: string): Promise<PreviewDataResult>;
|
|
24
|
+
sampleData(table: string, count?: number, database?: string): Promise<SampleDataResult>;
|
|
15
25
|
}
|