@cmd233/mcp-database-server 1.1.6 → 1.2.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.
@@ -1,7 +1,7 @@
1
1
  import { formatErrorResponse } from '../utils/formatUtils.js';
2
2
  // 导入所有工具实现
3
3
  import { readQuery, writeQuery, exportQuery } from '../tools/queryTools.js';
4
- import { createTable, alterTable, dropTable, listTables, describeTable } from '../tools/schemaTools.js';
4
+ import { createTable, alterTable, dropTable, listTables, describeTable, listViews, describeView, getViewDefinition } from '../tools/schemaTools.js';
5
5
  import { appendInsight, listInsights } from '../tools/insightTools.js';
6
6
  /**
7
7
  * 处理列出可用工具的请求
@@ -12,109 +12,498 @@ export function handleListTools() {
12
12
  tools: [
13
13
  {
14
14
  name: "read_query",
15
- description: "Execute SELECT queries to read data from the database",
15
+ title: "Read Query",
16
+ description: "Execute a SELECT query to read data from the database. " +
17
+ "Returns the complete result set with all matching rows and columns. " +
18
+ "Only SELECT statements are allowed - use write_query for data modifications. " +
19
+ "Supports all database types: SQLite, SQL Server, PostgreSQL, MySQL.",
16
20
  inputSchema: {
17
21
  type: "object",
18
22
  properties: {
19
- query: { type: "string" },
23
+ query: {
24
+ type: "string",
25
+ description: "The SQL SELECT query to execute (e.g., 'SELECT * FROM users WHERE active = 1')"
26
+ },
20
27
  },
21
28
  required: ["query"],
22
29
  },
30
+ outputSchema: {
31
+ type: "object",
32
+ properties: {
33
+ rows: {
34
+ type: "array",
35
+ description: "Array of result rows from the query"
36
+ },
37
+ columns: {
38
+ type: "array",
39
+ description: "Array of column names in the result set"
40
+ }
41
+ }
42
+ },
43
+ annotations: {
44
+ readOnlyHint: true,
45
+ idempotentHint: true
46
+ }
23
47
  },
24
48
  {
25
49
  name: "write_query",
26
- description: "Execute INSERT, UPDATE, or DELETE queries",
50
+ title: "Write Query",
51
+ description: "Execute INSERT, UPDATE, DELETE, or TRUNCATE queries to modify database data. " +
52
+ "Returns the number of affected rows. " +
53
+ "Cannot be used for SELECT queries - use read_query instead. " +
54
+ "Supports all database types: SQLite, SQL Server, PostgreSQL, MySQL. " +
55
+ "Requires confirm=true as a safety measure to prevent accidental data modification.",
27
56
  inputSchema: {
28
57
  type: "object",
29
58
  properties: {
30
- query: { type: "string" },
59
+ query: {
60
+ type: "string",
61
+ description: "The SQL INSERT/UPDATE/DELETE/TRUNCATE query to execute"
62
+ },
63
+ confirm: {
64
+ type: "boolean",
65
+ description: "Must be set to true to confirm data modification"
66
+ },
31
67
  },
32
68
  required: ["query"],
33
69
  },
70
+ outputSchema: {
71
+ type: "object",
72
+ properties: {
73
+ affected_rows: {
74
+ type: "number",
75
+ description: "Number of rows affected by the operation"
76
+ },
77
+ last_id: {
78
+ type: "number",
79
+ description: "ID of the last inserted row (for INSERT operations)"
80
+ }
81
+ }
82
+ },
83
+ annotations: {
84
+ readOnlyHint: false,
85
+ destructiveHint: true
86
+ }
34
87
  },
35
88
  {
36
89
  name: "create_table",
37
- description: "Create new tables in the database",
90
+ title: "Create Table",
91
+ description: "Create a new table in the database using a CREATE TABLE statement. " +
92
+ "Supports all standard SQL table creation syntax including column definitions, " +
93
+ "constraints, indexes, and relationships. " +
94
+ "Works with SQLite, SQL Server, PostgreSQL, and MySQL. " +
95
+ "Requires confirm=true as a safety measure to prevent accidental table creation.",
38
96
  inputSchema: {
39
97
  type: "object",
40
98
  properties: {
41
- query: { type: "string" },
99
+ query: {
100
+ type: "string",
101
+ description: "The complete CREATE TABLE SQL statement"
102
+ },
103
+ confirm: {
104
+ type: "boolean",
105
+ description: "Must be set to true to confirm table creation"
106
+ },
42
107
  },
43
108
  required: ["query"],
44
109
  },
110
+ outputSchema: {
111
+ type: "object",
112
+ properties: {
113
+ success: {
114
+ type: "boolean",
115
+ description: "True if the table was created successfully"
116
+ },
117
+ message: {
118
+ type: "string",
119
+ description: "Success message with table name"
120
+ }
121
+ }
122
+ },
123
+ annotations: {
124
+ readOnlyHint: false,
125
+ destructiveHint: true
126
+ }
45
127
  },
46
128
  {
47
129
  name: "alter_table",
48
- description: "Modify existing table schema (add columns, rename tables, etc.)",
130
+ title: "Alter Table",
131
+ description: "Modify an existing table's structure using ALTER TABLE statements. " +
132
+ "Supports adding columns, dropping columns, renaming columns, changing data types, " +
133
+ "and other table modifications. " +
134
+ "The table must exist before alterations can be made. " +
135
+ "Requires confirm=true as a safety measure to prevent accidental schema changes.",
49
136
  inputSchema: {
50
137
  type: "object",
51
138
  properties: {
52
- query: { type: "string" },
139
+ query: {
140
+ type: "string",
141
+ description: "The ALTER TABLE SQL statement to modify table structure"
142
+ },
143
+ confirm: {
144
+ type: "boolean",
145
+ description: "Must be set to true to confirm table alteration"
146
+ },
53
147
  },
54
148
  required: ["query"],
55
149
  },
150
+ outputSchema: {
151
+ type: "object",
152
+ properties: {
153
+ success: {
154
+ type: "boolean",
155
+ description: "True if the table was altered successfully"
156
+ },
157
+ message: {
158
+ type: "string",
159
+ description: "Success message confirming the alteration"
160
+ }
161
+ }
162
+ },
163
+ annotations: {
164
+ readOnlyHint: false,
165
+ destructiveHint: true
166
+ }
56
167
  },
57
168
  {
58
169
  name: "drop_table",
59
- description: "Remove a table from the database with safety confirmation",
170
+ title: "Drop Table",
171
+ description: "Permanently delete a table from the database. " +
172
+ "This operation cannot be undone - all data and structure will be lost. " +
173
+ "Requires confirm=true to execute as a safety measure. " +
174
+ "Validates that the table exists before attempting deletion.",
60
175
  inputSchema: {
61
176
  type: "object",
62
177
  properties: {
63
- table_name: { type: "string" },
64
- confirm: { type: "boolean" },
178
+ table_name: {
179
+ type: "string",
180
+ description: "Name of the table to delete"
181
+ },
182
+ confirm: {
183
+ type: "boolean",
184
+ description: "Must be set to true to confirm table deletion"
185
+ },
65
186
  },
66
187
  required: ["table_name", "confirm"],
67
188
  },
189
+ outputSchema: {
190
+ type: "object",
191
+ properties: {
192
+ success: {
193
+ type: "boolean",
194
+ description: "True if the table was dropped successfully"
195
+ },
196
+ message: {
197
+ type: "string",
198
+ description: "Success message with the dropped table name"
199
+ }
200
+ }
201
+ },
202
+ annotations: {
203
+ readOnlyHint: false,
204
+ destructiveHint: true
205
+ }
68
206
  },
69
207
  {
70
208
  name: "export_query",
71
- description: "Export query results to various formats (CSV, JSON)",
209
+ title: "Export Query",
210
+ description: "Execute a SELECT query and export the results in CSV or JSON format. " +
211
+ "Only SELECT queries are allowed. " +
212
+ "CSV format returns comma-separated values with headers. " +
213
+ "JSON format returns the raw result array. " +
214
+ "Useful for data analysis, reporting, or data transfer.",
72
215
  inputSchema: {
73
216
  type: "object",
74
217
  properties: {
75
- query: { type: "string" },
76
- format: { type: "string", enum: ["csv", "json"] },
218
+ query: {
219
+ type: "string",
220
+ description: "The SQL SELECT query to execute and export"
221
+ },
222
+ format: {
223
+ type: "string",
224
+ enum: ["csv", "json"],
225
+ description: "Output format: 'csv' for comma-separated values, 'json' for raw JSON array"
226
+ },
77
227
  },
78
228
  required: ["query", "format"],
79
229
  },
230
+ outputSchema: {
231
+ type: "object",
232
+ properties: {
233
+ data: {
234
+ type: "string",
235
+ description: "Exported data in the requested format"
236
+ },
237
+ format: {
238
+ type: "string",
239
+ description: "The format of the exported data"
240
+ }
241
+ }
242
+ },
243
+ annotations: {
244
+ readOnlyHint: true,
245
+ idempotentHint: true
246
+ }
80
247
  },
81
248
  {
82
249
  name: "list_tables",
83
- description: "Get a list of all tables in the database",
250
+ title: "List Tables",
251
+ description: "Retrieve a list of all table names in the current database. " +
252
+ "Returns only table names without structure details. " +
253
+ "Use describe_table to get detailed column information for a specific table. " +
254
+ "Set include_views=true to also include database views (SQL Server only).",
84
255
  inputSchema: {
85
256
  type: "object",
86
- properties: {},
257
+ properties: {
258
+ include_views: {
259
+ type: "boolean",
260
+ description: "Set to true to include views in the result (SQL Server only)"
261
+ },
262
+ },
263
+ },
264
+ outputSchema: {
265
+ type: "object",
266
+ properties: {
267
+ tables: {
268
+ type: "array",
269
+ items: {
270
+ type: "object",
271
+ properties: {
272
+ name: { type: "string", description: "Table or view name" },
273
+ type: { type: "string", enum: ["table", "view"], description: "Object type" }
274
+ }
275
+ },
276
+ description: "Array of table/view names and types in the database"
277
+ }
278
+ }
87
279
  },
280
+ annotations: {
281
+ readOnlyHint: true,
282
+ idempotentHint: true
283
+ }
88
284
  },
89
285
  {
90
286
  name: "describe_table",
91
- description: "View schema information for a specific table",
287
+ title: "Describe Table",
288
+ description: "Get detailed structural information about a specific table or view. " +
289
+ "Returns column name, data type, nullable status, default value, " +
290
+ "primary key status, and column comment (if supported). " +
291
+ "Supports both tables and views (SQL Server only for views). " +
292
+ "The table or view must exist in the database.",
92
293
  inputSchema: {
93
294
  type: "object",
94
295
  properties: {
95
- table_name: { type: "string" },
296
+ table_name: {
297
+ type: "string",
298
+ description: "Name of the table or view to describe"
299
+ },
96
300
  },
97
301
  required: ["table_name"],
98
302
  },
303
+ outputSchema: {
304
+ type: "object",
305
+ properties: {
306
+ name: { type: "string", description: "Table or view name" },
307
+ type: { type: "string", enum: ["table", "view"], description: "Object type" },
308
+ columns: {
309
+ type: "array",
310
+ description: "Array of column definitions",
311
+ items: {
312
+ type: "object",
313
+ properties: {
314
+ name: { type: "string", description: "Column name" },
315
+ type: { type: "string", description: "Data type" },
316
+ notnull: { type: "boolean", description: "Whether the column is NOT NULL" },
317
+ default_value: { type: "string", description: "Default value" },
318
+ primary_key: { type: "boolean", description: "Whether the column is a primary key" },
319
+ comment: { type: "string", description: "Column comment" }
320
+ }
321
+ }
322
+ }
323
+ }
324
+ },
325
+ annotations: {
326
+ readOnlyHint: true,
327
+ idempotentHint: true
328
+ }
329
+ },
330
+ {
331
+ name: "list_views",
332
+ title: "List Views",
333
+ description: "Retrieve a list of all view names in the current database. " +
334
+ "Only works with SQL Server databases. " +
335
+ "Returns only view names without structure details. " +
336
+ "Use describe_view to get detailed column information for a specific view.",
337
+ inputSchema: {
338
+ type: "object",
339
+ properties: {},
340
+ },
341
+ outputSchema: {
342
+ type: "object",
343
+ properties: {
344
+ views: {
345
+ type: "array",
346
+ items: { type: "string" },
347
+ description: "Array of view names in the database"
348
+ }
349
+ }
350
+ },
351
+ annotations: {
352
+ readOnlyHint: true,
353
+ idempotentHint: true
354
+ }
355
+ },
356
+ {
357
+ name: "describe_view",
358
+ title: "Describe View",
359
+ description: "Get detailed structural information about a specific view. " +
360
+ "Only works with SQL Server databases. " +
361
+ "Returns column name, data type, nullable status, default value, " +
362
+ "primary key status, and column comment (if available). " +
363
+ "The view must exist in the database.",
364
+ inputSchema: {
365
+ type: "object",
366
+ properties: {
367
+ view_name: {
368
+ type: "string",
369
+ description: "Name of the view to describe"
370
+ },
371
+ },
372
+ required: ["view_name"],
373
+ },
374
+ outputSchema: {
375
+ type: "object",
376
+ properties: {
377
+ name: { type: "string", description: "View name" },
378
+ type: { type: "string", description: "Always 'view'" },
379
+ columns: {
380
+ type: "array",
381
+ description: "Array of column definitions",
382
+ items: {
383
+ type: "object",
384
+ properties: {
385
+ name: { type: "string", description: "Column name" },
386
+ type: { type: "string", description: "Data type" },
387
+ notnull: { type: "boolean", description: "Whether the column is NOT NULL" },
388
+ default_value: { type: "string", description: "Default value" },
389
+ primary_key: { type: "boolean", description: "Whether the column is a primary key" },
390
+ comment: { type: "string", description: "Column comment" }
391
+ }
392
+ }
393
+ }
394
+ }
395
+ },
396
+ annotations: {
397
+ readOnlyHint: true,
398
+ idempotentHint: true
399
+ }
400
+ },
401
+ {
402
+ name: "get_view_definition",
403
+ title: "Get View Definition",
404
+ description: "Retrieve the SQL definition (CREATE VIEW statement) of a specific view. " +
405
+ "Only works with SQL Server databases. " +
406
+ "Returns the complete CREATE VIEW SQL statement. " +
407
+ "Note: Views created WITH ENCRYPTION cannot have their definition retrieved.",
408
+ inputSchema: {
409
+ type: "object",
410
+ properties: {
411
+ view_name: {
412
+ type: "string",
413
+ description: "Name of the view to get definition for"
414
+ },
415
+ },
416
+ required: ["view_name"],
417
+ },
418
+ outputSchema: {
419
+ type: "object",
420
+ properties: {
421
+ name: { type: "string", description: "View name" },
422
+ definition: { type: "string", description: "CREATE VIEW SQL statement" },
423
+ message: { type: "string", description: "Additional information if definition is unavailable" }
424
+ }
425
+ },
426
+ annotations: {
427
+ readOnlyHint: true,
428
+ idempotentHint: true
429
+ }
99
430
  },
100
431
  {
101
432
  name: "append_insight",
102
- description: "Add a business insight to the memo",
433
+ title: "Append Insight",
434
+ description: "Add a business insight to the SQLite insights memo. " +
435
+ "Only works with SQLite databases - creates and uses an mcp_insights table. " +
436
+ "Each insight is stored with a timestamp for tracking. " +
437
+ "Useful for maintaining notes during analysis sessions. " +
438
+ "Requires confirm=true as a safety measure to prevent accidental data insertion.",
103
439
  inputSchema: {
104
440
  type: "object",
105
441
  properties: {
106
- insight: { type: "string" },
442
+ insight: {
443
+ type: "string",
444
+ description: "The business insight text to store in the memo"
445
+ },
446
+ confirm: {
447
+ type: "boolean",
448
+ description: "Must be set to true to confirm adding the insight"
449
+ },
107
450
  },
108
451
  required: ["insight"],
109
452
  },
453
+ outputSchema: {
454
+ type: "object",
455
+ properties: {
456
+ success: {
457
+ type: "boolean",
458
+ description: "True if the insight was added successfully"
459
+ },
460
+ id: {
461
+ type: "number",
462
+ description: "ID of the newly created insight entry"
463
+ },
464
+ message: {
465
+ type: "string",
466
+ description: "Success message"
467
+ }
468
+ }
469
+ },
470
+ annotations: {
471
+ readOnlyHint: false,
472
+ destructiveHint: false
473
+ }
110
474
  },
111
475
  {
112
476
  name: "list_insights",
113
- description: "List all business insights in the memo",
477
+ title: "List Insights",
478
+ description: "Retrieve all stored business insights from the SQLite insights memo. " +
479
+ "Only works with SQLite databases. " +
480
+ "Returns insights in descending order by creation time (newest first). " +
481
+ "Returns an empty list if no insights have been stored yet.",
114
482
  inputSchema: {
115
483
  type: "object",
116
484
  properties: {},
117
485
  },
486
+ outputSchema: {
487
+ type: "object",
488
+ properties: {
489
+ insights: {
490
+ type: "array",
491
+ description: "Array of insight entries",
492
+ items: {
493
+ type: "object",
494
+ properties: {
495
+ id: { type: "number", description: "Insight ID" },
496
+ insight: { type: "string", description: "Insight text" },
497
+ created_at: { type: "string", description: "Creation timestamp" }
498
+ }
499
+ }
500
+ }
501
+ }
502
+ },
503
+ annotations: {
504
+ readOnlyHint: true,
505
+ idempotentHint: true
506
+ }
118
507
  },
119
508
  ],
120
509
  };
@@ -131,25 +520,31 @@ export async function handleToolCall(name, args) {
131
520
  case "read_query":
132
521
  return await readQuery(args.query);
133
522
  case "write_query":
134
- return await writeQuery(args.query);
523
+ return await writeQuery(args.query, args.confirm);
135
524
  case "create_table":
136
- return await createTable(args.query);
525
+ return await createTable(args.query, args.confirm);
137
526
  case "alter_table":
138
- return await alterTable(args.query);
527
+ return await alterTable(args.query, args.confirm);
139
528
  case "drop_table":
140
529
  return await dropTable(args.table_name, args.confirm);
141
530
  case "export_query":
142
531
  return await exportQuery(args.query, args.format);
143
532
  case "list_tables":
144
- return await listTables();
533
+ return await listTables(args.include_views);
145
534
  case "describe_table":
146
535
  return await describeTable(args.table_name);
536
+ case "list_views":
537
+ return await listViews();
538
+ case "describe_view":
539
+ return await describeView(args.view_name);
540
+ case "get_view_definition":
541
+ return await getViewDefinition(args.view_name);
147
542
  case "append_insight":
148
- return await appendInsight(args.insight);
543
+ return await appendInsight(args.insight, args.confirm);
149
544
  case "list_insights":
150
545
  return await listInsights();
151
546
  default:
152
- throw new Error(`Unknown tool: ${name}`);
547
+ throw new Error(`未知的工具: ${name}`);
153
548
  }
154
549
  }
155
550
  catch (error) {
package/dist/src/index.js CHANGED
@@ -27,12 +27,12 @@ const server = new Server({
27
27
  // 解析命令行参数
28
28
  const args = process.argv.slice(2);
29
29
  if (args.length === 0) {
30
- logger.error("Please provide database connection information");
31
- logger.error("Usage for SQLite: node index.js <database_file_path>");
32
- logger.error("Usage for SQL Server: node index.js --sqlserver --server <server> --database <database> [--user <user> --password <password>]");
33
- logger.error("Usage for PostgreSQL: node index.js --postgresql --host <host> --database <database> [--user <user> --password <password> --port <port>]");
34
- logger.error("Usage for MySQL: node index.js --mysql --host <host> --database <database> [--user <user> --password <password> --port <port>]");
35
- logger.error("Usage for MySQL with AWS IAM: node index.js --mysql --aws-iam-auth --host <rds-endpoint> --database <database> --user <aws-username> --aws-region <region>");
30
+ logger.error("请提供数据库连接信息");
31
+ logger.error("SQLite 用法: node index.js <database_file_path>");
32
+ logger.error("SQL Server 用法: node index.js --sqlserver --server <server> --database <database> [--user <user> --password <password>]");
33
+ logger.error("PostgreSQL 用法: node index.js --postgresql --host <host> --database <database> [--user <user> --password <password> --port <port>]");
34
+ logger.error("MySQL 用法: node index.js --mysql --host <host> --database <database> [--user <user> --password <password> --port <port>]");
35
+ logger.error("MySQL with AWS IAM 用法: node index.js --mysql --aws-iam-auth --host <rds-endpoint> --database <database> --user <aws-username> --aws-region <region>");
36
36
  process.exit(1);
37
37
  }
38
38
  // 解析参数以确定数据库类型和连接信息
@@ -67,7 +67,7 @@ if (args.includes('--sqlserver')) {
67
67
  }
68
68
  // 验证 SQL Server 连接信息
69
69
  if (!connectionInfo.server || !connectionInfo.database) {
70
- logger.error("Error: SQL Server requires --server and --database parameters");
70
+ logger.error("错误: SQL Server 需要 --server --database 参数");
71
71
  process.exit(1);
72
72
  }
73
73
  }
@@ -109,7 +109,7 @@ else if (args.includes('--postgresql') || args.includes('--postgres')) {
109
109
  }
110
110
  // 验证 PostgreSQL 连接信息
111
111
  if (!connectionInfo.host || !connectionInfo.database) {
112
- logger.error("Error: PostgreSQL requires --host and --database parameters");
112
+ logger.error("错误: PostgreSQL 需要 --host --database 参数");
113
113
  process.exit(1);
114
114
  }
115
115
  }
@@ -165,22 +165,22 @@ else if (args.includes('--mysql')) {
165
165
  }
166
166
  // 验证 MySQL 连接信息
167
167
  if (!connectionInfo.host || !connectionInfo.database) {
168
- logger.error("Error: MySQL requires --host and --database parameters");
168
+ logger.error("错误: MySQL 需要 --host --database 参数");
169
169
  process.exit(1);
170
170
  }
171
171
  // AWS IAM 认证的额外验证
172
172
  if (connectionInfo.awsIamAuth) {
173
173
  if (!connectionInfo.user) {
174
- logger.error("Error: AWS IAM authentication requires --user parameter");
174
+ logger.error("错误: AWS IAM 认证需要 --user 参数");
175
175
  process.exit(1);
176
176
  }
177
177
  if (!connectionInfo.awsRegion) {
178
- logger.error("Error: AWS IAM authentication requires --aws-region parameter");
178
+ logger.error("错误: AWS IAM 认证需要 --aws-region 参数");
179
179
  process.exit(1);
180
180
  }
181
181
  // 为 AWS IAM 认证自动启用 SSL (必需)
182
182
  connectionInfo.ssl = true;
183
- logger.info("AWS IAM authentication enabled - SSL automatically configured");
183
+ logger.info("AWS IAM 认证已启用 - SSL 已自动配置");
184
184
  }
185
185
  }
186
186
  else {
@@ -204,21 +204,21 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
204
204
  });
205
205
  // 优雅处理关闭信号
206
206
  process.on('SIGINT', async () => {
207
- logger.info('Shutting down gracefully...');
207
+ logger.info('正在优雅关闭...');
208
208
  await closeDatabase();
209
209
  process.exit(0);
210
210
  });
211
211
  process.on('SIGTERM', async () => {
212
- logger.info('Shutting down gracefully...');
212
+ logger.info('正在优雅关闭...');
213
213
  await closeDatabase();
214
214
  process.exit(0);
215
215
  });
216
216
  // 添加全局错误处理器
217
217
  process.on('uncaughtException', (error) => {
218
- logger.error('Uncaught exception:', error);
218
+ logger.error('未捕获的异常:', error);
219
219
  });
220
220
  process.on('unhandledRejection', (reason, promise) => {
221
- logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
221
+ logger.error('未处理的 Promise 拒绝:', promise, '原因:', reason);
222
222
  });
223
223
  /**
224
224
  * 启动服务器
@@ -248,12 +248,12 @@ async function runServer() {
248
248
  logger.info('Server running. Press Ctrl+C to exit.');
249
249
  }
250
250
  catch (error) {
251
- logger.error("Failed to initialize:", error);
251
+ logger.error("初始化失败:", error);
252
252
  process.exit(1);
253
253
  }
254
254
  }
255
255
  // 启动服务器
256
256
  runServer().catch(error => {
257
- logger.error("Server initialization failed:", error);
257
+ logger.error("服务器初始化失败:", error);
258
258
  process.exit(1);
259
259
  });