@jwjp/ftp-mcp 0.3.0 → 0.3.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 jwjp
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 jwjp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -18,6 +18,8 @@ Install the public `@jwjp/ftp-mcp` package globally. Its executable is `ftp-work
18
18
  npm install -g @jwjp/ftp-mcp
19
19
  ```
20
20
 
21
+ Recent npm versions may report that install scripts for `ssh2` and `cpu-features` were blocked. Those scripts only build optional native speedups; SFTP works without them, so you can leave them blocked.
22
+
21
23
  Or run it from a GitHub checkout:
22
24
 
23
25
  ```sh
@@ -28,7 +30,7 @@ npm ci
28
30
 
29
31
  The MCP examples below use the GitHub checkout. After a global npm install, set the MCP command to `ftp-workspace-mcp` and omit the script path and `node` argument.
30
32
 
31
- Set the following variables in the environment that launches the MCP server. Keep passwords, private keys, and passphrases out of the project, chat, Git, `CLAUDE.md`, and shared MCP configuration. `.env.example` is a reference only; this server does not load `.env` files. An empty value counts as unset, so an unresolved `${NAME}` reference falls back to the former `FTP_*` name or the default instead of overriding it.
33
+ Set the following variables in the environment that launches the MCP server. Keep passwords, private keys, and passphrases out of the project, chat, Git, `CLAUDE.md`, and shared MCP configuration. `.env.example` is a reference only; this server does not load `.env` files. An empty or whitespace-only value counts as unset, so an unresolved `${NAME}` reference falls back to the former `FTP_*` name or the default instead of overriding it.
32
34
 
33
35
  | Variable | Meaning |
34
36
  | --- | --- |
@@ -117,9 +119,9 @@ Replace the script path and set the required variables locally before starting C
117
119
  4. `remote_preview_upload({"path":"src/example.php"})`. Review the path and both hashes. It returns an `uploadToken` when content differs.
118
120
  5. `remote_upload({"path":"src/example.php","uploadToken":"..."})` within ten minutes. A changed local or remote file invalidates the preview.
119
121
 
120
- Upload tokens are single-use and held only in the MCP process. Restarting it requires a fresh preview. Uploads first write and verify a temporary remote file, then rename it into place. For FTP replacement, the old file is moved to a backup name because some servers refuse to rename over an existing file. If the final rename fails, the server checks whether the target is absent before attempting to restore the backup. If it cannot confirm the result, it reports the target and backup paths for inspection. The server also keeps the FTP backup until it verifies the final file and reports the backup location if verification or cleanup fails. For SFTP replacement, the server must support the `posix-rename@openssh.com` extension.
122
+ Upload tokens are single-use and held only in the MCP process. Restarting it requires a fresh preview. Uploads first check the remote listing against the preview, so a file that appeared, disappeared, or changed size stops the upload before any bytes are sent. They then write a temporary remote file, verify it by SHA-256, compare the remote target with the preview by SHA-256 again, and rename the temporary file into place. After the rename, the listing must show a file of the uploaded size. For FTP replacement, the old file is moved to a backup name because some servers refuse to rename over an existing file. If the final rename fails, the server checks whether the target is absent before attempting to restore the backup. If it cannot confirm the result, it reports the target and backup paths for inspection. The server also keeps the FTP backup until it verifies the final file and reports the backup location if verification or cleanup fails. For SFTP replacement, the server must support the `posix-rename@openssh.com` extension.
121
123
 
122
- Each MCP process keeps one remote connection and runs one tool call on it at a time. A connection unused for `REMOTE_IDLE_TIMEOUT_MS` is closed, and an idle connection is checked (FTP `NOOP`, or an SFTP directory query) before reuse, so one the server has dropped is replaced. A failed tool call closes its connection, and the next call reconnects. Each MCP process runs one upload or download at a time. Other MCP processes, editors, and remote writers can still change files between verification and rename. FTP replacement has a brief interval when the target name is absent. A remote replacement can change permissions, and an interruption can leave a temporary or backup file. Use a separate deployment or backup process when interruption cannot be tolerated.
124
+ Each MCP process keeps one remote connection and runs one tool call on it at a time. A connection unused for `REMOTE_IDLE_TIMEOUT_MS` is closed, and an idle connection is checked (FTP `NOOP`, or an SFTP directory query) before reuse, so one the server has dropped is replaced. A check that gets no answer within 5 seconds, as happens when a firewall drops an idle connection silently, also leads to a new connection. A failed tool call closes its connection, and the next call reconnects. Each MCP process runs one upload or download at a time. Other MCP processes, editors, and remote writers can still change files between verification and rename. FTP replacement has a brief interval when the target name is absent. A remote replacement can change permissions, and an interruption can leave a temporary or backup file. Use a separate deployment or backup process when interruption cannot be tolerated.
123
125
 
124
126
  ## Boundaries
125
127
 
@@ -146,4 +148,8 @@ python -m pip install -r integration/requirements.txt
146
148
  python integration/run.py
147
149
  ```
