@jwjp/ftp-mcp 0.7.1 → 0.8.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # FTP Workspace MCP
2
2
 
3
- A local stdio MCP server for AI coding work on projects reachable through **FTP or SFTP**. Explicit and implicit FTPS are also supported. It works with any project language or framework and exposes four file-level tools: `remote_list`, `remote_download`, `remote_preview_upload`, and `remote_upload`. Two more, `remote_preview_delete` and `remote_delete`, appear only when `REMOTE_ALLOW_DELETE=true`.
3
+ A local stdio MCP server for AI coding work on projects reachable through **FTP or SFTP**. Explicit and implicit FTPS are also supported. It works with any project language or framework and exposes five file-level tools: `remote_list`, `remote_compare`, `remote_download`, `remote_preview_upload`, and `remote_upload`. Two more, `remote_preview_delete` and `remote_delete`, appear only when `REMOTE_ALLOW_DELETE=true`.
4
4
 
5
5
  The server has no recursive sync, directory deletion, or shell execution tool. Deleting single files is off by default. In version 0.2, the tool names changed from `ftp_*` to `remote_*` to reflect SFTP support.
6
6
 
@@ -128,6 +128,18 @@ Upload tokens are single-use and held only in the MCP process. Restarting it req
128
128
 
129
129
  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 replacement keeps the permission bits of the file it replaces: the server copies them to the temporary file before the rename. SFTP reads and sets them directly. FTP reads them from a Unix-style listing and sets them with `SITE CHMOD`; a server that lists no permissions, such as most Windows servers, is left alone. If the server refuses the change, the upload still completes and its result carries a warning with the previous mode. Ownership is not preserved. An interruption can leave a temporary or backup file. Use a separate deployment or backup process when interruption cannot be tolerated.
130
130
 
131
+ ### Comparing a directory
132
+
133
+ Call `remote_compare({"path":"src"})`. It compares one local directory with its remote counterpart, one level deep, and gives each entry a status:
134
+
135
+ - `localOnly` or `remoteOnly`
136
+ - `typeDiffers`: a file on one side, a directory on the other
137
+ - `both`: a directory on both sides
138
+ - `sizeDiffers`, or `sizeUnknown` when the listing gives no size
139
+ - `sameSize`
140
+
141
+ With `"hash":true`, up to 20 same-size files per page are read on both sides and marked `same` or `differs`. `hashLimited` says some were not checked. Pages and `cursor` work as in `remote_list`. It changes nothing.
142
+
131
143
  ### Deleting a remote file
132
144
 
133
145
  With `REMOTE_ALLOW_DELETE=true`, the server also offers:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jwjp/ftp-mcp",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "description": "A guarded FTP/SFTP MCP server for local AI coding workflows",
5
5
  "mcpName": "io.github.jwjp/ftp-mcp",
