@ecubelabs/mysql-mcp 1.1.0-next.3 → 2.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +243 -455
- package/dist/index.js +8 -7
- package/dist/src/config/index.js +259 -128
- package/dist/src/db/index.js +19 -11
- package/dist/src/ssh/config.js +0 -135
- package/dist/src/ssh/known-hosts.js +1 -4
- package/dist/src/ssh/tunnel.js +26 -98
- package/package.json +2 -2
package/dist/src/ssh/config.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
1
|
import * as os from 'os';
|
|
3
2
|
import * as path from 'path';
|
|
4
3
|
export function expandHome(p) {
|
|
@@ -10,137 +9,3 @@ export function expandHome(p) {
|
|
|
10
9
|
}
|
|
11
10
|
return p;
|
|
12
11
|
}
|
|
13
|
-
function splitDirective(line) {
|
|
14
|
-
const trimmed = line.trim();
|
|
15
|
-
if (!trimmed || trimmed.startsWith('#')) {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
const eq = trimmed.indexOf('=');
|
|
19
|
-
const ws = trimmed.search(/\s/);
|
|
20
|
-
let sepIndex;
|
|
21
|
-
if (eq !== -1 && (ws === -1 || eq < ws)) {
|
|
22
|
-
sepIndex = eq;
|
|
23
|
-
}
|
|
24
|
-
else if (ws !== -1) {
|
|
25
|
-
sepIndex = ws;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
const keyword = trimmed.slice(0, sepIndex).trim().toLowerCase();
|
|
31
|
-
const value = trimmed
|
|
32
|
-
.slice(sepIndex + 1)
|
|
33
|
-
.trim()
|
|
34
|
-
.replace(/^=/, '')
|
|
35
|
-
.trim();
|
|
36
|
-
if (!keyword || !value) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
return [keyword, value];
|
|
40
|
-
}
|
|
41
|
-
function parseLocalForward(value) {
|
|
42
|
-
const parts = value.split(/\s+/).filter(Boolean);
|
|
43
|
-
if (parts.length < 2) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
const localRaw = parts[0];
|
|
47
|
-
const localPortStr = localRaw.includes(':') ? localRaw.slice(localRaw.lastIndexOf(':') + 1) : localRaw;
|
|
48
|
-
const localPort = Number(localPortStr);
|
|
49
|
-
if (!Number.isInteger(localPort) || localPort < 0 || localPort > 65535) {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
let remoteHost;
|
|
53
|
-
let remotePortStr;
|
|
54
|
-
const remoteRaw = parts[1];
|
|
55
|
-
const lastColon = remoteRaw.lastIndexOf(':');
|
|
56
|
-
if (lastColon > 0) {
|
|
57
|
-
remoteHost = remoteRaw.slice(0, lastColon);
|
|
58
|
-
remotePortStr = remoteRaw.slice(lastColon + 1);
|
|
59
|
-
}
|
|
60
|
-
else if (parts.length >= 3) {
|
|
61
|
-
remoteHost = remoteRaw;
|
|
62
|
-
remotePortStr = parts[2];
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
const remotePort = Number(remotePortStr);
|
|
68
|
-
if (!remoteHost || !Number.isInteger(remotePort) || remotePort <= 0) {
|
|
69
|
-
return null;
|
|
70
|
-
}
|
|
71
|
-
return { localPort, remoteHost, remotePort };
|
|
72
|
-
}
|
|
73
|
-
export function readSSHConfigHost(alias, configPath = path.join(os.homedir(), '.ssh', 'config')) {
|
|
74
|
-
if (!fs.existsSync(configPath)) {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
const wanted = alias.trim().toLowerCase();
|
|
78
|
-
let raw;
|
|
79
|
-
try {
|
|
80
|
-
raw = fs.readFileSync(configPath, 'utf8');
|
|
81
|
-
}
|
|
82
|
-
catch (err) {
|
|
83
|
-
throw new Error(`Failed to read SSH config at ${configPath}: ${err.message}`);
|
|
84
|
-
}
|
|
85
|
-
let inBlock = false;
|
|
86
|
-
const entry = {};
|
|
87
|
-
let found = false;
|
|
88
|
-
for (const line of raw.split(/\r?\n/)) {
|
|
89
|
-
const directive = splitDirective(line);
|
|
90
|
-
if (!directive) {
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const [keyword, value] = directive;
|
|
94
|
-
if (keyword === 'host') {
|
|
95
|
-
inBlock = value.split(/\s+/).some((token) => token.toLowerCase() === wanted);
|
|
96
|
-
if (inBlock) {
|
|
97
|
-
found = true;
|
|
98
|
-
}
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
if (keyword === 'match') {
|
|
102
|
-
inBlock = false;
|
|
103
|
-
continue;
|
|
104
|
-
}
|
|
105
|
-
if (!inBlock) {
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
switch (keyword) {
|
|
109
|
-
case 'hostname':
|
|
110
|
-
entry.hostName ??= value;
|
|
111
|
-
break;
|
|
112
|
-
case 'user':
|
|
113
|
-
entry.user ??= value;
|
|
114
|
-
break;
|
|
115
|
-
case 'port': {
|
|
116
|
-
const port = Number(value);
|
|
117
|
-
if (Number.isInteger(port) && port > 0 && port <= 65535) {
|
|
118
|
-
entry.port ??= port;
|
|
119
|
-
}
|
|
120
|
-
break;
|
|
121
|
-
}
|
|
122
|
-
case 'identityfile':
|
|
123
|
-
entry.identityFile ??= expandHome(value.replace(/^"|"$/g, ''));
|
|
124
|
-
break;
|
|
125
|
-
case 'userknownhostsfile':
|
|
126
|
-
entry.userKnownHostsFiles ??=
|
|
127
|
-
value.toLowerCase() === 'none'
|
|
128
|
-
? []
|
|
129
|
-
: value.split(/\s+/).map((file) => expandHome(file.replace(/^"|"$/g, '')));
|
|
130
|
-
break;
|
|
131
|
-
case 'hostkeyalias':
|
|
132
|
-
entry.hostKeyAlias ??= value;
|
|
133
|
-
break;
|
|
134
|
-
case 'localforward': {
|
|
135
|
-
const forward = parseLocalForward(value);
|
|
136
|
-
if (forward && !entry.localForward) {
|
|
137
|
-
entry.localForward = forward;
|
|
138
|
-
}
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
default:
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return found ? entry : null;
|
|
146
|
-
}
|
|
@@ -3,10 +3,7 @@ import * as fs from 'fs';
|
|
|
3
3
|
import { expandHome } from './config.js';
|
|
4
4
|
export const DEFAULT_USER_KNOWN_HOSTS_FILES = ['~/.ssh/known_hosts', '~/.ssh/known_hosts2'];
|
|
5
5
|
const GLOBAL_KNOWN_HOSTS_FILES = ['/etc/ssh/ssh_known_hosts', '/etc/ssh/ssh_known_hosts2'];
|
|
6
|
-
export function knownHostsName(host, port
|
|
7
|
-
if (hostKeyAlias) {
|
|
8
|
-
return hostKeyAlias;
|
|
9
|
-
}
|
|
6
|
+
export function knownHostsName(host, port) {
|
|
10
7
|
const lowered = host.toLowerCase();
|
|
11
8
|
return port === 22 ? lowered : `[${lowered}]:${port}`;
|
|
12
9
|
}
|
package/dist/src/ssh/tunnel.js
CHANGED
|
@@ -1,95 +1,44 @@
|
|
|
1
1
|
import * as fs from 'fs';
|
|
2
2
|
import * as net from 'net';
|
|
3
3
|
import { Client as SSHClient } from 'ssh2';
|
|
4
|
-
import { MYSQL_HOST, MYSQL_PORT,
|
|
4
|
+
import { MYSQL_HOST, MYSQL_PORT, SSH_ENABLED, SSH_HOST, SSH_PASSPHRASE, SSH_PORT, SSH_PRIVATE_KEY_PATH, SSH_USER, } from '../config/index.js';
|
|
5
5
|
import { log } from '../utils/index.js';
|
|
6
|
-
import { expandHome
|
|
6
|
+
import { expandHome } from './config.js';
|
|
7
7
|
import { DEFAULT_USER_KNOWN_HOSTS_FILES, hostKeyAlgorithms, isKnownHostKey, knownHostsName, lookupKnownHosts, } from './known-hosts.js';
|
|
8
|
-
class LocalPortTakenError extends Error {
|
|
9
|
-
}
|
|
10
8
|
const RECONNECT_MAX_ATTEMPTS = 3;
|
|
11
9
|
const RECONNECT_BASE_DELAY_MS = 1000;
|
|
12
|
-
const PROBE_TIMEOUT_MS = 750;
|
|
13
10
|
const SSH_READY_TIMEOUT_MS = 20000;
|
|
14
11
|
let resolvedTunnelConfig = null;
|
|
15
12
|
export function resolveTunnelConfig() {
|
|
16
13
|
if (resolvedTunnelConfig) {
|
|
17
14
|
return resolvedTunnelConfig;
|
|
18
15
|
}
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
throw new Error(`${SSH_CONFIG_HOST_SOURCE ?? 'MYSQL_SSH_CONFIG_HOST'}="${configHost}" was not found as a Host alias in ~/.ssh/config. ` +
|
|
25
|
-
`Either add the alias or set MYSQL_SSH_HOST / MYSQL_SSH_USER explicitly.`);
|
|
26
|
-
}
|
|
27
|
-
log('info', `[ssh] resolved alias "${configHost}" from ~/.ssh/config: ` +
|
|
28
|
-
JSON.stringify({
|
|
29
|
-
hostName: fromConfig.hostName,
|
|
30
|
-
user: fromConfig.user,
|
|
31
|
-
port: fromConfig.port,
|
|
32
|
-
localForward: fromConfig.localForward,
|
|
33
|
-
}));
|
|
34
|
-
}
|
|
35
|
-
const sshHost = SSH_HOST ?? fromConfig?.hostName;
|
|
36
|
-
const sshUser = SSH_USER ?? fromConfig?.user;
|
|
37
|
-
const sshPort = SSH_PORT ?? fromConfig?.port ?? 22;
|
|
38
|
-
const privateKeyPath = expandHome(SSH_PRIVATE_KEY_PATH ?? fromConfig?.identityFile ?? '~/.ssh/id_rsa');
|
|
39
|
-
const localPort = SSH_LOCAL_PORT ?? fromConfig?.localForward?.localPort ?? 0;
|
|
40
|
-
const remoteHost = fromConfig?.localForward?.remoteHost ?? MYSQL_HOST;
|
|
41
|
-
const remotePort = fromConfig?.localForward?.remotePort ?? MYSQL_PORT;
|
|
42
|
-
const missing = [];
|
|
43
|
-
if (!sshHost) {
|
|
44
|
-
missing.push('MYSQL_SSH_HOST (or a HostName in the alias)');
|
|
45
|
-
}
|
|
46
|
-
if (!sshUser) {
|
|
47
|
-
missing.push('MYSQL_SSH_USER (or a User in the alias)');
|
|
48
|
-
}
|
|
49
|
-
if (!remoteHost) {
|
|
50
|
-
missing.push('MYSQL_HOST (or a LocalForward target in the alias)');
|
|
51
|
-
}
|
|
52
|
-
if (missing.length > 0) {
|
|
53
|
-
throw new Error(`SSH tunnel is enabled but underspecified. Missing: ${missing.join(', ')}.`);
|
|
16
|
+
const sshHost = SSH_HOST;
|
|
17
|
+
const sshUser = SSH_USER;
|
|
18
|
+
const remoteHost = MYSQL_HOST;
|
|
19
|
+
if (!sshHost || !sshUser || !remoteHost) {
|
|
20
|
+
throw new Error('SSH tunnel is enabled but underspecified. Pass --ssh-host, --ssh-user and --host together.');
|
|
54
21
|
}
|
|
22
|
+
const sshPort = SSH_PORT ?? 22;
|
|
23
|
+
const privateKeyPath = expandHome(SSH_PRIVATE_KEY_PATH ?? '~/.ssh/id_rsa');
|
|
55
24
|
if (!fs.existsSync(privateKeyPath)) {
|
|
56
25
|
throw new Error(`SSH private key not found at "${privateKeyPath}". ` +
|
|
57
|
-
`
|
|
26
|
+
`Pass --ssh-private-key-path with the key that authenticates ${sshUser}@${sshHost}.`);
|
|
58
27
|
}
|
|
59
28
|
resolvedTunnelConfig = {
|
|
60
|
-
sshHost
|
|
29
|
+
sshHost,
|
|
61
30
|
sshPort,
|
|
62
|
-
sshUser
|
|
31
|
+
sshUser,
|
|
63
32
|
privateKeyPath,
|
|
64
33
|
passphrase: SSH_PASSPHRASE,
|
|
65
|
-
knownHostsFiles:
|
|
66
|
-
hostKeyName: knownHostsName(sshHost, sshPort
|
|
67
|
-
sshTarget:
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
remotePort,
|
|
34
|
+
knownHostsFiles: DEFAULT_USER_KNOWN_HOSTS_FILES,
|
|
35
|
+
hostKeyName: knownHostsName(sshHost, sshPort),
|
|
36
|
+
sshTarget: `${sshPort === 22 ? '' : `-p ${sshPort} `}${sshUser}@${sshHost}`,
|
|
37
|
+
remoteHost,
|
|
38
|
+
remotePort: MYSQL_PORT,
|
|
71
39
|
};
|
|
72
40
|
return resolvedTunnelConfig;
|
|
73
41
|
}
|
|
74
|
-
function probeLocalPort(port) {
|
|
75
|
-
return new Promise((resolve) => {
|
|
76
|
-
const socket = new net.Socket();
|
|
77
|
-
let settled = false;
|
|
78
|
-
const finish = (result) => {
|
|
79
|
-
if (settled) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
settled = true;
|
|
83
|
-
socket.destroy();
|
|
84
|
-
resolve(result);
|
|
85
|
-
};
|
|
86
|
-
socket.setTimeout(PROBE_TIMEOUT_MS);
|
|
87
|
-
socket.once('connect', () => finish(true));
|
|
88
|
-
socket.once('timeout', () => finish(false));
|
|
89
|
-
socket.once('error', () => finish(false));
|
|
90
|
-
socket.connect(port, '127.0.0.1');
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
42
|
let endpointPromise = null;
|
|
94
43
|
let localServer = null;
|
|
95
44
|
let sshClient = null;
|
|
@@ -241,17 +190,13 @@ export function forwardSocket(socket, client, cfg, unavailable = null) {
|
|
|
241
190
|
stream.once('close', () => socket.destroy());
|
|
242
191
|
});
|
|
243
192
|
}
|
|
244
|
-
function startLocalServer(cfg
|
|
193
|
+
function startLocalServer(cfg) {
|
|
245
194
|
return new Promise((resolve, reject) => {
|
|
246
195
|
const server = net.createServer((socket) => forwardSocket(socket, sshClient, cfg, fatalError));
|
|
247
196
|
server.once('error', (err) => {
|
|
248
|
-
|
|
249
|
-
reject(new LocalPortTakenError(`Local port ${requestedPort} was claimed by another process.`));
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
reject(new Error(`Failed to open local tunnel listener on port ${requestedPort}: ${err.message}`));
|
|
197
|
+
reject(new Error(`Failed to open local tunnel listener: ${err.message}`));
|
|
253
198
|
});
|
|
254
|
-
server.listen(
|
|
199
|
+
server.listen(0, '127.0.0.1', () => {
|
|
255
200
|
const address = server.address();
|
|
256
201
|
if (!address || typeof address === 'string') {
|
|
257
202
|
reject(new Error('Local tunnel listener reported no TCP address.'));
|
|
@@ -270,38 +215,21 @@ export function ensureTunnel() {
|
|
|
270
215
|
endpointPromise = (async () => {
|
|
271
216
|
const cfg = resolveTunnelConfig();
|
|
272
217
|
activeConfig = cfg;
|
|
273
|
-
|
|
274
|
-
log('error', `[ssh] 127.0.0.1:${cfg.localPort} is already accepting connections; reusing it (MYSQL_SSH_REUSE_EXISTING=true).`);
|
|
275
|
-
activeEndpoint = { host: '127.0.0.1', port: cfg.localPort, reused: true };
|
|
276
|
-
return activeEndpoint;
|
|
277
|
-
}
|
|
278
|
-
log('info', `[ssh] opening tunnel ${cfg.sshUser}@${cfg.sshHost}:${cfg.sshPort} -> ${cfg.remoteHost}:${cfg.remotePort} ` +
|
|
279
|
-
`(local port ${cfg.localPort === 0 ? 'auto' : cfg.localPort})`);
|
|
218
|
+
log('info', `[ssh] opening tunnel ${cfg.sshUser}@${cfg.sshHost}:${cfg.sshPort} -> ${cfg.remoteHost}:${cfg.remotePort}`);
|
|
280
219
|
const client = await connectSSH(cfg);
|
|
281
220
|
sshClient = client;
|
|
282
221
|
let port;
|
|
283
222
|
try {
|
|
284
|
-
port = await startLocalServer(cfg
|
|
223
|
+
port = await startLocalServer(cfg);
|
|
285
224
|
}
|
|
286
225
|
catch (err) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
throw err;
|
|
291
|
-
}
|
|
292
|
-
log('error', `[ssh] local port ${cfg.localPort} is taken; opening our own tunnel on an auto-assigned port instead.`);
|
|
293
|
-
try {
|
|
294
|
-
port = await startLocalServer(cfg, 0);
|
|
295
|
-
}
|
|
296
|
-
catch (fallbackErr) {
|
|
297
|
-
client.destroy();
|
|
298
|
-
sshClient = null;
|
|
299
|
-
throw fallbackErr;
|
|
300
|
-
}
|
|
226
|
+
client.destroy();
|
|
227
|
+
sshClient = null;
|
|
228
|
+
throw err;
|
|
301
229
|
}
|
|
302
230
|
attachDropHandler(client, cfg);
|
|
303
231
|
log('info', `[ssh] tunnel listening on 127.0.0.1:${port}`);
|
|
304
|
-
activeEndpoint = { host: '127.0.0.1', port
|
|
232
|
+
activeEndpoint = { host: '127.0.0.1', port };
|
|
305
233
|
return activeEndpoint;
|
|
306
234
|
})();
|
|
307
235
|
endpointPromise.catch(() => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecubelabs/mysql-mcp",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-next.1",
|
|
4
4
|
"description": "Read-only MySQL MCP server that reaches its database through an SSH bastion, with per-environment profiles",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"type": "module",
|
|
14
14
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
15
|
+
"node": ">=18.7"
|
|
16
16
|
},
|
|
17
17
|
"main": "dist/index.js",
|
|
18
18
|
"bin": "dist/index.js",
|