@ecubelabs/mysql-mcp 1.0.0-next.6 → 1.0.0-next.7
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 +448 -296
- package/dist/index.js +239 -377
- package/dist/src/catalog/collect.js +2 -3
- package/dist/src/catalog/context.js +187 -0
- package/dist/src/catalog/docs.js +12 -14
- package/dist/src/catalog/index.js +99 -59
- package/dist/src/catalog/lock.js +113 -0
- package/dist/src/catalog/render.js +10 -4
- package/dist/src/catalog/store.js +76 -193
- package/dist/src/catalog/types.js +12 -0
- package/dist/src/config/args.js +30 -0
- package/dist/src/config/index.js +0 -175
- package/dist/src/config/load.js +169 -0
- package/dist/src/config/schema.js +351 -0
- package/dist/src/config/secrets.js +128 -0
- package/dist/src/config/ssl.js +25 -0
- package/dist/src/db/index.js +379 -379
- package/dist/src/db/permissions.js +16 -32
- package/dist/src/db/registry.js +178 -0
- package/dist/src/db/utils.js +1 -3
- package/dist/src/init/check.js +31 -0
- package/dist/src/init/cli.js +26 -0
- package/dist/src/init/config-edit.js +95 -0
- package/dist/src/init/prompt.js +82 -0
- package/dist/src/init/wizard.js +223 -0
- package/dist/src/server/catalog-tool.js +91 -0
- package/dist/src/server/connections.js +117 -0
- package/dist/src/ssh/config.js +27 -34
- package/dist/src/ssh/tunnel.js +155 -238
- package/dist/src/utils/index.js +35 -4
- package/package.json +4 -4
package/dist/src/ssh/tunnel.js
CHANGED
|
@@ -1,102 +1,12 @@
|
|
|
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, SSH_CONFIG_HOST, SSH_ENABLED, SSH_HOST, SSH_LOCAL_PORT, SSH_PASSPHRASE, SSH_PORT, SSH_PRIVATE_KEY_PATH, SSH_REUSE_EXISTING, SSH_USER, } from '../config/index.js';
|
|
5
4
|
import { log } from '../utils/index.js';
|
|
6
5
|
import { expandHome, readSSHConfigHost } from './config.js';
|
|
7
6
|
import { DEFAULT_USER_KNOWN_HOSTS_FILES, hostKeyAlgorithms, isKnownHostKey, knownHostsName, lookupKnownHosts, } from './known-hosts.js';
|
|
8
|
-
class LocalPortTakenError extends Error {
|
|
9
|
-
}
|
|
10
7
|
const RECONNECT_MAX_ATTEMPTS = 3;
|
|
11
8
|
const RECONNECT_BASE_DELAY_MS = 1000;
|
|
12
|
-
const PROBE_TIMEOUT_MS = 750;
|
|
13
9
|
const SSH_READY_TIMEOUT_MS = 20000;
|
|
14
|
-
let resolvedTunnelConfig = null;
|
|
15
|
-
export function resolveTunnelConfig() {
|
|
16
|
-
if (resolvedTunnelConfig) {
|
|
17
|
-
return resolvedTunnelConfig;
|
|
18
|
-
}
|
|
19
|
-
const configHost = SSH_CONFIG_HOST;
|
|
20
|
-
let fromConfig = null;
|
|
21
|
-
if (configHost) {
|
|
22
|
-
fromConfig = readSSHConfigHost(configHost);
|
|
23
|
-
if (!fromConfig) {
|
|
24
|
-
throw new Error(`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(', ')}.`);
|
|
54
|
-
}
|
|
55
|
-
if (!fs.existsSync(privateKeyPath)) {
|
|
56
|
-
throw new Error(`SSH private key not found at "${privateKeyPath}". ` +
|
|
57
|
-
`Set MYSQL_SSH_PRIVATE_KEY_PATH to the key that authenticates ${sshUser}@${sshHost}.`);
|
|
58
|
-
}
|
|
59
|
-
resolvedTunnelConfig = {
|
|
60
|
-
sshHost: sshHost,
|
|
61
|
-
sshPort,
|
|
62
|
-
sshUser: sshUser,
|
|
63
|
-
privateKeyPath,
|
|
64
|
-
passphrase: SSH_PASSPHRASE,
|
|
65
|
-
knownHostsFiles: fromConfig?.userKnownHostsFiles ?? DEFAULT_USER_KNOWN_HOSTS_FILES,
|
|
66
|
-
hostKeyName: knownHostsName(sshHost, sshPort, fromConfig?.hostKeyAlias),
|
|
67
|
-
sshTarget: configHost ?? `${sshPort === 22 ? '' : `-p ${sshPort} `}${sshUser}@${sshHost}`,
|
|
68
|
-
localPort,
|
|
69
|
-
remoteHost: remoteHost,
|
|
70
|
-
remotePort,
|
|
71
|
-
};
|
|
72
|
-
return resolvedTunnelConfig;
|
|
73
|
-
}
|
|
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
|
-
let endpointPromise = null;
|
|
94
|
-
let localServer = null;
|
|
95
|
-
let sshClient = null;
|
|
96
|
-
let activeConfig = null;
|
|
97
|
-
let activeEndpoint = null;
|
|
98
|
-
let shuttingDown = false;
|
|
99
|
-
let fatalError = null;
|
|
100
10
|
function connectSSH(cfg) {
|
|
101
11
|
return new Promise((resolve, reject) => {
|
|
102
12
|
const known = lookupKnownHosts(cfg.hostKeyName, cfg.knownHostsFiles);
|
|
@@ -168,54 +78,6 @@ function connectSSH(cfg) {
|
|
|
168
78
|
client.connect(connectConfig);
|
|
169
79
|
});
|
|
170
80
|
}
|
|
171
|
-
async function reconnect(cfg) {
|
|
172
|
-
for (let attempt = 1; attempt <= RECONNECT_MAX_ATTEMPTS; attempt++) {
|
|
173
|
-
if (shuttingDown) {
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
const delay = RECONNECT_BASE_DELAY_MS * 2 ** (attempt - 1);
|
|
177
|
-
log('error', `[ssh] tunnel dropped; reconnect attempt ${attempt}/${RECONNECT_MAX_ATTEMPTS} in ${delay}ms`);
|
|
178
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
179
|
-
if (shuttingDown) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
try {
|
|
183
|
-
const client = await connectSSH(cfg);
|
|
184
|
-
sshClient = client;
|
|
185
|
-
fatalError = null;
|
|
186
|
-
attachDropHandler(client, cfg);
|
|
187
|
-
log('info', `[ssh] tunnel reconnected on attempt ${attempt}`);
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
catch (err) {
|
|
191
|
-
log('error', `[ssh] reconnect attempt ${attempt} failed: ${err.message}`);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
fatalError = new Error(`SSH tunnel to ${cfg.sshUser}@${cfg.sshHost}:${cfg.sshPort} could not be re-established after ${RECONNECT_MAX_ATTEMPTS} attempts. ` +
|
|
195
|
-
`Restart the MCP server once the bastion is reachable again.`);
|
|
196
|
-
log('error', `[ssh] ${fatalError.message}`);
|
|
197
|
-
}
|
|
198
|
-
let reconnecting = false;
|
|
199
|
-
function attachDropHandler(client, cfg) {
|
|
200
|
-
const onDrop = () => {
|
|
201
|
-
if (shuttingDown || reconnecting) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
if (sshClient !== client) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
sshClient = null;
|
|
208
|
-
reconnecting = true;
|
|
209
|
-
void reconnect(cfg).finally(() => {
|
|
210
|
-
reconnecting = false;
|
|
211
|
-
});
|
|
212
|
-
};
|
|
213
|
-
client.once('close', onDrop);
|
|
214
|
-
client.once('error', (err) => {
|
|
215
|
-
log('error', `[ssh] transport error: ${err.message}`);
|
|
216
|
-
onDrop();
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
81
|
export function forwardSocket(socket, client, cfg, unavailable = null) {
|
|
220
82
|
socket.on('error', (err) => {
|
|
221
83
|
log('error', `[ssh] local tunnel socket closed with error: ${err.message}`);
|
|
@@ -241,117 +103,172 @@ export function forwardSocket(socket, client, cfg, unavailable = null) {
|
|
|
241
103
|
stream.once('close', () => socket.destroy());
|
|
242
104
|
});
|
|
243
105
|
}
|
|
244
|
-
function
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
106
|
+
export function resolveBastion(bastion, options = {}) {
|
|
107
|
+
const where = `bastions.${bastion.name}`;
|
|
108
|
+
let alias = null;
|
|
109
|
+
if (bastion.sshConfigHost) {
|
|
110
|
+
alias = readSSHConfigHost(bastion.sshConfigHost, options.sshConfigPath);
|
|
111
|
+
if (!alias) {
|
|
112
|
+
throw new Error(`${where}.sshConfigHost: "${bastion.sshConfigHost}" was not found as a Host alias in ` +
|
|
113
|
+
`${options.sshConfigPath ?? '~/.ssh/config'}. Add the alias or set "host" and "user" under ${where}.`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const sshHost = bastion.host ?? alias?.hostName;
|
|
117
|
+
const sshUser = bastion.user ?? alias?.user;
|
|
118
|
+
const sshPort = bastion.port ?? alias?.port ?? 22;
|
|
119
|
+
const missing = [];
|
|
120
|
+
if (!sshHost) {
|
|
121
|
+
missing.push('"host" (or a HostName line in the alias)');
|
|
122
|
+
}
|
|
123
|
+
if (!sshUser) {
|
|
124
|
+
missing.push('"user" (or a User line in the alias)');
|
|
125
|
+
}
|
|
126
|
+
if (missing.length > 0) {
|
|
127
|
+
throw new Error(`${where}: SSH target is underspecified. Missing: ${missing.join(', ')}.`);
|
|
128
|
+
}
|
|
129
|
+
const privateKeyPath = expandHome(bastion.privateKeyPath ?? alias?.identityFile ?? '~/.ssh/id_rsa');
|
|
130
|
+
if (!fs.existsSync(privateKeyPath)) {
|
|
131
|
+
throw new Error(`${where}: SSH private key not found at "${privateKeyPath}". ` +
|
|
132
|
+
`Set "privateKeyPath" to the key that authenticates ${sshUser}@${sshHost}.`);
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
sshHost: sshHost,
|
|
136
|
+
sshPort,
|
|
137
|
+
sshUser: sshUser,
|
|
138
|
+
privateKeyPath,
|
|
139
|
+
knownHostsFiles: alias?.userKnownHostsFiles ?? DEFAULT_USER_KNOWN_HOSTS_FILES,
|
|
140
|
+
hostKeyName: knownHostsName(sshHost, sshPort, alias?.hostKeyAlias),
|
|
141
|
+
sshTarget: bastion.sshConfigHost ?? `${sshPort === 22 ? '' : `-p ${sshPort} `}${sshUser}@${sshHost}`,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
export class SshSession {
|
|
145
|
+
config;
|
|
146
|
+
connect;
|
|
147
|
+
client = null;
|
|
148
|
+
startPromise = null;
|
|
149
|
+
listeners = new Map();
|
|
150
|
+
reconnecting = false;
|
|
151
|
+
closed = false;
|
|
152
|
+
fatalError = null;
|
|
153
|
+
constructor(config, connect = connectSSH) {
|
|
154
|
+
this.config = config;
|
|
155
|
+
this.connect = connect;
|
|
156
|
+
}
|
|
157
|
+
openListener(remoteHost, remotePort) {
|
|
158
|
+
const key = `${remoteHost}:${remotePort}`;
|
|
159
|
+
let pending = this.listeners.get(key);
|
|
160
|
+
if (!pending) {
|
|
161
|
+
pending = this.start().then(() => this.listen(remoteHost, remotePort));
|
|
162
|
+
this.listeners.set(key, pending);
|
|
163
|
+
pending.catch(() => {
|
|
164
|
+
if (this.listeners.get(key) === pending) {
|
|
165
|
+
this.listeners.delete(key);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return pending.then(({ endpoint }) => endpoint);
|
|
170
|
+
}
|
|
171
|
+
start() {
|
|
172
|
+
if (!this.startPromise) {
|
|
173
|
+
const cfg = this.config;
|
|
174
|
+
log('info', `[ssh] opening ${cfg.sshUser}@${cfg.sshHost}:${cfg.sshPort}`);
|
|
175
|
+
this.startPromise = this.connect(cfg).then((client) => {
|
|
176
|
+
if (this.closed) {
|
|
177
|
+
client.end();
|
|
178
|
+
throw new Error('SSH session was closed while connecting.');
|
|
179
|
+
}
|
|
180
|
+
this.client = client;
|
|
181
|
+
this.attachDropHandler(client);
|
|
182
|
+
});
|
|
183
|
+
this.startPromise.catch(() => {
|
|
184
|
+
this.startPromise = null;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return this.startPromise;
|
|
188
|
+
}
|
|
189
|
+
listen(remoteHost, remotePort) {
|
|
190
|
+
return new Promise((resolve, reject) => {
|
|
191
|
+
const server = net.createServer((socket) => forwardSocket(socket, this.client, { remoteHost, remotePort }, this.fatalError));
|
|
192
|
+
server.once('error', (err) => reject(new Error(`Failed to open local tunnel listener: ${err.message}`)));
|
|
193
|
+
server.listen(0, '127.0.0.1', () => {
|
|
194
|
+
const address = server.address();
|
|
195
|
+
if (!address || typeof address === 'string') {
|
|
196
|
+
server.close();
|
|
197
|
+
reject(new Error('Local tunnel listener reported no TCP address.'));
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
log('info', `[ssh] 127.0.0.1:${address.port} -> ${remoteHost}:${remotePort} via ${this.config.sshTarget}`);
|
|
201
|
+
resolve({ endpoint: { host: '127.0.0.1', port: address.port }, server });
|
|
202
|
+
});
|
|
253
203
|
});
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
204
|
+
}
|
|
205
|
+
attachDropHandler(client) {
|
|
206
|
+
const onDrop = () => {
|
|
207
|
+
if (this.closed || this.reconnecting || this.client !== client) {
|
|
258
208
|
return;
|
|
259
209
|
}
|
|
260
|
-
|
|
261
|
-
|
|
210
|
+
this.client = null;
|
|
211
|
+
this.reconnecting = true;
|
|
212
|
+
void this.reconnect().finally(() => {
|
|
213
|
+
this.reconnecting = false;
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
client.once('close', onDrop);
|
|
217
|
+
client.once('error', (err) => {
|
|
218
|
+
log('error', `[ssh] transport error on ${this.config.sshTarget}: ${err.message}`);
|
|
219
|
+
onDrop();
|
|
262
220
|
});
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
export function ensureTunnel() {
|
|
266
|
-
if (!SSH_ENABLED) {
|
|
267
|
-
return Promise.resolve(null);
|
|
268
221
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
return
|
|
222
|
+
async reconnect() {
|
|
223
|
+
const cfg = this.config;
|
|
224
|
+
for (let attempt = 1; attempt <= RECONNECT_MAX_ATTEMPTS; attempt++) {
|
|
225
|
+
const delay = RECONNECT_BASE_DELAY_MS * 2 ** (attempt - 1);
|
|
226
|
+
log('error', `[ssh] ${cfg.sshTarget} dropped; reconnect attempt ${attempt}/${RECONNECT_MAX_ATTEMPTS} in ${delay}ms`);
|
|
227
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
228
|
+
if (this.closed) {
|
|
229
|
+
return;
|
|
277
230
|
}
|
|
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})`);
|
|
280
|
-
const client = await connectSSH(cfg);
|
|
281
|
-
sshClient = client;
|
|
282
|
-
let port;
|
|
283
231
|
try {
|
|
284
|
-
|
|
232
|
+
const client = await this.connect(cfg);
|
|
233
|
+
if (this.closed) {
|
|
234
|
+
client.end();
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
this.client = client;
|
|
238
|
+
this.fatalError = null;
|
|
239
|
+
this.attachDropHandler(client);
|
|
240
|
+
log('info', `[ssh] ${cfg.sshTarget} reconnected on attempt ${attempt}`);
|
|
241
|
+
return;
|
|
285
242
|
}
|
|
286
243
|
catch (err) {
|
|
287
|
-
|
|
288
|
-
client.destroy();
|
|
289
|
-
sshClient = null;
|
|
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
|
-
}
|
|
244
|
+
log('error', `[ssh] reconnect attempt ${attempt} failed: ${err.message}`);
|
|
301
245
|
}
|
|
302
|
-
attachDropHandler(client, cfg);
|
|
303
|
-
log('info', `[ssh] tunnel listening on 127.0.0.1:${port}`);
|
|
304
|
-
activeEndpoint = { host: '127.0.0.1', port, reused: false };
|
|
305
|
-
return activeEndpoint;
|
|
306
|
-
})();
|
|
307
|
-
endpointPromise.catch(() => {
|
|
308
|
-
endpointPromise = null;
|
|
309
|
-
activeConfig = null;
|
|
310
|
-
activeEndpoint = null;
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
return endpointPromise;
|
|
314
|
-
}
|
|
315
|
-
export function getTunnelEndpoint() {
|
|
316
|
-
return activeEndpoint;
|
|
317
|
-
}
|
|
318
|
-
export function getTunnelFatalError() {
|
|
319
|
-
return fatalError;
|
|
320
|
-
}
|
|
321
|
-
export function describeTunnel() {
|
|
322
|
-
if (!SSH_ENABLED) {
|
|
323
|
-
return null;
|
|
324
|
-
}
|
|
325
|
-
if (!activeConfig) {
|
|
326
|
-
return 'ssh tunnel: enabled (not yet established)';
|
|
327
|
-
}
|
|
328
|
-
const { sshUser, sshHost, remoteHost, remotePort } = activeConfig;
|
|
329
|
-
return `ssh tunnel: ${sshUser}@${sshHost} -> ${remoteHost}:${remotePort}`;
|
|
330
|
-
}
|
|
331
|
-
export async function stopTunnel() {
|
|
332
|
-
shuttingDown = true;
|
|
333
|
-
const server = localServer;
|
|
334
|
-
localServer = null;
|
|
335
|
-
if (server) {
|
|
336
|
-
await new Promise((resolve) => {
|
|
337
|
-
server.close(() => resolve());
|
|
338
|
-
server.unref();
|
|
339
|
-
setTimeout(resolve, 1000);
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
const client = sshClient;
|
|
343
|
-
sshClient = null;
|
|
344
|
-
if (client) {
|
|
345
|
-
try {
|
|
346
|
-
client.end();
|
|
347
|
-
client.destroy();
|
|
348
246
|
}
|
|
349
|
-
|
|
350
|
-
|
|
247
|
+
this.fatalError = new Error(`SSH tunnel to ${cfg.sshUser}@${cfg.sshHost}:${cfg.sshPort} could not be re-established after ` +
|
|
248
|
+
`${RECONNECT_MAX_ATTEMPTS} attempts. Restart the MCP server once the bastion is reachable again.`);
|
|
249
|
+
log('error', `[ssh] ${this.fatalError.message}`);
|
|
250
|
+
}
|
|
251
|
+
async close() {
|
|
252
|
+
this.closed = true;
|
|
253
|
+
const opened = await Promise.allSettled(this.listeners.values());
|
|
254
|
+
this.listeners.clear();
|
|
255
|
+
await Promise.all(opened.map((result) => result.status === 'fulfilled'
|
|
256
|
+
? new Promise((resolve) => {
|
|
257
|
+
result.value.server.close(() => resolve());
|
|
258
|
+
result.value.server.unref();
|
|
259
|
+
setTimeout(resolve, 1000).unref();
|
|
260
|
+
})
|
|
261
|
+
: undefined));
|
|
262
|
+
const client = this.client;
|
|
263
|
+
this.client = null;
|
|
264
|
+
if (client) {
|
|
265
|
+
try {
|
|
266
|
+
client.end();
|
|
267
|
+
client.destroy();
|
|
268
|
+
}
|
|
269
|
+
catch (err) {
|
|
270
|
+
log('error', `[ssh] error closing SSH client: ${err.message}`);
|
|
271
|
+
}
|
|
351
272
|
}
|
|
352
273
|
}
|
|
353
|
-
endpointPromise = null;
|
|
354
|
-
activeConfig = null;
|
|
355
|
-
activeEndpoint = null;
|
|
356
|
-
shuttingDown = false;
|
|
357
274
|
}
|
package/dist/src/utils/index.js
CHANGED
|
@@ -1,13 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export function tableResourceUri(connection, schema, table) {
|
|
2
|
+
return `mysql://${connection}/tables/${encodeURIComponent(schema)}/${encodeURIComponent(table)}`;
|
|
3
|
+
}
|
|
4
|
+
export function parseResourceUri(uri) {
|
|
5
|
+
const match = /^mysql:\/\/([^/]+)\/(.*)$/.exec(uri);
|
|
6
|
+
if (!match) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const [, connection, rest] = match;
|
|
10
|
+
if (rest === 'tables') {
|
|
11
|
+
return { connection, kind: 'tables' };
|
|
12
|
+
}
|
|
13
|
+
if (rest === 'schemas') {
|
|
14
|
+
return { connection, kind: 'schemas' };
|
|
15
|
+
}
|
|
16
|
+
const parts = rest.split('/');
|
|
17
|
+
if (parts.length !== 3 || parts[0] !== 'tables' || !parts[1] || !parts[2]) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
return {
|
|
22
|
+
connection,
|
|
23
|
+
kind: 'table',
|
|
24
|
+
schema: decodeURIComponent(parts[1]),
|
|
25
|
+
table: decodeURIComponent(parts[2]),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
4
31
|
}
|
|
5
32
|
export function sharedOnce(fn) {
|
|
6
33
|
let running;
|
|
7
34
|
return (...args) => (running ??= fn(...args));
|
|
8
35
|
}
|
|
36
|
+
let loggingEnabled = false;
|
|
37
|
+
export function setLogging(enabled) {
|
|
38
|
+
loggingEnabled = enabled;
|
|
39
|
+
}
|
|
9
40
|
export function log(type = 'info', ...args) {
|
|
10
|
-
if (!
|
|
41
|
+
if (!loggingEnabled) {
|
|
11
42
|
return;
|
|
12
43
|
}
|
|
13
44
|
const prefix = type === 'error' ? '[error]' : '[info]';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecubelabs/mysql-mcp",
|
|
3
|
-
"version": "1.0.0-next.
|
|
4
|
-
"description": "Read-only MySQL MCP server that reaches its
|
|
3
|
+
"version": "1.0.0-next.7",
|
|
4
|
+
"description": "Read-only MySQL MCP server that reaches its databases through an SSH bastion, one process per environment with many connections",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"url": "https://github.com/Ecube-Labs/skynet.git",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"watch": "tsc --watch",
|
|
29
29
|
"test": "tsc -p tsconfig.test.json && vitest run",
|
|
30
30
|
"test:watch": "vitest",
|
|
31
|
-
"start:stage": "
|
|
32
|
-
"start:prod": "
|
|
31
|
+
"start:stage": "node dist/index.js --env stage",
|
|
32
|
+
"start:prod": "node dist/index.js --env prod"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@modelcontextprotocol/sdk": "^1.31.0",
|