148
150
 
151
+ To test an npm tarball locally, run `npm pack` and pass the printed filename to `python integration/run.py --package <filename>`. The integration suite installs the tarball in a temporary directory and starts the installed `ftp-workspace-mcp` command.
152
+
153
+ To release, bump the version in `package.json` and `package-lock.json` through a pull request, then publish a GitHub release tagged `v<version>` on the merge commit. The `Publish` workflow checks the tag, runs the tests, and verifies transfers through the installed CLI. It publishes the verified tarball through npm trusted publishing.
154
+
149
155
  Test against your own server before relying on a production upload. Contributions are welcome under the MIT license.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jwjp/ftp-mcp",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "A guarded FTP/SFTP MCP server for local AI coding workflows",
5
5
  "repository": {
6
6
  "type": "git",
package/src/config.js CHANGED
@@ -7,9 +7,11 @@ export const DEFAULT_IDLE_TIMEOUT_MS = 30_000;
7
7
 
8
8
  export function loadConfig(env = process.env) {
9
9
  // MCP clients often substitute an unset variable reference such as
10
- // "${MY_REMOTE_HOST}" with an empty string. Treat empty values as unset so
11
- // they fall back to legacy names and defaults instead of overriding them.
12
- env = Object.fromEntries(Object.entries(env).filter(([, value]) => value !== ''));
10
+ // "${MY_REMOTE_HOST}" with an empty string, and a hand-edited value may be
11
+ // only spaces. Treat blank values as unset so they fall back to legacy names
12
+ // and defaults instead of overriding them. Other values keep their spaces,
13
+ // which a password may contain.
14
+ env = Object.fromEntries(Object.entries(env).filter(([, value]) => typeof value !== 'string' || value.trim() !== ''));
13
15
  const get = (name, legacy) => env[name] ?? env[legacy];
14
16
  const legacyProtocol = env.FTP_SECURE === 'false' ? 'ftp' : env.FTP_SECURE === 'implicit' ? 'ftps-implicit' : 'ftps';
15
17
  const protocol = env.REMOTE_PROTOCOL ?? legacyProtocol;
package/src/transports.js CHANGED
@@ -9,6 +9,10 @@ import SftpClient from 'ssh2-sftp-client';
9
9
  import { DEFAULT_SFTP_TIMEOUT_MS } from './config.js';
10
10
  import { UserError } from './paths.js';
11
11
 
12
+ // How long a reused idle connection may take to answer a keepalive check
13
+ // before it is closed and replaced.
14
+ export const PING_TIMEOUT_MS = 5_000;
15
+
12
16
  export class FtpTransport {
13
17
  constructor(config, client = new FtpClient(30_000)) {
14
18
  this.config = config;
@@ -24,7 +28,17 @@ export class FtpTransport {
24
28
  async cd(root) { await this.client.cd(root); }
25
29
  async ping() {
26
30
  if (this.client.closed) throw new Error('FTP connection is closed.');
27
- await this.client.send('NOOP');
31
+ // A connection dropped silently by a firewall or NAT would otherwise wait
32
+ // for basic-ftp's 30 s timeout before the caller gets a new connection.
33
+ let timer;
34
+ const expired = new Promise((_, reject) => {
35
+ timer = setTimeout(() => {
36
+ this.client.close();
37
+ reject(new Error('FTP keepalive check timed out.'));
38
+ }, PING_TIMEOUT_MS);
39
+ });
40
+ try { await Promise.race([this.client.send('NOOP'), expired]); }
41
+ finally { clearTimeout(timer); }
28
42
  }
29
43
  async list(relative) { return this.client.list(relative); }
30
44
  async downloadTo(local, relative) { await this.client.downloadTo(local, relative); }
@@ -80,9 +94,9 @@ export class SftpTransport {
80
94
  // The timeout measures inactivity. A transfer passes `progress` a callback
81
95
  // to call for each chunk, so a large file that keeps moving is not cut off
82
96
  // while a stalled request still is.
83
- async #run(operation, action) {
97
+ async #run(operation, action, limitMs = Infinity) {
84
98
  if (this.#timeoutError) throw this.#timeoutError;
85
- const timeoutMs = this.config.sftpTimeoutMs ?? DEFAULT_SFTP_TIMEOUT_MS;
99
+ const timeoutMs = Math.min(this.config.sftpTimeoutMs ?? DEFAULT_SFTP_TIMEOUT_MS, limitMs);
86
100
  let timer;
87
101
  let settled = false;
88
102
  let expire;
@@ -123,7 +137,8 @@ export class SftpTransport {
123
137
  if (await this.#run('root check', () => this.client.exists(root)) !== 'd') throw new UserError('Remote root is not a directory.');
124
138
  this.root = root;
125
139
  }
126
- async ping() { await this.#run('keepalive check', () => this.client.cwd()); }
140
+ // A short limit lets a silently dropped connection be replaced quickly.
141
+ async ping() { await this.#run('keepalive check', () => this.client.cwd(), PING_TIMEOUT_MS); }
127
142
  remote(relative) {
128
143
  if (!this.root) throw new Error('SFTP root is not set.');
129
144
  return path.posix.join(this.root, relative);
package/src/workspace.js CHANGED
@@ -289,7 +289,7 @@ export class FtpWorkspace {
289
289
  const token = changed ? randomUUID() : null;
290
290
  if (token) {
291
291
  if (this.previews.size >= 100) this.previews.delete(this.previews.keys().next().value);
292
- this.previews.set(token, { relative, localSha256, remoteSha256, expires: Date.now() + 10 * 60_000 });
292
+ this.previews.set(token, { relative, localSha256, remoteSha256, remoteBytes: remote?.bytes ?? null, expires: Date.now() + 10 * 60_000 });
293
293
  }
294
294
  return { path: relative, changed, localSha256, remoteSha256, localBytes: local.bytes, remoteBytes: remote?.bytes ?? null, uploadToken: token };
295
295
  });
@@ -307,11 +307,17 @@ export class FtpWorkspace {
307
307
  const local = await this.localFile(relative);
308
308
  if (local.sha256 !== preview.localSha256) throw new UserError('Local file changed since the preview.');
309
309
  return this.withClient(async client => {
310
- const remote = await this.readRemote(client, relative);
311
- if ((remote?.sha256 ?? null) !== preview.remoteSha256) throw new UserError('Remote file changed since the preview.');
310
+ // A listing catches a remote file that appeared, disappeared, or changed
311
+ // size before any bytes are sent. A same-size change is caught by the
312
+ // full comparison right before the rename.
313
+ const current = await this.#remoteEntry(client, relative);
314
+ if ((current === null) !== (preview.remoteSha256 === null) || (current && Number.isSafeInteger(current.size) && current.size !== preview.remoteBytes)) {
315
+ throw new UserError('Remote file changed since the preview.');
316
+ }
312
317
  const parent = path.posix.dirname(relative);
313
318
  const staged = path.posix.join(parent === '.' ? '' : parent, `.mcp-upload-${randomUUID()}.tmp`);
314
319
  let backup = null;
320
+ let stagedFile;
315
321
  try {
316
322
  const { handle } = await this.#openLocal(relative);
317
323
  if (!handle) throw new UserError('Local file changed during upload.');
@@ -321,7 +327,7 @@ export class FtpWorkspace {
321
327
  source.destroy();
322
328
  await handle.close();
323
329
  }
324
- const stagedFile = await this.readRemote(client, staged);
330
+ stagedFile = await this.readRemote(client, staged);
325
331
  if (stagedFile?.sha256 !== preview.localSha256) throw new UserError('Staged upload verification failed.');
326
332
  const latestLocal = await this.localFile(relative);
327
333
  if (latestLocal.sha256 !== preview.localSha256) throw new UserError('Local file changed during upload.');
@@ -338,16 +344,21 @@ export class FtpWorkspace {
338
344
  throw error;
339
345
  }
340
346
  const recovery = backup ? ` The previous version remains at ${backup}.` : '';
347
+ // The staged bytes were verified by hash, and a rename does not change
348
+ // them. Confirm the rename put a file of that size in place; another
349
+ // full read could not exclude a writer that changes it afterwards.
341
350
  let uploaded;
342
- try { uploaded = await this.readRemote(client, relative); }
351
+ try { uploaded = await this.entry(client, relative); }
343
352
  catch { throw new UserError(`Upload rename completed, but verification failed; inspect the remote file before retrying.${recovery}`); }
344
- if (uploaded?.sha256 !== preview.localSha256) throw new UserError(`Upload verification failed; inspect the remote file before retrying.${recovery}`);
353
+ if (uploaded?.type !== FileType.File || (Number.isSafeInteger(uploaded.size) && uploaded.size !== stagedFile.bytes)) {
354
+ throw new UserError(`Upload verification failed; inspect the remote file before retrying.${recovery}`);
355
+ }
345
356
  let warning;
346
357
  if (backup) {
347
358
  try { await client.remove(backup); }
348
359
  catch { warning = `Upload succeeded, but the previous version remains at ${backup}; remove it manually.`; }
349
360
  }
350
- return { path: relative, status: 'uploaded', ...uploaded, ...(warning ? { warning } : {}) };
361
+ return { path: relative, status: 'uploaded', ...stagedFile, ...(warning ? { warning } : {}) };
351
362
  });
352
363
  }
353
364
  }