@berthojoris/mcp-mysql-server 1.43.5 → 1.44.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 +22 -0
- package/DOCUMENTATIONS.md +19 -3
- package/README.md +7 -1
- package/bin/mcp-mysql.js +168 -168
- package/dist/cache/queryCache.js +1 -1
- package/dist/config/config.js +2 -1
- package/dist/config/featureConfig.d.ts +5 -0
- package/dist/config/featureConfig.js +11 -13
- package/dist/db/connection.d.ts +8 -1
- package/dist/db/connection.js +19 -14
- package/dist/mcp-server.js +30 -302
- package/dist/security/securityLayer.d.ts +21 -2
- package/dist/security/securityLayer.js +151 -21
- package/dist/tools/toolArgumentValidation.js +22 -22
- package/dist/tools/toolRegistry.d.ts +1 -1
- package/dist/tools/toolRegistry.js +7 -1
- package/manifest.json +1 -1
- package/package.json +86 -78
- package/dist/auth/authService.d.ts +0 -29
- package/dist/auth/authService.js +0 -114
- package/dist/tools/backupRestoreTools.d.ts +0 -18
- package/dist/tools/backupRestoreTools.js +0 -70
- package/dist/tools/documentationGeneratorTools.d.ts +0 -145
- package/dist/tools/documentationGeneratorTools.js +0 -820
- package/dist/tools/forecastingTools.d.ts +0 -36
- package/dist/tools/forecastingTools.js +0 -256
- package/dist/tools/functionTools.d.ts +0 -87
- package/dist/tools/functionTools.js +0 -358
- package/dist/tools/indexRecommendationTools.d.ts +0 -50
- package/dist/tools/indexRecommendationTools.js +0 -451
- package/dist/tools/intelligentQueryTools.d.ts +0 -94
- package/dist/tools/intelligentQueryTools.js +0 -713
- package/dist/tools/performanceTools.d.ts +0 -76
- package/dist/tools/performanceTools.js +0 -505
- package/dist/tools/processTools.d.ts +0 -61
- package/dist/tools/processTools.js +0 -181
- package/dist/tools/queryVisualizationTools.d.ts +0 -22
- package/dist/tools/queryVisualizationTools.js +0 -155
- package/dist/tools/schemaDesignTools.d.ts +0 -67
- package/dist/tools/schemaDesignTools.js +0 -359
- package/dist/tools/schemaPatternTools.d.ts +0 -19
- package/dist/tools/schemaPatternTools.js +0 -253
- package/dist/tools/schemaVersioningTools.d.ts +0 -138
- package/dist/tools/schemaVersioningTools.js +0 -1008
- package/dist/tools/securityAuditTools.d.ts +0 -39
- package/dist/tools/securityAuditTools.js +0 -319
- package/dist/tools/smartDiscoveryTools.d.ts +0 -163
- package/dist/tools/smartDiscoveryTools.js +0 -750
- package/dist/tools/testDataTools.d.ts +0 -26
- package/dist/tools/testDataTools.js +0 -325
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,28 @@ 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.44.0] - 2026-10-26
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- **Read-only default permissions**: when neither permissions nor categories are configured, the server enables only `list,read,utility` instead of every tool. Configurations that relied on the old default must now list permissions explicitly.
|
|
12
|
+
- **No implicit `mysql` schema**: `DB_NAME` no longer falls back to the `mysql` system schema. Without a configured database, database-scoped tools return "No database specified".
|
|
13
|
+
- **Query cache disabled by default**: opt in with `CACHE_ENABLED=true`.
|
|
14
|
+
- `CallTool` dispatch now resolves tools to their `MySQLMCP` method through the same path as the Cursor bridge, replacing the per-tool `switch`. The server version is read from `package.json`.
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
- SQL validation no longer rejects `#`, `;`, `--`, or `/* */` inside string literals and quoted identifiers (e.g. `WHERE color = '#fff'`). Literals containing backslashes still get the strict raw-text check because their meaning depends on `NO_BACKSLASH_ESCAPES`.
|
|
18
|
+
- `WITH` (CTE) queries are classified by their main statement (SELECT/UPDATE/DELETE) instead of being rejected. CTEs in read queries require `execute`, as derived tables do.
|
|
19
|
+
- When the cache is enabled, writes executed inside transactions and transaction commits now clear it, and table-name guessing was replaced by clearing the whole cache on any data-changing statement (covers db-qualified names, multi-table writes, DDL, and `EXPLAIN ANALYZE`).
|
|
20
|
+
- `npm run build` now removes `dist/` first, so the stale `dist/auth/authService.js` (JWT code with a default secret and an undeclared `jsonwebtoken` dependency) is no longer published.
|
|
21
|
+
- `npm run dev` (ts-node) works again: local imports no longer use `.js` suffixes that ts-node could not resolve.
|
|
22
|
+
- Restricted permission profiles no longer log "Unknown tool" warnings for tools mapped only by their snake_case name.
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
- Jest test suite in `src/__tests__/` covering the SQL validator, default permissions, and cache invalidation.
|
|
26
|
+
|
|
27
|
+
### Removed
|
|
28
|
+
- Unused `next-env.d.ts`. Removed the inaccurate "PII masking in exports" claim from the documentation; results are returned unmasked. The catalog remains at 88 tools.
|
|
29
|
+
|
|
8
30
|
## [1.43.5] - 2026-10-12
|
|
9
31
|
|
|
10
32
|
### Added
|
package/DOCUMENTATIONS.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# MySQL MCP Server - Documentation
|
|
2
2
|
|
|
3
|
-
**Last Updated:** 2026-10-
|
|
4
|
-
**Version:** 1.
|
|
3
|
+
**Last Updated:** 2026-10-26 13:35:39
|
|
4
|
+
**Version:** 1.44.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).
|
|
@@ -65,6 +65,12 @@ Configure MySQL MCP with two access-control layers:
|
|
|
65
65
|
}
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
+
### Defaults (v1.44.0, 2026-10-26 13:35:39)
|
|
69
|
+
|
|
70
|
+
- **Permissions**: when neither permissions nor categories are given, the server starts **read-only** with `list,read,utility`. Earlier versions enabled every tool. To enable everything, pass the full list: `list,read,create,update,delete,execute,ddl,utility,transaction,procedure`.
|
|
71
|
+
- **Database**: when the connection string has no database and `DB_NAME` is unset, no database is selected and database-scoped tools return "No database specified". Earlier versions fell back to the `mysql` system schema.
|
|
72
|
+
- **Query cache**: disabled by default. Set `CACHE_ENABLED=true` to opt in (`CACHE_TTL_MS`, default `60000`; `CACHE_MAX_SIZE`, default `100`). When enabled, any statement that can change data (outside or inside a transaction) and every transaction commit clears the whole cache. Writes made by other clients are not detected, so cached results can lag by up to the TTL.
|
|
73
|
+
|
|
68
74
|
---
|
|
69
75
|
|
|
70
76
|
### Cursor Compatibility Bridge
|
|
@@ -128,6 +134,8 @@ For CSV exports, use `export_table_to_csv` for table-based exports and `export_q
|
|
|
128
134
|
Tool enabled = (Has Permission) AND (Has Category OR No categories specified)
|
|
129
135
|
```
|
|
130
136
|
|
|
137
|
+
With no permissions and no categories configured, the effective permissions are `list,read,utility` (read-only default since v1.44.0). Categories alone keep all permissions and filter by category.
|
|
138
|
+
|
|
131
139
|
CSV exports require both `utility` and `read` permissions; their category remains `utilities`.
|
|
132
140
|
|
|
133
141
|
`execute_in_transaction` requires `transaction` plus the permission for the SQL operation: `read` for SELECT, `create` or `execute` for INSERT, `update` or `execute` for UPDATE, and `delete` for DELETE. DDL also requires `ddl` and an enabled `execute_ddl` tool. Transaction permission alone does not authorize reading or modifying data.
|
|
@@ -459,6 +467,8 @@ await mcp.call("commit_transaction", {});
|
|
|
459
467
|
- **Tool-level**: Each tool requires specific permissions
|
|
460
468
|
- **Execution-time enforcement**: Tool permissions and categories are checked for every call, including file-backed Cursor requests.
|
|
461
469
|
- **Mandatory SQL restrictions**: File access, account/privilege changes, dangerous server operations, comments, and multiple statements remain blocked even with `execute`. Execute permission permits advanced SELECT features, not bypassing these restrictions.
|
|
470
|
+
- **Quoted content (v1.44.0)**: Comment, statement-separator, and keyword checks ignore the contents of string literals and quoted identifiers, so values such as `'#fff'`, `'a;b'`, or `'x -- y'` are accepted. A literal containing a backslash is lexed differently depending on `NO_BACKSLASH_ESCAPES`, so such queries are checked strictly against the raw text.
|
|
471
|
+
- **Common table expressions (v1.44.0)**: `WITH ... SELECT/UPDATE/DELETE` is authorized as its main statement. Like derived tables and UNION, a CTE in a read query requires `execute`. A `WITH` clause that cannot be parsed is rejected.
|
|
462
472
|
|
|
463
473
|
### Vulnerability Reporting
|
|
464
474
|
|
|
@@ -466,8 +476,8 @@ Suspected security issues must be reported privately via GitHub Private Vulnerab
|
|
|
466
476
|
|
|
467
477
|
### Data Protection
|
|
468
478
|
|
|
469
|
-
- **PII Masking**: Automatic sensitive data redaction in exports
|
|
470
479
|
- **Query Validation**: Input validation and SQL injection prevention
|
|
480
|
+
- **No result masking**: Query and export results are returned unmasked. Restrict sensitive columns with database privileges or views.
|
|
471
481
|
|
|
472
482
|
### Connection Security
|
|
473
483
|
|
|
@@ -494,6 +504,12 @@ If your client reports `MCP error -32000: Connection closed`, the server process
|
|
|
494
504
|
|
|
495
505
|
If you see `ReferenceError: exports is not defined in ES module scope` in the server stderr logs, update to **v1.33.2+** (fixes an ESM/CJS mismatch that caused the MCP process to exit immediately).
|
|
496
506
|
|
|
507
|
+
### Development
|
|
508
|
+
|
|
509
|
+
- `npm run build` removes `dist/` before compiling, so files deleted from `src/` are never published.
|
|
510
|
+
- `npm run dev` runs `src/mcp-server.ts` with ts-node (fixed in v1.44.0).
|
|
511
|
+
- `npm test` runs the Jest suite in `src/__tests__/` (SQL validator, permission defaults, cache invalidation).
|
|
512
|
+
|
|
497
513
|
### Error Messages
|
|
498
514
|
|
|
499
515
|
The system provides detailed error messages indicating:
|
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-10-
|
|
7
|
+
**Last Updated:** 2026-10-26 13:35:39
|
|
8
8
|
|
|
9
9
|
[](https://www.npmjs.com/package/@berthojoris/mcp-mysql-server)
|
|
10
10
|
[](https://www.npmjs.com/package/@berthojoris/mcp-mysql-server)
|
|
@@ -271,6 +271,12 @@ Control database access with a **dual-layer filtering system** that provides bot
|
|
|
271
271
|
| `transaction` | BEGIN, COMMIT, ROLLBACK | ACID operations |
|
|
272
272
|
| `utility` | Connection testing, diagnostics | Troubleshooting |
|
|
273
273
|
|
|
274
|
+
**Changes in v1.44.0 (review before upgrading):**
|
|
275
|
+
- **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`.
|
|
276
|
+
- **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.
|
|
277
|
+
- **Query cache off by default**: opt in with `CACHE_ENABLED=true`. Any write and every transaction commit now clears the cache.
|
|
278
|
+
- **SQL validation**: `#`, `;`, and `--` inside string values (e.g. `'#fff'`) are no longer rejected. `WITH` (CTE) queries are supported and, like derived tables, need `execute` in read queries.
|
|
279
|
+
|
|
274
280
|
**Security changes in v1.43.4:**
|
|
275
281
|
- `EXPLAIN ANALYZE` runs the statement it wraps (MySQL 8.0.18+), so it now requires that statement's permission: `delete` for DELETE, `update` or `execute` for UPDATE, `read` for SELECT. Plan-only `EXPLAIN` still needs just `list` or `read`.
|
|
276
282
|
|
package/bin/mcp-mysql.js
CHANGED
|
@@ -1,168 +1,168 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* MCP MySQL Server CLI
|
|
5
|
-
* This script allows the MySQL MCP server to be run via NPX
|
|
6
|
-
* It implements the Model Context Protocol using stdio transport
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const dotenv = require("dotenv");
|
|
10
|
-
|
|
11
|
-
dotenv.config();
|
|
12
|
-
|
|
13
|
-
// Get MySQL connection string, permissions, and optional categories
|
|
14
|
-
const args = process.argv.slice(2);
|
|
15
|
-
const mysqlUrl = args.shift();
|
|
16
|
-
|
|
17
|
-
let permissions;
|
|
18
|
-
let categories;
|
|
19
|
-
|
|
20
|
-
for (let i = 0; i < args.length; i++) {
|
|
21
|
-
const arg = args[i];
|
|
22
|
-
|
|
23
|
-
if (permissions === undefined) {
|
|
24
|
-
permissions = arg;
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (categories === undefined) {
|
|
29
|
-
categories = arg;
|
|
30
|
-
continue;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (!mysqlUrl) {
|
|
35
|
-
console.error("Error: MySQL connection URL is required");
|
|
36
|
-
console.error(
|
|
37
|
-
"Usage: mcp-mysql mysql://user:password@host:port/dbname [permissions] [categories]",
|
|
38
|
-
);
|
|
39
|
-
console.error("");
|
|
40
|
-
console.error("Examples:");
|
|
41
|
-
console.error(" #
|
|
42
|
-
console.error(" mcp-mysql mysql://root:pass@localhost:3306/mydb");
|
|
43
|
-
console.error("");
|
|
44
|
-
console.error(" # Permission-based filtering only (Layer 1)");
|
|
45
|
-
console.error(
|
|
46
|
-
' mcp-mysql mysql://root:pass@localhost:3306/mydb "list,read,utility"',
|
|
47
|
-
);
|
|
48
|
-
console.error("");
|
|
49
|
-
console.error(
|
|
50
|
-
" # Dual-layer: Permissions + Categories (fine-grained control)",
|
|
51
|
-
);
|
|
52
|
-
console.error(
|
|
53
|
-
' mcp-mysql mysql://root:pass@localhost:3306/mydb "list,read,utility" "database_discovery,custom_queries,analysis"',
|
|
54
|
-
);
|
|
55
|
-
console.error("");
|
|
56
|
-
console.error("Permissions (Layer 1 - Broad Control):");
|
|
57
|
-
console.error(
|
|
58
|
-
" list, read, create, update, delete, execute, ddl, utility, transaction, procedure",
|
|
59
|
-
);
|
|
60
|
-
console.error("");
|
|
61
|
-
console.error("Categories (Layer 2 - Fine-Grained Control, Optional):");
|
|
62
|
-
console.error(
|
|
63
|
-
" database_discovery, crud_operations, bulk_operations, custom_queries,",
|
|
64
|
-
);
|
|
65
|
-
console.error(
|
|
66
|
-
" schema_management, utilities, transaction_management, stored_procedures,",
|
|
67
|
-
);
|
|
68
|
-
console.error(
|
|
69
|
-
" views_management, triggers_management, index_management,",
|
|
70
|
-
);
|
|
71
|
-
console.error(
|
|
72
|
-
" constraint_management, table_maintenance, query_optimization,",
|
|
73
|
-
);
|
|
74
|
-
console.error(
|
|
75
|
-
" analysis, seed_operations",
|
|
76
|
-
);
|
|
77
|
-
console.error("");
|
|
78
|
-
console.error("Filtering Logic:");
|
|
79
|
-
console.error(
|
|
80
|
-
" - If only permissions: All tools within those permissions enabled",
|
|
81
|
-
);
|
|
82
|
-
console.error(
|
|
83
|
-
" - If permissions + categories: Only tools matching BOTH layers enabled",
|
|
84
|
-
);
|
|
85
|
-
console.error(" - If nothing specified:
|
|
86
|
-
process.exit(1);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Parse the MySQL URL to extract connection details
|
|
90
|
-
let connectionConfig;
|
|
91
|
-
let database;
|
|
92
|
-
try {
|
|
93
|
-
const url = new URL(mysqlUrl);
|
|
94
|
-
|
|
95
|
-
// Remove leading slash from pathname and make database optional
|
|
96
|
-
database = url.pathname.replace(/^\//, "") || null;
|
|
97
|
-
|
|
98
|
-
// Extract username and password from auth.
|
|
99
|
-
// Both fields may be URL-encoded (e.g. "@" -> "%40"), so always decode
|
|
100
|
-
// whatever the URL parser hands us, including when only one side is set.
|
|
101
|
-
const decodeAuthField = (value) =>
|
|
102
|
-
value ? decodeURIComponent(value) : value;
|
|
103
|
-
const auth = {
|
|
104
|
-
user: decodeAuthField(url.username) || "root",
|
|
105
|
-
password: decodeAuthField(url.password) || "",
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
connectionConfig = {
|
|
109
|
-
host: url.hostname,
|
|
110
|
-
port: url.port || 3306,
|
|
111
|
-
...auth,
|
|
112
|
-
...(database ? { database } : {}),
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
// Set environment variables for the server
|
|
116
|
-
process.env.DB_HOST = connectionConfig.host;
|
|
117
|
-
process.env.DB_PORT = connectionConfig.port;
|
|
118
|
-
process.env.DB_USER = connectionConfig.user;
|
|
119
|
-
process.env.DB_PASSWORD = connectionConfig.password;
|
|
120
|
-
if (database) {
|
|
121
|
-
process.env.DB_NAME = database;
|
|
122
|
-
}
|
|
123
|
-
} catch (error) {
|
|
124
|
-
console.error("Error parsing MySQL URL:", error.message);
|
|
125
|
-
console.error(
|
|
126
|
-
"Usage: npx @berthojoris/mcp-mysql-server mysql://user:password@host:port/dbname",
|
|
127
|
-
);
|
|
128
|
-
process.exit(1);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const dbMessage = database
|
|
132
|
-
? `${connectionConfig.host}:${connectionConfig.port}/${database}`
|
|
133
|
-
: `${connectionConfig.host}:${connectionConfig.port} (no specific database selected)`;
|
|
134
|
-
|
|
135
|
-
// Set permissions/categories as environment variables if provided
|
|
136
|
-
if (permissions) {
|
|
137
|
-
process.env.MCP_PERMISSIONS = permissions;
|
|
138
|
-
console.error(`Permissions (Layer 1): ${permissions}`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (categories) {
|
|
142
|
-
process.env.MCP_CATEGORIES = categories;
|
|
143
|
-
console.error(`Categories (Layer 2): ${categories}`);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (!permissions && !categories) {
|
|
147
|
-
console.error("Access Control:
|
|
148
|
-
} else if (permissions && categories) {
|
|
149
|
-
console.error("Filtering Mode: Dual-layer (Permissions + Categories)");
|
|
150
|
-
} else if (permissions && !categories) {
|
|
151
|
-
console.error("Filtering Mode: Permission-based only");
|
|
152
|
-
} else if (!permissions && categories) {
|
|
153
|
-
console.error("Filtering Mode: Category-only");
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Log to stderr (not stdout, which is used for MCP protocol)
|
|
157
|
-
console.error(`Starting MySQL MCP server with connection to ${dbMessage}`);
|
|
158
|
-
|
|
159
|
-
// Run the MCP server directly (no child process spawn)
|
|
160
|
-
// This ensures proper stdio communication for MCP protocol
|
|
161
|
-
try {
|
|
162
|
-
// Directly require and execute the MCP server module
|
|
163
|
-
// This avoids stdio forwarding issues with child processes
|
|
164
|
-
require("../dist/mcp-server.js");
|
|
165
|
-
} catch (error) {
|
|
166
|
-
console.error("Error starting MCP server:", error.message);
|
|
167
|
-
process.exit(1);
|
|
168
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MCP MySQL Server CLI
|
|
5
|
+
* This script allows the MySQL MCP server to be run via NPX
|
|
6
|
+
* It implements the Model Context Protocol using stdio transport
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const dotenv = require("dotenv");
|
|
10
|
+
|
|
11
|
+
dotenv.config();
|
|
12
|
+
|
|
13
|
+
// Get MySQL connection string, permissions, and optional categories
|
|
14
|
+
const args = process.argv.slice(2);
|
|
15
|
+
const mysqlUrl = args.shift();
|
|
16
|
+
|
|
17
|
+
let permissions;
|
|
18
|
+
let categories;
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < args.length; i++) {
|
|
21
|
+
const arg = args[i];
|
|
22
|
+
|
|
23
|
+
if (permissions === undefined) {
|
|
24
|
+
permissions = arg;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (categories === undefined) {
|
|
29
|
+
categories = arg;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!mysqlUrl) {
|
|
35
|
+
console.error("Error: MySQL connection URL is required");
|
|
36
|
+
console.error(
|
|
37
|
+
"Usage: mcp-mysql mysql://user:password@host:port/dbname [permissions] [categories]",
|
|
38
|
+
);
|
|
39
|
+
console.error("");
|
|
40
|
+
console.error("Examples:");
|
|
41
|
+
console.error(" # Read-only default (list,read,utility)");
|
|
42
|
+
console.error(" mcp-mysql mysql://root:pass@localhost:3306/mydb");
|
|
43
|
+
console.error("");
|
|
44
|
+
console.error(" # Permission-based filtering only (Layer 1)");
|
|
45
|
+
console.error(
|
|
46
|
+
' mcp-mysql mysql://root:pass@localhost:3306/mydb "list,read,utility"',
|
|
47
|
+
);
|
|
48
|
+
console.error("");
|
|
49
|
+
console.error(
|
|
50
|
+
" # Dual-layer: Permissions + Categories (fine-grained control)",
|
|
51
|
+
);
|
|
52
|
+
console.error(
|
|
53
|
+
' mcp-mysql mysql://root:pass@localhost:3306/mydb "list,read,utility" "database_discovery,custom_queries,analysis"',
|
|
54
|
+
);
|
|
55
|
+
console.error("");
|
|
56
|
+
console.error("Permissions (Layer 1 - Broad Control):");
|
|
57
|
+
console.error(
|
|
58
|
+
" list, read, create, update, delete, execute, ddl, utility, transaction, procedure",
|
|
59
|
+
);
|
|
60
|
+
console.error("");
|
|
61
|
+
console.error("Categories (Layer 2 - Fine-Grained Control, Optional):");
|
|
62
|
+
console.error(
|
|
63
|
+
" database_discovery, crud_operations, bulk_operations, custom_queries,",
|
|
64
|
+
);
|
|
65
|
+
console.error(
|
|
66
|
+
" schema_management, utilities, transaction_management, stored_procedures,",
|
|
67
|
+
);
|
|
68
|
+
console.error(
|
|
69
|
+
" views_management, triggers_management, index_management,",
|
|
70
|
+
);
|
|
71
|
+
console.error(
|
|
72
|
+
" constraint_management, table_maintenance, query_optimization,",
|
|
73
|
+
);
|
|
74
|
+
console.error(
|
|
75
|
+
" analysis, seed_operations",
|
|
76
|
+
);
|
|
77
|
+
console.error("");
|
|
78
|
+
console.error("Filtering Logic:");
|
|
79
|
+
console.error(
|
|
80
|
+
" - If only permissions: All tools within those permissions enabled",
|
|
81
|
+
);
|
|
82
|
+
console.error(
|
|
83
|
+
" - If permissions + categories: Only tools matching BOTH layers enabled",
|
|
84
|
+
);
|
|
85
|
+
console.error(" - If nothing specified: Read-only default (list,read,utility)");
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Parse the MySQL URL to extract connection details
|
|
90
|
+
let connectionConfig;
|
|
91
|
+
let database;
|
|
92
|
+
try {
|
|
93
|
+
const url = new URL(mysqlUrl);
|
|
94
|
+
|
|
95
|
+
// Remove leading slash from pathname and make database optional
|
|
96
|
+
database = url.pathname.replace(/^\//, "") || null;
|
|
97
|
+
|
|
98
|
+
// Extract username and password from auth.
|
|
99
|
+
// Both fields may be URL-encoded (e.g. "@" -> "%40"), so always decode
|
|
100
|
+
// whatever the URL parser hands us, including when only one side is set.
|
|
101
|
+
const decodeAuthField = (value) =>
|
|
102
|
+
value ? decodeURIComponent(value) : value;
|
|
103
|
+
const auth = {
|
|
104
|
+
user: decodeAuthField(url.username) || "root",
|
|
105
|
+
password: decodeAuthField(url.password) || "",
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
connectionConfig = {
|
|
109
|
+
host: url.hostname,
|
|
110
|
+
port: url.port || 3306,
|
|
111
|
+
...auth,
|
|
112
|
+
...(database ? { database } : {}),
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// Set environment variables for the server
|
|
116
|
+
process.env.DB_HOST = connectionConfig.host;
|
|
117
|
+
process.env.DB_PORT = connectionConfig.port;
|
|
118
|
+
process.env.DB_USER = connectionConfig.user;
|
|
119
|
+
process.env.DB_PASSWORD = connectionConfig.password;
|
|
120
|
+
if (database) {
|
|
121
|
+
process.env.DB_NAME = database;
|
|
122
|
+
}
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.error("Error parsing MySQL URL:", error.message);
|
|
125
|
+
console.error(
|
|
126
|
+
"Usage: npx @berthojoris/mcp-mysql-server mysql://user:password@host:port/dbname",
|
|
127
|
+
);
|
|
128
|
+
process.exit(1);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const dbMessage = database
|
|
132
|
+
? `${connectionConfig.host}:${connectionConfig.port}/${database}`
|
|
133
|
+
: `${connectionConfig.host}:${connectionConfig.port} (no specific database selected)`;
|
|
134
|
+
|
|
135
|
+
// Set permissions/categories as environment variables if provided
|
|
136
|
+
if (permissions) {
|
|
137
|
+
process.env.MCP_PERMISSIONS = permissions;
|
|
138
|
+
console.error(`Permissions (Layer 1): ${permissions}`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (categories) {
|
|
142
|
+
process.env.MCP_CATEGORIES = categories;
|
|
143
|
+
console.error(`Categories (Layer 2): ${categories}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!permissions && !categories) {
|
|
147
|
+
console.error("Access Control: Read-only default (list,read,utility)");
|
|
148
|
+
} else if (permissions && categories) {
|
|
149
|
+
console.error("Filtering Mode: Dual-layer (Permissions + Categories)");
|
|
150
|
+
} else if (permissions && !categories) {
|
|
151
|
+
console.error("Filtering Mode: Permission-based only");
|
|
152
|
+
} else if (!permissions && categories) {
|
|
153
|
+
console.error("Filtering Mode: Category-only");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Log to stderr (not stdout, which is used for MCP protocol)
|
|
157
|
+
console.error(`Starting MySQL MCP server with connection to ${dbMessage}`);
|
|
158
|
+
|
|
159
|
+
// Run the MCP server directly (no child process spawn)
|
|
160
|
+
// This ensures proper stdio communication for MCP protocol
|
|
161
|
+
try {
|
|
162
|
+
// Directly require and execute the MCP server module
|
|
163
|
+
// This avoids stdio forwarding issues with child processes
|
|
164
|
+
require("../dist/mcp-server.js");
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error("Error starting MCP server:", error.message);
|
|
167
|
+
process.exit(1);
|
|
168
|
+
}
|
package/dist/cache/queryCache.js
CHANGED
|
@@ -9,7 +9,7 @@ const crypto_1 = __importDefault(require("crypto"));
|
|
|
9
9
|
* Default cache configuration
|
|
10
10
|
*/
|
|
11
11
|
const DEFAULT_CONFIG = {
|
|
12
|
-
enabled: true
|
|
12
|
+
enabled: false, // Opt in with CACHE_ENABLED=true; writes by other clients are not detected
|
|
13
13
|
ttlMs: 60000, // 1 minute default TTL
|
|
14
14
|
maxSize: 100, // Maximum 100 cached queries
|
|
15
15
|
maxMemoryMB: 50, // Maximum 50MB memory usage
|
package/dist/config/config.js
CHANGED
|
@@ -12,7 +12,8 @@ exports.dbConfig = {
|
|
|
12
12
|
port: parseInt(process.env.DB_PORT || '3306', 10),
|
|
13
13
|
user: process.env.DB_USER || 'root',
|
|
14
14
|
password: process.env.DB_PASSWORD || '',
|
|
15
|
-
database
|
|
15
|
+
// Empty means no database selected; never default to the mysql system schema
|
|
16
|
+
database: process.env.DB_NAME || '',
|
|
16
17
|
};
|
|
17
18
|
exports.default = {
|
|
18
19
|
database: exports.dbConfig,
|
|
@@ -43,6 +43,11 @@ export declare const toolCategoryMap: Record<string, ToolCategory>;
|
|
|
43
43
|
* Map of tool names to their documentation categories (New Enhanced System)
|
|
44
44
|
*/
|
|
45
45
|
export declare const toolDocCategoryMap: Record<string, DocCategory>;
|
|
46
|
+
/**
|
|
47
|
+
* Permissions used when neither permissions nor categories are configured.
|
|
48
|
+
* Read-only by default; write and schema access must be granted explicitly.
|
|
49
|
+
*/
|
|
50
|
+
export declare const DEFAULT_PERMISSIONS = "list,read,utility";
|
|
46
51
|
/**
|
|
47
52
|
* Class to manage feature configuration based on runtime or environment variables
|
|
48
53
|
* Supports dual-layer filtering:
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.featureConfig = exports.FeatureConfig = exports.toolDocCategoryMap = exports.toolCategoryMap = exports.DocCategory = exports.ToolCategory = void 0;
|
|
6
|
+
exports.featureConfig = exports.FeatureConfig = exports.DEFAULT_PERMISSIONS = exports.toolDocCategoryMap = exports.toolCategoryMap = exports.DocCategory = exports.ToolCategory = void 0;
|
|
7
7
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
8
8
|
// Load environment variables
|
|
9
9
|
dotenv_1.default.config();
|
|
@@ -365,6 +365,11 @@ const legacyToDocCategoryMap = {
|
|
|
365
365
|
transaction: [DocCategory.TRANSACTION_MANAGEMENT, DocCategory.SEED_OPERATIONS],
|
|
366
366
|
procedure: [DocCategory.STORED_PROCEDURES],
|
|
367
367
|
};
|
|
368
|
+
/**
|
|
369
|
+
* Permissions used when neither permissions nor categories are configured.
|
|
370
|
+
* Read-only by default; write and schema access must be granted explicitly.
|
|
371
|
+
*/
|
|
372
|
+
exports.DEFAULT_PERMISSIONS = "list,read,utility";
|
|
368
373
|
/**
|
|
369
374
|
* Class to manage feature configuration based on runtime or environment variables
|
|
370
375
|
* Supports dual-layer filtering:
|
|
@@ -403,12 +408,9 @@ class FeatureConfig {
|
|
|
403
408
|
* Layer 2 (categories): Fine-grained control using documentation categories (optional)
|
|
404
409
|
*/
|
|
405
410
|
parseConfig(permissionsStr, categoriesStr) {
|
|
406
|
-
// If both are empty,
|
|
411
|
+
// If both are empty, fall back to the read-only default permissions
|
|
407
412
|
if (!permissionsStr.trim() && !categoriesStr.trim()) {
|
|
408
|
-
|
|
409
|
-
legacy: new Set(Object.values(ToolCategory)),
|
|
410
|
-
doc: new Set(Object.values(DocCategory)),
|
|
411
|
-
};
|
|
413
|
+
permissionsStr = exports.DEFAULT_PERMISSIONS;
|
|
412
414
|
}
|
|
413
415
|
// Parse Layer 1: Permissions (legacy categories)
|
|
414
416
|
let legacySet = new Set();
|
|
@@ -504,11 +506,6 @@ class FeatureConfig {
|
|
|
504
506
|
return (`Permission denied: This tool also requires '${missingPermission}' permission (Layer 1). ` +
|
|
505
507
|
`Add '${missingPermission}' to the permissions argument.`);
|
|
506
508
|
}
|
|
507
|
-
const isAllEnabled = !this.originalPermissionsString.trim() &&
|
|
508
|
-
!this.originalCategoriesString.trim();
|
|
509
|
-
if (isAllEnabled) {
|
|
510
|
-
return `Unknown error: All tools should be enabled but '${toolName}' was blocked.`;
|
|
511
|
-
}
|
|
512
509
|
// Build error message based on dual-layer or single-layer mode
|
|
513
510
|
if (this.useDualLayer) {
|
|
514
511
|
const hasPermission = legacyCategory
|
|
@@ -591,7 +588,8 @@ class FeatureConfig {
|
|
|
591
588
|
*/
|
|
592
589
|
getConfigSnapshot() {
|
|
593
590
|
return {
|
|
594
|
-
permissions: this.originalPermissionsString ||
|
|
591
|
+
permissions: this.originalPermissionsString ||
|
|
592
|
+
(this.originalCategoriesString ? "all" : exports.DEFAULT_PERMISSIONS),
|
|
595
593
|
categories: this.originalCategoriesString ||
|
|
596
594
|
(this.useDualLayer ? "" : "derived from permissions"),
|
|
597
595
|
filteringMode: this.getFilteringMode(),
|
|
@@ -605,7 +603,7 @@ class FeatureConfig {
|
|
|
605
603
|
getFilteringMode() {
|
|
606
604
|
if (!this.originalPermissionsString.trim() &&
|
|
607
605
|
!this.originalCategoriesString.trim()) {
|
|
608
|
-
return
|
|
606
|
+
return `Default read-only (${exports.DEFAULT_PERMISSIONS})`;
|
|
609
607
|
}
|
|
610
608
|
if (this.useDualLayer) {
|
|
611
609
|
return "Dual-layer (Permissions + Categories)";
|
package/dist/db/connection.d.ts
CHANGED
|
@@ -13,7 +13,14 @@ declare class DatabaseConnection {
|
|
|
13
13
|
getConnection(): Promise<mysql.PoolConnection>;
|
|
14
14
|
query<T>(sql: string, params?: any[], useCache?: boolean): Promise<T>;
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
16
|
+
* Whether a statement cannot change data seen by cached SELECTs.
|
|
17
|
+
* EXPLAIN/DESCRIBE ANALYZE executes the wrapped statement, so it is excluded.
|
|
18
|
+
*/
|
|
19
|
+
static isReadOnlyStatement(sql: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Clear the whole cache when a statement may have changed data. Writes can
|
|
22
|
+
* touch tables that are hard to extract reliably (multi-table UPDATE/DELETE,
|
|
23
|
+
* db-qualified names, DDL, procedures), so no table-level guessing is done.
|
|
17
24
|
*/
|
|
18
25
|
private invalidateCacheForWriteOperation;
|
|
19
26
|
testConnection(): Promise<{
|
package/dist/db/connection.js
CHANGED
|
@@ -17,7 +17,8 @@ class DatabaseConnection {
|
|
|
17
17
|
port: config_1.dbConfig.port,
|
|
18
18
|
user: config_1.dbConfig.user,
|
|
19
19
|
password: config_1.dbConfig.password,
|
|
20
|
-
|
|
20
|
+
// No default schema unless one was configured
|
|
21
|
+
database: config_1.dbConfig.database || undefined,
|
|
21
22
|
waitForConnections: true,
|
|
22
23
|
connectionLimit: 10,
|
|
23
24
|
queueLimit: 0,
|
|
@@ -67,9 +68,7 @@ class DatabaseConnection {
|
|
|
67
68
|
this.queryCache.set(sql, params, results);
|
|
68
69
|
}
|
|
69
70
|
// Invalidate cache for write operations
|
|
70
|
-
|
|
71
|
-
this.invalidateCacheForWriteOperation(sql);
|
|
72
|
-
}
|
|
71
|
+
this.invalidateCacheForWriteOperation(sql);
|
|
73
72
|
return results;
|
|
74
73
|
}
|
|
75
74
|
catch (error) {
|
|
@@ -79,18 +78,20 @@ class DatabaseConnection {
|
|
|
79
78
|
}
|
|
80
79
|
}
|
|
81
80
|
/**
|
|
82
|
-
*
|
|
81
|
+
* Whether a statement cannot change data seen by cached SELECTs.
|
|
82
|
+
* EXPLAIN/DESCRIBE ANALYZE executes the wrapped statement, so it is excluded.
|
|
83
|
+
*/
|
|
84
|
+
static isReadOnlyStatement(sql) {
|
|
85
|
+
return /^(?:SELECT|SHOW)\b|^(?:EXPLAIN|DESCRIBE|DESC)\b(?!\s+ANALYZE\b)/i.test(sql.trim());
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Clear the whole cache when a statement may have changed data. Writes can
|
|
89
|
+
* touch tables that are hard to extract reliably (multi-table UPDATE/DELETE,
|
|
90
|
+
* db-qualified names, DDL, procedures), so no table-level guessing is done.
|
|
83
91
|
*/
|
|
84
92
|
invalidateCacheForWriteOperation(sql) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const updateMatch = sql.match(/UPDATE\s+[\`"']?(\w+)[\`"']?/i);
|
|
88
|
-
const deleteMatch = sql.match(/DELETE\s+FROM\s+[\`"']?(\w+)[\`"']?/i);
|
|
89
|
-
const truncateMatch = sql.match(/TRUNCATE\s+(?:TABLE\s+)?[\`"']?(\w+)[\`"']?/i);
|
|
90
|
-
const dropMatch = sql.match(/DROP\s+TABLE\s+(?:IF\s+EXISTS\s+)?[\`"']?(\w+)[\`"']?/i);
|
|
91
|
-
const match = insertMatch || updateMatch || deleteMatch || truncateMatch || dropMatch;
|
|
92
|
-
if (match && match[1]) {
|
|
93
|
-
this.queryCache.invalidateTable(match[1]);
|
|
93
|
+
if (!DatabaseConnection.isReadOnlyStatement(sql)) {
|
|
94
|
+
this.queryCache.clear();
|
|
94
95
|
}
|
|
95
96
|
}
|
|
96
97
|
async testConnection() {
|
|
@@ -286,6 +287,8 @@ class DatabaseConnection {
|
|
|
286
287
|
await transaction.connection.commit();
|
|
287
288
|
transaction.connection.release();
|
|
288
289
|
this.activeTransactions.delete(transactionId);
|
|
290
|
+
// Reads cached while the transaction was open predate its committed writes
|
|
291
|
+
this.queryCache.clear();
|
|
289
292
|
}
|
|
290
293
|
catch (error) {
|
|
291
294
|
// If commit fails, rollback and release connection
|
|
@@ -395,6 +398,8 @@ class DatabaseConnection {
|
|
|
395
398
|
const [results] = await transaction.connection.query(sql, params);
|
|
396
399
|
const duration = Date.now() - startTime;
|
|
397
400
|
queryLogger_1.QueryLogger.log(sql, params, duration, "success");
|
|
401
|
+
// DDL commits implicitly, so its effects are visible before COMMIT
|
|
402
|
+
this.invalidateCacheForWriteOperation(sql);
|
|
398
403
|
// Reset timeout on successful activity and update last activity
|
|
399
404
|
this.resetTransactionTimeout(transactionId);
|
|
400
405
|
transaction.lastActivity = now;
|