@berthojoris/mcp-mysql-server 1.45.2 → 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.
- package/CHANGELOG.md +19 -0
- package/DOCUMENTATIONS.md +38 -8
- package/README.md +19 -2
- package/bin/mcp-mysql.js +3 -2
- package/dist/config/config.d.ts +1 -0
- package/dist/config/config.js +4 -0
- package/dist/config/databaseScope.d.ts +10 -0
- package/dist/config/databaseScope.js +36 -0
- package/dist/config/featureConfig.js +5 -5
- package/dist/index.d.ts +2 -0
- package/dist/mcp-server.js +17 -9
- package/dist/optimization/queryOptimizer.d.ts +13 -5
- package/dist/optimization/queryOptimizer.js +85 -58
- package/dist/security/databaseQueryScope.d.ts +5 -0
- package/dist/security/databaseQueryScope.js +184 -0
- package/dist/security/securityLayer.js +8 -2
- package/dist/tools/analysisTools.js +2 -26
- package/dist/tools/constraintTools.js +2 -26
- package/dist/tools/crudTools.d.ts +1 -0
- package/dist/tools/crudTools.js +6 -1
- package/dist/tools/dataExportTools.d.ts +1 -0
- package/dist/tools/dataExportTools.js +7 -21
- package/dist/tools/databaseTools.d.ts +1 -0
- package/dist/tools/databaseTools.js +23 -62
- package/dist/tools/fulltextSearchTools.js +2 -26
- package/dist/tools/indexTools.js +2 -26
- package/dist/tools/maintenanceTools.d.ts +3 -1
- package/dist/tools/maintenanceTools.js +24 -36
- package/dist/tools/migrationTools.js +2 -20
- package/dist/tools/queryTools.js +9 -0
- package/dist/tools/relationalSeederTools.js +2 -9
- package/dist/tools/smartQueryBuilderTools.d.ts +2 -0
- package/dist/tools/smartQueryBuilderTools.js +10 -29
- package/dist/tools/storedProcedureTools.js +2 -28
- package/dist/tools/toolArgumentValidation.js +22 -1
- package/dist/tools/triggerTools.js +2 -26
- package/dist/tools/utilityTools.js +6 -1
- package/dist/tools/viewTools.js +2 -26
- package/dist/validation/schemas.d.ts +6 -0
- package/dist/validation/schemas.js +3 -1
- package/manifest.json +6 -10
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,25 @@ All notable changes to the MySQL MCP Server will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.47.0] - 2026-12-01
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- `--databases=<name,...>` and `MCP_ALLOWED_DATABASES` add database names to the schema in the MySQL URL, preserving the same tool permissions across the allowlist.
|
|
12
|
+
- Database-aware tools and qualified SQL now enforce the configured database allowlist; `list_databases` returns only configured databases.
|
|
13
|
+
|
|
14
|
+
## [1.46.0] - 2026-10-26
|
|
15
|
+
|
|
16
|
+
### Security
|
|
17
|
+
- `run_select_query` validates optimizer `hints` before adding them to the SQL. Table and index names must be identifiers, strategies and `MAX_EXECUTION_TIME`/`RESOURCE_GROUP`/`SET_VAR` values must match their MySQL forms, and invalid values are dropped. The query with hints applied is validated again before it runs.
|
|
18
|
+
- System schema checks (`mysql.user`, `mysql.db`, and the `execute` requirement for `information_schema`/`mysql`/`performance_schema`/`sys`) now also match names with whitespace around the dot.
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
- **Breaking:** `optimize_table`, `repair_table`, `flush_table`, and `optimize_fulltext` require `ddl` instead of `utility`, so the read-only default (`list,read,utility`) no longer enables them. `analyze_table`, `check_table`, and `analyze_index` stay in `utility`.
|
|
22
|
+
- `flush_table` flushes only tables in the connected database (all of them when `table_name` is omitted) and rejects `with_read_lock`, which left the lock held on a pooled connection. The parameter was removed from the tool schema. The catalog remains at 88 tools.
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
- Jest tests for hint validation, qualified system schema names, and the maintenance tool permissions.
|
|
26
|
+
|
|
8
27
|
## [1.45.2] - 2026-10-26
|
|
9
28
|
|
|
10
29
|
### Changed
|
package/DOCUMENTATIONS.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# MySQL MCP Server - Documentation
|
|
2
2
|
|
|
3
|
-
**Last Updated:** 2026-
|
|
4
|
-
**Version:** 1.
|
|
3
|
+
**Last Updated:** 2026-12-01 14:33:14
|
|
4
|
+
**Version:** 1.47.0
|
|
5
5
|
**Total Tools:** 88
|
|
6
6
|
|
|
7
7
|
Comprehensive documentation for the MySQL MCP Server. For quick start, see [README.md](README.md).
|
|
@@ -48,6 +48,30 @@ Configure MySQL MCP with two access-control layers:
|
|
|
48
48
|
**Layer 1 (Permissions)**: Broad operation control
|
|
49
49
|
**Layer 2 (Categories)**: Fine-grained tool filtering
|
|
50
50
|
|
|
51
|
+
### Multiple database access
|
|
52
|
+
|
|
53
|
+
Use `--databases` to add databases to the one in the MySQL URL. The URL database remains the default for unqualified queries. Database-aware tools that accept a `database` argument (including `read_records`, `read_table_schema`, `list_tables`, and schema tools) can target any allowed database. The allowlist also applies to explicitly qualified SQL; cross-database queries are allowed only when every referenced database is in the allowlist. `list_databases` returns only allowed databases.
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"mcpServers": {
|
|
58
|
+
"mysql": {
|
|
59
|
+
"command": "npx",
|
|
60
|
+
"type": "stdio",
|
|
61
|
+
"args": [
|
|
62
|
+
"-y",
|
|
63
|
+
"@berthojoris/mcp-mysql-server",
|
|
64
|
+
"mysql://user:pass@localhost:3306/mydb",
|
|
65
|
+
"list,read,utility",
|
|
66
|
+
"--databases=database_a,database_b"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
In this example, the server may access `mydb`, `database_a`, and `database_b`, with the same `list,read,utility` permissions. Omit `--databases` to retain single-database access. The option can also be set with `MCP_ALLOWED_DATABASES=database_a,database_b`. The MySQL account must still have grants for each allowed database; MySQL grants remain the database-level security boundary.
|
|
74
|
+
|
|
51
75
|
> **Credentials with special characters**: if the username or password in the connection string contains URL-reserved characters (`@`, `:`, `/`, `?`, `#`, `%`, `&`, etc.), URL-encode them — e.g. the password `p@ss:w0rd` becomes `p%40ss%3Aw0rd`, giving `mysql://user:p%40ss%3Aw0rd@host:3306/db`. The server passes both the username and password through `decodeURIComponent()` before connecting, including when only one of the two is set.
|
|
52
76
|
|
|
53
77
|
### Environment Variables
|
|
@@ -253,8 +277,8 @@ For CSV exports, use `export_table_to_csv` for table-based exports and `export_q
|
|
|
253
277
|
| `update` | Update records | `update_record`, `bulk_update` |
|
|
254
278
|
| `delete` | Delete records | `delete_record`, `bulk_delete`, DELETE via `execute_write_query` (requires both `execute` and `delete`) |
|
|
255
279
|
| `execute` | Custom SQL (INSERT/UPDATE; not DELETE without `delete`) | `execute_write_query` |
|
|
256
|
-
| `ddl` | Schema changes | `create_table`, `alter_table` |
|
|
257
|
-
| `utility` | Utility operations | `test_connection`, `analyze_table` |
|
|
280
|
+
| `ddl` | Schema changes; rebuilding, repairing or flushing tables | `create_table`, `alter_table`, `optimize_table`, `repair_table`, `flush_table`, `optimize_fulltext` |
|
|
281
|
+
| `utility` | Utility operations | `test_connection`, `analyze_table`, `check_table`, `analyze_index` |
|
|
258
282
|
| `transaction` | Transaction management | `begin_transaction`, `commit_transaction` |
|
|
259
283
|
| `procedure` | Stored procedures | `create_stored_procedure`, `execute_stored_procedure` |
|
|
260
284
|
|
|
@@ -274,6 +298,12 @@ Information queries such as SHOW/DESCRIBE/EXPLAIN require `list` or `read`. MySQ
|
|
|
274
298
|
|
|
275
299
|
`EXPLAIN ANALYZE` (MySQL 8.0.18+) executes the statement it wraps, including multi-table UPDATE/DELETE, so it is authorized as that inner statement: `read` for SELECT, `update` or `execute` for UPDATE, and `delete` for DELETE. Plan-only `EXPLAIN` (without ANALYZE) remains an information query and only requires `list` or `read` (2026-10-12 20:39:13).
|
|
276
300
|
|
|
301
|
+
`optimize_table`, `repair_table`, `flush_table`, and `optimize_fulltext` rebuild, repair, or flush tables, so they require `ddl` instead of `utility` and are no longer part of the read-only default. `flush_table` only flushes tables in the connected database (all of them when `table_name` is omitted) and rejects `with_read_lock`, because the lock would stay held on a pooled connection after the call returns. `analyze_table`, `check_table`, and `analyze_index` remain `utility` tools (2026-10-26 16:39:47).
|
|
302
|
+
|
|
303
|
+
`run_select_query` optimizer `hints` are added to the SQL after it is validated, so every hint value is checked first: table and index names must be plain identifiers, `SEMIJOIN`/`SUBQUERY` strategies must be MySQL strategy names, `MAX_EXECUTION_TIME` must be a positive integer, and `SET_VAR` must be `name=value`. Invalid values are dropped, and the query with hints applied is validated again before it runs (2026-10-26 16:39:47).
|
|
304
|
+
|
|
305
|
+
Schema-qualified names are matched with any whitespace around the dot, so `mysql . user` is blocked like `mysql.user`, and `information_schema . tables` still requires `execute` (2026-10-26 16:39:47).
|
|
306
|
+
|
|
277
307
|
`execute_write_query` accepts only INSERT, UPDATE, and DELETE. Schema changes must use enabled DDL tools, even when `execute` and `ddl` permissions are both present.
|
|
278
308
|
|
|
279
309
|
---
|
|
@@ -337,7 +367,7 @@ Information queries such as SHOW/DESCRIBE/EXPLAIN require `list` or `read`. MySQ
|
|
|
337
367
|
- `get_fulltext_info` - Get FULLTEXT index information
|
|
338
368
|
- `drop_fulltext_index` - Drop FULLTEXT indexes
|
|
339
369
|
- `get_fulltext_stats` - Get FULLTEXT index statistics
|
|
340
|
-
- `optimize_fulltext` - Optimize FULLTEXT indexes
|
|
370
|
+
- `optimize_fulltext` - Optimize FULLTEXT indexes (requires `ddl`)
|
|
341
371
|
|
|
342
372
|
### 9. Constraint Management (7 tools)
|
|
343
373
|
- `list_foreign_keys` - List foreign keys
|
|
@@ -373,12 +403,12 @@ Information queries such as SHOW/DESCRIBE/EXPLAIN require `list` or `read`. MySQ
|
|
|
373
403
|
|
|
374
404
|
### 13. Table Maintenance (8 tools)
|
|
375
405
|
- `analyze_table` - Update statistics
|
|
376
|
-
- `optimize_table` - Reclaim space
|
|
406
|
+
- `optimize_table` - Reclaim space (requires `ddl`)
|
|
377
407
|
- `check_table` - Check for errors
|
|
378
|
-
- `repair_table` - Repair corrupted tables
|
|
408
|
+
- `repair_table` - Repair corrupted tables (requires `ddl`)
|
|
379
409
|
- `truncate_table` - Remove all rows
|
|
380
410
|
- `get_table_status` - Get table statistics
|
|
381
|
-
- `flush_table` - Close/reopen
|
|
411
|
+
- `flush_table` - Close/reopen tables in the connected database (requires `ddl`)
|
|
382
412
|
- `get_table_size` - Get size information
|
|
383
413
|
|
|
384
414
|
### 14. Transaction Management (5 tools)
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
**A production-ready Model Context Protocol (MCP) server for MySQL database integration with AI agents**
|
|
6
6
|
|
|
7
|
-
**Last Updated:** 2026-
|
|
7
|
+
**Last Updated:** 2026-12-01 14:33:14
|
|
8
8
|
|
|
9
9
|
[](https://www.npmjs.com/package/@berthojoris/mcp-mysql-server)
|
|
10
10
|
[](https://www.npmjs.com/package/@berthojoris/mcp-mysql-server)
|
|
@@ -47,6 +47,18 @@ Add to your AI agent config (`.mcp.json`, `.cursor/mcp.json`, etc.):
|
|
|
47
47
|
|
|
48
48
|
For dual-layer and environment-variable configuration, see **[DOCUMENTATIONS.md → Configuration](DOCUMENTATIONS.md#configuration)**. To connect remote clients such as ChatGPT over HTTP, see [Remote Access](#remote-access-chatgpt--other-remote-mcp-clients).
|
|
49
49
|
|
|
50
|
+
To allow additional databases while keeping the same permissions, add `--databases` after the permissions argument. The database in the URL stays the default:
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
"args": [
|
|
54
|
+
"-y",
|
|
55
|
+
"@berthojoris/mcp-mysql-server",
|
|
56
|
+
"mysql://user:pass@localhost:3306/mydb",
|
|
57
|
+
"list,read,utility",
|
|
58
|
+
"--databases=database_a,database_b"
|
|
59
|
+
]
|
|
60
|
+
```
|
|
61
|
+
|
|
50
62
|
---
|
|
51
63
|
|
|
52
64
|
## Installation
|
|
@@ -349,11 +361,16 @@ Control database access with a **dual-layer filtering system** that provides bot
|
|
|
349
361
|
| `update` | UPDATE existing records | Data maintenance |
|
|
350
362
|
| `delete` | DELETE records | Data cleanup |
|
|
351
363
|
| `execute` | Execute custom INSERT/UPDATE SQL (DELETE requires `delete` permission too) | Complex write operations |
|
|
352
|
-
| `ddl` | CREATE/ALTER/DROP tables | Schema management |
|
|
364
|
+
| `ddl` | CREATE/ALTER/DROP tables; optimize, repair, and flush tables | Schema management |
|
|
353
365
|
| `procedure` | Stored procedures (CREATE/DROP/EXECUTE) | Procedure management |
|
|
354
366
|
| `transaction` | BEGIN, COMMIT, ROLLBACK | ACID operations |
|
|
355
367
|
| `utility` | Connection testing, diagnostics | Troubleshooting |
|
|
356
368
|
|
|
369
|
+
**Security changes in v1.46.0 (review before upgrading):**
|
|
370
|
+
- `optimize_table`, `repair_table`, `flush_table`, and `optimize_fulltext` now require `ddl` instead of `utility`, so the read-only default no longer includes them. `flush_table` is limited to the connected database and no longer accepts `with_read_lock`.
|
|
371
|
+
- `run_select_query` drops invalid optimizer hint values and validates the query again after hints are applied.
|
|
372
|
+
- System schema names are detected with whitespace around the dot (e.g. `mysql . user`).
|
|
373
|
+
|
|
357
374
|
**Changes in v1.44.0 (review before upgrading):**
|
|
358
375
|
- **Read-only by default**: with no permissions and no categories, the server now enables only `list,read,utility` (previously every tool). To keep full access, pass `list,read,create,update,delete,execute,ddl,utility,transaction,procedure`.
|
|
359
376
|
- **No implicit `mysql` schema**: without a database in the connection string or `DB_NAME`, database-scoped tools report "No database specified" instead of using the `mysql` system schema.
|
package/bin/mcp-mysql.js
CHANGED
|
@@ -17,6 +17,7 @@ const FLAG_ENV = {
|
|
|
17
17
|
transport: "MCP_TRANSPORT",
|
|
18
18
|
host: "MCP_HTTP_HOST",
|
|
19
19
|
port: "MCP_HTTP_PORT",
|
|
20
|
+
databases: "MCP_ALLOWED_DATABASES",
|
|
20
21
|
};
|
|
21
22
|
|
|
22
23
|
const positional = [];
|
|
@@ -37,7 +38,7 @@ for (let i = 0; i < rawArgs.length; i++) {
|
|
|
37
38
|
const envName = FLAG_ENV[flag];
|
|
38
39
|
if (!envName) {
|
|
39
40
|
console.error(`Error: unknown option ${arg}`);
|
|
40
|
-
console.error("Options: --transport=stdio|http, --http, --stdio, --host=<addr>, --port=<n
|
|
41
|
+
console.error("Options: --transport=stdio|http, --http, --stdio, --host=<addr>, --port=<n>, --databases=<name,...>");
|
|
41
42
|
process.exit(1);
|
|
42
43
|
}
|
|
43
44
|
const value = inlineValue !== undefined ? inlineValue : rawArgs[++i];
|
|
@@ -72,7 +73,7 @@ for (let i = 0; i < args.length; i++) {
|
|
|
72
73
|
if (!mysqlUrl) {
|
|
73
74
|
console.error("Error: MySQL connection URL is required");
|
|
74
75
|
console.error(
|
|
75
|
-
"Usage: mcp-mysql mysql://user:password@host:port/dbname [permissions] [categories] [--transport=stdio|http] [--host=<addr>] [--port=<n>]",
|
|
76
|
+
"Usage: mcp-mysql mysql://user:password@host:port/dbname [permissions] [categories] [--databases=<name,...>] [--transport=stdio|http] [--host=<addr>] [--port=<n>]",
|
|
76
77
|
);
|
|
77
78
|
console.error("");
|
|
78
79
|
console.error("Examples:");
|
package/dist/config/config.d.ts
CHANGED
package/dist/config/config.js
CHANGED
|
@@ -14,6 +14,10 @@ exports.dbConfig = {
|
|
|
14
14
|
password: process.env.DB_PASSWORD || '',
|
|
15
15
|
// Empty means no database selected; never default to the mysql system schema
|
|
16
16
|
database: process.env.DB_NAME || '',
|
|
17
|
+
allowedDatabases: (process.env.MCP_ALLOWED_DATABASES || '')
|
|
18
|
+
.split(',')
|
|
19
|
+
.map((database) => database.trim())
|
|
20
|
+
.filter(Boolean),
|
|
17
21
|
};
|
|
18
22
|
exports.default = {
|
|
19
23
|
database: exports.dbConfig,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface DatabaseAccessResult {
|
|
2
|
+
valid: boolean;
|
|
3
|
+
database: string;
|
|
4
|
+
error?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const getAllowedDatabases: () => string[];
|
|
7
|
+
export declare const isDatabaseAllowed: (database: string) => boolean;
|
|
8
|
+
export declare const validateDatabaseAccess: (requestedDatabase?: string) => DatabaseAccessResult;
|
|
9
|
+
export declare const requireAllowedDatabase: (requestedDatabase?: string) => string;
|
|
10
|
+
export declare const quoteDatabaseIdentifier: (database: string) => string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.quoteDatabaseIdentifier = exports.requireAllowedDatabase = exports.validateDatabaseAccess = exports.isDatabaseAllowed = exports.getAllowedDatabases = void 0;
|
|
4
|
+
const config_1 = require("./config");
|
|
5
|
+
const getAllowedDatabases = () => [...new Set([config_1.dbConfig.database, ...config_1.dbConfig.allowedDatabases].filter(Boolean))];
|
|
6
|
+
exports.getAllowedDatabases = getAllowedDatabases;
|
|
7
|
+
const isDatabaseAllowed = (database) => (0, exports.getAllowedDatabases)().includes(database);
|
|
8
|
+
exports.isDatabaseAllowed = isDatabaseAllowed;
|
|
9
|
+
const validateDatabaseAccess = (requestedDatabase) => {
|
|
10
|
+
const database = requestedDatabase || config_1.dbConfig.database;
|
|
11
|
+
if (!database) {
|
|
12
|
+
return {
|
|
13
|
+
valid: false,
|
|
14
|
+
database: "",
|
|
15
|
+
error: "No database specified in connection string or database allowlist.",
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
if (!(0, exports.isDatabaseAllowed)(database)) {
|
|
19
|
+
return {
|
|
20
|
+
valid: false,
|
|
21
|
+
database: "",
|
|
22
|
+
error: `Access denied. Database '${database}' is not in the allowed database list.`,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return { valid: true, database };
|
|
26
|
+
};
|
|
27
|
+
exports.validateDatabaseAccess = validateDatabaseAccess;
|
|
28
|
+
const requireAllowedDatabase = (requestedDatabase) => {
|
|
29
|
+
const result = (0, exports.validateDatabaseAccess)(requestedDatabase);
|
|
30
|
+
if (!result.valid)
|
|
31
|
+
throw new Error(result.error);
|
|
32
|
+
return result.database;
|
|
33
|
+
};
|
|
34
|
+
exports.requireAllowedDatabase = requireAllowedDatabase;
|
|
35
|
+
const quoteDatabaseIdentifier = (database) => `\`${database.replace(/`/g, "``")}\``;
|
|
36
|
+
exports.quoteDatabaseIdentifier = quoteDatabaseIdentifier;
|
|
@@ -154,14 +154,14 @@ exports.toolCategoryMap = {
|
|
|
154
154
|
addUniqueConstraint: ToolCategory.DDL,
|
|
155
155
|
dropConstraint: ToolCategory.DDL,
|
|
156
156
|
addCheckConstraint: ToolCategory.DDL,
|
|
157
|
-
// Table maintenance tools
|
|
157
|
+
// Table maintenance tools (rebuilding, repairing or flushing tables needs ddl)
|
|
158
158
|
analyzeTable: ToolCategory.UTILITY,
|
|
159
|
-
optimizeTable: ToolCategory.
|
|
159
|
+
optimizeTable: ToolCategory.DDL,
|
|
160
160
|
checkTable: ToolCategory.UTILITY,
|
|
161
|
-
repairTable: ToolCategory.
|
|
161
|
+
repairTable: ToolCategory.DDL,
|
|
162
162
|
truncateTable: ToolCategory.DDL,
|
|
163
163
|
getTableStatus: ToolCategory.LIST,
|
|
164
|
-
flushTable: ToolCategory.
|
|
164
|
+
flushTable: ToolCategory.DDL,
|
|
165
165
|
getTableSize: ToolCategory.LIST,
|
|
166
166
|
// Query optimization tools
|
|
167
167
|
analyzeQuery: ToolCategory.UTILITY,
|
|
@@ -176,7 +176,7 @@ exports.toolCategoryMap = {
|
|
|
176
176
|
getFulltextInfo: ToolCategory.LIST,
|
|
177
177
|
dropFulltextIndex: ToolCategory.DDL,
|
|
178
178
|
getFulltextStats: ToolCategory.LIST,
|
|
179
|
-
optimizeFulltext: ToolCategory.
|
|
179
|
+
optimizeFulltext: ToolCategory.DDL,
|
|
180
180
|
};
|
|
181
181
|
const toolAdditionalPermissions = {
|
|
182
182
|
exportTableToCSV: [ToolCategory.READ],
|
package/dist/index.d.ts
CHANGED
|
@@ -40,6 +40,7 @@ export declare class MySQLMCP {
|
|
|
40
40
|
}>;
|
|
41
41
|
readTableSchema(params: {
|
|
42
42
|
table_name: string;
|
|
43
|
+
database?: string;
|
|
43
44
|
}): Promise<{
|
|
44
45
|
status: string;
|
|
45
46
|
data?: import("./validation/schemas").ColumnInfo[];
|
|
@@ -351,6 +352,7 @@ export declare class MySQLMCP {
|
|
|
351
352
|
}>;
|
|
352
353
|
exportTableToCSV(params: {
|
|
353
354
|
table_name: string;
|
|
355
|
+
database?: string;
|
|
354
356
|
filters?: any[];
|
|
355
357
|
pagination?: {
|
|
356
358
|
page: number;
|
package/dist/mcp-server.js
CHANGED
|
@@ -266,6 +266,10 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
266
266
|
type: "string",
|
|
267
267
|
description: "Name of the table to read schema from",
|
|
268
268
|
},
|
|
269
|
+
database: {
|
|
270
|
+
type: "string",
|
|
271
|
+
description: "Optional: allowed database name (defaults to the database in the connection URL)",
|
|
272
|
+
},
|
|
269
273
|
},
|
|
270
274
|
required: ["table_name"],
|
|
271
275
|
},
|
|
@@ -298,6 +302,10 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
298
302
|
type: "string",
|
|
299
303
|
description: "Name of the table to read from",
|
|
300
304
|
},
|
|
305
|
+
database: {
|
|
306
|
+
type: "string",
|
|
307
|
+
description: "Optional: allowed database name (defaults to the database in the connection URL)",
|
|
308
|
+
},
|
|
301
309
|
filters: {
|
|
302
310
|
type: "array",
|
|
303
311
|
description: "Array of filter conditions",
|
|
@@ -1345,6 +1353,10 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
1345
1353
|
type: "string",
|
|
1346
1354
|
description: "Name of the table to export",
|
|
1347
1355
|
},
|
|
1356
|
+
database: {
|
|
1357
|
+
type: "string",
|
|
1358
|
+
description: "Optional: allowed database name (defaults to the database in the connection URL)",
|
|
1359
|
+
},
|
|
1348
1360
|
filters: {
|
|
1349
1361
|
type: "array",
|
|
1350
1362
|
description: "Array of filter conditions",
|
|
@@ -1976,7 +1988,7 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
1976
1988
|
},
|
|
1977
1989
|
{
|
|
1978
1990
|
name: "optimize_table",
|
|
1979
|
-
description: "Optimizes a table to reclaim unused space and defragment data.",
|
|
1991
|
+
description: "Optimizes a table to reclaim unused space and defragment data. Rebuilds the table, so it requires 'ddl' permission.",
|
|
1980
1992
|
inputSchema: {
|
|
1981
1993
|
type: "object",
|
|
1982
1994
|
properties: {
|
|
@@ -2017,7 +2029,7 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
2017
2029
|
},
|
|
2018
2030
|
{
|
|
2019
2031
|
name: "repair_table",
|
|
2020
|
-
description: "Repairs a corrupted table (MyISAM, ARCHIVE, CSV only).",
|
|
2032
|
+
description: "Repairs a corrupted table (MyISAM, ARCHIVE, CSV only). Requires 'ddl' permission.",
|
|
2021
2033
|
inputSchema: {
|
|
2022
2034
|
type: "object",
|
|
2023
2035
|
properties: {
|
|
@@ -2073,17 +2085,13 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
2073
2085
|
},
|
|
2074
2086
|
{
|
|
2075
2087
|
name: "flush_table",
|
|
2076
|
-
description: "Flushes table(s) - closes and reopens them.",
|
|
2088
|
+
description: "Flushes table(s) in the connected database - closes and reopens them. Requires 'ddl' permission.",
|
|
2077
2089
|
inputSchema: {
|
|
2078
2090
|
type: "object",
|
|
2079
2091
|
properties: {
|
|
2080
2092
|
table_name: {
|
|
2081
2093
|
type: "string",
|
|
2082
|
-
description: "Optional: specific table (omit for all tables)",
|
|
2083
|
-
},
|
|
2084
|
-
with_read_lock: {
|
|
2085
|
-
type: "boolean",
|
|
2086
|
-
description: "Acquire read lock after flushing",
|
|
2094
|
+
description: "Optional: specific table (omit for all tables in the connected database)",
|
|
2087
2095
|
},
|
|
2088
2096
|
database: {
|
|
2089
2097
|
type: "string",
|
|
@@ -2263,7 +2271,7 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
2263
2271
|
},
|
|
2264
2272
|
{
|
|
2265
2273
|
name: "optimize_fulltext",
|
|
2266
|
-
description: "Optimizes a table to update FULLTEXT index statistics and reclaim space. Use this after bulk inserts or updates to improve search performance.",
|
|
2274
|
+
description: "Optimizes a table to update FULLTEXT index statistics and reclaim space. Use this after bulk inserts or updates to improve search performance. Rebuilds the table, so it requires 'ddl' permission.",
|
|
2267
2275
|
inputSchema: {
|
|
2268
2276
|
type: "object",
|
|
2269
2277
|
properties: {
|
|
@@ -76,15 +76,23 @@ export declare class QueryOptimizer {
|
|
|
76
76
|
* Escape special regex characters in a string
|
|
77
77
|
*/
|
|
78
78
|
private escapeRegex;
|
|
79
|
-
/**
|
|
80
|
-
* Sanitize identifier (table name, index name) to prevent injection
|
|
81
|
-
* Only allows alphanumeric characters, underscores, and dots (for schema.table)
|
|
82
|
-
*/
|
|
83
|
-
private sanitizeIdentifier;
|
|
84
79
|
/**
|
|
85
80
|
* Validate that an identifier is safe to use
|
|
86
81
|
*/
|
|
87
82
|
private isValidIdentifier;
|
|
83
|
+
/**
|
|
84
|
+
* Return the values that are safe identifiers. Hints are added after the
|
|
85
|
+
* query has been validated, so anything else is dropped rather than quoted.
|
|
86
|
+
*/
|
|
87
|
+
private safeIdentifiers;
|
|
88
|
+
/**
|
|
89
|
+
* Return a SEMIJOIN/SUBQUERY strategy list if every strategy is allowed
|
|
90
|
+
*/
|
|
91
|
+
private safeStrategy;
|
|
92
|
+
/**
|
|
93
|
+
* Return a positive integer no larger than max, or null
|
|
94
|
+
*/
|
|
95
|
+
private safePositiveInteger;
|
|
88
96
|
/**
|
|
89
97
|
* Apply optimizer hints to a SELECT query
|
|
90
98
|
*/
|
|
@@ -28,14 +28,6 @@ class QueryOptimizer {
|
|
|
28
28
|
escapeRegex(str) {
|
|
29
29
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Sanitize identifier (table name, index name) to prevent injection
|
|
33
|
-
* Only allows alphanumeric characters, underscores, and dots (for schema.table)
|
|
34
|
-
*/
|
|
35
|
-
sanitizeIdentifier(identifier) {
|
|
36
|
-
// Remove any characters that aren't alphanumeric, underscore, or dot
|
|
37
|
-
return identifier.replace(/[^a-zA-Z0-9_\.]/g, "");
|
|
38
|
-
}
|
|
39
31
|
/**
|
|
40
32
|
* Validate that an identifier is safe to use
|
|
41
33
|
*/
|
|
@@ -44,6 +36,37 @@ class QueryOptimizer {
|
|
|
44
36
|
// Max length 64 (MySQL limit)
|
|
45
37
|
return /^[a-zA-Z_][a-zA-Z0-9_\.]{0,63}$/.test(identifier);
|
|
46
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Return the values that are safe identifiers. Hints are added after the
|
|
41
|
+
* query has been validated, so anything else is dropped rather than quoted.
|
|
42
|
+
*/
|
|
43
|
+
safeIdentifiers(value) {
|
|
44
|
+
const values = Array.isArray(value) ? value : [value];
|
|
45
|
+
return values.filter((item) => typeof item === "string" && this.isValidIdentifier(item));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Return a SEMIJOIN/SUBQUERY strategy list if every strategy is allowed
|
|
49
|
+
*/
|
|
50
|
+
safeStrategy(strategy, allowed) {
|
|
51
|
+
if (typeof strategy !== "string") {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const strategies = strategy
|
|
55
|
+
.split(",")
|
|
56
|
+
.map((item) => item.trim().toUpperCase());
|
|
57
|
+
return strategies.every((item) => allowed.includes(item))
|
|
58
|
+
? strategies.join(", ")
|
|
59
|
+
: null;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Return a positive integer no larger than max, or null
|
|
63
|
+
*/
|
|
64
|
+
safePositiveInteger(value, max) {
|
|
65
|
+
const number = Number(value);
|
|
66
|
+
return Number.isInteger(number) && number > 0 && number <= max
|
|
67
|
+
? number
|
|
68
|
+
: null;
|
|
69
|
+
}
|
|
47
70
|
/**
|
|
48
71
|
* Apply optimizer hints to a SELECT query
|
|
49
72
|
*/
|
|
@@ -110,33 +133,22 @@ class QueryOptimizer {
|
|
|
110
133
|
}
|
|
111
134
|
// Process shorthand hints
|
|
112
135
|
if (hints.maxExecutionTime !== undefined) {
|
|
113
|
-
//
|
|
114
|
-
const maxTime = Math.floor(Math.abs(Number(hints.maxExecutionTime)));
|
|
115
|
-
if (maxTime
|
|
116
|
-
// Max 1 year in ms
|
|
136
|
+
// Max 1 year in ms
|
|
137
|
+
const maxTime = this.safePositiveInteger(Math.floor(Math.abs(Number(hints.maxExecutionTime))), 31536000000);
|
|
138
|
+
if (maxTime !== null) {
|
|
117
139
|
hintParts.push(`MAX_EXECUTION_TIME(${maxTime})`);
|
|
118
140
|
}
|
|
119
141
|
}
|
|
120
142
|
if (hints.forceIndex) {
|
|
121
|
-
const indexes =
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const sanitizedIndexes = indexes
|
|
125
|
-
.map((idx) => this.sanitizeIdentifier(idx))
|
|
126
|
-
.filter((idx) => this.isValidIdentifier(idx));
|
|
127
|
-
if (sanitizedIndexes.length > 0) {
|
|
128
|
-
hintParts.push(`INDEX(${sanitizedIndexes.join(", ")})`);
|
|
143
|
+
const indexes = this.safeIdentifiers(hints.forceIndex);
|
|
144
|
+
if (indexes.length > 0) {
|
|
145
|
+
hintParts.push(`INDEX(${indexes.join(", ")})`);
|
|
129
146
|
}
|
|
130
147
|
}
|
|
131
148
|
if (hints.ignoreIndex) {
|
|
132
|
-
const indexes =
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const sanitizedIndexes = indexes
|
|
136
|
-
.map((idx) => this.sanitizeIdentifier(idx))
|
|
137
|
-
.filter((idx) => this.isValidIdentifier(idx));
|
|
138
|
-
if (sanitizedIndexes.length > 0) {
|
|
139
|
-
hintParts.push(`NO_INDEX(${sanitizedIndexes.join(", ")})`);
|
|
149
|
+
const indexes = this.safeIdentifiers(hints.ignoreIndex);
|
|
150
|
+
if (indexes.length > 0) {
|
|
151
|
+
hintParts.push(`NO_INDEX(${indexes.join(", ")})`);
|
|
140
152
|
}
|
|
141
153
|
}
|
|
142
154
|
return hintParts.join(" ");
|
|
@@ -145,6 +157,10 @@ class QueryOptimizer {
|
|
|
145
157
|
* Format a single optimizer hint
|
|
146
158
|
*/
|
|
147
159
|
formatHint(hint) {
|
|
160
|
+
const table = this.safeIdentifiers(hint.table)[0];
|
|
161
|
+
if (hint.table !== undefined && !table) {
|
|
162
|
+
return "";
|
|
163
|
+
}
|
|
148
164
|
switch (hint.type) {
|
|
149
165
|
// Join-Order Hints
|
|
150
166
|
case "JOIN_FIXED_ORDER":
|
|
@@ -153,7 +169,10 @@ class QueryOptimizer {
|
|
|
153
169
|
case "JOIN_PREFIX":
|
|
154
170
|
case "JOIN_SUFFIX":
|
|
155
171
|
if (hint.tables && hint.tables.length > 0) {
|
|
156
|
-
|
|
172
|
+
const tables = this.safeIdentifiers(hint.tables);
|
|
173
|
+
if (tables.length === hint.tables.length) {
|
|
174
|
+
return `${hint.type}(${tables.join(", ")})`;
|
|
175
|
+
}
|
|
157
176
|
}
|
|
158
177
|
return "";
|
|
159
178
|
// Table-Level Hints
|
|
@@ -165,8 +184,8 @@ class QueryOptimizer {
|
|
|
165
184
|
case "NO_HASH_JOIN":
|
|
166
185
|
case "MERGE":
|
|
167
186
|
case "NO_MERGE":
|
|
168
|
-
if (
|
|
169
|
-
return `${hint.type}(${
|
|
187
|
+
if (table) {
|
|
188
|
+
return `${hint.type}(${table})`;
|
|
170
189
|
}
|
|
171
190
|
return `${hint.type}()`;
|
|
172
191
|
// Index-Level Hints
|
|
@@ -180,41 +199,48 @@ class QueryOptimizer {
|
|
|
180
199
|
case "NO_RANGE_OPTIMIZATION":
|
|
181
200
|
case "SKIP_SCAN":
|
|
182
201
|
case "NO_SKIP_SCAN":
|
|
183
|
-
if (
|
|
184
|
-
const indexes =
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
202
|
+
if (table && hint.index) {
|
|
203
|
+
const indexes = this.safeIdentifiers(hint.index);
|
|
204
|
+
const indexCount = Array.isArray(hint.index) ? hint.index.length : 1;
|
|
205
|
+
if (indexes.length === 0 || indexes.length !== indexCount) {
|
|
206
|
+
return "";
|
|
207
|
+
}
|
|
208
|
+
return `${hint.type}(${table} ${indexes.join(", ")})`;
|
|
188
209
|
}
|
|
189
|
-
else if (
|
|
190
|
-
return `${hint.type}(${
|
|
210
|
+
else if (table) {
|
|
211
|
+
return `${hint.type}(${table})`;
|
|
191
212
|
}
|
|
192
213
|
return "";
|
|
193
214
|
// Subquery Hints
|
|
194
215
|
case "SEMIJOIN":
|
|
195
216
|
case "NO_SEMIJOIN":
|
|
196
217
|
if (hint.strategy) {
|
|
197
|
-
|
|
218
|
+
const strategy = this.safeStrategy(hint.strategy, [
|
|
219
|
+
"DUPSWEEDOUT", "FIRSTMATCH", "LOOSESCAN", "MATERIALIZATION",
|
|
220
|
+
]);
|
|
221
|
+
return strategy ? `${hint.type}(${strategy})` : "";
|
|
198
222
|
}
|
|
199
223
|
return `${hint.type}()`;
|
|
200
|
-
case "SUBQUERY":
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
return "";
|
|
224
|
+
case "SUBQUERY": {
|
|
225
|
+
const strategy = this.safeStrategy(hint.strategy, [
|
|
226
|
+
"INTOEXISTS", "MATERIALIZATION",
|
|
227
|
+
]);
|
|
228
|
+
return strategy ? `SUBQUERY(${strategy})` : "";
|
|
229
|
+
}
|
|
205
230
|
// Miscellaneous
|
|
206
|
-
case "MAX_EXECUTION_TIME":
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
return "";
|
|
231
|
+
case "MAX_EXECUTION_TIME": {
|
|
232
|
+
const maxTime = this.safePositiveInteger(hint.value, 31536000000);
|
|
233
|
+
return maxTime !== null ? `MAX_EXECUTION_TIME(${maxTime})` : "";
|
|
234
|
+
}
|
|
235
|
+
case "RESOURCE_GROUP": {
|
|
236
|
+
const group = this.safeIdentifiers(hint.value)[0];
|
|
237
|
+
return group ? `RESOURCE_GROUP(${group})` : "";
|
|
238
|
+
}
|
|
216
239
|
case "SET_VAR":
|
|
217
|
-
|
|
240
|
+
// name=value, where value is a number, a word, or a quoted list such
|
|
241
|
+
// as optimizer_switch='mrr=on,mrr_cost_based=off'
|
|
242
|
+
if (typeof hint.value === "string" &&
|
|
243
|
+
/^[a-zA-Z_][a-zA-Z0-9_]{0,63}s*=s*(?:[a-zA-Z0-9_.]{1,64}|'[a-zA-Z0-9_=,.]{1,256}')$/.test(hint.value)) {
|
|
218
244
|
return `SET_VAR(${hint.value})`;
|
|
219
245
|
}
|
|
220
246
|
return "";
|
|
@@ -234,9 +260,10 @@ class QueryOptimizer {
|
|
|
234
260
|
return query;
|
|
235
261
|
}
|
|
236
262
|
// Find table references and add USE INDEX after them
|
|
237
|
-
const indexes =
|
|
238
|
-
|
|
239
|
-
|
|
263
|
+
const indexes = this.safeIdentifiers(hints.useIndex);
|
|
264
|
+
if (indexes.length === 0) {
|
|
265
|
+
return query;
|
|
266
|
+
}
|
|
240
267
|
const useIndexClause = `USE INDEX (${indexes.join(", ")})`;
|
|
241
268
|
// Simple approach: add after FROM clause table
|
|
242
269
|
// This is a basic implementation - complex queries may need more sophisticated parsing
|