6
6
  "repository": {
package/src/server.js CHANGED
@@ -5,7 +5,7 @@ import { existsSync, readFileSync, realpathSync } from 'node:fs';
5
5
  import * as z from 'zod/v4';
6
6
  import { loadConfig } from './config.js';
7
7
  import { UserError } from './paths.js';
8
- import { FtpWorkspace, LIST_LIMIT } from './workspace.js';
8
+ import { COMPARE_HASH_LIMIT, FtpWorkspace, LIST_LIMIT } from './workspace.js';
9
9
 
10
10
  const { version } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
11
11
  const relativeInput = z.string().max(4096);
@@ -28,6 +28,21 @@ const listOutput = z.object({
28
28
  truncated: z.boolean(),
29
29
  nextCursor: z.string().nullable()
30
30
  });
31
+ const compareOutput = z.object({
32
+ path: z.string(),
33
+ localExists: z.boolean(),
34
+ remoteExists: z.boolean(),
35
+ entries: z.array(z.object({
36
+ name: z.string(),
37
+ type: z.enum(['directory', 'file', 'mixed']),
38
+ status: z.enum(['localOnly', 'remoteOnly', 'typeDiffers', 'both', 'sizeDiffers', 'sizeUnknown', 'sameSize', 'same', 'differs']),
39
+ localBytes: z.number().nullable(),
40
+ remoteBytes: z.number().nullable()
41
+ })),
42
+ truncated: z.boolean(),
43
+ nextCursor: z.string().nullable(),
44
+ hashLimited: z.boolean().optional()
45
+ });
31
46
  const downloadOutput = z.object({
32
47
  path: z.string(),
33
48
  status: z.enum(['downloaded', 'unchanged']),
@@ -92,7 +107,7 @@ function tool(name, workspace, fn) {
92
107
 
93
108
  export function createServer(workspace) {
94
109
  const server = new McpServer({ name: 'ftp-workspace-mcp', version }, {
95
- instructions: 'Use relative paths beneath the configured roots. List or download only needed files. Call remote_preview_upload before remote_upload. '
110
+ instructions: 'Use relative paths beneath the configured roots. List or download only needed files; remote_compare shows which files in a directory differ. Call remote_preview_upload before remote_upload. '
96
111
  + (workspace.config?.allowDelete
97
112
  ? 'Delete a remote file only when the user asked for it: call remote_preview_delete, show the user the path, then call remote_delete. There is no recursive sync or directory deletion.'
98
113
  : 'There are no delete or recursive sync tools.')
@@ -103,6 +118,12 @@ export function createServer(workspace) {
103
118
  outputSchema: listOutput,
104
119
  annotations: { readOnlyHint: true }
105
120
  }, tool('remote_list', workspace, ({ path, cursor }) => workspace.list(path, cursor)));
121
+ server.registerTool('remote_compare', {
122
+ description: `Compare one local directory with its remote counterpart, one level deep, sorted by name, up to ${LIST_LIMIT} entries per page. Status is localOnly, remoteOnly, typeDiffers, both (directories), sizeDiffers, sizeUnknown, or sameSize. With hash true, up to ${COMPARE_HASH_LIMIT} same-size files per page are read and marked same or differs; hashLimited says some were not. Changes nothing.`,
123
+ inputSchema: z.object({ path: relativeInput.default(''), cursor: relativeInput.optional(), hash: z.boolean().default(false) }),
124
+ outputSchema: compareOutput,
125
+ annotations: { readOnlyHint: true }
126
+ }, tool('remote_compare', workspace, ({ path, cursor, hash }) => workspace.compare(path, cursor, hash)));
106
127
  server.registerTool('remote_download', {
107
128
  description: 'Download one remote file into the configured local root. Existing differing files require their current SHA-256 as expectedLocalSha256.',
108
129
  inputSchema: z.object({ path: relativeInput, expectedLocalSha256: sha256.optional() }),
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
- await this.client.access({
24
- host, port, user, password, secure,
25
- secureOptions: secure ? { rejectUnauthorized: true, ...(isIP(host) ? {} : { servername: host }) } : undefined
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
@@ -10,6 +10,8 @@ import { createTransport } from './transports.js';
10
10
  const CHUNK_BYTES = 64 * 1024;
11
11
  const LOCAL_HASH_CHUNK_BYTES = 1024 * 1024;
12
12
  export const LIST_LIMIT = 200;
13
+ // Same-size file pairs hashed per remote_compare page when hash is true.
14
+ export const COMPARE_HASH_LIMIT = 20;
13
15
 
14
16
  // Closing a connection cannot undo a completed transfer. Do not turn a
15
17
  // successful upload into a failure that prompts an unsafe retry.
@@ -185,6 +187,87 @@ export class FtpWorkspace {
185
187
  });
186
188
  }
187
189
 
190
+ // Allowed entries of one local directory, or null if it does not exist.
191
+ async #localEntries(relative) {
192
+ const directory = relative ? await localPath(this.config.localRoot, relative, this.config.extra) : await fs.realpath(this.config.localRoot);
193
+ let dirents;
194
+ try { dirents = await fs.readdir(directory, { withFileTypes: true }); }
195
+ catch (error) {
196
+ if (error.code === 'ENOENT') return null;
197
+ if (error.code === 'ENOTDIR') throw new UserError('Local path is not a directory.');
198
+ throw error;
199
+ }
200
+ const entries = new Map();
201
+ for (const dirent of dirents) {
202
+ // Symlinks and special files are never transferred, so they are not compared.
203
+ if (!dirent.isFile() && !dirent.isDirectory()) continue;
204
+ if (safeExcluded(relative, dirent.name, this.config.extra)) continue;
205
+ const size = dirent.isFile() ? (await fs.lstat(path.join(directory, dirent.name))).size : null;
206
+ entries.set(dirent.name, { type: dirent.isFile() ? 'file' : 'directory', size });
207
+ }
208
+ return entries;
209
+ }
210
+
211
+ // Compare one local directory with its remote counterpart, one level deep.
212
+ // It reads listings, and with hash also the contents of same-size files.
213
+ async compare(relative = '', cursor = '', hash = false) {
214
+ relative = safePath(relative, this.config.extra, { allowRoot: true });
215
+ const local = await this.#localEntries(relative);
216
+ return this.withClient(async client => {
217
+ let remote = null;
218
+ const found = relative ? await this.entry(client, relative) : { type: FileType.Directory };
219
+ if (found && found.type !== FileType.Directory) throw new UserError('Remote path is not a directory.');
220
+ if (found) {
221
+ remote = new Map();
222
+ for (const item of await client.list(relative || '.')) {
223
+ if (item.name.includes('/') || item.type === FileType.SymbolicLink || safeExcluded(relative, item.name, this.config.extra)) continue;
224
+ // A directory's listed size (often 4096) says nothing about its contents.
225
+ const directory = item.type === FileType.Directory;
226
+ remote.set(item.name, { type: directory ? 'directory' : 'file', size: !directory && Number.isFinite(item.size) ? item.size : null });
227
+ }
228
+ }
229
+ const names = [...new Set([...(local?.keys() ?? []), ...(remote?.keys() ?? [])])]
230
+ .filter(name => name > cursor)
231
+ .sort((a, b) => a < b ? -1 : Number(a > b));
232
+ const page = names.slice(0, LIST_LIMIT);
233
+ const truncated = names.length > LIST_LIMIT;
234
+ let hashed = 0;
235
+ let hashLimited = false;
236
+ const entries = [];
237
+ for (const name of page) {
238
+ const mine = local?.get(name);
239
+ const theirs = remote?.get(name);
240
+ const entry = { name, type: (mine ?? theirs).type, status: null, localBytes: mine?.size ?? null, remoteBytes: theirs?.size ?? null };
241
+ if (!theirs) entry.status = 'localOnly';
242
+ else if (!mine) entry.status = 'remoteOnly';
243
+ else if (mine.type !== theirs.type) { entry.type = 'mixed'; entry.status = 'typeDiffers'; }
244
+ else if (mine.type === 'directory') entry.status = 'both';
245
+ else if (theirs.size === null || mine.size !== theirs.size) entry.status = theirs.size === null ? 'sizeUnknown' : 'sizeDiffers';
246
+ else entry.status = 'sameSize';
247
+ if (hash && entry.status === 'sameSize' && mine.size <= this.config.maxBytes) {
248
+ if (hashed < COMPARE_HASH_LIMIT) {
249
+ hashed++;
250
+ const child = relative ? `${relative}/${name}` : name;
251
+ const [localFile, remoteFile] = [await this.localFile(child), await this.readRemote(client, child)];
252
+ entry.status = localFile.sha256 === remoteFile?.sha256 ? 'same' : 'differs';
253
+ } else {
254
+ hashLimited = true;
255
+ }
256
+ }
257
+ entries.push(entry);
258
+ }
259
+ return {
260
+ path: relative,
261
+ localExists: local !== null,
262
+ remoteExists: remote !== null,
263
+ entries,
264
+ truncated,
265
+ nextCursor: truncated ? page.at(-1) : null,
266
+ ...(hash ? { hashLimited } : {})
267
+ };
268
+ });
269
+ }
270
+
188
271
  async #remoteEntry(client, relative) {
189
272
  const entry = await this.entry(client, relative);
190
273
  if (!entry) return null;