@j0hanz/filesystem-mcp 1.14.0 → 1.14.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 +47 -47
- package/dist/cli.js +2 -2
- package/dist/completions.js +54 -51
- package/dist/config.d.ts +13 -13
- package/dist/config.js +12 -12
- package/dist/index.js +1 -1
- package/dist/lib/abort.d.ts +7 -0
- package/dist/lib/abort.js +81 -0
- package/dist/lib/constants.d.ts +3 -1
- package/dist/lib/constants.js +8 -2
- package/dist/lib/errors.d.ts +7 -3
- package/dist/lib/errors.js +59 -39
- package/dist/lib/file-operations/core.d.ts +3 -3
- package/dist/lib/file-operations/core.js +23 -20
- package/dist/lib/file-operations/metadata.d.ts +2 -2
- package/dist/lib/file-operations/metadata.js +60 -19
- package/dist/lib/file-operations/search.js +83 -84
- package/dist/lib/file-operations/traversal.js +13 -15
- package/dist/lib/fs-helpers.d.ts +3 -10
- package/dist/lib/fs-helpers.js +20 -98
- package/dist/lib/globs.js +1 -1
- package/dist/lib/logger.d.ts +28 -0
- package/dist/lib/logger.js +91 -0
- package/dist/lib/observability.d.ts +7 -0
- package/dist/lib/observability.js +19 -9
- package/dist/lib/paths.js +55 -55
- package/dist/lib/resource-store.js +4 -4
- package/dist/lib/utils.d.ts +0 -12
- package/dist/lib/utils.js +0 -13
- package/dist/resources/generated-instructions.js +40 -31
- package/dist/resources/tool-catalog.js +32 -26
- package/dist/resources/tool-info.js +34 -29
- package/dist/resources/workflows.js +39 -18
- package/dist/resources.d.ts +1 -1
- package/dist/resources.js +4 -4
- package/dist/schemas.d.ts +66 -66
- package/dist/schemas.js +21 -44
- package/dist/server/bootstrap.d.ts +12 -11
- package/dist/server/bootstrap.js +95 -86
- package/dist/server/roots-manager.d.ts +5 -2
- package/dist/server/roots-manager.js +8 -6
- package/dist/server/task-store.d.ts +10 -0
- package/dist/server/task-store.js +73 -0
- package/dist/tools/apply-patch.js +26 -18
- package/dist/tools/calculate-hash.js +11 -22
- package/dist/tools/create-directory.js +10 -8
- package/dist/tools/delete-file.js +17 -15
- package/dist/tools/diff-files.js +15 -15
- package/dist/tools/edit-file.js +7 -4
- package/dist/tools/list-directory.js +6 -6
- package/dist/tools/move-file.js +98 -81
- package/dist/tools/read-multiple.js +4 -4
- package/dist/tools/read.js +5 -5
- package/dist/tools/replace-in-files.js +20 -23
- package/dist/tools/roots.js +1 -1
- package/dist/tools/search-content.js +32 -41
- package/dist/tools/search-files.js +54 -41
- package/dist/tools/shared.d.ts +3 -0
- package/dist/tools/shared.js +70 -28
- package/dist/tools/stat-many.js +11 -7
- package/dist/tools/stat.js +4 -4
- package/dist/tools/task-support.d.ts +10 -9
- package/dist/tools/task-support.js +94 -23
- package/dist/tools/tree.js +3 -3
- package/dist/tools/write-file.js +10 -7
- package/package.json +9 -9
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import * as path from 'node:path';
|
|
2
1
|
import { readFile, stat } from 'node:fs/promises';
|
|
2
|
+
import { basename, resolve } from 'node:path';
|
|
3
3
|
import { applyPatch, parsePatch } from 'diff';
|
|
4
|
+
import { withAbort } from '../lib/abort.js';
|
|
4
5
|
import { MAX_TEXT_FILE_SIZE, PARALLEL_CONCURRENCY } from '../lib/constants.js';
|
|
5
6
|
import { ErrorCode, McpError } from '../lib/errors.js';
|
|
6
|
-
import { atomicWriteFile, processInParallel
|
|
7
|
+
import { atomicWriteFile, processInParallel } from '../lib/fs-helpers.js';
|
|
8
|
+
import { Logger } from '../lib/logger.js';
|
|
7
9
|
import { assertAllowedFileAccess, validateExistingPath } from '../lib/paths.js';
|
|
8
10
|
import { ApplyPatchInputSchema, ApplyPatchOutputSchema } from '../schemas.js';
|
|
9
11
|
import { buildStructuredError, buildToolErrorResponse, buildToolResponse, DESTRUCTIVE_WRITE_TOOL_ANNOTATIONS, executeToolWithDiagnostics, withDefaultIcons, withValidatedArgs, wrapToolHandler, } from './shared.js';
|
|
@@ -26,7 +28,7 @@ export const APPLY_PATCH_TOOL = {
|
|
|
26
28
|
function assertPatchTargetSizeWithinLimit(filePath, size, maxFileSize) {
|
|
27
29
|
if (size <= maxFileSize)
|
|
28
30
|
return;
|
|
29
|
-
throw new McpError(ErrorCode.
|
|
31
|
+
throw new McpError(ErrorCode.TOO_LARGE, `File too large for patch (${size} bytes > ${maxFileSize} bytes).`, filePath, { size, maxFileSize });
|
|
30
32
|
}
|
|
31
33
|
function countStructuredPatchStats(diff) {
|
|
32
34
|
let linesAdded = 0;
|
|
@@ -67,14 +69,14 @@ async function applyDiff(filePath, diff, options, signal) {
|
|
|
67
69
|
return {
|
|
68
70
|
path: validPath,
|
|
69
71
|
applied: false,
|
|
70
|
-
error: buildStructuredError(new McpError(ErrorCode.
|
|
72
|
+
error: buildStructuredError(new McpError(ErrorCode.INVALID_INPUT, 'Patch application failed', validPath), ErrorCode.INVALID_INPUT, validPath),
|
|
71
73
|
};
|
|
72
74
|
}
|
|
73
75
|
if (patched === content) {
|
|
74
76
|
return {
|
|
75
77
|
path: validPath,
|
|
76
78
|
applied: false,
|
|
77
|
-
error: buildStructuredError(new McpError(ErrorCode.
|
|
79
|
+
error: buildStructuredError(new McpError(ErrorCode.INVALID_INPUT, 'Patch had no effect', validPath), ErrorCode.INVALID_INPUT, validPath),
|
|
78
80
|
};
|
|
79
81
|
}
|
|
80
82
|
const patchStats = countStructuredPatchStats(diff);
|
|
@@ -91,10 +93,10 @@ async function processMultiFilePatch(basePath, parsed, options, signal) {
|
|
|
91
93
|
return () => Promise.resolve({
|
|
92
94
|
path: '<unknown>',
|
|
93
95
|
applied: false,
|
|
94
|
-
error: buildStructuredError(new McpError(ErrorCode.
|
|
96
|
+
error: buildStructuredError(new McpError(ErrorCode.INVALID_INPUT, 'Missing file name in patch header'), ErrorCode.INVALID_INPUT),
|
|
95
97
|
});
|
|
96
98
|
}
|
|
97
|
-
const filePath =
|
|
99
|
+
const filePath = resolve(validBase, fileName);
|
|
98
100
|
return async () => {
|
|
99
101
|
try {
|
|
100
102
|
const result = await applyDiff(filePath, diff, options, signal);
|
|
@@ -104,7 +106,7 @@ async function processMultiFilePatch(basePath, parsed, options, signal) {
|
|
|
104
106
|
return {
|
|
105
107
|
path: fileName,
|
|
106
108
|
applied: false,
|
|
107
|
-
error: buildStructuredError(error, ErrorCode.
|
|
109
|
+
error: buildStructuredError(error, ErrorCode.INVALID_INPUT, filePath),
|
|
108
110
|
};
|
|
109
111
|
}
|
|
110
112
|
};
|
|
@@ -125,9 +127,12 @@ async function processMultiFilePatch(basePath, parsed, options, signal) {
|
|
|
125
127
|
.filter((r) => !r.applied)
|
|
126
128
|
.map((r) => r.path)
|
|
127
129
|
.join(', ');
|
|
128
|
-
throw new McpError(ErrorCode.
|
|
130
|
+
throw new McpError(ErrorCode.INVALID_INPUT, `All ${parsed.length} patches failed${label}. Files: ${failedPaths}. Regenerate via diff_files.`);
|
|
129
131
|
}
|
|
130
132
|
const text = `Applied ${totals.applied}/${parsed.length} file patches${label}`;
|
|
133
|
+
if (!options.dryRun) {
|
|
134
|
+
Logger.info(`apply_patch: ${basePath} (${totals.applied} file(s), +${totals.added}/-${totals.removed})`);
|
|
135
|
+
}
|
|
131
136
|
return buildToolResponse(text, {
|
|
132
137
|
ok: totals.applied === parsed.length,
|
|
133
138
|
path: basePath,
|
|
@@ -140,13 +145,13 @@ async function processMultiFilePatch(basePath, parsed, options, signal) {
|
|
|
140
145
|
}
|
|
141
146
|
async function handleApplyPatch(args, signal) {
|
|
142
147
|
if (!args.patch.trim()) {
|
|
143
|
-
throw new McpError(ErrorCode.
|
|
148
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Patch content is empty.');
|
|
144
149
|
}
|
|
145
150
|
const fuzzFactor = args.fuzzFactor ?? 0;
|
|
146
151
|
const parsed = parsePatch(args.patch);
|
|
147
152
|
const hasHunks = parsed.some((p) => p.hunks.length > 0);
|
|
148
153
|
if (!hasHunks) {
|
|
149
|
-
throw new McpError(ErrorCode.
|
|
154
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Patch must include unified hunk headers (@@ -n,n +n,n @@).');
|
|
150
155
|
}
|
|
151
156
|
const options = {
|
|
152
157
|
dryRun: args.dryRun,
|
|
@@ -158,17 +163,20 @@ async function handleApplyPatch(args, signal) {
|
|
|
158
163
|
}
|
|
159
164
|
const diff = parsed[0];
|
|
160
165
|
if (!diff) {
|
|
161
|
-
throw new McpError(ErrorCode.
|
|
166
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'No patch content found.');
|
|
162
167
|
}
|
|
163
168
|
const result = await applyDiff(args.path, diff, options, signal);
|
|
164
169
|
if (!result.applied) {
|
|
165
|
-
throw new McpError(ErrorCode.
|
|
166
|
-
? 'Patch had no effect
|
|
167
|
-
: 'Patch
|
|
170
|
+
throw new McpError(ErrorCode.INVALID_INPUT, result.error?.message === 'Patch had no effect'
|
|
171
|
+
? 'Patch had no effect. Content unchanged. Regenerate via diff_files.'
|
|
172
|
+
: 'Patch failed. Content may have changed. Regenerate via diff_files. For minor diffs, use fuzzFactor.');
|
|
168
173
|
}
|
|
169
174
|
const text = args.dryRun
|
|
170
175
|
? 'Dry run successful. Patch can be applied.'
|
|
171
176
|
: `Successfully patched ${args.path}`;
|
|
177
|
+
if (!args.dryRun) {
|
|
178
|
+
Logger.info(`apply_patch: ${args.path} (+${result.linesAdded ?? 0}/-${result.linesRemoved ?? 0})`);
|
|
179
|
+
}
|
|
172
180
|
return buildToolResponse(text, {
|
|
173
181
|
ok: true,
|
|
174
182
|
path: result.path,
|
|
@@ -186,16 +194,16 @@ export function registerApplyPatchTool(server, options = {}) {
|
|
|
186
194
|
timedSignal: {},
|
|
187
195
|
context: { path: args.path },
|
|
188
196
|
run: (signal) => handleApplyPatch(args, signal),
|
|
189
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
197
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.UNKNOWN, args.path),
|
|
190
198
|
});
|
|
191
199
|
const wrappedHandler = wrapToolHandler(handler, {
|
|
192
200
|
guard: options.isInitialized,
|
|
193
201
|
progressMessage: (args) => {
|
|
194
|
-
const name =
|
|
202
|
+
const name = basename(args.path);
|
|
195
203
|
return args.dryRun ? `🛠 patch: ${name} [dry run]` : `🛠 patch: ${name}`;
|
|
196
204
|
},
|
|
197
205
|
completionMessage: (args, result) => {
|
|
198
|
-
const name =
|
|
206
|
+
const name = basename(args.path);
|
|
199
207
|
if (result.isError)
|
|
200
208
|
return `🛠 patch: ${name} • failed`;
|
|
201
209
|
const sc = result.structuredContent;
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import * as fs from 'node:fs/promises';
|
|
2
|
-
import * as path from 'node:path';
|
|
3
1
|
import { createHash } from 'node:crypto';
|
|
4
2
|
import { createReadStream } from 'node:fs';
|
|
3
|
+
import { stat } from 'node:fs/promises';
|
|
4
|
+
import { basename, relative, win32 } from 'node:path';
|
|
5
5
|
import { pipeline } from 'node:stream/promises';
|
|
6
|
+
import { assertNotAborted, withAbort } from '../lib/abort.js';
|
|
6
7
|
import { PARALLEL_CONCURRENCY } from '../lib/constants.js';
|
|
7
8
|
import { ErrorCode } from '../lib/errors.js';
|
|
8
9
|
import { isIgnoredByGitignore, loadRootGitignore, } from '../lib/file-operations/core.js';
|
|
9
10
|
import { globEntries } from '../lib/file-operations/traversal.js';
|
|
10
|
-
import { assertNotAborted, withAbort } from '../lib/fs-helpers.js';
|
|
11
11
|
import { validateExistingPath } from '../lib/paths.js';
|
|
12
|
-
import { reportPeriodicProgress } from '../lib/utils.js';
|
|
13
12
|
import { CalculateHashInputSchema, CalculateHashOutputSchema, } from '../schemas.js';
|
|
14
13
|
import { buildToolErrorResponse, buildToolResponse, createToolProgressSession, executeToolWithDiagnostics, READ_ONLY_TOOL_ANNOTATIONS, resolveFinalProgressCurrent, withDefaultIcons, withValidatedArgs, wrapToolHandler, } from './shared.js';
|
|
15
14
|
import { registerToolTaskIfAvailable } from './task-support.js';
|
|
@@ -32,8 +31,8 @@ async function hashFile(filePath, encoding, signal) {
|
|
|
32
31
|
return encoding ? hasher.digest(encoding) : hasher.digest();
|
|
33
32
|
}
|
|
34
33
|
function toStableRelativePath(root, entryPath) {
|
|
35
|
-
const relativePath =
|
|
36
|
-
return relativePath.includes(
|
|
34
|
+
const relativePath = relative(root, entryPath);
|
|
35
|
+
return relativePath.includes(win32.sep)
|
|
37
36
|
? relativePath.replace(WINDOWS_PATH_SEPARATOR, '/')
|
|
38
37
|
: relativePath;
|
|
39
38
|
}
|
|
@@ -93,18 +92,11 @@ async function hashDirectory(dirPath, options = {}) {
|
|
|
93
92
|
const fileHash = await hashFile(task.filePath, undefined, signal);
|
|
94
93
|
entries.push({ path: task.relativePath, hash: fileHash });
|
|
95
94
|
filesHashed++;
|
|
96
|
-
|
|
97
|
-
throttleModulo: 25,
|
|
98
|
-
total: totalFiles,
|
|
99
|
-
});
|
|
95
|
+
onProgress?.({ current: filesHashed, total: totalFiles });
|
|
100
96
|
}
|
|
101
97
|
};
|
|
102
98
|
await Promise.all(Array.from({ length: concurrency }, () => worker()));
|
|
103
|
-
|
|
104
|
-
throttleModulo: 25,
|
|
105
|
-
total: totalFiles,
|
|
106
|
-
force: true,
|
|
107
|
-
});
|
|
99
|
+
onProgress?.({ current: filesHashed, total: totalFiles });
|
|
108
100
|
assertNotAborted(signal);
|
|
109
101
|
// Sort by path with byte-wise semantics for deterministic ordering.
|
|
110
102
|
entries.sort(comparePaths);
|
|
@@ -123,7 +115,7 @@ async function hashDirectory(dirPath, options = {}) {
|
|
|
123
115
|
async function handleCalculateHash(args, signal, onProgress) {
|
|
124
116
|
const validPath = await validateExistingPath(args.path, signal);
|
|
125
117
|
// Check if path is a directory or file
|
|
126
|
-
const stats = await withAbort(
|
|
118
|
+
const stats = await withAbort(stat(validPath), signal);
|
|
127
119
|
if (stats.isDirectory()) {
|
|
128
120
|
// Hash directory: composite hash of all files
|
|
129
121
|
const { hash, fileCount } = await hashDirectory(validPath, {
|
|
@@ -141,10 +133,7 @@ async function handleCalculateHash(args, signal, onProgress) {
|
|
|
141
133
|
else {
|
|
142
134
|
// Hash single file
|
|
143
135
|
const hash = await hashFile(validPath, 'hex', signal);
|
|
144
|
-
|
|
145
|
-
throttleModulo: 25,
|
|
146
|
-
force: true,
|
|
147
|
-
});
|
|
136
|
+
onProgress?.({ current: 1 });
|
|
148
137
|
return buildToolResponse(hash, {
|
|
149
138
|
ok: true,
|
|
150
139
|
path: validPath,
|
|
@@ -161,7 +150,7 @@ export function registerCalculateHashTool(server, options = {}) {
|
|
|
161
150
|
timedSignal: {},
|
|
162
151
|
context: { path: args.path },
|
|
163
152
|
run: async (signal) => {
|
|
164
|
-
const baseName =
|
|
153
|
+
const baseName = basename(args.path);
|
|
165
154
|
const progress = createToolProgressSession(extra, `🕮 hash: ${baseName}`);
|
|
166
155
|
const progressWithMessage = ({ current, total, }) => {
|
|
167
156
|
progress.update({
|
|
@@ -190,7 +179,7 @@ export function registerCalculateHashTool(server, options = {}) {
|
|
|
190
179
|
throw error;
|
|
191
180
|
}
|
|
192
181
|
},
|
|
193
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
182
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.UNKNOWN, args.path),
|
|
194
183
|
});
|
|
195
184
|
const wrappedHandler = wrapToolHandler(handler, {
|
|
196
185
|
guard: options.isInitialized,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { mkdir } from 'node:fs/promises';
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
|
+
import { withAbort } from '../lib/abort.js';
|
|
3
4
|
import { ErrorCode, McpError } from '../lib/errors.js';
|
|
4
|
-
import { withAbort } from '../lib/fs-helpers.js';
|
|
5
5
|
import { validatePathForWrite } from '../lib/paths.js';
|
|
6
6
|
import { CreateDirectoryInputSchema, CreateDirectoryOutputSchema, } from '../schemas.js';
|
|
7
7
|
import { buildToolErrorResponse, buildToolResponse, executeToolWithDiagnostics, IDEMPOTENT_WRITE_TOOL_ANNOTATIONS, withDefaultIcons, withValidatedArgs, wrapToolHandler, } from './shared.js';
|
|
@@ -22,12 +22,14 @@ async function handleCreateDirectory(args, signal) {
|
|
|
22
22
|
if (args.paths)
|
|
23
23
|
allPaths.push(...args.paths);
|
|
24
24
|
if (allPaths.length === 0) {
|
|
25
|
-
throw new McpError(ErrorCode.
|
|
25
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'No paths provided to create.');
|
|
26
26
|
}
|
|
27
27
|
const validPaths = await Promise.all(allPaths.map((p) => validatePathForWrite(p, signal)));
|
|
28
|
-
await Promise.all(validPaths.map((p) => withAbort(
|
|
28
|
+
await Promise.all(validPaths.map((p) => withAbort(mkdir(p, { recursive: true }), signal)));
|
|
29
|
+
const createdPath = validPaths.length === 1 ? validPaths[0] : undefined;
|
|
29
30
|
return buildToolResponse(`Successfully created ${validPaths.length} director${validPaths.length === 1 ? 'y' : 'ies'}`, {
|
|
30
31
|
ok: true,
|
|
32
|
+
...(createdPath ? { path: createdPath } : {}),
|
|
31
33
|
paths: validPaths,
|
|
32
34
|
});
|
|
33
35
|
}
|
|
@@ -39,20 +41,20 @@ export function registerCreateDirectoryTool(server, options = {}) {
|
|
|
39
41
|
timedSignal: {},
|
|
40
42
|
context: { path: args.path ?? args.paths?.[0] },
|
|
41
43
|
run: (signal) => handleCreateDirectory(args, signal),
|
|
42
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
44
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.UNKNOWN, args.path ?? args.paths?.[0]),
|
|
43
45
|
});
|
|
44
46
|
const wrappedHandler = wrapToolHandler(handler, {
|
|
45
47
|
guard: options.isInitialized,
|
|
46
48
|
progressMessage: (args) => {
|
|
47
49
|
if (args.path && !args.paths?.length) {
|
|
48
|
-
return `🛠 mkdir: ${
|
|
50
|
+
return `🛠 mkdir: ${basename(args.path)}`;
|
|
49
51
|
}
|
|
50
52
|
const count = (args.path ? 1 : 0) + (args.paths?.length ?? 0);
|
|
51
53
|
return `🛠 mkdir: ${count} directories`;
|
|
52
54
|
},
|
|
53
55
|
completionMessage: (args, result) => {
|
|
54
56
|
if (args.path && !args.paths?.length) {
|
|
55
|
-
const name =
|
|
57
|
+
const name = basename(args.path);
|
|
56
58
|
if (result.isError)
|
|
57
59
|
return `🛠 mkdir: ${name} • failed`;
|
|
58
60
|
return `🛠 mkdir: ${name}`;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { lstat, rm, rmdir } from 'node:fs/promises';
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
|
+
import { withAbort } from '../lib/abort.js';
|
|
3
4
|
import { ErrorCode, isNodeError, McpError } from '../lib/errors.js';
|
|
4
|
-
import {
|
|
5
|
+
import { Logger } from '../lib/logger.js';
|
|
5
6
|
import { isAllowedDirectoryRoot, validatePathForWrite } from '../lib/paths.js';
|
|
6
7
|
import { DeleteFileInputSchema, DeleteFileOutputSchema } from '../schemas.js';
|
|
7
8
|
import { buildToolErrorResponse, buildToolResponse, DESTRUCTIVE_WRITE_TOOL_ANNOTATIONS, executeToolWithDiagnostics, withDefaultIcons, withValidatedArgs, wrapToolHandler, } from './shared.js';
|
|
@@ -19,11 +20,11 @@ export const DELETE_FILE_TOOL = {
|
|
|
19
20
|
async function handleDeleteFile(args, signal) {
|
|
20
21
|
const validPath = await validatePathForWrite(args.path, signal);
|
|
21
22
|
if (isAllowedDirectoryRoot(validPath)) {
|
|
22
|
-
throw new McpError(ErrorCode.
|
|
23
|
+
throw new McpError(ErrorCode.ACCESS_DENIED, 'Deleting a workspace root directory is not allowed');
|
|
23
24
|
}
|
|
24
25
|
let stats;
|
|
25
26
|
try {
|
|
26
|
-
stats = await withAbort(
|
|
27
|
+
stats = await withAbort(lstat(validPath), signal);
|
|
27
28
|
}
|
|
28
29
|
catch (error) {
|
|
29
30
|
if (isNodeError(error) &&
|
|
@@ -39,14 +40,15 @@ async function handleDeleteFile(args, signal) {
|
|
|
39
40
|
if (stats.isDirectory() && !args.recursive) {
|
|
40
41
|
// Use rmdir for non-recursive directory deletes so non-empty directories
|
|
41
42
|
// consistently return ENOTEMPTY-style errors with actionable guidance.
|
|
42
|
-
await withAbort(
|
|
43
|
+
await withAbort(rmdir(validPath), signal);
|
|
43
44
|
}
|
|
44
45
|
else {
|
|
45
|
-
await withAbort(
|
|
46
|
+
await withAbort(rm(validPath, {
|
|
46
47
|
recursive: args.recursive,
|
|
47
48
|
force: args.ignoreIfNotExists,
|
|
48
49
|
}), signal);
|
|
49
50
|
}
|
|
51
|
+
Logger.info(`rm: ${args.path}`);
|
|
50
52
|
return buildToolResponse(`Successfully deleted: ${args.path}`, {
|
|
51
53
|
ok: true,
|
|
52
54
|
path: validPath,
|
|
@@ -63,29 +65,29 @@ export function registerDeleteFileTool(server, options = {}) {
|
|
|
63
65
|
onError: (error) => {
|
|
64
66
|
if (isNodeError(error)) {
|
|
65
67
|
if (error.code === 'ENOENT') {
|
|
66
|
-
return buildToolErrorResponse(error, ErrorCode.
|
|
68
|
+
return buildToolErrorResponse(error, ErrorCode.NOT_FOUND, args.path);
|
|
67
69
|
}
|
|
68
70
|
if (error.code === 'ENOTEMPTY') {
|
|
69
|
-
return buildToolErrorResponse(new Error(
|
|
71
|
+
return buildToolErrorResponse(new Error('Directory not empty. Set recursive: true.'), ErrorCode.INVALID_INPUT, args.path);
|
|
70
72
|
}
|
|
71
73
|
if (error.code === 'EISDIR') {
|
|
72
|
-
return buildToolErrorResponse(new Error(
|
|
74
|
+
return buildToolErrorResponse(new Error('Path is a directory. Set recursive: true.'), ErrorCode.INVALID_INPUT, args.path);
|
|
73
75
|
}
|
|
74
76
|
if (error.code === 'EEXIST') {
|
|
75
|
-
return buildToolErrorResponse(new Error(
|
|
77
|
+
return buildToolErrorResponse(new Error('Directory not empty. Set recursive: true.'), ErrorCode.INVALID_INPUT, args.path);
|
|
76
78
|
}
|
|
77
79
|
if (error.code === 'EPERM' || error.code === 'EACCES') {
|
|
78
|
-
return buildToolErrorResponse(error, ErrorCode.
|
|
80
|
+
return buildToolErrorResponse(error, ErrorCode.PERMISSION_DENIED, args.path);
|
|
79
81
|
}
|
|
80
82
|
}
|
|
81
|
-
return buildToolErrorResponse(error, ErrorCode.
|
|
83
|
+
return buildToolErrorResponse(error, ErrorCode.UNKNOWN, args.path);
|
|
82
84
|
},
|
|
83
85
|
});
|
|
84
86
|
const wrappedHandler = wrapToolHandler(handler, {
|
|
85
87
|
guard: options.isInitialized,
|
|
86
|
-
progressMessage: (args) => `🛠 rm: ${
|
|
88
|
+
progressMessage: (args) => `🛠 rm: ${basename(args.path)}`,
|
|
87
89
|
completionMessage: (args, result) => {
|
|
88
|
-
const name =
|
|
90
|
+
const name = basename(args.path);
|
|
89
91
|
if (result.isError)
|
|
90
92
|
return `🛠 rm: ${name} • failed`;
|
|
91
93
|
return `🛠 rm: ${name}`;
|
package/dist/tools/diff-files.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
3
|
import { formatPatch, structuredPatch } from 'diff';
|
|
4
|
+
import { withAbort } from '../lib/abort.js';
|
|
4
5
|
import { MAX_TEXT_FILE_SIZE } from '../lib/constants.js';
|
|
5
6
|
import { ErrorCode, McpError } from '../lib/errors.js';
|
|
6
|
-
import { withAbort } from '../lib/fs-helpers.js';
|
|
7
7
|
import { validateExistingPath } from '../lib/paths.js';
|
|
8
8
|
import { DiffFilesInputSchema, DiffFilesOutputSchema } from '../schemas.js';
|
|
9
9
|
import { buildResourceLink, buildToolErrorResponse, buildToolResponse, executeToolWithDiagnostics, maybeExternalizeTextContent, READ_ONLY_TOOL_ANNOTATIONS, withDefaultIcons, withValidatedArgs, wrapToolHandler, } from './shared.js';
|
|
@@ -35,7 +35,7 @@ function computeDiffStats(hunks) {
|
|
|
35
35
|
function assertDiffFileSizeWithinLimit(filePath, size, maxFileSize) {
|
|
36
36
|
if (size <= maxFileSize)
|
|
37
37
|
return;
|
|
38
|
-
throw new McpError(ErrorCode.
|
|
38
|
+
throw new McpError(ErrorCode.TOO_LARGE, `File too large for diff (${size} bytes > ${maxFileSize} bytes).`, filePath, { size, maxFileSize });
|
|
39
39
|
}
|
|
40
40
|
async function handleDiffFiles(args, signal, resourceStore) {
|
|
41
41
|
const maxFileSize = MAX_TEXT_FILE_SIZE;
|
|
@@ -44,17 +44,17 @@ async function handleDiffFiles(args, signal, resourceStore) {
|
|
|
44
44
|
validateExistingPath(args.modified, signal),
|
|
45
45
|
]);
|
|
46
46
|
const [originalStats, modifiedStats] = await Promise.all([
|
|
47
|
-
withAbort(
|
|
48
|
-
withAbort(
|
|
47
|
+
withAbort(stat(originalPath), signal),
|
|
48
|
+
withAbort(stat(modifiedPath), signal),
|
|
49
49
|
]);
|
|
50
50
|
assertDiffFileSizeWithinLimit(originalPath, originalStats.size, maxFileSize);
|
|
51
51
|
assertDiffFileSizeWithinLimit(modifiedPath, modifiedStats.size, maxFileSize);
|
|
52
52
|
const [originalContent, modifiedContent] = await Promise.all([
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
readFile(originalPath, { encoding: 'utf-8', signal }),
|
|
54
|
+
readFile(modifiedPath, { encoding: 'utf-8', signal }),
|
|
55
55
|
]);
|
|
56
56
|
const patchObj = await new Promise((resolve) => {
|
|
57
|
-
structuredPatch(
|
|
57
|
+
structuredPatch(basename(originalPath), basename(modifiedPath), originalContent, modifiedContent, undefined, undefined, {
|
|
58
58
|
...(args.context !== undefined ? { context: args.context } : {}),
|
|
59
59
|
ignoreWhitespace: args.ignoreWhitespace,
|
|
60
60
|
stripTrailingCr: args.stripTrailingCr,
|
|
@@ -65,7 +65,7 @@ async function handleDiffFiles(args, signal, resourceStore) {
|
|
|
65
65
|
});
|
|
66
66
|
});
|
|
67
67
|
if (!patchObj) {
|
|
68
|
-
throw new McpError(ErrorCode.
|
|
68
|
+
throw new McpError(ErrorCode.TIMEOUT, `Diff timed out or too complex.`, originalPath);
|
|
69
69
|
}
|
|
70
70
|
const isIdentical = patchObj.hunks.length === 0;
|
|
71
71
|
const diffText = isIdentical ? '' : formatPatch(patchObj);
|
|
@@ -108,18 +108,18 @@ export function registerDiffFilesTool(server, options = {}) {
|
|
|
108
108
|
timedSignal: {},
|
|
109
109
|
context: { path: args.original },
|
|
110
110
|
run: (signal) => handleDiffFiles(args, signal, options.resourceStore),
|
|
111
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
111
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.UNKNOWN, args.original),
|
|
112
112
|
});
|
|
113
113
|
const wrappedHandler = wrapToolHandler(handler, {
|
|
114
114
|
guard: options.isInitialized,
|
|
115
115
|
progressMessage: (args) => {
|
|
116
|
-
const n1 =
|
|
117
|
-
const n2 =
|
|
116
|
+
const n1 = basename(args.original);
|
|
117
|
+
const n2 = basename(args.modified);
|
|
118
118
|
return `🕮 diff: ${n1} ⟷ ${n2}`;
|
|
119
119
|
},
|
|
120
120
|
completionMessage: (args, result) => {
|
|
121
|
-
const n1 =
|
|
122
|
-
const n2 =
|
|
121
|
+
const n1 = basename(args.original);
|
|
122
|
+
const n2 = basename(args.modified);
|
|
123
123
|
if (result.isError)
|
|
124
124
|
return `🕮 diff: ${n1} ⟷ ${n2} • failed`;
|
|
125
125
|
const sc = result.structuredContent;
|
package/dist/tools/edit-file.js
CHANGED
|
@@ -2,9 +2,11 @@ import { readFile, stat } from 'node:fs/promises';
|
|
|
2
2
|
import { basename } from 'node:path';
|
|
3
3
|
import { createTwoFilesPatch, diffLines } from 'diff';
|
|
4
4
|
import RE2 from 're2';
|
|
5
|
+
import { withAbort } from '../lib/abort.js';
|
|
5
6
|
import { MAX_TEXT_FILE_SIZE } from '../lib/constants.js';
|
|
6
7
|
import { ErrorCode, McpError } from '../lib/errors.js';
|
|
7
|
-
import { atomicWriteFile
|
|
8
|
+
import { atomicWriteFile } from '../lib/fs-helpers.js';
|
|
9
|
+
import { Logger } from '../lib/logger.js';
|
|
8
10
|
import { assertAllowedFileAccess, validateExistingPath } from '../lib/paths.js';
|
|
9
11
|
import { EditFileInputSchema, EditFileOutputSchema } from '../schemas.js';
|
|
10
12
|
import { buildToolErrorResponse, buildToolResponse, DESTRUCTIVE_WRITE_TOOL_ANNOTATIONS, executeToolWithDiagnostics, withDefaultIcons, withValidatedArgs, wrapToolHandler, } from './shared.js';
|
|
@@ -161,7 +163,7 @@ async function loadEditableFile(requestedPath, signal) {
|
|
|
161
163
|
assertAllowedFileAccess(requestedPath, validPath);
|
|
162
164
|
const stats = await withAbort(stat(validPath), signal);
|
|
163
165
|
if (stats.size > MAX_TEXT_FILE_SIZE) {
|
|
164
|
-
throw new McpError(ErrorCode.
|
|
166
|
+
throw new McpError(ErrorCode.TOO_LARGE, `File too large for edit (${stats.size} bytes > ${MAX_TEXT_FILE_SIZE} bytes)`, requestedPath, { size: stats.size, maxFileSize: MAX_TEXT_FILE_SIZE });
|
|
165
167
|
}
|
|
166
168
|
const content = await readFile(validPath, { encoding: 'utf-8', signal });
|
|
167
169
|
return { validPath, content };
|
|
@@ -215,13 +217,14 @@ async function handleEditFile(args, signal) {
|
|
|
215
217
|
return buildToolResponse(`Dry run complete. ${editResult.appliedEdits} edits would be applied.`, structured);
|
|
216
218
|
}
|
|
217
219
|
if (editResult.appliedEdits === 0 && editResult.unmatchedEdits.length > 0) {
|
|
218
|
-
throw new McpError(ErrorCode.
|
|
220
|
+
throw new McpError(ErrorCode.INVALID_INPUT, `All ${editResult.unmatchedEdits.length} edits failed. Verify oldText matches exact file content.`, args.path);
|
|
219
221
|
}
|
|
220
222
|
if (editResult.appliedEdits > 0) {
|
|
221
223
|
await atomicWriteFile(validPath, editResult.content, {
|
|
222
224
|
encoding: 'utf-8',
|
|
223
225
|
signal,
|
|
224
226
|
});
|
|
227
|
+
Logger.info(`edit: ${args.path} (${editResult.appliedEdits} edits, +${editResult.linesAdded}/-${editResult.linesRemoved})`);
|
|
225
228
|
}
|
|
226
229
|
return buildToolResponse(buildEditMessage(args.path, editResult), structured);
|
|
227
230
|
}
|
|
@@ -233,7 +236,7 @@ export function registerEditFileTool(server, options = {}) {
|
|
|
233
236
|
timedSignal: {},
|
|
234
237
|
context: { path: args.path },
|
|
235
238
|
run: (signal) => handleEditFile(args, signal),
|
|
236
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
239
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.UNKNOWN, args.path),
|
|
237
240
|
});
|
|
238
241
|
const wrappedHandler = wrapToolHandler(handler, {
|
|
239
242
|
guard: options.isInitialized,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as path from 'node:path';
|
|
2
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import { DEFAULT_EXCLUDE_PATTERNS, MAX_LIST_ENTRIES, } from '../lib/constants.js';
|
|
5
5
|
import { ErrorCode, McpError } from '../lib/errors.js';
|
|
@@ -71,7 +71,7 @@ function decodeListCursor(cursor) {
|
|
|
71
71
|
catch {
|
|
72
72
|
// fall through to throw
|
|
73
73
|
}
|
|
74
|
-
throw new McpError(ErrorCode.
|
|
74
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Invalid or expired cursor.');
|
|
75
75
|
}
|
|
76
76
|
function resolveNextListCursor(snapshotId, offset, pageSize, totalEntries) {
|
|
77
77
|
if (!snapshotId)
|
|
@@ -166,7 +166,7 @@ async function handleListDirectory(args, signal) {
|
|
|
166
166
|
const cursor = decodeListCursor(args.cursor);
|
|
167
167
|
const snapshot = listSnapshots.get(cursor.snapshotId);
|
|
168
168
|
if (snapshot?.fingerprint !== fingerprint) {
|
|
169
|
-
throw new McpError(ErrorCode.
|
|
169
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Invalid or expired cursor.');
|
|
170
170
|
}
|
|
171
171
|
const { offset, snapshotId: storedSnapshotId } = cursor;
|
|
172
172
|
cursorOffset = offset;
|
|
@@ -200,13 +200,13 @@ export function registerListDirectoryTool(server, options = {}) {
|
|
|
200
200
|
outputSchema: ListDirectoryOutputSchema,
|
|
201
201
|
context: { path: args.path ?? '.' },
|
|
202
202
|
run: (signal) => handleListDirectory(args, signal),
|
|
203
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
203
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.NOT_DIRECTORY, args.path ?? '.'),
|
|
204
204
|
});
|
|
205
205
|
const wrappedHandler = wrapToolHandler(handler, {
|
|
206
206
|
guard: options.isInitialized,
|
|
207
|
-
progressMessage: (args) => `≣ ls: ${args.path ?
|
|
207
|
+
progressMessage: (args) => `≣ ls: ${args.path ? basename(args.path) : '.'}`,
|
|
208
208
|
completionMessage: (args, result) => {
|
|
209
|
-
const base = args.path ?
|
|
209
|
+
const base = args.path ? basename(args.path) : '.';
|
|
210
210
|
if (result.isError)
|
|
211
211
|
return `≣ ls: ${base} • failed`;
|
|
212
212
|
const sc = result.structuredContent;
|