@jwjp/ftp-mcp 0.7.0 → 0.7.2
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/package.json +1 -1
- package/src/transports.js +35 -5
- package/src/workspace.js +17 -0
package/package.json
CHANGED
package/src/transports.js
CHANGED
|
@@ -13,6 +13,31 @@ import { UserError } from './paths.js';
|
|
|
13
13
|
// before it is closed and replaced.
|
|
14
14
|
export const PING_TIMEOUT_MS = 5_000;
|
|
15
15
|
|
|
16
|
+
// Node.js TLS verification failures, such as DEPTH_ZERO_SELF_SIGNED_CERT,
|
|
17
|
+
// CERT_HAS_EXPIRED, or ERR_TLS_CERT_ALTNAME_INVALID.
|
|
18
|
+
const TLS_TRUST_ERROR = /CERT|SELF_SIGNED|UNABLE_TO_(GET|VERIFY)/u;
|
|
19
|
+
|
|
20
|
+
// Connection failures the user can fix get a message that says how. Other
|
|
21
|
+
// errors keep their generic handling, which shows only a code.
|
|
22
|
+
function ftpAccessError(error) {
|
|
23
|
+
if (error?.code === 530) return new UserError('FTP login failed (530). Check REMOTE_USER and REMOTE_PASSWORD.', { cause: error });
|
|
24
|
+
if (typeof error?.code === 'string' && TLS_TRUST_ERROR.test(error.code)) {
|
|
25
|
+
return new UserError(`The FTPS server's certificate could not be verified (${error.code}). If it is signed by a private CA, point NODE_EXTRA_CA_CERTS at that CA, or run Node.js with --use-system-ca. Certificate checks cannot be turned off.`, { cause: error });
|
|
26
|
+
}
|
|
27
|
+
return error;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function sftpAccessError(error) {
|
|
31
|
+
const message = String(error?.message ?? '');
|
|
32
|
+
if (message.includes('Host denied')) {
|
|
33
|
+
return new UserError("The SFTP server's host key does not match SFTP_HOST_KEY_SHA256. Confirm the fingerprint through your hosting provider before changing it; a mismatch can mean the connection is being intercepted.", { cause: error });
|
|
34
|
+
}
|
|
35
|
+
if (error?.level === 'client-authentication' || message.includes('authentication methods failed')) {
|
|
36
|
+
return new UserError('SFTP login failed. Check REMOTE_USER and REMOTE_PASSWORD, or SFTP_PRIVATE_KEY_FILE and SFTP_PASSPHRASE.', { cause: error });
|
|
37
|
+
}
|
|
38
|
+
return error;
|
|
39
|
+
}
|
|
40
|
+
|
|
16
41
|
export class FtpTransport {
|
|
17
42
|
constructor(config, client = new FtpClient(30_000)) {
|
|
18
43
|
this.config = config;
|
|
@@ -20,10 +45,14 @@ export class FtpTransport {
|
|
|
20
45
|
}
|
|
21
46
|
async access() {
|
|
22
47
|
const { host, port, user, password, secure } = this.config;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
48
|
+
try {
|
|
49
|
+
await this.client.access({
|
|
50
|
+
host, port, user, password, secure,
|
|
51
|
+
secureOptions: secure ? { rejectUnauthorized: true, ...(isIP(host) ? {} : { servername: host }) } : undefined
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw ftpAccessError(error);
|
|
55
|
+
}
|
|
27
56
|
}
|
|
28
57
|
async cd(root) { await this.client.cd(root); }
|
|
29
58
|
async ping() {
|
|
@@ -146,7 +175,8 @@ export class SftpTransport {
|
|
|
146
175
|
config.privateKey = await fs.readFile(this.config.sftpPrivateKeyFile);
|
|
147
176
|
if (this.config.sftpPassphrase) config.passphrase = this.config.sftpPassphrase;
|
|
148
177
|
}
|
|
149
|
-
await this.#run('connection', () => this.client.connect(config));
|
|
178
|
+
try { await this.#run('connection', () => this.client.connect(config)); }
|
|
179
|
+
catch (error) { throw sftpAccessError(error); }
|
|
150
180
|
}
|
|
151
181
|
async cd(root) {
|
|
152
182
|
if (await this.#run('root check', () => this.client.exists(root)) !== 'd') throw new UserError('Remote root is not a directory.');
|
package/src/workspace.js
CHANGED
|
@@ -30,6 +30,10 @@ export class FtpWorkspace {
|
|
|
30
30
|
#clientQueue = Promise.resolve();
|
|
31
31
|
#idle = null;
|
|
32
32
|
#closed = false;
|
|
33
|
+
// Parent directories already checked during the current operation, per
|
|
34
|
+
// connection. Checking each once saves a LIST per level on every lookup;
|
|
35
|
+
// a server-side change between checks was never excluded anyway.
|
|
36
|
+
#verified = new WeakMap();
|
|
33
37
|
|
|
34
38
|
constructor(config, clientFactory = () => createTransport(config)) {
|
|
35
39
|
this.config = config;
|
|
@@ -56,15 +60,18 @@ export class FtpWorkspace {
|
|
|
56
60
|
|
|
57
61
|
async #useClient(action) {
|
|
58
62
|
const client = await this.#acquire();
|
|
63
|
+
this.#verified.set(client, new Set());
|
|
59
64
|
let result;
|
|
60
65
|
try {
|
|
61
66
|
result = await action(client);
|
|
62
67
|
} catch (error) {
|
|
68
|
+
this.#verified.delete(client);
|
|
63
69
|
// A failed operation can leave a transfer half done or the connection
|
|
64
70
|
// in an unknown state. Never hand that connection to the next caller.
|
|
65
71
|
await closeQuietly(client);
|
|
66
72
|
throw error;
|
|
67
73
|
}
|
|
74
|
+
this.#verified.delete(client);
|
|
68
75
|
await this.#release(client);
|
|
69
76
|
return result;
|
|
70
77
|
}
|
|
@@ -121,7 +128,10 @@ export class FtpWorkspace {
|
|
|
121
128
|
|
|
122
129
|
async entry(client, relative) {
|
|
123
130
|
const parts = relative.split('/');
|
|
131
|
+
const verified = this.#verified.get(client);
|
|
124
132
|
for (let i = 0; i < parts.length; i++) {
|
|
133
|
+
const directory = parts.slice(0, i + 1).join('/');
|
|
134
|
+
if (i < parts.length - 1 && verified?.has(directory)) continue;
|
|
125
135
|
const parent = parts.slice(0, i).join('/') || '.';
|
|
126
136
|
const entries = await client.list(parent);
|
|
127
137
|
const found = entries.find(item => item.name === parts[i]);
|
|
@@ -132,6 +142,7 @@ export class FtpWorkspace {
|
|
|
132
142
|
if (found.type === FileType.SymbolicLink) throw new UserError('Remote symlink paths are not allowed.');
|
|
133
143
|
if (i < parts.length - 1 && found.type !== FileType.Directory) throw new UserError('Remote parent is not a directory.');
|
|
134
144
|
if (i === parts.length - 1) return found;
|
|
145
|
+
verified?.add(directory);
|
|
135
146
|
}
|
|
136
147
|
}
|
|
137
148
|
|
|
@@ -139,12 +150,16 @@ export class FtpWorkspace {
|
|
|
139
150
|
// An existing parent must be a real directory.
|
|
140
151
|
async #missingDirectories(client, relative) {
|
|
141
152
|
const parts = relative.split('/').slice(0, -1);
|
|
153
|
+
const verified = this.#verified.get(client);
|
|
142
154
|
for (let i = 0; i < parts.length; i++) {
|
|
155
|
+
const directory = parts.slice(0, i + 1).join('/');
|
|
156
|
+
if (verified?.has(directory)) continue;
|
|
143
157
|
const parent = parts.slice(0, i).join('/') || '.';
|
|
144
158
|
const found = (await client.list(parent)).find(item => item.name === parts[i]);
|
|
145
159
|
if (!found) return parts.slice(i).map((_, j) => parts.slice(0, i + j + 1).join('/'));
|
|
146
160
|
if (found.type === FileType.SymbolicLink) throw new UserError('Remote symlink paths are not allowed.');
|
|
147
161
|
if (found.type !== FileType.Directory) throw new UserError('Remote parent is not a directory.');
|
|
162
|
+
verified?.add(directory);
|
|
148
163
|
}
|
|
149
164
|
return [];
|
|
150
165
|
}
|
|
@@ -374,6 +389,7 @@ export class FtpWorkspace {
|
|
|
374
389
|
for (const directory of missing) {
|
|
375
390
|
await client.mkdir(directory);
|
|
376
391
|
created.push(directory);
|
|
392
|
+
this.#verified.get(client)?.add(directory);
|
|
377
393
|
}
|
|
378
394
|
} catch (error) {
|
|
379
395
|
throw await this.#removeCreated(client, created, error);
|
|
@@ -416,6 +432,7 @@ export class FtpWorkspace {
|
|
|
416
432
|
// rmdir remove only an empty directory, so another writer's files are kept.
|
|
417
433
|
async #removeCreated(client, created, error) {
|
|
418
434
|
for (const directory of created.toReversed()) {
|
|
435
|
+
this.#verified.get(client)?.delete(directory);
|
|
419
436
|
try { await client.rmdir(directory); }
|
|
420
437
|
catch {
|
|
421
438
|
const reason = error instanceof UserError ? error.message : 'Upload failed.';
|