@jwjp/ftp-mcp 0.7.2 → 0.8.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 +13 -1
- package/package.json +1 -1
- package/src/server.js +23 -2
- package/src/transports.js +6 -1
- package/src/workspace.js +97 -7
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
|
|
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
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
|
@@ -117,7 +117,12 @@ export class FtpTransport {
|
|
|
117
117
|
async mode(relative) {
|
|
118
118
|
const parent = path.posix.dirname(relative);
|
|
119
119
|
const name = path.posix.basename(relative);
|
|
120
|
-
|
|
120
|
+
return this.modeFromListing((await this.client.list(parent)).find(entry => entry.name === name));
|
|
121
|
+
}
|
|
122
|
+
// The listing is the only source of FTP permissions, so an entry without
|
|
123
|
+
// them means there are none to keep.
|
|
124
|
+
modeFromListing(entry) {
|
|
125
|
+
const permissions = entry?.permissions;
|
|
121
126
|
if (!permissions) return null;
|
|
122
127
|
return (permissions.user << 6) | (permissions.group << 3) | permissions.world;
|
|
123
128
|
}
|
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;
|
|
@@ -445,10 +528,15 @@ export class FtpWorkspace {
|
|
|
445
528
|
// A replacement is a new file, so it would get the server's default
|
|
446
529
|
// permissions. Give the staged file the target's permission bits before the
|
|
447
530
|
// rename, so a private file such as a 0600 configuration stays private.
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
531
|
+
// An FTP listing already carries the permission bits, so the entries read
|
|
532
|
+
// during this upload answer without another LIST. Other transports, and
|
|
533
|
+
// listings without bits, ask the server.
|
|
534
|
+
async #copyMode(client, target, staged) {
|
|
535
|
+
const modeOf = async ({ relative, entry }) => client.modeFromListing?.(entry) ?? client.mode(relative);
|
|
536
|
+
const mode = await modeOf(target);
|
|
537
|
+
const relative = target.relative;
|
|
538
|
+
if (mode === null || mode === await modeOf(staged)) return null;
|
|
539
|
+
try { await client.chmod(staged.relative, mode); }
|
|
452
540
|
catch { return `Upload succeeded, but the server refused to keep the previous permissions (${mode.toString(8).padStart(3, '0')}); check ${relative}.`; }
|
|
453
541
|
return null;
|
|
454
542
|
}
|
|
@@ -472,13 +560,15 @@ export class FtpWorkspace {
|
|
|
472
560
|
source.destroy();
|
|
473
561
|
await handle.close();
|
|
474
562
|
}
|
|
475
|
-
const
|
|
563
|
+
const stagedEntry = await this.#remoteEntry(client, staged);
|
|
564
|
+
const stagedFile = stagedEntry ? await this.#readRemote(client, staged, stagedEntry) : null;
|
|
476
565
|
if (stagedFile?.sha256 !== preview.localSha256) throw new UserError('Staged upload verification failed.');
|
|
477
566
|
const latestLocal = await this.localFile(relative);
|
|
478
567
|
if (latestLocal.sha256 !== preview.localSha256) throw new UserError('Local file changed during upload.');
|
|
479
|
-
const
|
|
568
|
+
const targetEntry = await this.#remoteEntry(client, relative);
|
|
569
|
+
const latestRemote = targetEntry ? await this.#readRemote(client, relative, targetEntry) : null;
|
|
480
570
|
if ((latestRemote?.sha256 ?? null) !== preview.remoteSha256) throw new UserError('Remote file changed during upload.');
|
|
481
|
-
const permissionWarning = latestRemote ? await this.#copyMode(client, relative, staged) : null;
|
|
571
|
+
const permissionWarning = latestRemote ? await this.#copyMode(client, { relative, entry: targetEntry }, { relative: staged, entry: stagedEntry }) : null;
|
|
482
572
|
const backup = await client.rename(staged, relative, latestRemote !== null);
|
|
483
573
|
return { backup, stagedFile, permissionWarning };
|
|
484
574
|
} catch (error) {
|