@j0hanz/filesystem-mcp 1.14.0 → 1.15.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/LICENSE +21 -0
- package/README.md +143 -71
- 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
package/dist/tools/shared.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import * as path from 'node:path';
|
|
2
1
|
import { channel } from 'node:diagnostics_channel';
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
+
import { createTimedAbortSignal } from '../lib/abort.js';
|
|
4
5
|
import { parseTrueEnvFlag } from '../lib/constants.js';
|
|
5
6
|
import { createDetailedError, ErrorCode, formatDetailedError, getSuggestion, McpError, } from '../lib/errors.js';
|
|
6
|
-
import {
|
|
7
|
-
import { withToolDiagnostics } from '../lib/observability.js';
|
|
7
|
+
import { Logger } from '../lib/logger.js';
|
|
8
|
+
import { withToolDiagnostics, } from '../lib/observability.js';
|
|
8
9
|
import { getAllowedDirectories } from '../lib/paths.js';
|
|
9
10
|
import { createBase64JsonCodec } from '../lib/zod-codecs.js';
|
|
10
11
|
export {} from './contract.js';
|
|
@@ -12,6 +13,20 @@ const MAX_INLINE_CONTENT_CHARS = parseInt(process.env['FS_CONTEXT_MAX_INLINE_CHA
|
|
|
12
13
|
const MAX_INLINE_PREVIEW_CHARS = 4_000;
|
|
13
14
|
const PROGRESS_RATE_LIMIT_MS = 50;
|
|
14
15
|
const CONTEXT_DIAGNOSTICS_CHANNEL = channel('filesystem-mcp:context');
|
|
16
|
+
// W3C Trace Context: version-traceid-parentid-traceflags
|
|
17
|
+
const TRACEPARENT_RE = /^[\da-f]{2}-[\da-f]{32}-[\da-f]{16}-[\da-f]{2}$/i;
|
|
18
|
+
function extractTraceContext(meta) {
|
|
19
|
+
const tp = meta?.traceparent;
|
|
20
|
+
if (typeof tp !== 'string' || !TRACEPARENT_RE.test(tp))
|
|
21
|
+
return undefined;
|
|
22
|
+
return {
|
|
23
|
+
traceparent: tp,
|
|
24
|
+
...(typeof meta?.tracestate === 'string'
|
|
25
|
+
? { tracestate: meta.tracestate }
|
|
26
|
+
: {}),
|
|
27
|
+
...(typeof meta?.baggage === 'string' ? { baggage: meta.baggage } : {}),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
15
30
|
function publishContextDiagnostics(event) {
|
|
16
31
|
if (!CONTEXT_DIAGNOSTICS_CHANNEL.hasSubscribers)
|
|
17
32
|
return;
|
|
@@ -45,11 +60,13 @@ function normalizeToolExecution(tool) {
|
|
|
45
60
|
? candidate['execution']
|
|
46
61
|
: undefined;
|
|
47
62
|
const executionTaskSupport = existingExecution?.['taskSupport'];
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
63
|
+
let resolvedTaskSupport;
|
|
64
|
+
if (isTaskSupportLevel(topLevelTaskSupport)) {
|
|
65
|
+
resolvedTaskSupport = topLevelTaskSupport;
|
|
66
|
+
}
|
|
67
|
+
else if (isTaskSupportLevel(executionTaskSupport)) {
|
|
68
|
+
resolvedTaskSupport = executionTaskSupport;
|
|
69
|
+
}
|
|
53
70
|
if (resolvedTaskSupport === undefined && topLevelTaskSupport === undefined) {
|
|
54
71
|
return tool;
|
|
55
72
|
}
|
|
@@ -106,6 +123,7 @@ export function maybeExternalizeTextContent(resourceStore, content, params) {
|
|
|
106
123
|
chars: content.length,
|
|
107
124
|
uri: entry.uri,
|
|
108
125
|
});
|
|
126
|
+
Logger.debug(`Content externalized: ${params.name} (${content.length} chars) → ${entry.uri}`);
|
|
109
127
|
return {
|
|
110
128
|
entry,
|
|
111
129
|
preview: buildTextPreview(content),
|
|
@@ -128,9 +146,12 @@ export function buildResourceLink(params) {
|
|
|
128
146
|
}
|
|
129
147
|
function resolveDetailedError(error, defaultCode, path) {
|
|
130
148
|
const detailed = createDetailedError(error, path);
|
|
131
|
-
if (detailed.code === ErrorCode.
|
|
149
|
+
if (detailed.code === ErrorCode.UNKNOWN) {
|
|
132
150
|
detailed.code = defaultCode;
|
|
133
|
-
|
|
151
|
+
const suggestion = getSuggestion(defaultCode);
|
|
152
|
+
if (suggestion) {
|
|
153
|
+
detailed.suggestion = suggestion;
|
|
154
|
+
}
|
|
134
155
|
}
|
|
135
156
|
return detailed;
|
|
136
157
|
}
|
|
@@ -156,13 +177,13 @@ function validateStructuredContent(toolName, outputSchema, structuredContent) {
|
|
|
156
177
|
if (parsed.success) {
|
|
157
178
|
return parsed.data;
|
|
158
179
|
}
|
|
159
|
-
throw new McpError(ErrorCode.
|
|
180
|
+
throw new McpError(ErrorCode.UNKNOWN, `Tool "${toolName}" returned invalid structuredContent.`, undefined, { errors: z.treeifyError(parsed.error) });
|
|
160
181
|
}
|
|
161
182
|
function validateToolResponse(toolName, result, outputSchema) {
|
|
162
183
|
if (!outputSchema)
|
|
163
184
|
return result;
|
|
164
185
|
if (!Object.hasOwn(result, 'structuredContent')) {
|
|
165
|
-
throw new McpError(ErrorCode.
|
|
186
|
+
throw new McpError(ErrorCode.UNKNOWN, `Tool "${toolName}" returned success without structuredContent.`);
|
|
166
187
|
}
|
|
167
188
|
return {
|
|
168
189
|
...result,
|
|
@@ -175,12 +196,20 @@ function parseToolArgs(schema, args) {
|
|
|
175
196
|
if (parsed.success) {
|
|
176
197
|
return parsed.data;
|
|
177
198
|
}
|
|
178
|
-
throw new McpError(ErrorCode.
|
|
199
|
+
throw new McpError(ErrorCode.INVALID_INPUT, `Invalid tool arguments:\n${z.prettifyError(parsed.error)}`);
|
|
179
200
|
}
|
|
180
201
|
export function withValidatedArgs(schema, handler) {
|
|
181
202
|
return async (args, extra) => {
|
|
182
|
-
|
|
183
|
-
|
|
203
|
+
try {
|
|
204
|
+
const normalizedArgs = parseToolArgs(schema, args);
|
|
205
|
+
return await handler(normalizedArgs, extra);
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
if (error instanceof McpError && error.code === ErrorCode.INVALID_INPUT) {
|
|
209
|
+
return buildToolErrorResponse(error, ErrorCode.INVALID_INPUT);
|
|
210
|
+
}
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
184
213
|
};
|
|
185
214
|
}
|
|
186
215
|
function canSendProgress(extra) {
|
|
@@ -232,7 +261,7 @@ export function buildFileInfoPayload(info) {
|
|
|
232
261
|
: {}),
|
|
233
262
|
};
|
|
234
263
|
}
|
|
235
|
-
const NOT_INITIALIZED_ERROR = new McpError(ErrorCode.
|
|
264
|
+
const NOT_INITIALIZED_ERROR = new McpError(ErrorCode.INVALID_INPUT, 'Client not initialized; wait for notifications/initialized');
|
|
236
265
|
async function withToolErrorHandling(run, onError) {
|
|
237
266
|
try {
|
|
238
267
|
return await run();
|
|
@@ -249,6 +278,7 @@ function getToolSignal(extraSignal, timedSignal) {
|
|
|
249
278
|
return { signal, cleanup };
|
|
250
279
|
}
|
|
251
280
|
export async function executeToolWithDiagnostics(options) {
|
|
281
|
+
const traceContext = extractTraceContext(options.extra._meta);
|
|
252
282
|
return withToolDiagnostics(options.toolName, () => withToolErrorHandling(async () => {
|
|
253
283
|
const { signal, cleanup } = getToolSignal(options.extra.signal, options.timedSignal);
|
|
254
284
|
try {
|
|
@@ -257,7 +287,10 @@ export async function executeToolWithDiagnostics(options) {
|
|
|
257
287
|
finally {
|
|
258
288
|
cleanup();
|
|
259
289
|
}
|
|
260
|
-
}, options.onError),
|
|
290
|
+
}, options.onError), {
|
|
291
|
+
...options.context,
|
|
292
|
+
...(traceContext ? { traceContext } : {}),
|
|
293
|
+
});
|
|
261
294
|
}
|
|
262
295
|
export function buildToolErrorResponse(error, defaultCode, path) {
|
|
263
296
|
const detailed = resolveDetailedError(error, defaultCode, path);
|
|
@@ -269,7 +302,7 @@ export function buildToolErrorResponse(error, defaultCode, path) {
|
|
|
269
302
|
};
|
|
270
303
|
}
|
|
271
304
|
function buildNotInitializedResult() {
|
|
272
|
-
return buildToolErrorResponse(NOT_INITIALIZED_ERROR, ErrorCode.
|
|
305
|
+
return buildToolErrorResponse(NOT_INITIALIZED_ERROR, ErrorCode.INVALID_INPUT);
|
|
273
306
|
}
|
|
274
307
|
async function reportProgress(extra, progress) {
|
|
275
308
|
await updateTaskStoreProgress(extra, progress);
|
|
@@ -283,6 +316,10 @@ function formatTaskStatusMessage(progress) {
|
|
|
283
316
|
}
|
|
284
317
|
return progress.message ?? `${progress.current}`;
|
|
285
318
|
}
|
|
319
|
+
function isBenignTaskStatusUpdateError(error) {
|
|
320
|
+
return (error instanceof Error &&
|
|
321
|
+
/Task .*not found|terminal status/iu.test(error.message));
|
|
322
|
+
}
|
|
286
323
|
async function updateTaskStoreProgress(extra, progress) {
|
|
287
324
|
const taskExtra = extra;
|
|
288
325
|
if (typeof taskExtra.taskId === 'string' &&
|
|
@@ -294,7 +331,9 @@ async function updateTaskStoreProgress(extra, progress) {
|
|
|
294
331
|
await store.updateTaskStatus(taskExtra.taskId, 'working', formatTaskStatusMessage(progress));
|
|
295
332
|
}
|
|
296
333
|
catch (error) {
|
|
297
|
-
|
|
334
|
+
if (isBenignTaskStatusUpdateError(error))
|
|
335
|
+
return;
|
|
336
|
+
Logger.error('Failed to update task status message:', error);
|
|
298
337
|
}
|
|
299
338
|
}
|
|
300
339
|
}
|
|
@@ -315,7 +354,7 @@ async function sendMcpProgressNotification(extra, progress) {
|
|
|
315
354
|
});
|
|
316
355
|
}
|
|
317
356
|
catch (error) {
|
|
318
|
-
|
|
357
|
+
Logger.error('Failed to send progress notification:', error);
|
|
319
358
|
}
|
|
320
359
|
}
|
|
321
360
|
}
|
|
@@ -480,14 +519,14 @@ export function resolvePathOrRoot(pathValue) {
|
|
|
480
519
|
return pathValue;
|
|
481
520
|
const roots = getAllowedDirectories();
|
|
482
521
|
if (roots.length === 0) {
|
|
483
|
-
throw new McpError(ErrorCode.
|
|
522
|
+
throw new McpError(ErrorCode.ACCESS_DENIED, 'No roots configured. Use roots tool, --allow-cwd, or MCP Roots protocol.');
|
|
484
523
|
}
|
|
485
524
|
if (roots.length > 1) {
|
|
486
|
-
throw new McpError(ErrorCode.
|
|
525
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Multiple roots configured. Provide an explicit path.');
|
|
487
526
|
}
|
|
488
527
|
const root = roots[0];
|
|
489
528
|
if (!root) {
|
|
490
|
-
throw new McpError(ErrorCode.
|
|
529
|
+
throw new McpError(ErrorCode.ACCESS_DENIED, 'Workspace root is unexpectedly undefined');
|
|
491
530
|
}
|
|
492
531
|
return root;
|
|
493
532
|
}
|
|
@@ -505,14 +544,17 @@ export function decodeOffsetCursor(cursor) {
|
|
|
505
544
|
catch {
|
|
506
545
|
// fall through to throw
|
|
507
546
|
}
|
|
508
|
-
throw new McpError(ErrorCode.
|
|
547
|
+
throw new McpError(ErrorCode.INVALID_INPUT, `Invalid cursor. Request the first page without a cursor.`);
|
|
509
548
|
}
|
|
510
549
|
export function buildBatchPathContext(paths, unitLabel = 'paths') {
|
|
511
550
|
const normalizedLabel = paths.length === 1 ? unitLabel.replace(/s$/i, '') : unitLabel;
|
|
512
|
-
const first =
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
551
|
+
const first = basename(paths[0] ?? '');
|
|
552
|
+
let extraPaths = '';
|
|
553
|
+
if (paths.length > 1) {
|
|
554
|
+
const secondPath = basename(paths[1] ?? '');
|
|
555
|
+
const ellipsis = paths.length > 2 ? '…' : '';
|
|
556
|
+
extraPaths = `, ${secondPath}${ellipsis}`;
|
|
557
|
+
}
|
|
516
558
|
return `${paths.length} ${normalizedLabel} [${first}${extraPaths}]`;
|
|
517
559
|
}
|
|
518
560
|
export function truncateProgressPattern(pattern, maxLength = 40) {
|
package/dist/tools/stat-many.js
CHANGED
|
@@ -38,15 +38,19 @@ async function handleGetMultipleFileInfo(args, signal, onProgress) {
|
|
|
38
38
|
path: entry.path,
|
|
39
39
|
info: entry.info ? buildFileInfoPayload(entry.info) : undefined,
|
|
40
40
|
error: entry.error
|
|
41
|
-
? buildStructuredError(entry.error, ErrorCode.
|
|
41
|
+
? buildStructuredError(entry.error, ErrorCode.NOT_FOUND, entry.path)
|
|
42
42
|
: undefined,
|
|
43
43
|
}));
|
|
44
44
|
const text = result.results
|
|
45
|
-
.map((entry) =>
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
.map((entry) => {
|
|
46
|
+
if (entry.error) {
|
|
47
|
+
return `${entry.path}: ${buildStructuredError(entry.error, ErrorCode.NOT_FOUND, entry.path).message}`;
|
|
48
|
+
}
|
|
49
|
+
if (entry.info) {
|
|
50
|
+
return formatFileInfoDetail(entry.info);
|
|
51
|
+
}
|
|
52
|
+
return entry.path;
|
|
53
|
+
})
|
|
50
54
|
.join('\n\n');
|
|
51
55
|
const structured = {
|
|
52
56
|
ok: true,
|
|
@@ -90,7 +94,7 @@ export function registerGetMultipleFileInfoTool(server, options = {}) {
|
|
|
90
94
|
throw error;
|
|
91
95
|
}
|
|
92
96
|
},
|
|
93
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
97
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.NOT_FOUND, primaryPath),
|
|
94
98
|
});
|
|
95
99
|
};
|
|
96
100
|
const wrappedHandler = wrapToolHandler(handler, {
|
package/dist/tools/stat.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
2
|
import { DEFAULT_SEARCH_TIMEOUT_MS } from '../lib/constants.js';
|
|
3
3
|
import { ErrorCode } from '../lib/errors.js';
|
|
4
4
|
import { getFileInfo } from '../lib/file-operations/metadata.js';
|
|
@@ -48,13 +48,13 @@ export function registerGetFileInfoTool(server, options = {}) {
|
|
|
48
48
|
timedSignal: { timeoutMs: DEFAULT_SEARCH_TIMEOUT_MS },
|
|
49
49
|
context: { path: args.path },
|
|
50
50
|
run: (signal) => handleGetFileInfo(args, signal),
|
|
51
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
51
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.NOT_FOUND, args.path),
|
|
52
52
|
});
|
|
53
53
|
const wrappedHandler = wrapToolHandler(handler, {
|
|
54
54
|
guard: options.isInitialized,
|
|
55
|
-
progressMessage: (args) => `🕮 stat: ${
|
|
55
|
+
progressMessage: (args) => `🕮 stat: ${basename(args.path)}`,
|
|
56
56
|
completionMessage: (args, result) => {
|
|
57
|
-
const name =
|
|
57
|
+
const name = basename(args.path);
|
|
58
58
|
if (result.isError)
|
|
59
59
|
return `🕮 stat: ${name} • failed`;
|
|
60
60
|
const sc = result.structuredContent;
|
|
@@ -2,20 +2,21 @@ import type { ToolTaskHandler } from '@modelcontextprotocol/sdk/experimental/tas
|
|
|
2
2
|
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
3
|
import type { AnySchema, SchemaOutput, ShapeOutput, ZodRawShapeCompat } from '@modelcontextprotocol/sdk/server/zod-compat.js';
|
|
4
4
|
import type { RequestTaskStore } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
5
|
-
import type
|
|
5
|
+
import { type IconInfo, type ToolExtra, type ToolResult } from './shared.js';
|
|
6
6
|
type TaskToolExtra = ToolExtra & {
|
|
7
7
|
taskId?: string;
|
|
8
8
|
taskStore?: RequestTaskStore;
|
|
9
9
|
taskRequestedTtl?: number | null;
|
|
10
10
|
};
|
|
11
|
-
type
|
|
12
|
-
|
|
13
|
-
export declare function
|
|
11
|
+
export type ToolSchema = ZodRawShapeCompat | AnySchema | undefined;
|
|
12
|
+
type ToolArgs<Args extends ToolSchema> = Args extends ZodRawShapeCompat ? ShapeOutput<Args> : Args extends AnySchema ? SchemaOutput<Args> : undefined;
|
|
13
|
+
export declare function registerToolTaskIfAvailable<Args extends ToolSchema, Result>(server: McpServer, toolName: string, toolDef: object, run: (args: ToolArgs<Args>, extra: TaskToolExtra) => Promise<ToolResult<Result>>, iconInfo: IconInfo | undefined, guard?: () => boolean): boolean;
|
|
14
|
+
interface TaskHandlerOptions {
|
|
14
15
|
guard?: () => boolean;
|
|
15
16
|
toolName?: string;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
cancelPollMs?: number;
|
|
18
|
+
pollIntervalMs?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function createToolTaskHandler<Result>(run: (args: undefined, extra: TaskToolExtra) => Promise<ToolResult<Result>>, options?: TaskHandlerOptions): ToolTaskHandler;
|
|
21
|
+
export declare function createToolTaskHandler<Args extends ZodRawShapeCompat | AnySchema, Result>(run: (args: ToolArgs<Args>, extra: TaskToolExtra) => Promise<ToolResult<Result>>, options?: TaskHandlerOptions): ToolTaskHandler<Args>;
|
|
21
22
|
export {};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { CallToolResultSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
1
2
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
3
|
import { channel } from 'node:diagnostics_channel';
|
|
3
4
|
import { performance } from 'node:perf_hooks';
|
|
4
5
|
import { format } from 'node:util';
|
|
5
|
-
import {
|
|
6
|
-
import { DEFAULT_TASK_TTL_MS, MAX_CONCURRENT_TASKS, MAX_TASK_TTL_MS, } from '../lib/constants.js';
|
|
6
|
+
import { DEFAULT_TASK_TTL_MS, MAX_CONCURRENT_TASKS, MAX_TASK_TTL_MS, TASK_CANCEL_POLL_MS, TASK_POLL_INTERVAL_MS, } from '../lib/constants.js';
|
|
7
7
|
import { ErrorCode, McpError } from '../lib/errors.js';
|
|
8
|
+
import { Logger } from '../lib/logger.js';
|
|
8
9
|
import { isRecord } from '../lib/utils.js';
|
|
9
10
|
import { buildToolErrorResponse, maybeStripStructuredContentFromResult, withDefaultIcons, } from './shared.js';
|
|
10
11
|
const taskContext = new AsyncLocalStorage();
|
|
@@ -50,6 +51,7 @@ function hasTaskToolCapability(server) {
|
|
|
50
51
|
isRecord(tasks.requests.tools.call));
|
|
51
52
|
}
|
|
52
53
|
const TASK_STATUS_NOTIFICATION_METHOD = 'notifications/tasks/status';
|
|
54
|
+
const TASK_CREATED_NOTIFICATION_METHOD = 'notifications/tasks/created';
|
|
53
55
|
function isRequestTaskStore(value) {
|
|
54
56
|
return (isRecord(value) &&
|
|
55
57
|
typeof value.createTask === 'function' &&
|
|
@@ -67,13 +69,13 @@ function isTaskExtra(value) {
|
|
|
67
69
|
}
|
|
68
70
|
function asCreateTaskExtra(value) {
|
|
69
71
|
if (!isCreateTaskExtra(value)) {
|
|
70
|
-
throw new McpError(ErrorCode.
|
|
72
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Task store not configured.');
|
|
71
73
|
}
|
|
72
74
|
return value;
|
|
73
75
|
}
|
|
74
76
|
function asTaskRequestExtra(value) {
|
|
75
77
|
if (!isTaskExtra(value)) {
|
|
76
|
-
throw new McpError(ErrorCode.
|
|
78
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Task id or store missing.');
|
|
77
79
|
}
|
|
78
80
|
return value;
|
|
79
81
|
}
|
|
@@ -90,11 +92,11 @@ function isTaskStatus(value) {
|
|
|
90
92
|
}
|
|
91
93
|
function normalizeGetTaskResult(value) {
|
|
92
94
|
if (!isRecord(value) || typeof value.taskId !== 'string') {
|
|
93
|
-
throw new McpError(ErrorCode.
|
|
95
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Invalid task object.');
|
|
94
96
|
}
|
|
95
97
|
const status = isTaskStatus(value.status) ? value.status : undefined;
|
|
96
98
|
if (!status) {
|
|
97
|
-
throw new McpError(ErrorCode.
|
|
99
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Invalid task status.');
|
|
98
100
|
}
|
|
99
101
|
const createdAt = typeof value.createdAt === 'string'
|
|
100
102
|
? value.createdAt
|
|
@@ -123,7 +125,7 @@ function normalizeCallToolResult(value) {
|
|
|
123
125
|
const parsed = CallToolResultSchema.safeParse(value);
|
|
124
126
|
if (parsed.success)
|
|
125
127
|
return parsed.data;
|
|
126
|
-
throw new McpError(ErrorCode.
|
|
128
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Invalid stored task result.');
|
|
127
129
|
}
|
|
128
130
|
function getToolResultErrorCode(result) {
|
|
129
131
|
if (!isRecord(result) || result.isError !== true)
|
|
@@ -143,7 +145,7 @@ function getToolResultErrorCode(result) {
|
|
|
143
145
|
return match ? match[1] : undefined;
|
|
144
146
|
}
|
|
145
147
|
function isCancelledToolResult(result) {
|
|
146
|
-
return getToolResultErrorCode(result) === ErrorCode.
|
|
148
|
+
return getToolResultErrorCode(result) === ErrorCode.CANCELLED;
|
|
147
149
|
}
|
|
148
150
|
function resolveTaskResultStatuses(result) {
|
|
149
151
|
if (isCancelledToolResult(result)) {
|
|
@@ -192,6 +194,31 @@ function buildTaskStatusNotificationParams(task) {
|
|
|
192
194
|
params.statusMessage = task.statusMessage;
|
|
193
195
|
return params;
|
|
194
196
|
}
|
|
197
|
+
async function notifyTaskCreatedIfPossible(extra, taskId, toolName) {
|
|
198
|
+
const { sendNotification } = extra;
|
|
199
|
+
if (typeof sendNotification !== 'function')
|
|
200
|
+
return;
|
|
201
|
+
const notify = sendNotification;
|
|
202
|
+
try {
|
|
203
|
+
await notify({
|
|
204
|
+
method: TASK_CREATED_NOTIFICATION_METHOD,
|
|
205
|
+
params: {
|
|
206
|
+
_meta: {
|
|
207
|
+
'modelcontextprotocol.io/related-task': {
|
|
208
|
+
taskId,
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
publishTaskDiagnostics({
|
|
216
|
+
phase: 'task_status_notify_failed',
|
|
217
|
+
taskId,
|
|
218
|
+
...(toolName ? { toolName } : {}),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
195
222
|
async function notifyTaskStatusIfPossible(extra, taskStore, taskId, toolName) {
|
|
196
223
|
const { sendNotification } = extra;
|
|
197
224
|
if (typeof sendNotification !== 'function')
|
|
@@ -222,13 +249,13 @@ async function notifyTaskStatusIfPossible(extra, taskStore, taskId, toolName) {
|
|
|
222
249
|
}
|
|
223
250
|
function getTaskStore(extra) {
|
|
224
251
|
if (!extra.taskStore) {
|
|
225
|
-
throw new McpError(ErrorCode.
|
|
252
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Task store not configured.');
|
|
226
253
|
}
|
|
227
254
|
return extra.taskStore;
|
|
228
255
|
}
|
|
229
256
|
function getTaskId(extra) {
|
|
230
257
|
if (!extra.taskId) {
|
|
231
|
-
throw new McpError(ErrorCode.
|
|
258
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Task id missing.');
|
|
232
259
|
}
|
|
233
260
|
return extra.taskId;
|
|
234
261
|
}
|
|
@@ -260,6 +287,10 @@ async function isTaskAlreadyTerminal(taskStore, taskId) {
|
|
|
260
287
|
return false;
|
|
261
288
|
}
|
|
262
289
|
}
|
|
290
|
+
function isMissingTaskStoreError(error) {
|
|
291
|
+
return (error instanceof Error &&
|
|
292
|
+
/Task .*not found|not found after cancellation/iu.test(error.message));
|
|
293
|
+
}
|
|
263
294
|
async function countActiveTasks(taskStore) {
|
|
264
295
|
if (typeof taskStore.listTasks !== 'function')
|
|
265
296
|
return 0;
|
|
@@ -281,11 +312,11 @@ function resolveRequestedTaskTtl(requestedTtl) {
|
|
|
281
312
|
if (requestedTtl == null)
|
|
282
313
|
return DEFAULT_TASK_TTL_MS;
|
|
283
314
|
if (!Number.isFinite(requestedTtl)) {
|
|
284
|
-
throw new McpError(ErrorCode.
|
|
315
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Task TTL must be finite.');
|
|
285
316
|
}
|
|
286
317
|
const normalized = Math.trunc(requestedTtl);
|
|
287
318
|
if (normalized <= 0) {
|
|
288
|
-
throw new McpError(ErrorCode.
|
|
319
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Task TTL must be > 0.');
|
|
289
320
|
}
|
|
290
321
|
return Math.min(normalized, MAX_TASK_TTL_MS);
|
|
291
322
|
}
|
|
@@ -294,18 +325,40 @@ async function safelyStoreTaskResult(taskStore, taskId, storedStatus, result) {
|
|
|
294
325
|
await taskStore.storeTaskResult(taskId, storedStatus, result);
|
|
295
326
|
}
|
|
296
327
|
catch (error) {
|
|
328
|
+
if (isMissingTaskStoreError(error))
|
|
329
|
+
return;
|
|
297
330
|
// If task was already cancelled/failed by another process, ignore the write error
|
|
298
331
|
if (await isTaskAlreadyTerminal(taskStore, taskId))
|
|
299
332
|
return;
|
|
300
333
|
throw error;
|
|
301
334
|
}
|
|
302
335
|
}
|
|
303
|
-
async function
|
|
336
|
+
async function isTaskCancelled(taskStore, taskId) {
|
|
337
|
+
try {
|
|
338
|
+
const task = await taskStore.getTask(taskId);
|
|
339
|
+
return isRecord(task) && task.status === 'cancelled';
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
async function runTaskInBackground(run, args, extra, taskStore, taskId, toolName, cancelPollMs) {
|
|
346
|
+
// Create a dedicated AbortController for background execution.
|
|
347
|
+
// The original request signal is stale once createTask returns.
|
|
348
|
+
const taskAbort = new AbortController();
|
|
349
|
+
const taskExtra = { ...extra, signal: taskAbort.signal };
|
|
350
|
+
// Poll the task store for client-initiated cancellation.
|
|
351
|
+
const cancelPoller = setInterval(() => {
|
|
352
|
+
void isTaskCancelled(taskStore, taskId).then((cancelled) => {
|
|
353
|
+
if (cancelled)
|
|
354
|
+
taskAbort.abort(new Error('Task cancelled by client'));
|
|
355
|
+
});
|
|
356
|
+
}, cancelPollMs ?? TASK_CANCEL_POLL_MS);
|
|
304
357
|
const start = performance.now();
|
|
305
358
|
let taskStatuses;
|
|
306
359
|
let result;
|
|
307
360
|
try {
|
|
308
|
-
const rawResult = await taskContext.run({ taskId, toolName, startTime: start }, () => run(args,
|
|
361
|
+
const rawResult = await taskContext.run({ taskId, toolName, startTime: start }, () => run(args, taskExtra));
|
|
309
362
|
taskStatuses = resolveTaskResultStatuses(rawResult);
|
|
310
363
|
result = isErrorResult(rawResult)
|
|
311
364
|
? withoutStructuredContent(rawResult)
|
|
@@ -313,7 +366,10 @@ async function runTaskInBackground(run, args, extra, taskStore, taskId, toolName
|
|
|
313
366
|
}
|
|
314
367
|
catch (error) {
|
|
315
368
|
taskStatuses = { storedStatus: 'failed', reportedStatus: 'failed' };
|
|
316
|
-
result = maybeStripStructuredContentFromResult(buildToolErrorResponse(error, ErrorCode.
|
|
369
|
+
result = maybeStripStructuredContentFromResult(buildToolErrorResponse(error, ErrorCode.UNKNOWN));
|
|
370
|
+
}
|
|
371
|
+
finally {
|
|
372
|
+
clearInterval(cancelPoller);
|
|
317
373
|
}
|
|
318
374
|
const durationMs = performance.now() - start;
|
|
319
375
|
try {
|
|
@@ -328,7 +384,7 @@ async function runTaskInBackground(run, args, extra, taskStore, taskId, toolName
|
|
|
328
384
|
await notifyTaskStatusIfPossible(extra, taskStore, taskId, toolName);
|
|
329
385
|
}
|
|
330
386
|
catch (innerError) {
|
|
331
|
-
|
|
387
|
+
Logger.error(format('Failed to store task result for task %s:', taskId), innerError);
|
|
332
388
|
const syntheticTask = {
|
|
333
389
|
taskId,
|
|
334
390
|
status: taskStatuses.reportedStatus === 'cancelled' ? 'cancelled' : 'failed',
|
|
@@ -339,10 +395,16 @@ async function runTaskInBackground(run, args, extra, taskStore, taskId, toolName
|
|
|
339
395
|
};
|
|
340
396
|
const { sendNotification } = extra;
|
|
341
397
|
if (typeof sendNotification === 'function') {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
398
|
+
try {
|
|
399
|
+
await sendNotification({
|
|
400
|
+
method: TASK_STATUS_NOTIFICATION_METHOD,
|
|
401
|
+
params: buildTaskStatusNotificationParams(syntheticTask),
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
// Best effort only: transport may already be closed while a cancelled
|
|
406
|
+
// task is still unwinding in the background.
|
|
407
|
+
}
|
|
346
408
|
}
|
|
347
409
|
}
|
|
348
410
|
}
|
|
@@ -379,15 +441,23 @@ export function createToolTaskHandler(run, options) {
|
|
|
379
441
|
const extra = asCreateTaskExtra(maybeExtra ?? argsOrExtra);
|
|
380
442
|
const args = (maybeExtra ? argsOrExtra : undefined);
|
|
381
443
|
if (options?.guard && !options.guard()) {
|
|
382
|
-
throw new McpError(ErrorCode.
|
|
444
|
+
throw new McpError(ErrorCode.INVALID_INPUT, 'Client not initialized; wait for notifications/initialized');
|
|
383
445
|
}
|
|
384
446
|
const taskStore = getTaskStore(extra);
|
|
385
447
|
if ((await countActiveTasks(taskStore)) >= MAX_CONCURRENT_TASKS) {
|
|
386
|
-
throw new McpError(ErrorCode.
|
|
448
|
+
throw new McpError(ErrorCode.INVALID_INPUT, `Too many active tasks (limit: ${String(MAX_CONCURRENT_TASKS)}).`);
|
|
387
449
|
}
|
|
388
450
|
const task = await taskStore.createTask({
|
|
389
451
|
ttl: resolveRequestedTaskTtl(extra.taskRequestedTtl),
|
|
452
|
+
pollInterval: options?.pollIntervalMs ?? TASK_POLL_INTERVAL_MS,
|
|
390
453
|
});
|
|
454
|
+
const toolLabel = options?.toolName ?? 'tool';
|
|
455
|
+
try {
|
|
456
|
+
await taskStore.updateTaskStatus(task.taskId, 'working', `${toolLabel}: starting`);
|
|
457
|
+
}
|
|
458
|
+
catch {
|
|
459
|
+
// Best effort — status message is informational.
|
|
460
|
+
}
|
|
391
461
|
publishTaskDiagnostics({
|
|
392
462
|
phase: 'task_created',
|
|
393
463
|
taskId: task.taskId,
|
|
@@ -399,8 +469,9 @@ export function createToolTaskHandler(run, options) {
|
|
|
399
469
|
taskStore,
|
|
400
470
|
taskId: task.taskId,
|
|
401
471
|
};
|
|
472
|
+
void notifyTaskCreatedIfPossible(taskExtra, task.taskId, options?.toolName);
|
|
402
473
|
void notifyTaskStatusIfPossible(taskExtra, taskStore, task.taskId, options?.toolName);
|
|
403
|
-
void runTaskInBackground(run, args, taskExtra, taskStore, task.taskId, options?.toolName);
|
|
474
|
+
void runTaskInBackground(run, args, taskExtra, taskStore, task.taskId, options?.toolName, options?.cancelPollMs);
|
|
404
475
|
return { task };
|
|
405
476
|
});
|
|
406
477
|
const getTask = (async (argsOrExtra, maybeExtra) => {
|
package/dist/tools/tree.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
2
|
import { DEFAULT_SEARCH_TIMEOUT_MS } from '../lib/constants.js';
|
|
3
3
|
import { ErrorCode } from '../lib/errors.js';
|
|
4
4
|
import { formatTreeAscii, treeDirectory, } from '../lib/file-operations/metadata.js';
|
|
@@ -48,7 +48,7 @@ export function registerTreeTool(server, options = {}) {
|
|
|
48
48
|
timedSignal: { timeoutMs: DEFAULT_SEARCH_TIMEOUT_MS },
|
|
49
49
|
context: { path: targetPath },
|
|
50
50
|
run: async (signal) => {
|
|
51
|
-
const context = args.path ?
|
|
51
|
+
const context = args.path ? basename(args.path) : '.';
|
|
52
52
|
let progressCursor = 0;
|
|
53
53
|
const knownTotal = args.maxEntries;
|
|
54
54
|
notifyProgress(extra, {
|
|
@@ -93,7 +93,7 @@ export function registerTreeTool(server, options = {}) {
|
|
|
93
93
|
throw error;
|
|
94
94
|
}
|
|
95
95
|
},
|
|
96
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
96
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.NOT_DIRECTORY, targetPath),
|
|
97
97
|
});
|
|
98
98
|
};
|
|
99
99
|
const wrappedHandler = wrapToolHandler(handler, {
|
package/dist/tools/write-file.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { mkdir } from 'node:fs/promises';
|
|
2
|
+
import { basename, dirname } from 'node:path';
|
|
3
|
+
import { withAbort } from '../lib/abort.js';
|
|
3
4
|
import { ErrorCode } from '../lib/errors.js';
|
|
4
|
-
import { atomicWriteFile
|
|
5
|
+
import { atomicWriteFile } from '../lib/fs-helpers.js';
|
|
6
|
+
import { Logger } from '../lib/logger.js';
|
|
5
7
|
import { validatePathForWrite } from '../lib/paths.js';
|
|
6
8
|
import { formatBytes } from '../config.js';
|
|
7
9
|
import { WriteFileInputSchema, WriteFileOutputSchema } from '../schemas.js';
|
|
@@ -19,9 +21,10 @@ export const WRITE_FILE_TOOL = {
|
|
|
19
21
|
async function handleWriteFile(args, signal) {
|
|
20
22
|
const validPath = await validatePathForWrite(args.path, signal);
|
|
21
23
|
// Ensure parent directory exists
|
|
22
|
-
await withAbort(
|
|
24
|
+
await withAbort(mkdir(dirname(validPath), { recursive: true }), signal);
|
|
23
25
|
await atomicWriteFile(validPath, args.content, { encoding: 'utf-8', signal });
|
|
24
26
|
const bytesWritten = Buffer.byteLength(args.content, 'utf-8');
|
|
27
|
+
Logger.info(`write: ${args.path} (${bytesWritten} bytes)`);
|
|
25
28
|
return buildToolResponse(`Successfully wrote to file: ${args.path}`, {
|
|
26
29
|
ok: true,
|
|
27
30
|
path: validPath,
|
|
@@ -36,13 +39,13 @@ export function registerWriteFileTool(server, options = {}) {
|
|
|
36
39
|
timedSignal: {},
|
|
37
40
|
context: { path: args.path },
|
|
38
41
|
run: (signal) => handleWriteFile(args, signal),
|
|
39
|
-
onError: (error) => buildToolErrorResponse(error, ErrorCode.
|
|
42
|
+
onError: (error) => buildToolErrorResponse(error, ErrorCode.UNKNOWN, args.path),
|
|
40
43
|
});
|
|
41
44
|
const wrappedHandler = wrapToolHandler(handler, {
|
|
42
45
|
guard: options.isInitialized,
|
|
43
|
-
progressMessage: (args) => `🛠 write: ${
|
|
46
|
+
progressMessage: (args) => `🛠 write: ${basename(args.path)}`,
|
|
44
47
|
completionMessage: (args, result) => {
|
|
45
|
-
const name =
|
|
48
|
+
const name = basename(args.path);
|
|
46
49
|
if (result.isError)
|
|
47
50
|
return `🛠 write: ${name} • failed`;
|
|
48
51
|
const sc = result.structuredContent;
|