@hyqf98/easy_db_mcp_server 2.0.0 → 2.0.2

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 CHANGED
@@ -71,7 +71,7 @@ export EASYDB_DATABASE=/path/to/database.db
71
71
  "mcpServers": {
72
72
  "database": {
73
73
  "command": "npx",
74
- "args": ["hyqf98@easy_db_mcp_server"],
74
+ "args": ["@hyqf98/easy_db_mcp_server"],
75
75
  "env": {
76
76
  "EASYDB_TYPE": "mysql",
77
77
  "EASYDB_HOST": "localhost",
@@ -4,6 +4,11 @@ export declare class MySQLAdapter implements DatabaseAdapter {
4
4
  private pool?;
5
5
  private config;
6
6
  constructor(config: DatabaseConfig);
7
+ /**
8
+ * Get the final database name to use.
9
+ * Priority: 1. Parameter passed in method call, 2. Config database (from environment variable)
10
+ */
11
+ private getDatabaseName;
7
12
  connect(): Promise<void>;
8
13
  listTables(database?: string): Promise<TableInfo[]>;
9
14
  describeTable(table: string, database?: string): Promise<TableColumn[]>;
@@ -7,6 +7,19 @@ export class MySQLAdapter {
7
7
  constructor(config) {
8
8
  this.config = config;
9
9
  }
10
+ /**
11
+ * Get the final database name to use.
12
+ * Priority: 1. Parameter passed in method call, 2. Config database (from environment variable)
13
+ */
14
+ getDatabaseName(paramDb) {
15
+ if (paramDb && paramDb.trim().length > 0) {
16
+ return paramDb.trim();
17
+ }
18
+ if (this.config.database && this.config.database.trim().length > 0) {
19
+ return this.config.database;
20
+ }
21
+ throw new Error('Database name is required. Either set EASYDB_DATABASE environment variable or pass the database parameter.');
22
+ }
10
23
  async connect() {
11
24
  this.pool = mysql.createPool({
12
25
  host: this.config.host,
@@ -24,9 +37,7 @@ export class MySQLAdapter {
24
37
  async listTables(database) {
25
38
  if (!this.pool)
26
39
  throw new Error('Not connected');
27
- const dbName = database || this.config.database;
28
- if (!dbName)
29
- throw new Error('Database name required');
40
+ const dbName = this.getDatabaseName(database);
30
41
  const [rows] = await this.pool.query('SELECT TABLE_NAME as name, TABLE_ROWS as rowCount FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?', [dbName]);
31
42
  return rows.map((row) => ({
32
43
  name: row.name,
@@ -36,9 +47,7 @@ export class MySQLAdapter {
36
47
  async describeTable(table, database) {
37
48
  if (!this.pool)
38
49
  throw new Error('Not connected');
39
- const dbName = database || this.config.database;
40
- if (!dbName)
41
- throw new Error('Database name required');
50
+ const dbName = this.getDatabaseName(database);
42
51
  const [rows] = await this.pool.query(`SELECT
43
52
  COLUMN_NAME as name,
44
53
  DATA_TYPE as type,
@@ -68,9 +77,10 @@ export class MySQLAdapter {
68
77
  // Get a connection from the pool to handle database switching
69
78
  const connection = await this.pool.getConnection();
70
79
  try {
71
- // Switch to the specified database if provided
72
- if (database && database !== this.config.database) {
73
- await connection.query(`USE ${mysql.escapeId(database)}`);
80
+ // Switch to the specified database if different from config
81
+ const targetDb = this.getDatabaseName(database);
82
+ if (targetDb !== this.config.database) {
83
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
74
84
  }
75
85
  const [rows] = await connection.query(sql);
76
86
  return {
@@ -88,9 +98,10 @@ export class MySQLAdapter {
88
98
  // Get a connection from the pool to handle database switching
89
99
  const connection = await this.pool.getConnection();
90
100
  try {
91
- // Switch to the specified database if provided
92
- if (database && database !== this.config.database) {
93
- await connection.query(`USE ${mysql.escapeId(database)}`);
101
+ // Switch to the specified database if different from config
102
+ const targetDb = this.getDatabaseName(database);
103
+ if (targetDb !== this.config.database) {
104
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
94
105
  }
95
106
  const [result] = await connection.query(sql);
96
107
  // @ts-ignore - MySQL result structure
@@ -118,9 +129,10 @@ export class MySQLAdapter {
118
129
  throw new Error('Not connected');
119
130
  const connection = await this.pool.getConnection();
120
131
  try {
121
- // Switch to specified database if needed
122
- if (database && database !== this.config.database) {
123
- await connection.query(`USE ${mysql.escapeId(database)}`);
132
+ // Switch to specified database if different from config
133
+ const targetDb = this.getDatabaseName(database);
134
+ if (targetDb !== this.config.database) {
135
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
124
136
  }
125
137
  await connection.beginTransaction();
126
138
  const results = [];
@@ -157,8 +169,9 @@ export class MySQLAdapter {
157
169
  }
158
170
  const connection = await this.pool.getConnection();
159
171
  try {
160
- if (database && database !== this.config.database) {
161
- await connection.query(`USE ${mysql.escapeId(database)}`);
172
+ const targetDb = this.getDatabaseName(database);
173
+ if (targetDb !== this.config.database) {
174
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
162
175
  }
163
176
  // Build bulk insert query
164
177
  const columns = Object.keys(data[0]);
@@ -194,8 +207,9 @@ export class MySQLAdapter {
194
207
  throw new Error('Not connected');
195
208
  const connection = await this.pool.getConnection();
196
209
  try {
197
- if (database && database !== this.config.database) {
198
- await connection.query(`USE ${mysql.escapeId(database)}`);
210
+ const targetDb = this.getDatabaseName(database);
211
+ if (targetDb !== this.config.database) {
212
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
199
213
  }
200
214
  // Build SET clause
201
215
  const setClause = Object.entries(updates.set)
@@ -224,8 +238,9 @@ export class MySQLAdapter {
224
238
  throw new Error('Not connected');
225
239
  const connection = await this.pool.getConnection();
226
240
  try {
227
- if (database && database !== this.config.database) {
228
- await connection.query(`USE ${mysql.escapeId(database)}`);
241
+ const targetDb = this.getDatabaseName(database);
242
+ if (targetDb !== this.config.database) {
243
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
229
244
  }
230
245
  // Build query
231
246
  let query = `SELECT * FROM ${mysql.escapeId(table)}`;
@@ -286,8 +301,9 @@ export class MySQLAdapter {
286
301
  throw new Error('Not connected');
287
302
  const connection = await this.pool.getConnection();
288
303
  try {
289
- if (database && database !== this.config.database) {
290
- await connection.query(`USE ${mysql.escapeId(database)}`);
304
+ const targetDb = this.getDatabaseName(database);
305
+ if (targetDb !== this.config.database) {
306
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
291
307
  }
292
308
  const columnDefs = columns.map((col) => {
293
309
  let def = `${mysql.escapeId(col.name)} ${col.type}`;
@@ -313,8 +329,9 @@ export class MySQLAdapter {
313
329
  throw new Error('Not connected');
314
330
  const connection = await this.pool.getConnection();
315
331
  try {
316
- if (database && database !== this.config.database) {
317
- await connection.query(`USE ${mysql.escapeId(database)}`);
332
+ const targetDb = this.getDatabaseName(database);
333
+ if (targetDb !== this.config.database) {
334
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
318
335
  }
319
336
  const query = ifExists
320
337
  ? `DROP TABLE IF EXISTS ${mysql.escapeId(table)}`
@@ -331,11 +348,9 @@ export class MySQLAdapter {
331
348
  throw new Error('Not connected');
332
349
  const connection = await this.pool.getConnection();
333
350
  try {
334
- const dbName = database || this.config.database;
335
- if (!dbName)
336
- throw new Error('Database name required');
337
- if (database && database !== this.config.database) {
338
- await connection.query(`USE ${mysql.escapeId(database)}`);
351
+ const dbName = this.getDatabaseName(database);
352
+ if (dbName !== this.config.database) {
353
+ await connection.query(`USE ${mysql.escapeId(dbName)}`);
339
354
  }
340
355
  // Get row count
341
356
  const [countResult] = await connection.query(`SELECT COUNT(*) as count FROM ${mysql.escapeId(table)}`);
@@ -367,8 +382,9 @@ export class MySQLAdapter {
367
382
  throw new Error('Not connected');
368
383
  const connection = await this.pool.getConnection();
369
384
  try {
370
- if (database && database !== this.config.database) {
371
- await connection.query(`USE ${mysql.escapeId(database)}`);
385
+ const targetDb = this.getDatabaseName(database);
386
+ if (targetDb !== this.config.database) {
387
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
372
388
  }
373
389
  // Get total row count
374
390
  const [countResult] = await connection.query(`SELECT COUNT(*) as count FROM ${mysql.escapeId(table)}`);
@@ -398,8 +414,9 @@ export class MySQLAdapter {
398
414
  throw new Error('Not connected');
399
415
  const connection = await this.pool.getConnection();
400
416
  try {
401
- if (database && database !== this.config.database) {
402
- await connection.query(`USE ${mysql.escapeId(database)}`);
417
+ const targetDb = this.getDatabaseName(database);
418
+ if (targetDb !== this.config.database) {
419
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
403
420
  }
404
421
  const [rows] = await connection.query(`SELECT * FROM ${mysql.escapeId(table)} ORDER BY RAND() LIMIT ${count}`);
405
422
  return {
@@ -4,6 +4,11 @@ export declare class PostgreSQLAdapter implements DatabaseAdapter {
4
4
  private pool?;
5
5
  private config;
6
6
  constructor(config: DatabaseConfig);
7
+ /**
8
+ * Get the final database name to use.
9
+ * Priority: 1. Parameter passed in method call, 2. Config database (from environment variable)
10
+ */
11
+ private getDatabaseName;
7
12
  connect(): Promise<void>;
8
13
  listTables(database?: string): Promise<TableInfo[]>;
9
14
  describeTable(table: string, database?: string): Promise<TableColumn[]>;
@@ -8,6 +8,19 @@ export class PostgreSQLAdapter {
8
8
  constructor(config) {
9
9
  this.config = config;
10
10
  }
11
+ /**
12
+ * Get the final database name to use.
13
+ * Priority: 1. Parameter passed in method call, 2. Config database (from environment variable)
14
+ */
15
+ getDatabaseName(paramDb) {
16
+ if (paramDb && paramDb.trim().length > 0) {
17
+ return paramDb.trim();
18
+ }
19
+ if (this.config.database && this.config.database.trim().length > 0) {
20
+ return this.config.database;
21
+ }
22
+ throw new Error('Database name is required. Either set EASYDB_DATABASE environment variable or pass the database parameter.');
23
+ }
11
24
  async connect() {
12
25
  this.pool = new Pool({
13
26
  host: this.config.host,
@@ -24,9 +37,8 @@ export class PostgreSQLAdapter {
24
37
  async listTables(database) {
25
38
  if (!this.pool)
26
39
  throw new Error('Not connected');
27
- const dbName = database || this.config.database;
28
- if (!dbName)
29
- throw new Error('Database name required');
40
+ // Note: In PostgreSQL, the pool is connected to a specific database
41
+ // For cross-database queries, use the default pool connected to config.database
30
42
  const result = await this.pool.query(`SELECT tablename as name, n_live_tup as "rowCount"
31
43
  FROM pg_stat_user_tables
32
44
  WHERE schemaname = 'public'`);
@@ -38,7 +50,6 @@ export class PostgreSQLAdapter {
38
50
  async describeTable(table, database) {
39
51
  if (!this.pool)
40
52
  throw new Error('Not connected');
41
- const dbName = database || this.config.database;
42
53
  const result = await this.pool.query(`SELECT
43
54
  column_name as name,
44
55
  data_type as type,
@@ -71,8 +82,8 @@ export class PostgreSQLAdapter {
71
82
  throw new Error('Only SELECT queries allowed in executeQuery');
72
83
  }
73
84
  // If database is specified and different from default, use it
74
- const dbName = database || this.config.database;
75
- if (dbName && dbName !== this.config.database) {
85
+ const targetDb = this.getDatabaseName(database);
86
+ if (targetDb !== this.config.database) {
76
87
  // For PostgreSQL, we need to connect to the specific database
77
88
  // Create a temporary pool for the different database
78
89
  const tempPool = new Pool({
@@ -80,7 +91,7 @@ export class PostgreSQLAdapter {
80
91
  port: this.config.port || 5432,
81
92
  user: this.config.user,
82
93
  password: this.config.password,
83
- database: dbName,
94
+ database: targetDb,
84
95
  max: 1,
85
96
  });
86
97
  try {
@@ -104,8 +115,8 @@ export class PostgreSQLAdapter {
104
115
  if (!this.pool)
105
116
  throw new Error('Not connected');
106
117
  // If database is specified and different from default, use it
107
- const dbName = database || this.config.database;
108
- if (dbName && dbName !== this.config.database) {
118
+ const targetDb = this.getDatabaseName(database);
119
+ if (targetDb !== this.config.database) {
109
120
  // For PostgreSQL, we need to connect to the specific database
110
121
  // Create a temporary pool for the different database
111
122
  const tempPool = new Pool({
@@ -113,7 +124,7 @@ export class PostgreSQLAdapter {
113
124
  port: this.config.port || 5432,
114
125
  user: this.config.user,
115
126
  password: this.config.password,
116
- database: dbName,
127
+ database: targetDb,
117
128
  max: 1,
118
129
  });
119
130
  try {
@@ -150,13 +161,14 @@ export class PostgreSQLAdapter {
150
161
  throw new Error('Not connected');
151
162
  let client = null;
152
163
  // If different database, use temporary connection
153
- if (database && database !== this.config.database) {
164
+ const targetDb = this.getDatabaseName(database);
165
+ if (targetDb !== this.config.database) {
154
166
  const tempPool = new pg.Pool({
155
167
  host: this.config.host,
156
168
  port: this.config.port || 5432,
157
169
  user: this.config.user,
158
170
  password: this.config.password,
159
- database,
171
+ database: targetDb,
160
172
  max: 1,
161
173
  });
162
174
  try {
@@ -224,15 +236,15 @@ export class PostgreSQLAdapter {
224
236
  return { insertedRows: 0, duplicateRows: 0 };
225
237
  }
226
238
  let pool = this.pool;
227
- let dbName = database || this.config.database;
228
239
  // Use temporary pool for different database
229
- if (database && database !== this.config.database) {
240
+ const targetDb = this.getDatabaseName(database);
241
+ if (targetDb !== this.config.database) {
230
242
  pool = new pg.Pool({
231
243
  host: this.config.host,
232
244
  port: this.config.port || 5432,
233
245
  user: this.config.user,
234
246
  password: this.config.password,
235
- database,
247
+ database: targetDb,
236
248
  max: 1,
237
249
  });
238
250
  }
@@ -274,13 +286,14 @@ export class PostgreSQLAdapter {
274
286
  if (!this.pool)
275
287
  throw new Error('Not connected');
276
288
  let pool = this.pool;
277
- if (database && database !== this.config.database) {
289
+ const targetDb = this.getDatabaseName(database);
290
+ if (targetDb !== this.config.database) {
278
291
  pool = new pg.Pool({
279
292
  host: this.config.host,
280
293
  port: this.config.port || 5432,
281
294
  user: this.config.user,
282
295
  password: this.config.password,
283
- database,
296
+ database: targetDb,
284
297
  max: 1,
285
298
  });
286
299
  }
@@ -312,13 +325,14 @@ export class PostgreSQLAdapter {
312
325
  if (!this.pool)
313
326
  throw new Error('Not connected');
314
327
  let pool = this.pool;
315
- if (database && database !== this.config.database) {
328
+ const targetDb = this.getDatabaseName(database);
329
+ if (targetDb !== this.config.database) {
316
330
  pool = new pg.Pool({
317
331
  host: this.config.host,
318
332
  port: this.config.port || 5432,
319
333
  user: this.config.user,
320
334
  password: this.config.password,
321
- database,
335
+ database: targetDb,
322
336
  max: 1,
323
337
  });
324
338
  }
@@ -380,13 +394,14 @@ export class PostgreSQLAdapter {
380
394
  if (!this.pool)
381
395
  throw new Error('Not connected');
382
396
  let pool = this.pool;
383
- if (database && database !== this.config.database) {
397
+ const targetDb = this.getDatabaseName(database);
398
+ if (targetDb !== this.config.database) {
384
399
  pool = new pg.Pool({
385
400
  host: this.config.host,
386
401
  port: this.config.port || 5432,
387
402
  user: this.config.user,
388
403
  password: this.config.password,
389
- database,
404
+ database: targetDb,
390
405
  max: 1,
391
406
  });
392
407
  }
@@ -417,13 +432,14 @@ export class PostgreSQLAdapter {
417
432
  if (!this.pool)
418
433
  throw new Error('Not connected');
419
434
  let pool = this.pool;
420
- if (database && database !== this.config.database) {
435
+ const targetDb = this.getDatabaseName(database);
436
+ if (targetDb !== this.config.database) {
421
437
  pool = new pg.Pool({
422
438
  host: this.config.host,
423
439
  port: this.config.port || 5432,
424
440
  user: this.config.user,
425
441
  password: this.config.password,
426
- database,
442
+ database: targetDb,
427
443
  max: 1,
428
444
  });
429
445
  }
@@ -445,13 +461,14 @@ export class PostgreSQLAdapter {
445
461
  if (!this.pool)
446
462
  throw new Error('Not connected');
447
463
  let pool = this.pool;
448
- if (database && database !== this.config.database) {
464
+ const targetDb = this.getDatabaseName(database);
465
+ if (targetDb !== this.config.database) {
449
466
  pool = new pg.Pool({
450
467
  host: this.config.host,
451
468
  port: this.config.port || 5432,
452
469
  user: this.config.user,
453
470
  password: this.config.password,
454
- database,
471
+ database: targetDb,
455
472
  max: 1,
456
473
  });
457
474
  }
@@ -487,13 +504,14 @@ export class PostgreSQLAdapter {
487
504
  if (!this.pool)
488
505
  throw new Error('Not connected');
489
506
  let pool = this.pool;
490
- if (database && database !== this.config.database) {
507
+ const targetDb = this.getDatabaseName(database);
508
+ if (targetDb !== this.config.database) {
491
509
  pool = new pg.Pool({
492
510
  host: this.config.host,
493
511
  port: this.config.port || 5432,
494
512
  user: this.config.user,
495
513
  password: this.config.password,
496
- database,
514
+ database: targetDb,
497
515
  max: 1,
498
516
  });
499
517
  }
@@ -528,13 +546,14 @@ export class PostgreSQLAdapter {
528
546
  if (!this.pool)
529
547
  throw new Error('Not connected');
530
548
  let pool = this.pool;
531
- if (database && database !== this.config.database) {
549
+ const targetDb = this.getDatabaseName(database);
550
+ if (targetDb !== this.config.database) {
532
551
  pool = new pg.Pool({
533
552
  host: this.config.host,
534
553
  port: this.config.port || 5432,
535
554
  user: this.config.user,
536
555
  password: this.config.password,
537
- database,
556
+ database: targetDb,
538
557
  max: 1,
539
558
  });
540
559
  }
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import { createAdapter } from './database/factory.js';
7
7
  // Create server instance with new API
8
8
  const server = new McpServer({
9
9
  name: 'hyqf98@easy_db_mcp_server',
10
- version: '2.0.0',
10
+ version: '2.0.2',
11
11
  }, {
12
12
  capabilities: {
13
13
  tools: {},
@@ -27,13 +27,19 @@ catch (error) {
27
27
  process.exit(1);
28
28
  }
29
29
  // Helper function to validate database parameter
30
+ // Priority: 1. Parameter passed in method call, 2. Environment variable EASYDB_DATABASE
30
31
  const getDatabase = (paramDb) => {
31
- if (paramDb) {
32
- return paramDb;
32
+ // If parameter is provided (non-empty string), use it
33
+ if (paramDb && paramDb.trim().length > 0) {
34
+ console.error(`[Database] Using parameter-specified database: ${paramDb}`);
35
+ return paramDb.trim();
33
36
  }
34
- if (cachedConfig.database) {
37
+ // Fall back to environment variable
38
+ if (cachedConfig.database && cachedConfig.database.trim().length > 0) {
39
+ console.error(`[Database] Using environment variable database: ${cachedConfig.database}`);
35
40
  return cachedConfig.database;
36
41
  }
42
+ // Neither is available
37
43
  throw new Error('Database name is required. Either set EASYDB_DATABASE environment variable or pass the database parameter in the tool call.');
38
44
  };
39
45
  // Register existing tools with new API
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@hyqf98/easy_db_mcp_server",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "MCP server for database access (MySQL, PostgreSQL, SQLite)",
5
5
  "type": "module",
6
6
  "bin": {
7
- "hyqf98@easy_db_mcp_server": "dist/index.js"
7
+ "easy_db_mcp_server": "dist/index.js"
8
8
  },
9
9
  "scripts": {
10
10
  "build": "tsc",
@@ -29,6 +29,20 @@ export class MySQLAdapter implements DatabaseAdapter {
29
29
  this.config = config;
30
30
  }
31
31
 
32
+ /**
33
+ * Get the final database name to use.
34
+ * Priority: 1. Parameter passed in method call, 2. Config database (from environment variable)
35
+ */
36
+ private getDatabaseName(paramDb?: string): string {
37
+ if (paramDb && paramDb.trim().length > 0) {
38
+ return paramDb.trim();
39
+ }
40
+ if (this.config.database && this.config.database.trim().length > 0) {
41
+ return this.config.database;
42
+ }
43
+ throw new Error('Database name is required. Either set EASYDB_DATABASE environment variable or pass the database parameter.');
44
+ }
45
+
32
46
  async connect(): Promise<void> {
33
47
  this.pool = mysql.createPool({
34
48
  host: this.config.host,
@@ -48,8 +62,7 @@ export class MySQLAdapter implements DatabaseAdapter {
48
62
  async listTables(database?: string): Promise<TableInfo[]> {
49
63
  if (!this.pool) throw new Error('Not connected');
50
64
 
51
- const dbName = database || this.config.database;
52
- if (!dbName) throw new Error('Database name required');
65
+ const dbName = this.getDatabaseName(database);
53
66
 
54
67
  const [rows] = await this.pool.query<mysql.RowDataPacket[]>(
55
68
  'SELECT TABLE_NAME as name, TABLE_ROWS as rowCount FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?',
@@ -65,8 +78,7 @@ export class MySQLAdapter implements DatabaseAdapter {
65
78
  async describeTable(table: string, database?: string): Promise<TableColumn[]> {
66
79
  if (!this.pool) throw new Error('Not connected');
67
80
 
68
- const dbName = database || this.config.database;
69
- if (!dbName) throw new Error('Database name required');
81
+ const dbName = this.getDatabaseName(database);
70
82
 
71
83
  const [rows] = await this.pool.query<mysql.RowDataPacket[]>(
72
84
  `SELECT
@@ -104,9 +116,10 @@ export class MySQLAdapter implements DatabaseAdapter {
104
116
  const connection = await this.pool.getConnection();
105
117
 
106
118
  try {
107
- // Switch to the specified database if provided
108
- if (database && database !== this.config.database) {
109
- await connection.query(`USE ${mysql.escapeId(database)}`);
119
+ // Switch to the specified database if different from config
120
+ const targetDb = this.getDatabaseName(database);
121
+ if (targetDb !== this.config.database) {
122
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
110
123
  }
111
124
 
112
125
  const [rows] = await connection.query(sql);
@@ -127,9 +140,10 @@ export class MySQLAdapter implements DatabaseAdapter {
127
140
  const connection = await this.pool.getConnection();
128
141
 
129
142
  try {
130
- // Switch to the specified database if provided
131
- if (database && database !== this.config.database) {
132
- await connection.query(`USE ${mysql.escapeId(database)}`);
143
+ // Switch to the specified database if different from config
144
+ const targetDb = this.getDatabaseName(database);
145
+ if (targetDb !== this.config.database) {
146
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
133
147
  }
134
148
 
135
149
  const [result] = await connection.query(sql);
@@ -162,9 +176,10 @@ export class MySQLAdapter implements DatabaseAdapter {
162
176
  const connection = await this.pool.getConnection();
163
177
 
164
178
  try {
165
- // Switch to specified database if needed
166
- if (database && database !== this.config.database) {
167
- await connection.query(`USE ${mysql.escapeId(database)}`);
179
+ // Switch to specified database if different from config
180
+ const targetDb = this.getDatabaseName(database);
181
+ if (targetDb !== this.config.database) {
182
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
168
183
  }
169
184
 
170
185
  await connection.beginTransaction();
@@ -213,8 +228,9 @@ export class MySQLAdapter implements DatabaseAdapter {
213
228
  const connection = await this.pool.getConnection();
214
229
 
215
230
  try {
216
- if (database && database !== this.config.database) {
217
- await connection.query(`USE ${mysql.escapeId(database)}`);
231
+ const targetDb = this.getDatabaseName(database);
232
+ if (targetDb !== this.config.database) {
233
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
218
234
  }
219
235
 
220
236
  // Build bulk insert query
@@ -258,8 +274,9 @@ export class MySQLAdapter implements DatabaseAdapter {
258
274
  const connection = await this.pool.getConnection();
259
275
 
260
276
  try {
261
- if (database && database !== this.config.database) {
262
- await connection.query(`USE ${mysql.escapeId(database)}`);
277
+ const targetDb = this.getDatabaseName(database);
278
+ if (targetDb !== this.config.database) {
279
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
263
280
  }
264
281
 
265
282
  // Build SET clause
@@ -297,8 +314,9 @@ export class MySQLAdapter implements DatabaseAdapter {
297
314
  const connection = await this.pool.getConnection();
298
315
 
299
316
  try {
300
- if (database && database !== this.config.database) {
301
- await connection.query(`USE ${mysql.escapeId(database)}`);
317
+ const targetDb = this.getDatabaseName(database);
318
+ if (targetDb !== this.config.database) {
319
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
302
320
  }
303
321
 
304
322
  // Build query
@@ -365,8 +383,9 @@ export class MySQLAdapter implements DatabaseAdapter {
365
383
  const connection = await this.pool.getConnection();
366
384
 
367
385
  try {
368
- if (database && database !== this.config.database) {
369
- await connection.query(`USE ${mysql.escapeId(database)}`);
386
+ const targetDb = this.getDatabaseName(database);
387
+ if (targetDb !== this.config.database) {
388
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
370
389
  }
371
390
 
372
391
  const columnDefs = columns.map((col) => {
@@ -394,8 +413,9 @@ export class MySQLAdapter implements DatabaseAdapter {
394
413
  const connection = await this.pool.getConnection();
395
414
 
396
415
  try {
397
- if (database && database !== this.config.database) {
398
- await connection.query(`USE ${mysql.escapeId(database)}`);
416
+ const targetDb = this.getDatabaseName(database);
417
+ if (targetDb !== this.config.database) {
418
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
399
419
  }
400
420
 
401
421
  const query = ifExists
@@ -416,11 +436,9 @@ export class MySQLAdapter implements DatabaseAdapter {
416
436
  const connection = await this.pool.getConnection();
417
437
 
418
438
  try {
419
- const dbName = database || this.config.database;
420
- if (!dbName) throw new Error('Database name required');
421
-
422
- if (database && database !== this.config.database) {
423
- await connection.query(`USE ${mysql.escapeId(database)}`);
439
+ const dbName = this.getDatabaseName(database);
440
+ if (dbName !== this.config.database) {
441
+ await connection.query(`USE ${mysql.escapeId(dbName)}`);
424
442
  }
425
443
 
426
444
  // Get row count
@@ -475,8 +493,9 @@ export class MySQLAdapter implements DatabaseAdapter {
475
493
  const connection = await this.pool.getConnection();
476
494
 
477
495
  try {
478
- if (database && database !== this.config.database) {
479
- await connection.query(`USE ${mysql.escapeId(database)}`);
496
+ const targetDb = this.getDatabaseName(database);
497
+ if (targetDb !== this.config.database) {
498
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
480
499
  }
481
500
 
482
501
  // Get total row count
@@ -514,8 +533,9 @@ export class MySQLAdapter implements DatabaseAdapter {
514
533
  const connection = await this.pool.getConnection();
515
534
 
516
535
  try {
517
- if (database && database !== this.config.database) {
518
- await connection.query(`USE ${mysql.escapeId(database)}`);
536
+ const targetDb = this.getDatabaseName(database);
537
+ if (targetDb !== this.config.database) {
538
+ await connection.query(`USE ${mysql.escapeId(targetDb)}`);
519
539
  }
520
540
 
521
541
  const [rows] = await connection.query(
@@ -31,6 +31,20 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
31
31
  this.config = config;
32
32
  }
33
33
 
34
+ /**
35
+ * Get the final database name to use.
36
+ * Priority: 1. Parameter passed in method call, 2. Config database (from environment variable)
37
+ */
38
+ private getDatabaseName(paramDb?: string): string {
39
+ if (paramDb && paramDb.trim().length > 0) {
40
+ return paramDb.trim();
41
+ }
42
+ if (this.config.database && this.config.database.trim().length > 0) {
43
+ return this.config.database;
44
+ }
45
+ throw new Error('Database name is required. Either set EASYDB_DATABASE environment variable or pass the database parameter.');
46
+ }
47
+
34
48
  async connect(): Promise<void> {
35
49
  this.pool = new Pool({
36
50
  host: this.config.host,
@@ -49,9 +63,8 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
49
63
  async listTables(database?: string): Promise<TableInfo[]> {
50
64
  if (!this.pool) throw new Error('Not connected');
51
65
 
52
- const dbName = database || this.config.database;
53
- if (!dbName) throw new Error('Database name required');
54
-
66
+ // Note: In PostgreSQL, the pool is connected to a specific database
67
+ // For cross-database queries, use the default pool connected to config.database
55
68
  const result = await this.pool.query(
56
69
  `SELECT tablename as name, n_live_tup as "rowCount"
57
70
  FROM pg_stat_user_tables
@@ -67,8 +80,6 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
67
80
  async describeTable(table: string, database?: string): Promise<TableColumn[]> {
68
81
  if (!this.pool) throw new Error('Not connected');
69
82
 
70
- const dbName = database || this.config.database;
71
-
72
83
  const result = await this.pool.query(
73
84
  `SELECT
74
85
  column_name as name,
@@ -107,8 +118,8 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
107
118
  }
108
119
 
109
120
  // If database is specified and different from default, use it
110
- const dbName = database || this.config.database;
111
- if (dbName && dbName !== this.config.database) {
121
+ const targetDb = this.getDatabaseName(database);
122
+ if (targetDb !== this.config.database) {
112
123
  // For PostgreSQL, we need to connect to the specific database
113
124
  // Create a temporary pool for the different database
114
125
  const tempPool = new Pool({
@@ -116,7 +127,7 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
116
127
  port: this.config.port || 5432,
117
128
  user: this.config.user,
118
129
  password: this.config.password,
119
- database: dbName,
130
+ database: targetDb,
120
131
  max: 1,
121
132
  });
122
133
 
@@ -143,8 +154,8 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
143
154
  if (!this.pool) throw new Error('Not connected');
144
155
 
145
156
  // If database is specified and different from default, use it
146
- const dbName = database || this.config.database;
147
- if (dbName && dbName !== this.config.database) {
157
+ const targetDb = this.getDatabaseName(database);
158
+ if (targetDb !== this.config.database) {
148
159
  // For PostgreSQL, we need to connect to the specific database
149
160
  // Create a temporary pool for the different database
150
161
  const tempPool = new Pool({
@@ -152,7 +163,7 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
152
163
  port: this.config.port || 5432,
153
164
  user: this.config.user,
154
165
  password: this.config.password,
155
- database: dbName,
166
+ database: targetDb,
156
167
  max: 1,
157
168
  });
158
169
 
@@ -197,13 +208,14 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
197
208
  let client: pg.PoolClient | null = null;
198
209
 
199
210
  // If different database, use temporary connection
200
- if (database && database !== this.config.database) {
211
+ const targetDb = this.getDatabaseName(database);
212
+ if (targetDb !== this.config.database) {
201
213
  const tempPool = new pg.Pool({
202
214
  host: this.config.host,
203
215
  port: this.config.port || 5432,
204
216
  user: this.config.user,
205
217
  password: this.config.password,
206
- database,
218
+ database: targetDb,
207
219
  max: 1,
208
220
  });
209
221
 
@@ -282,16 +294,16 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
282
294
  }
283
295
 
284
296
  let pool: pg.Pool = this.pool;
285
- let dbName = database || this.config.database;
286
297
 
287
298
  // Use temporary pool for different database
288
- if (database && database !== this.config.database) {
299
+ const targetDb = this.getDatabaseName(database);
300
+ if (targetDb !== this.config.database) {
289
301
  pool = new pg.Pool({
290
302
  host: this.config.host,
291
303
  port: this.config.port || 5432,
292
304
  user: this.config.user,
293
305
  password: this.config.password,
294
- database,
306
+ database: targetDb,
295
307
  max: 1,
296
308
  });
297
309
  }
@@ -343,13 +355,14 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
343
355
 
344
356
  let pool: pg.Pool = this.pool;
345
357
 
346
- if (database && database !== this.config.database) {
358
+ const targetDb = this.getDatabaseName(database);
359
+ if (targetDb !== this.config.database) {
347
360
  pool = new pg.Pool({
348
361
  host: this.config.host,
349
362
  port: this.config.port || 5432,
350
363
  user: this.config.user,
351
364
  password: this.config.password,
352
- database,
365
+ database: targetDb,
353
366
  max: 1,
354
367
  });
355
368
  }
@@ -389,13 +402,14 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
389
402
 
390
403
  let pool: pg.Pool = this.pool;
391
404
 
392
- if (database && database !== this.config.database) {
405
+ const targetDb = this.getDatabaseName(database);
406
+ if (targetDb !== this.config.database) {
393
407
  pool = new pg.Pool({
394
408
  host: this.config.host,
395
409
  port: this.config.port || 5432,
396
410
  user: this.config.user,
397
411
  password: this.config.password,
398
- database,
412
+ database: targetDb,
399
413
  max: 1,
400
414
  });
401
415
  }
@@ -463,13 +477,14 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
463
477
 
464
478
  let pool: pg.Pool = this.pool;
465
479
 
466
- if (database && database !== this.config.database) {
480
+ const targetDb = this.getDatabaseName(database);
481
+ if (targetDb !== this.config.database) {
467
482
  pool = new pg.Pool({
468
483
  host: this.config.host,
469
484
  port: this.config.port || 5432,
470
485
  user: this.config.user,
471
486
  password: this.config.password,
472
- database,
487
+ database: targetDb,
473
488
  max: 1,
474
489
  });
475
490
  }
@@ -502,13 +517,14 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
502
517
 
503
518
  let pool: pg.Pool = this.pool;
504
519
 
505
- if (database && database !== this.config.database) {
520
+ const targetDb = this.getDatabaseName(database);
521
+ if (targetDb !== this.config.database) {
506
522
  pool = new pg.Pool({
507
523
  host: this.config.host,
508
524
  port: this.config.port || 5432,
509
525
  user: this.config.user,
510
526
  password: this.config.password,
511
- database,
527
+ database: targetDb,
512
528
  max: 1,
513
529
  });
514
530
  }
@@ -534,13 +550,14 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
534
550
 
535
551
  let pool: pg.Pool = this.pool;
536
552
 
537
- if (database && database !== this.config.database) {
553
+ const targetDb = this.getDatabaseName(database);
554
+ if (targetDb !== this.config.database) {
538
555
  pool = new pg.Pool({
539
556
  host: this.config.host,
540
557
  port: this.config.port || 5432,
541
558
  user: this.config.user,
542
559
  password: this.config.password,
543
- database,
560
+ database: targetDb,
544
561
  max: 1,
545
562
  });
546
563
  }
@@ -599,13 +616,14 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
599
616
 
600
617
  let pool: pg.Pool = this.pool;
601
618
 
602
- if (database && database !== this.config.database) {
619
+ const targetDb = this.getDatabaseName(database);
620
+ if (targetDb !== this.config.database) {
603
621
  pool = new pg.Pool({
604
622
  host: this.config.host,
605
623
  port: this.config.port || 5432,
606
624
  user: this.config.user,
607
625
  password: this.config.password,
608
- database,
626
+ database: targetDb,
609
627
  max: 1,
610
628
  });
611
629
  }
@@ -648,13 +666,14 @@ export class PostgreSQLAdapter implements DatabaseAdapter {
648
666
 
649
667
  let pool: pg.Pool = this.pool;
650
668
 
651
- if (database && database !== this.config.database) {
669
+ const targetDb = this.getDatabaseName(database);
670
+ if (targetDb !== this.config.database) {
652
671
  pool = new pg.Pool({
653
672
  host: this.config.host,
654
673
  port: this.config.port || 5432,
655
674
  user: this.config.user,
656
675
  password: this.config.password,
657
- database,
676
+ database: targetDb,
658
677
  max: 1,
659
678
  });
660
679
  }
package/src/index.ts CHANGED
@@ -10,7 +10,7 @@ import type { DatabaseAdapter } from './database/base.js';
10
10
  const server = new McpServer(
11
11
  {
12
12
  name: 'hyqf98@easy_db_mcp_server',
13
- version: '2.0.0',
13
+ version: '2.0.2',
14
14
  },
15
15
  {
16
16
  capabilities: {
@@ -34,13 +34,19 @@ try {
34
34
  }
35
35
 
36
36
  // Helper function to validate database parameter
37
+ // Priority: 1. Parameter passed in method call, 2. Environment variable EASYDB_DATABASE
37
38
  const getDatabase = (paramDb?: string): string => {
38
- if (paramDb) {
39
- return paramDb;
39
+ // If parameter is provided (non-empty string), use it
40
+ if (paramDb && paramDb.trim().length > 0) {
41
+ console.error(`[Database] Using parameter-specified database: ${paramDb}`);
42
+ return paramDb.trim();
40
43
  }
41
- if (cachedConfig.database) {
44
+ // Fall back to environment variable
45
+ if (cachedConfig.database && cachedConfig.database.trim().length > 0) {
46
+ console.error(`[Database] Using environment variable database: ${cachedConfig.database}`);
42
47
  return cachedConfig.database;
43
48
  }
49
+ // Neither is available
44
50
  throw new Error(
45
51
  'Database name is required. Either set EASYDB_DATABASE environment variable or pass the database parameter in the tool call.'
46
52
  );