@berthojoris/mcp-mysql-server 1.46.0 → 1.47.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/DOCUMENTATIONS.md +26 -2
  3. package/README.md +13 -1
  4. package/bin/mcp-mysql.js +3 -2
  5. package/dist/config/config.d.ts +1 -0
  6. package/dist/config/config.js +4 -0
  7. package/dist/config/databaseScope.d.ts +10 -0
  8. package/dist/config/databaseScope.js +36 -0
  9. package/dist/index.d.ts +2 -0
  10. package/dist/mcp-server.js +12 -0
  11. package/dist/security/databaseQueryScope.d.ts +5 -0
  12. package/dist/security/databaseQueryScope.js +184 -0
  13. package/dist/security/securityLayer.js +5 -0
  14. package/dist/tools/analysisTools.js +2 -26
  15. package/dist/tools/constraintTools.js +2 -26
  16. package/dist/tools/crudTools.d.ts +1 -0
  17. package/dist/tools/crudTools.js +6 -1
  18. package/dist/tools/dataExportTools.d.ts +1 -0
  19. package/dist/tools/dataExportTools.js +7 -21
  20. package/dist/tools/databaseTools.d.ts +1 -0
  21. package/dist/tools/databaseTools.js +23 -62
  22. package/dist/tools/fulltextSearchTools.js +2 -26
  23. package/dist/tools/indexTools.js +2 -26
  24. package/dist/tools/maintenanceTools.js +2 -26
  25. package/dist/tools/migrationTools.js +2 -20
  26. package/dist/tools/relationalSeederTools.js +2 -9
  27. package/dist/tools/smartQueryBuilderTools.d.ts +2 -0
  28. package/dist/tools/smartQueryBuilderTools.js +10 -29
  29. package/dist/tools/storedProcedureTools.js +2 -28
  30. package/dist/tools/toolArgumentValidation.js +22 -1
  31. package/dist/tools/triggerTools.js +2 -26
  32. package/dist/tools/utilityTools.js +6 -1
  33. package/dist/tools/viewTools.js +2 -26
  34. package/dist/validation/schemas.d.ts +6 -0
  35. package/dist/validation/schemas.js +3 -1
  36. package/package.json +1 -1
@@ -6,7 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.DatabaseTools = void 0;
7
7
  const connection_1 = __importDefault(require("../db/connection"));
8
8
  const schemas_1 = require("../validation/schemas");
9
- const config_1 = require("../config/config");
9
+ const databaseScope_1 = require("../config/databaseScope");
10
+ const quoteDatabase = (database) => `\`${database.replace(/`/g, "``")}\``;
10
11
  class DatabaseTools {
11
12
  constructor() {
12
13
  this.db = connection_1.default.getInstance();
@@ -17,26 +18,16 @@ class DatabaseTools {
17
18
  */
18
19
  async listDatabases() {
19
20
  try {
20
- // Only return the database specified in the connection string
21
- // This is a security measure to prevent access to other databases
22
- if (!config_1.dbConfig.database) {
21
+ const databases = (0, databaseScope_1.getAllowedDatabases)();
22
+ if (databases.length === 0) {
23
23
  return {
24
24
  status: "error",
25
- error: "No database specified in connection string. Please specify a database name in your MySQL connection URL.",
26
- };
27
- }
28
- // Verify the database exists and is accessible
29
- const results = await this.db.query("SELECT DATABASE() as current_database");
30
- const currentDatabase = results[0]?.current_database;
31
- if (!currentDatabase) {
32
- return {
33
- status: "error",
34
- error: "No database selected. Please ensure your connection string includes a valid database name.",
25
+ error: "No database specified in the connection string or database allowlist.",
35
26
  };
36
27
  }
37
28
  return {
38
29
  status: "success",
39
- data: [currentDatabase],
30
+ data: databases,
40
31
  };
41
32
  }
42
33
  catch (error) {
@@ -58,26 +49,11 @@ class DatabaseTools {
58
49
  };
59
50
  }
60
51
  try {
61
- // Security validation: if database is specified, ensure it matches the connected database
62
- if (params.database) {
63
- if (!config_1.dbConfig.database) {
64
- return {
65
- status: "error",
66
- error: "No database specified in connection string. Cannot access other databases.",
67
- };
68
- }
69
- if (params.database !== config_1.dbConfig.database) {
70
- return {
71
- status: "error",
72
- error: `Access denied. You can only access the connected database '${config_1.dbConfig.database}'. Requested database '${params.database}' is not allowed.`,
73
- };
74
- }
75
- }
76
- let query = "SHOW TABLES";
77
- // If database is specified and validated, use it
78
- if (params.database) {
79
- query = `SHOW TABLES FROM \`${params.database}\``;
52
+ const databaseAccess = (0, databaseScope_1.validateDatabaseAccess)(params.database);
53
+ if (!databaseAccess.valid) {
54
+ return { status: "error", error: databaseAccess.error };
80
55
  }
56
+ const query = `SHOW TABLES FROM ${quoteDatabase(databaseAccess.database)}`;
81
57
  const results = await this.db.query(query);
82
58
  const tables = results.map((row) => {
83
59
  // Extract the table name from the first column (which might have different names)
@@ -109,6 +85,10 @@ class DatabaseTools {
109
85
  };
110
86
  }
111
87
  try {
88
+ const databaseAccess = (0, databaseScope_1.validateDatabaseAccess)(params.database);
89
+ if (!databaseAccess.valid) {
90
+ return { status: "error", error: databaseAccess.error };
91
+ }
112
92
  const query = `
113
93
  SELECT
114
94
  COLUMN_NAME as column_name,
@@ -126,10 +106,7 @@ class DatabaseTools {
126
106
  ORDER BY
127
107
  ORDINAL_POSITION
128
108
  `;
129
- const results = await this.db.query(query, [
130
- config_1.dbConfig.database,
131
- params.table_name,
132
- ]);
109
+ const results = await this.db.query(query, [databaseAccess.database, params.table_name]);
133
110
  return {
134
111
  status: "success",
135
112
  data: results,
@@ -149,19 +126,11 @@ class DatabaseTools {
149
126
  async getDatabaseSummary(params) {
150
127
  try {
151
128
  // Security validation
152
- if (params.database && params.database !== config_1.dbConfig.database) {
153
- return {
154
- status: "error",
155
- error: `Access denied. You can only access the connected database '${config_1.dbConfig.database}'.`,
156
- };
157
- }
158
- const database = params.database || config_1.dbConfig.database;
159
- if (!database) {
160
- return {
161
- status: "error",
162
- error: "No database specified and none connected.",
163
- };
129
+ const access = (0, databaseScope_1.validateDatabaseAccess)(params.database);
130
+ if (!access.valid) {
131
+ return { status: "error", error: access.error };
164
132
  }
133
+ const database = access.database;
165
134
  const maxTables = params.max_tables ? Math.min(Math.max(params.max_tables, 1), 500) : undefined;
166
135
  const includeRelationships = params.include_relationships ?? true;
167
136
  // Get total table count first
@@ -287,19 +256,11 @@ class DatabaseTools {
287
256
  async getSchemaERD(params) {
288
257
  try {
289
258
  // Security validation
290
- if (params.database && params.database !== config_1.dbConfig.database) {
291
- return {
292
- status: "error",
293
- error: `Access denied. You can only access the connected database '${config_1.dbConfig.database}'.`,
294
- };
295
- }
296
- const database = params.database || config_1.dbConfig.database;
297
- if (!database) {
298
- return {
299
- status: "error",
300
- error: "No database specified and none connected.",
301
- };
259
+ const access = (0, databaseScope_1.validateDatabaseAccess)(params.database);
260
+ if (!access.valid) {
261
+ return { status: "error", error: access.error };
302
262
  }
263
+ const database = access.database;
303
264
  // Get tables and columns
304
265
  const columnsQuery = `
305
266
  SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_KEY
@@ -4,39 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.FulltextSearchTools = void 0;
7
+ const databaseScope_1 = require("../config/databaseScope");
7
8
  const connection_1 = __importDefault(require("../db/connection"));
8
- const config_1 = require("../config/config");
9
9
  class FulltextSearchTools {
10
10
  constructor(security) {
11
11
  this.db = connection_1.default.getInstance();
12
12
  this.security = security;
13
13
  }
14
14
  validateDatabaseAccess(requestedDatabase) {
15
- const connectedDatabase = config_1.dbConfig.database;
16
- if (!connectedDatabase) {
17
- return {
18
- valid: false,
19
- database: "",
20
- error: "No database specified in connection string. Cannot access any database.",
21
- };
22
- }
23
- if (!requestedDatabase) {
24
- return {
25
- valid: true,
26
- database: connectedDatabase,
27
- };
28
- }
29
- if (requestedDatabase !== connectedDatabase) {
30
- return {
31
- valid: false,
32
- database: "",
33
- error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
34
- };
35
- }
36
- return {
37
- valid: true,
38
- database: connectedDatabase,
39
- };
15
+ return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
40
16
  }
41
17
  async createFulltextIndex(params) {
42
18
  try {
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.IndexTools = void 0;
7
+ const databaseScope_1 = require("../config/databaseScope");
7
8
  const connection_1 = __importDefault(require("../db/connection"));
8
- const config_1 = require("../config/config");
9
9
  class IndexTools {
10
10
  constructor(security) {
11
11
  this.db = connection_1.default.getInstance();
@@ -15,31 +15,7 @@ class IndexTools {
15
15
  * Validate database access - ensures only the connected database can be accessed
16
16
  */
17
17
  validateDatabaseAccess(requestedDatabase) {
18
- const connectedDatabase = config_1.dbConfig.database;
19
- if (!connectedDatabase) {
20
- return {
21
- valid: false,
22
- database: "",
23
- error: "No database specified in connection string. Cannot access any database.",
24
- };
25
- }
26
- if (!requestedDatabase) {
27
- return {
28
- valid: true,
29
- database: connectedDatabase,
30
- };
31
- }
32
- if (requestedDatabase !== connectedDatabase) {
33
- return {
34
- valid: false,
35
- database: "",
36
- error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
37
- };
38
- }
39
- return {
40
- valid: true,
41
- database: connectedDatabase,
42
- };
18
+ return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
43
19
  }
44
20
  /**
45
21
  * List all indexes for a table
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.MaintenanceTools = void 0;
7
+ const databaseScope_1 = require("../config/databaseScope");
7
8
  const connection_1 = __importDefault(require("../db/connection"));
8
- const config_1 = require("../config/config");
9
9
  class MaintenanceTools {
10
10
  constructor(security) {
11
11
  this.db = connection_1.default.getInstance();
@@ -15,31 +15,7 @@ class MaintenanceTools {
15
15
  * Validate database access - ensures only the connected database can be accessed
16
16
  */
17
17
  validateDatabaseAccess(requestedDatabase) {
18
- const connectedDatabase = config_1.dbConfig.database;
19
- if (!connectedDatabase) {
20
- return {
21
- valid: false,
22
- database: "",
23
- error: "No database specified in connection string. Cannot access any database.",
24
- };
25
- }
26
- if (!requestedDatabase) {
27
- return {
28
- valid: true,
29
- database: connectedDatabase,
30
- };
31
- }
32
- if (requestedDatabase !== connectedDatabase) {
33
- return {
34
- valid: false,
35
- database: "",
36
- error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
37
- };
38
- }
39
- return {
40
- valid: true,
41
- database: connectedDatabase,
42
- };
18
+ return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
43
19
  }
44
20
  /**
45
21
  * Analyze table to update index statistics
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.MigrationTools = void 0;
7
+ const databaseScope_1 = require("../config/databaseScope");
7
8
  const connection_1 = __importDefault(require("../db/connection"));
8
- const config_1 = require("../config/config");
9
9
  /**
10
10
  * Data Migration Tools for MySQL MCP Server
11
11
  * Provides utilities for copying, moving, and transforming data between tables
@@ -19,25 +19,7 @@ class MigrationTools {
19
19
  * Validate database access
20
20
  */
21
21
  validateDatabaseAccess(requestedDatabase) {
22
- const connectedDatabase = config_1.dbConfig.database;
23
- if (!connectedDatabase) {
24
- return {
25
- valid: false,
26
- database: "",
27
- error: "No database configured. Please specify a database in your connection settings.",
28
- };
29
- }
30
- if (requestedDatabase && requestedDatabase !== connectedDatabase) {
31
- return {
32
- valid: false,
33
- database: "",
34
- error: `Access denied: You are connected to '${connectedDatabase}' but requested '${requestedDatabase}'. Cross-database access is not permitted.`,
35
- };
36
- }
37
- return {
38
- valid: true,
39
- database: connectedDatabase,
40
- };
22
+ return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
41
23
  }
42
24
  /**
43
25
  * Escape string value for SQL
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.RelationalSeederTools = void 0;
7
7
  const connection_1 = __importDefault(require("../db/connection"));
8
- const config_1 = require("../config/config");
8
+ const databaseScope_1 = require("../config/databaseScope");
9
9
  class RelationalSeederTools {
10
10
  constructor(security) {
11
11
  this.db = connection_1.default.getInstance();
@@ -439,14 +439,7 @@ class RelationalSeederTools {
439
439
  }
440
440
  }
441
441
  resolveDatabase(database) {
442
- const connectedDatabase = config_1.dbConfig.database;
443
- if (!connectedDatabase) {
444
- throw new Error("No database configured. Set DB_NAME or include a database in the MySQL URL.");
445
- }
446
- if (database && database !== connectedDatabase) {
447
- throw new Error(`Access denied. You can only access the connected database '${connectedDatabase}'. Requested '${database}'.`);
448
- }
449
- return database || connectedDatabase;
442
+ return (0, databaseScope_1.requireAllowedDatabase)(database);
450
443
  }
451
444
  async loadSchema(database) {
452
445
  const columns = await this.db.query(`
@@ -1,6 +1,7 @@
1
1
  import { SecurityLayer } from "../security/securityLayer";
2
2
  export interface QueryBuildingState {
3
3
  session_id: string;
4
+ database?: string;
4
5
  intent: string;
5
6
  context?: "analytics" | "reporting" | "data_entry" | "schema_exploration";
6
7
  tables: string[];
@@ -80,6 +81,7 @@ export declare class SmartQueryBuilderTools {
80
81
  private security;
81
82
  constructor(security: SecurityLayer);
82
83
  private validateDatabaseAccess;
84
+ private quoteTableReference;
83
85
  private getDatabaseSchema;
84
86
  private generateSessionId;
85
87
  private findQueryTemplate;
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.SmartQueryBuilderTools = void 0;
7
+ const databaseScope_1 = require("../config/databaseScope");
7
8
  const connection_1 = __importDefault(require("../db/connection"));
8
- const config_1 = require("../config/config");
9
9
  const queryTemplates = [
10
10
  {
11
11
  name: "Customer Analysis",
@@ -66,31 +66,11 @@ class SmartQueryBuilderTools {
66
66
  this.security = security;
67
67
  }
68
68
  validateDatabaseAccess(requestedDatabase) {
69
- const connectedDatabase = config_1.dbConfig.database;
70
- if (!connectedDatabase) {
71
- return {
72
- valid: false,
73
- database: "",
74
- error: "No database specified in connection string. Cannot access any database.",
75
- };
76
- }
77
- if (!requestedDatabase) {
78
- return {
79
- valid: true,
80
- database: connectedDatabase,
81
- };
82
- }
83
- if (requestedDatabase !== connectedDatabase) {
84
- return {
85
- valid: false,
86
- database: "",
87
- error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
88
- };
89
- }
90
- return {
91
- valid: true,
92
- database: connectedDatabase,
93
- };
69
+ return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
70
+ }
71
+ quoteTableReference(database, table) {
72
+ const quote = (value) => `\`${value.replace(/`/g, "``")}\``;
73
+ return database ? `${quote(database)}.${quote(table)}` : quote(table);
94
74
  }
95
75
  async getDatabaseSchema(database) {
96
76
  const tables = await this.db.query(`SELECT TABLE_NAME, TABLE_ROWS
@@ -185,9 +165,9 @@ class SmartQueryBuilderTools {
185
165
  const columnList = state.selected_columns.map(col => col.alias ? `${col.table}.${col.column} AS ${col.alias}` : `${col.table}.${col.column}`).join(", ");
186
166
  query += columnList;
187
167
  }
188
- query += ` FROM ${state.tables[0]}`;
168
+ query += ` FROM ${this.quoteTableReference(state.database, state.tables[0])}`;
189
169
  for (const join of state.joins) {
190
- query += ` ${join.join_type} JOIN ${join.to_table} ON ${join.from_table}.${join.from_column} = ${join.to_table}.${join.to_column}`;
170
+ query += ` ${join.join_type} JOIN ${this.quoteTableReference(state.database, join.to_table)} ON ${join.from_table}.${join.from_column} = ${join.to_table}.${join.to_column}`;
191
171
  }
192
172
  if (state.conditions.length > 0) {
193
173
  query += " WHERE ";
@@ -218,7 +198,7 @@ class SmartQueryBuilderTools {
218
198
  query += groupList;
219
199
  }
220
200
  if (state.aggregations.length > 0 && state.group_by.length === 0) {
221
- query = "SELECT " + state.aggregations.map(agg => `${agg.function}(${agg.table}.${agg.column}) AS ${agg.alias}`).join(", ") + ` FROM ${state.tables[0]}`;
201
+ query = "SELECT " + state.aggregations.map(agg => `${agg.function}(${agg.table}.${agg.column}) AS ${agg.alias}`).join(", ") + ` FROM ${this.quoteTableReference(state.database, state.tables[0])}`;
222
202
  }
223
203
  if (state.conditions.length > 0 && state.aggregations.length > 0) {
224
204
  const whereIndex = query.indexOf(" WHERE ");
@@ -280,6 +260,7 @@ class SmartQueryBuilderTools {
280
260
  current_step: "select_tables",
281
261
  state: {
282
262
  session_id: sessionId,
263
+ database,
283
264
  intent,
284
265
  context,
285
266
  tables: [],
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.StoredProcedureTools = void 0;
7
+ const databaseScope_1 = require("../config/databaseScope");
7
8
  const connection_1 = __importDefault(require("../db/connection"));
8
- const config_1 = require("../config/config");
9
9
  const schemas_1 = require("../validation/schemas");
10
10
  class StoredProcedureTools {
11
11
  constructor(security) {
@@ -16,33 +16,7 @@ class StoredProcedureTools {
16
16
  * Validate database access - ensures only the connected database can be accessed
17
17
  */
18
18
  validateDatabaseAccess(requestedDatabase) {
19
- const connectedDatabase = config_1.dbConfig.database;
20
- if (!connectedDatabase) {
21
- return {
22
- valid: false,
23
- database: "",
24
- error: "No database specified in connection string. Cannot access any database.",
25
- };
26
- }
27
- // If no database is requested, use the connected database
28
- if (!requestedDatabase) {
29
- return {
30
- valid: true,
31
- database: connectedDatabase,
32
- };
33
- }
34
- // If a specific database is requested, ensure it matches the connected database
35
- if (requestedDatabase !== connectedDatabase) {
36
- return {
37
- valid: false,
38
- database: "",
39
- error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
40
- };
41
- }
42
- return {
43
- valid: true,
44
- database: connectedDatabase,
45
- };
19
+ return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
46
20
  }
47
21
  /**
48
22
  * List all stored procedures in the current database
@@ -217,7 +217,15 @@ function validateToolArguments(name, args) {
217
217
  case "create_record":
218
218
  return (0, inputValidation_1.validateCreateRecord)(args);
219
219
  case "read_records":
220
- return (0, inputValidation_1.validateReadRecords)(args);
220
+ {
221
+ const result = (0, inputValidation_1.validateReadRecords)(args);
222
+ if (!result.valid)
223
+ return result;
224
+ if (args.database !== undefined && !(0, inputValidation_1.validateValue)(args.database).valid) {
225
+ return { valid: false, errors: ["Invalid database name"] };
226
+ }
227
+ return result;
228
+ }
221
229
  case "update_record":
222
230
  return (0, inputValidation_1.validateUpdateRecord)(args);
223
231
  case "delete_record":
@@ -266,6 +274,9 @@ function validateToolArguments(name, args) {
266
274
  return { valid: true };
267
275
  case "get_column_statistics":
268
276
  case "read_table_schema":
277
+ if (args.database !== undefined && !(0, inputValidation_1.validateValue)(args.database).valid) {
278
+ return { valid: false, errors: ["Invalid database name"] };
279
+ }
269
280
  if (args.table_name) {
270
281
  const validation = (0, inputValidation_1.validateTableName)(args.table_name);
271
282
  if (!validation.valid) {
@@ -276,6 +287,16 @@ function validateToolArguments(name, args) {
276
287
  }
277
288
  }
278
289
  return { valid: true };
290
+ case "export_table_to_csv":
291
+ if (args.database !== undefined && !(0, inputValidation_1.validateValue)(args.database).valid) {
292
+ return { valid: false, errors: ["Invalid database name"] };
293
+ }
294
+ if (args.table_name) {
295
+ const validation = (0, inputValidation_1.validateTableName)(args.table_name);
296
+ if (!validation.valid)
297
+ return { valid: false, errors: [validation.error || "Invalid table name"] };
298
+ }
299
+ return { valid: true };
279
300
  default:
280
301
  return { valid: true };
281
302
  }
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.TriggerTools = void 0;
7
+ const databaseScope_1 = require("../config/databaseScope");
7
8
  const connection_1 = __importDefault(require("../db/connection"));
8
- const config_1 = require("../config/config");
9
9
  class TriggerTools {
10
10
  constructor(security) {
11
11
  this.db = connection_1.default.getInstance();
@@ -15,31 +15,7 @@ class TriggerTools {
15
15
  * Validate database access - ensures only the connected database can be accessed
16
16
  */
17
17
  validateDatabaseAccess(requestedDatabase) {
18
- const connectedDatabase = config_1.dbConfig.database;
19
- if (!connectedDatabase) {
20
- return {
21
- valid: false,
22
- database: "",
23
- error: "No database specified in connection string. Cannot access any database.",
24
- };
25
- }
26
- if (!requestedDatabase) {
27
- return {
28
- valid: true,
29
- database: connectedDatabase,
30
- };
31
- }
32
- if (requestedDatabase !== connectedDatabase) {
33
- return {
34
- valid: false,
35
- database: "",
36
- error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
37
- };
38
- }
39
- return {
40
- valid: true,
41
- database: connectedDatabase,
42
- };
18
+ return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
43
19
  }
44
20
  /**
45
21
  * List all triggers in the current database
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.UtilityTools = void 0;
7
7
  const connection_1 = __importDefault(require("../db/connection"));
8
8
  const config_1 = require("../config/config");
9
+ const databaseScope_1 = require("../config/databaseScope");
9
10
  const schemas_1 = require("../validation/schemas");
10
11
  const fs_1 = __importDefault(require("fs"));
11
12
  const path_1 = __importDefault(require("path"));
@@ -216,7 +217,11 @@ class UtilityTools {
216
217
  */
217
218
  async getAllTablesRelationships(params) {
218
219
  try {
219
- const databaseName = params?.database || this.db.pool.pool.config.connectionConfig.database;
220
+ const databaseAccess = (0, databaseScope_1.validateDatabaseAccess)(params?.database);
221
+ if (!databaseAccess.valid) {
222
+ return { status: "error", error: databaseAccess.error };
223
+ }
224
+ const databaseName = databaseAccess.database;
220
225
  // Get all tables in the database
221
226
  const tablesQuery = `
222
227
  SELECT TABLE_NAME as table_name
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ViewTools = void 0;
7
+ const databaseScope_1 = require("../config/databaseScope");
7
8
  const connection_1 = __importDefault(require("../db/connection"));
8
- const config_1 = require("../config/config");
9
9
  class ViewTools {
10
10
  constructor(security) {
11
11
  this.db = connection_1.default.getInstance();
@@ -15,31 +15,7 @@ class ViewTools {
15
15
  * Validate database access - ensures only the connected database can be accessed
16
16
  */
17
17
  validateDatabaseAccess(requestedDatabase) {
18
- const connectedDatabase = config_1.dbConfig.database;
19
- if (!connectedDatabase) {
20
- return {
21
- valid: false,
22
- database: "",
23
- error: "No database specified in connection string. Cannot access any database.",
24
- };
25
- }
26
- if (!requestedDatabase) {
27
- return {
28
- valid: true,
29
- database: connectedDatabase,
30
- };
31
- }
32
- if (requestedDatabase !== connectedDatabase) {
33
- return {
34
- valid: false,
35
- database: "",
36
- error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
37
- };
38
- }
39
- return {
40
- valid: true,
41
- database: connectedDatabase,
42
- };
18
+ return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
43
19
  }
44
20
  /**
45
21
  * List all views in the current database
@@ -39,6 +39,9 @@ export declare const readTableSchemaSchema: {
39
39
  table_name: {
40
40
  type: string;
41
41
  };
42
+ database: {
43
+ type: string;
44
+ };
42
45
  };
43
46
  additionalProperties: boolean;
44
47
  };
@@ -63,6 +66,9 @@ export declare const readRecordsSchema: {
63
66
  table_name: {
64
67
  type: string;
65
68
  };
69
+ database: {
70
+ type: string;
71
+ };
66
72
  filters: {
67
73
  type: string;
68
74
  items: {
@@ -19,7 +19,8 @@ exports.readTableSchemaSchema = {
19
19
  type: 'object',
20
20
  required: ['table_name'],
21
21
  properties: {
22
- table_name: { type: 'string' }
22
+ table_name: { type: 'string' },
23
+ database: { type: 'string' }
23
24
  },
24
25
  additionalProperties: false
25
26
  };
@@ -40,6 +41,7 @@ exports.readRecordsSchema = {
40
41
  required: ['table_name'],
41
42
  properties: {
42
43
  table_name: { type: 'string' },
44
+ database: { type: 'string' },
43
45
  filters: {
44
46
  type: 'array',
45
47
  items: {