@j0hanz/filesystem-mcp 2.7.5 → 2.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 +290 -394
- package/dist/cli-help.d.ts.map +1 -1
- package/dist/cli-help.js +15 -7
- package/dist/cli-help.js.map +1 -1
- package/dist/cli.d.ts +1 -9
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/core/concurrency.d.ts +7 -0
- package/dist/core/concurrency.d.ts.map +1 -1
- package/dist/core/concurrency.js +19 -0
- package/dist/core/concurrency.js.map +1 -1
- package/dist/core/errors.d.ts +4 -3
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +5 -16
- package/dist/core/errors.js.map +1 -1
- package/dist/core/fs.d.ts +19 -11
- package/dist/core/fs.d.ts.map +1 -1
- package/dist/core/fs.js +168 -53
- package/dist/core/fs.js.map +1 -1
- package/dist/core/glob.d.ts +8 -0
- package/dist/core/glob.d.ts.map +1 -1
- package/dist/core/glob.js +192 -29
- package/dist/core/glob.js.map +1 -1
- package/dist/core/input-required.d.ts +12 -0
- package/dist/core/input-required.d.ts.map +1 -1
- package/dist/core/input-required.js +7 -2
- package/dist/core/input-required.js.map +1 -1
- package/dist/core/observability.d.ts +2 -0
- package/dist/core/observability.d.ts.map +1 -1
- package/dist/core/observability.js +7 -0
- package/dist/core/observability.js.map +1 -1
- package/dist/core/path-completer.d.ts.map +1 -1
- package/dist/core/path-completer.js +11 -30
- package/dist/core/path-completer.js.map +1 -1
- package/dist/core/path.d.ts +7 -18
- package/dist/core/path.d.ts.map +1 -1
- package/dist/core/path.js +22 -55
- package/dist/core/path.js.map +1 -1
- package/dist/core/read.d.ts +4 -1
- package/dist/core/read.d.ts.map +1 -1
- package/dist/core/read.js +25 -16
- package/dist/core/read.js.map +1 -1
- package/dist/core/schema.d.ts +6 -1
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +17 -3
- package/dist/core/schema.js.map +1 -1
- package/dist/core/search.d.ts +7 -0
- package/dist/core/search.d.ts.map +1 -1
- package/dist/core/search.js +154 -66
- package/dist/core/search.js.map +1 -1
- package/dist/core/store.d.ts +36 -1
- package/dist/core/store.d.ts.map +1 -1
- package/dist/core/store.js +71 -6
- package/dist/core/store.js.map +1 -1
- package/dist/core/util.d.ts +37 -0
- package/dist/core/util.d.ts.map +1 -1
- package/dist/core/util.js +62 -0
- package/dist/core/util.js.map +1 -1
- package/dist/index.js +5 -16
- package/dist/index.js.map +1 -1
- package/dist/resources.d.ts +5 -1
- package/dist/resources.d.ts.map +1 -1
- package/dist/resources.js +45 -22
- package/dist/resources.js.map +1 -1
- package/dist/server.d.ts +10 -5
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +8 -8
- package/dist/server.js.map +1 -1
- package/dist/tools/create.d.ts.map +1 -1
- package/dist/tools/create.js +93 -90
- package/dist/tools/create.js.map +1 -1
- package/dist/tools/define.d.ts +10 -6
- package/dist/tools/define.d.ts.map +1 -1
- package/dist/tools/define.js +49 -16
- package/dist/tools/define.js.map +1 -1
- package/dist/tools/delete.d.ts.map +1 -1
- package/dist/tools/delete.js +39 -21
- package/dist/tools/delete.js.map +1 -1
- package/dist/tools/edit.d.ts.map +1 -1
- package/dist/tools/edit.js +37 -39
- package/dist/tools/edit.js.map +1 -1
- package/dist/tools/find-files.d.ts.map +1 -1
- package/dist/tools/find-files.js +49 -60
- package/dist/tools/find-files.js.map +1 -1
- package/dist/tools/index.d.ts +2 -10
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +1 -8
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/list.d.ts.map +1 -1
- package/dist/tools/list.js +59 -69
- package/dist/tools/list.js.map +1 -1
- package/dist/tools/move.d.ts.map +1 -1
- package/dist/tools/move.js +39 -5
- package/dist/tools/move.js.map +1 -1
- package/dist/tools/patch.d.ts.map +1 -1
- package/dist/tools/patch.js +88 -86
- package/dist/tools/patch.js.map +1 -1
- package/dist/tools/read.d.ts.map +1 -1
- package/dist/tools/read.js +30 -24
- package/dist/tools/read.js.map +1 -1
- package/dist/tools/replace-text.d.ts.map +1 -1
- package/dist/tools/replace-text.js +60 -46
- package/dist/tools/replace-text.js.map +1 -1
- package/dist/tools/search-text.d.ts.map +1 -1
- package/dist/tools/search-text.js +48 -64
- package/dist/tools/search-text.js.map +1 -1
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +2 -1
- package/dist/transport/http.js.map +1 -1
- package/dist/transport/shared.d.ts +7 -5
- package/dist/transport/shared.d.ts.map +1 -1
- package/dist/transport/shared.js +8 -2
- package/dist/transport/shared.js.map +1 -1
- package/dist/transport/stdio.d.ts +4 -1
- package/dist/transport/stdio.d.ts.map +1 -1
- package/dist/transport/stdio.js +46 -12
- package/dist/transport/stdio.js.map +1 -1
- package/package.json +3 -2
package/dist/tools/create.js
CHANGED
|
@@ -3,9 +3,9 @@ import * as z from 'zod/v4';
|
|
|
3
3
|
import { processInParallel } from "../core/concurrency.js";
|
|
4
4
|
import { ErrorCode, formatUnknownErrorMessage, FsError, rethrowIfAborted, } from "../core/errors.js";
|
|
5
5
|
import { buildWrittenFileMeta, writtenFileLinks } from "../core/file-uri.js";
|
|
6
|
-
import {
|
|
6
|
+
import { destExists } from "../core/fs.js";
|
|
7
7
|
import { confirmKey, describeRefusal, pendingRoundTrip, readAcceptedChoice, } from "../core/input-required.js";
|
|
8
|
-
import { detectMimeFromContent
|
|
8
|
+
import { detectMimeFromContent } from "../core/mime.js";
|
|
9
9
|
import { isSamePath } from "../core/path-utils.js";
|
|
10
10
|
import { RequiredPath } from "../core/schema.js";
|
|
11
11
|
import { getMaxTextFileSize, PARALLEL_CONCURRENCY } from "../core/util.js";
|
|
@@ -116,7 +116,7 @@ export const CREATE = defineTool({
|
|
|
116
116
|
if (round !== undefined)
|
|
117
117
|
return round;
|
|
118
118
|
}
|
|
119
|
-
const batch = await runOverPaths(args.files, ctx, ErrorCode.UNKNOWN, async ({ path, content, append }) => {
|
|
119
|
+
const batch = await runOverPaths(args.files, ctx, ErrorCode.UNKNOWN, async ({ path, content, append, overwrite }) => {
|
|
120
120
|
const pendingPath = pendingByPath.get(path);
|
|
121
121
|
if (pendingPath !== undefined) {
|
|
122
122
|
const key = confirmKey(pendingSorted.indexOf(pendingPath));
|
|
@@ -127,90 +127,18 @@ export const CREATE = defineTool({
|
|
|
127
127
|
throw new FsError(ErrorCode.CANCELLED, `create cancelled: overwrite of "${path}" was ${describeRefusal(ctx, key)}`, path);
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
-
|
|
131
|
-
//
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
let meta;
|
|
140
|
-
let created;
|
|
141
|
-
let modified;
|
|
142
|
-
if (append) {
|
|
143
|
-
const appended = await ctx.fs.appendFile(path, content, {
|
|
144
|
-
signal: ctx.signal,
|
|
145
|
-
});
|
|
146
|
-
validPath = appended.validPath;
|
|
147
|
-
// The append is the commit point: the bytes are on disk, so
|
|
148
|
-
// everything below is best-effort RESULT METADATA, not part of the
|
|
149
|
-
// write. It runs unwired to ctx.signal, and a metadata failure
|
|
150
|
-
// degrades the result instead of failing it — either would report
|
|
151
|
-
// the append as failed and a client retry would append twice. The
|
|
152
|
-
// stat covers the committed validPath, never the input path: a
|
|
153
|
-
// symlink there could have been swapped between write and stat,
|
|
154
|
-
// and the result must describe the file the bytes landed in.
|
|
155
|
-
let stats;
|
|
156
|
-
let mimeType;
|
|
157
|
-
let kind;
|
|
158
|
-
let lineCount = 0;
|
|
159
|
-
try {
|
|
160
|
-
stats = (await ctx.fs.stat(appended.validPath)).stats;
|
|
161
|
-
// Sniff the resulting file's leading bytes, not the appended
|
|
162
|
-
// chunk: text appended to a binary file is still a binary file.
|
|
163
|
-
const sample = await ctx.fs.readLeadingSample(appended.validPath, MIME_SAMPLE_SIZE);
|
|
164
|
-
const mimeInfo = detectMimeFromContent(appended.validPath, sample);
|
|
165
|
-
mimeType = mimeInfo.mimeType;
|
|
166
|
-
kind = mimeInfo.kind;
|
|
167
|
-
lineCount = await countFileLines(appended.validPath);
|
|
130
|
+
// Lock per resolved file so two entries naming one file (e.g. through a
|
|
131
|
+
// symlinked parent) cannot clobber each other.
|
|
132
|
+
return ctx.fs.lockForWrite(path, async (resolvedTarget) => {
|
|
133
|
+
// The file may have appeared since the overwrite set was planned.
|
|
134
|
+
if (!append &&
|
|
135
|
+
!overwrite &&
|
|
136
|
+
pendingPath === undefined &&
|
|
137
|
+
(await destExists(ctx.fs, resolvedTarget, 'create'))) {
|
|
138
|
+
throw new FsError(ErrorCode.INVALID_INPUT, `create refused: "${path}" appeared after overwrite confirmation was checked`, path);
|
|
168
139
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
// A POSIX mode-0222 file appends fine but cannot be opened 'r';
|
|
172
|
-
// fall back to the chunk for MIME (an approximation) and report
|
|
173
|
-
// the line count as unknown-as-zero rather than fail the append.
|
|
174
|
-
const mimeInfo = detectMimeFromContent(appended.validPath, content);
|
|
175
|
-
mimeType = mimeInfo.mimeType;
|
|
176
|
-
kind = mimeInfo.kind;
|
|
177
|
-
}
|
|
178
|
-
// A failed stat leaves the size unknown, so nothing is advertised.
|
|
179
|
-
meta = {
|
|
180
|
-
size: stats?.size ?? 0,
|
|
181
|
-
lineCount,
|
|
182
|
-
mimeType,
|
|
183
|
-
kind,
|
|
184
|
-
...writtenFileLinks(appended.validPath, mimeType, stats?.size, false, stats?.mtime.toISOString()),
|
|
185
|
-
};
|
|
186
|
-
created = (stats?.birthtime ?? EPOCH).toISOString();
|
|
187
|
-
modified = (stats?.mtime ?? EPOCH).toISOString();
|
|
188
|
-
}
|
|
189
|
-
else {
|
|
190
|
-
const written = await ctx.fs.writeFile(path, content, {
|
|
191
|
-
signal: ctx.signal,
|
|
192
|
-
});
|
|
193
|
-
validPath = written.validPath;
|
|
194
|
-
const fileStats = (await ctx.fs.stat(path, { signal: ctx.signal })).stats;
|
|
195
|
-
created = fileStats.birthtime.toISOString();
|
|
196
|
-
modified = fileStats.mtime.toISOString();
|
|
197
|
-
meta = buildWrittenFileMeta({
|
|
198
|
-
validPath: written.validPath,
|
|
199
|
-
content,
|
|
200
|
-
lastModified: modified,
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
const file = {
|
|
204
|
-
path: validPath,
|
|
205
|
-
size: meta.size,
|
|
206
|
-
lineCount: meta.lineCount,
|
|
207
|
-
mimeType: meta.mimeType,
|
|
208
|
-
kind: meta.kind,
|
|
209
|
-
resourceUri: meta.resourceUri,
|
|
210
|
-
created,
|
|
211
|
-
modified,
|
|
212
|
-
};
|
|
213
|
-
return meta.resourceLink ? { file, resourceLink: meta.resourceLink } : { file };
|
|
140
|
+
return createOne(path, content, append, ctx);
|
|
141
|
+
});
|
|
214
142
|
});
|
|
215
143
|
const results = [];
|
|
216
144
|
const failures = [];
|
|
@@ -238,10 +166,7 @@ export const CREATE = defineTool({
|
|
|
238
166
|
// paths and dropped every per-file size, line count and error the caller
|
|
239
167
|
// asked `create` for. Supplying no text makes this a data tool, so
|
|
240
168
|
// `define.ts` renders the JSON and keeps it in `structuredContent`.
|
|
241
|
-
|
|
242
|
-
return { structured, resources: links, isError: isTotalFailure(batch.summary) };
|
|
243
|
-
}
|
|
244
|
-
return { structured, isError: isTotalFailure(batch.summary) };
|
|
169
|
+
return { structured, resources: links, isError: isTotalFailure(batch.summary) };
|
|
245
170
|
},
|
|
246
171
|
progress: (args) => ({
|
|
247
172
|
label: 'Create',
|
|
@@ -250,4 +175,82 @@ export const CREATE = defineTool({
|
|
|
250
175
|
: `${String(args.files.length)} files`,
|
|
251
176
|
}),
|
|
252
177
|
});
|
|
178
|
+
async function createOne(path, content, append, ctx) {
|
|
179
|
+
await ctx.fs.mkdir(dirname(path), { recursive: true });
|
|
180
|
+
// Overwrite: file content == `content`, so content-derived meta is
|
|
181
|
+
// exact and the atomic temp+rename write protects the existing mode.
|
|
182
|
+
// Append: the resulting file is everything that was there plus
|
|
183
|
+
// `content`, so size/created/modified come from a real post-append
|
|
184
|
+
// stat, MIME from the resulting file's leading bytes, and the line
|
|
185
|
+
// count streams the file — reading a multi-GB log back whole is the
|
|
186
|
+
// round-trip append exists to avoid.
|
|
187
|
+
let validPath;
|
|
188
|
+
let meta;
|
|
189
|
+
let created;
|
|
190
|
+
let modified;
|
|
191
|
+
if (append) {
|
|
192
|
+
const appended = await ctx.fs.appendFile(path, content, {
|
|
193
|
+
signal: ctx.signal,
|
|
194
|
+
});
|
|
195
|
+
validPath = appended.validPath;
|
|
196
|
+
// The append is the commit point: the bytes are on disk, so
|
|
197
|
+
// everything below is best-effort RESULT METADATA, not part of the
|
|
198
|
+
// write. It runs unwired to ctx.signal, and a metadata failure
|
|
199
|
+
// degrades the result instead of failing it — either would report
|
|
200
|
+
// the append as failed and a client retry would append twice. The
|
|
201
|
+
// stat covers the committed validPath, never the input path: a
|
|
202
|
+
// symlink there could have been swapped between write and stat,
|
|
203
|
+
// and the result must describe the file the bytes landed in.
|
|
204
|
+
let stats;
|
|
205
|
+
let mimeType;
|
|
206
|
+
let kind;
|
|
207
|
+
let lineCount = 0;
|
|
208
|
+
try {
|
|
209
|
+
stats = (await ctx.fs.stat(appended.validPath)).stats;
|
|
210
|
+
// Sniff the resulting file's leading bytes, not the appended
|
|
211
|
+
// chunk: text appended to a binary file is still a binary file.
|
|
212
|
+
const sample = await ctx.fs.readLeadingSample(appended.validPath);
|
|
213
|
+
const mimeInfo = detectMimeFromContent(appended.validPath, sample);
|
|
214
|
+
mimeType = mimeInfo.mimeType;
|
|
215
|
+
kind = mimeInfo.kind;
|
|
216
|
+
lineCount = await ctx.fs.countFileLines(appended.validPath);
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
ctx.log('warning', `create append: result metadata degraded for ${appended.validPath}: ${formatUnknownErrorMessage(error)}`, 'create');
|
|
220
|
+
// A POSIX mode-0222 file appends fine but cannot be opened 'r';
|
|
221
|
+
// fall back to the chunk for MIME (an approximation) and report
|
|
222
|
+
// the line count as unknown-as-zero rather than fail the append.
|
|
223
|
+
const mimeInfo = detectMimeFromContent(appended.validPath, content);
|
|
224
|
+
mimeType = mimeInfo.mimeType;
|
|
225
|
+
kind = mimeInfo.kind;
|
|
226
|
+
}
|
|
227
|
+
// A failed stat leaves the size unknown, so nothing is advertised.
|
|
228
|
+
meta = {
|
|
229
|
+
size: stats?.size ?? 0,
|
|
230
|
+
lineCount,
|
|
231
|
+
mimeType,
|
|
232
|
+
kind,
|
|
233
|
+
...writtenFileLinks(appended.validPath, mimeType, stats?.size, false, stats?.mtime.toISOString()),
|
|
234
|
+
};
|
|
235
|
+
created = (stats?.birthtime ?? EPOCH).toISOString();
|
|
236
|
+
modified = (stats?.mtime ?? EPOCH).toISOString();
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
const written = await ctx.fs.writeFile(path, content, {
|
|
240
|
+
signal: ctx.signal,
|
|
241
|
+
});
|
|
242
|
+
validPath = written.validPath;
|
|
243
|
+
const fileStats = (await ctx.fs.stat(path, { signal: ctx.signal })).stats;
|
|
244
|
+
created = fileStats.birthtime.toISOString();
|
|
245
|
+
modified = fileStats.mtime.toISOString();
|
|
246
|
+
meta = buildWrittenFileMeta({
|
|
247
|
+
validPath: written.validPath,
|
|
248
|
+
content,
|
|
249
|
+
lastModified: modified,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
const { resourceLink, ...fileMeta } = meta;
|
|
253
|
+
const file = { path: validPath, ...fileMeta, created, modified };
|
|
254
|
+
return resourceLink ? { file, resourceLink } : { file };
|
|
255
|
+
}
|
|
253
256
|
//# sourceMappingURL=create.js.map
|
package/dist/tools/create.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.js","sourceRoot":"","sources":["../../src/tools/create.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,E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,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAC3C,CAAC;QACF,yEAAyE;QACzE,yEAAyE;QACzE,mEAAmE;QACnE,oEAAoE;QACpE,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACrB,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,cAAc,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;QAClF,CAAC;QAED,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,cAAc,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;IAChE,CAAC;IACD,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACnB,KAAK,EAAE,QAAQ;QACf,OAAO,EACL,IAAI,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC;YACrB,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,IAAI,EAAE,CAAC;YACrC,CAAC,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ;KAC3C,CAAC;CACH,CAAC,CAAC","sourcesContent":["import type { ContentBlock } from '@modelcontextprotocol/server';\n\nimport { basename, dirname } from 'node:path';\n\nimport * as z from 'zod/v4';\n\nimport { processInParallel } from '../core/concurrency.ts';\nimport {\n ErrorCode,\n formatUnknownErrorMessage,\n FsError,\n type Problem,\n rethrowIfAborted,\n} from '../core/errors.ts';\nimport { buildWrittenFileMeta, writtenFileLinks, type WrittenFileMeta } from '../core/file-uri.ts';\nimport { countFileLines, destExists, type Stats } from '../core/fs.ts';\nimport {\n confirmKey,\n describeRefusal,\n pendingRoundTrip,\n readAcceptedChoice,\n} from '../core/input-required.ts';\nimport { detectMimeFromContent, type FileKind, MIME_SAMPLE_SIZE } from '../core/mime.ts';\nimport { isSamePath } from '../core/path-utils.ts';\nimport { RequiredPath } from '../core/schema.ts';\nimport { getMaxTextFileSize, PARALLEL_CONCURRENCY } from '../core/util.ts';\nimport { isTotalFailure, runOverPaths } from './batch.ts';\nimport { defineTool } from './define.ts';\n\nconst EPOCH = new Date(0);\n\nconst CreateFileItemSchema = z.strictObject({\n path: RequiredPath.describe('Absolute path where the file will be created'),\n content: z\n .string()\n .refine((val) => val.length <= getMaxTextFileSize(), {\n message: 'Content exceeds maximum allowed text file size',\n })\n .describe('Text content to write, verbatim.'),\n append: z\n .boolean()\n .optional()\n .describe(\n 'Append the content to the end of the file instead of overwriting; creates the file if it does not exist',\n ),\n overwrite: z\n .boolean()\n .optional()\n .describe(\n 'Replace an existing file without asking the user; without it an existing file prompts for confirmation',\n ),\n});\n\ntype CreateFileResult = Omit<WrittenFileMeta, 'resourceLink'> & {\n path: string;\n created: string;\n modified: string;\n};\n\nconst CreateInputSchema = z\n .strictObject({\n files: z\n .array(CreateFileItemSchema)\n .min(1)\n .max(100)\n .describe(\n 'List of files to create (max 100); each entry requires path and content; combined content is capped at the file-size limit',\n ),\n })\n .superRefine((value, ctx) => {\n // Entries write in parallel and each replaces its file whole, so two\n // entries for one path race: the last rename wins and the other's\n // content is lost while both report success. One entry per file.\n for (const [index, file] of value.files.entries()) {\n const first = value.files.findIndex((other) => isSamePath(other.path, file.path));\n if (first < index) {\n ctx.addIssue({\n code: 'custom',\n path: ['files', index, 'path'],\n message: `duplicate of files[${String(first)}].path; one entry per file`,\n input: value,\n });\n }\n }\n // One call's content is what one inbound message carries; the transports\n // size their message bound from this same limit (getMaxInboundMessageBytes),\n // so a batch that passes here always fits on the wire. Counted in the same\n // UTF-16 units as the per-file refine above.\n const total = value.files.reduce((sum, file) => sum + file.content.length, 0);\n const limit = getMaxTextFileSize();\n if (total > limit) {\n ctx.addIssue({\n code: 'custom',\n path: ['files'],\n message: `combined content is ${String(total)} characters; one create call carries at most ${String(limit)} (FS_MAX_FILE_SIZE). Split the batch.`,\n input: value,\n });\n }\n });\n\ninterface CreateFailureItem {\n path: string;\n error: Problem;\n}\n\ninterface CreateOutput {\n files: CreateFileResult[];\n failures?: CreateFailureItem[];\n skipped?: string[];\n}\n\nexport const CREATE = defineTool<typeof CreateInputSchema, CreateOutput>({\n name: 'create',\n title: 'Create Files',\n description:\n 'Write whole text files: create new ones, overwrite, or append. Missing parent directories are created. ' +\n 'edit or patch changes part of an existing file.',\n input: CreateInputSchema,\n readOnlyHint: false,\n accessPaths: (args) => args.files.map((f) => f.path),\n run: async (args, ctx) => {\n // Phase 1 (no mutation): which entries would replace a file that exists\n // right now? Those need the user's word before anything is written (R14).\n // append never destroys, and overwrite: true is that word given up front.\n // A path that fails validation here is left for runOverPaths to report.\n // A file that appears between rounds needs no re-stat before the write:\n // the retry re-plans, and the grown pending set fails the R9 check.\n const { results: planned } = await processInParallel(\n args.files,\n async (entry) => {\n if (entry.append || entry.overwrite) return undefined;\n try {\n const validPath = await ctx.fs.pathGuard.validatePathForWrite(entry.path);\n return (await destExists(ctx.fs, validPath, 'create')) ? validPath : undefined;\n } catch (error) {\n rethrowIfAborted(error);\n return undefined;\n }\n },\n PARALLEL_CONCURRENCY,\n ctx.signal,\n );\n const pendingByPath = new Map<string, string>();\n for (const { index, value } of planned) {\n const requested = args.files[index]?.path;\n if (value && requested) pendingByPath.set(requested, value);\n }\n const pendingSorted = [...new Set(pendingByPath.values())].sort();\n if (pendingSorted.length > 0) {\n // Round 1 returns input_required; a retry whose verified state does not\n // bind this overwrite set throws (R9) via `pendingRoundTrip`.\n const round = await pendingRoundTrip(ctx, {\n op: 'create',\n pending: pendingSorted,\n buildInputs: (paths) =>\n paths.map((target, i) => ({\n key: confirmKey(i),\n message: `\"${target}\" already exists. Overwrite it?`,\n choices: ['overwrite', 'skip'],\n })),\n });\n if (round !== undefined) return round;\n }\n\n const batch = await runOverPaths<\n z.infer<typeof CreateFileItemSchema>,\n { file: CreateFileResult; resourceLink?: ContentBlock } | { skipped: string }\n >(args.files, ctx, ErrorCode.UNKNOWN, async ({ path, content, append }) => {\n const pendingPath = pendingByPath.get(path);\n if (pendingPath !== undefined) {\n const key = confirmKey(pendingSorted.indexOf(pendingPath));\n const choice = readAcceptedChoice(ctx.inputResponses, key);\n if (choice === 'skip') return { skipped: path };\n if (choice !== 'overwrite') {\n throw new FsError(\n ErrorCode.CANCELLED,\n `create cancelled: overwrite of \"${path}\" was ${describeRefusal(ctx, key)}`,\n path,\n );\n }\n }\n\n await ctx.fs.mkdir(dirname(path), { recursive: true });\n\n // Overwrite: file content == `content`, so content-derived meta is\n // exact and the atomic temp+rename write protects the existing mode.\n // Append: the resulting file is everything that was there plus\n // `content`, so size/created/modified come from a real post-append\n // stat, MIME from the resulting file's leading bytes, and the line\n // count streams the file — reading a multi-GB log back whole is the\n // round-trip append exists to avoid.\n let validPath: string;\n let meta: WrittenFileMeta;\n let created: string;\n let modified: string;\n if (append) {\n const appended = await ctx.fs.appendFile(path, content, {\n signal: ctx.signal,\n });\n validPath = appended.validPath;\n // The append is the commit point: the bytes are on disk, so\n // everything below is best-effort RESULT METADATA, not part of the\n // write. It runs unwired to ctx.signal, and a metadata failure\n // degrades the result instead of failing it — either would report\n // the append as failed and a client retry would append twice. The\n // stat covers the committed validPath, never the input path: a\n // symlink there could have been swapped between write and stat,\n // and the result must describe the file the bytes landed in.\n let stats: Stats | undefined;\n let mimeType: string;\n let kind: FileKind;\n let lineCount = 0;\n try {\n stats = (await ctx.fs.stat(appended.validPath)).stats;\n // Sniff the resulting file's leading bytes, not the appended\n // chunk: text appended to a binary file is still a binary file.\n const sample = await ctx.fs.readLeadingSample(appended.validPath, MIME_SAMPLE_SIZE);\n const mimeInfo = detectMimeFromContent(appended.validPath, sample);\n mimeType = mimeInfo.mimeType;\n kind = mimeInfo.kind;\n lineCount = await countFileLines(appended.validPath);\n } catch (error) {\n ctx.log(\n 'warning',\n `create append: result metadata degraded for ${appended.validPath}: ${formatUnknownErrorMessage(error)}`,\n 'create',\n );\n // A POSIX mode-0222 file appends fine but cannot be opened 'r';\n // fall back to the chunk for MIME (an approximation) and report\n // the line count as unknown-as-zero rather than fail the append.\n const mimeInfo = detectMimeFromContent(appended.validPath, content);\n mimeType = mimeInfo.mimeType;\n kind = mimeInfo.kind;\n }\n // A failed stat leaves the size unknown, so nothing is advertised.\n meta = {\n size: stats?.size ?? 0,\n lineCount,\n mimeType,\n kind,\n ...writtenFileLinks(\n appended.validPath,\n mimeType,\n stats?.size,\n false,\n stats?.mtime.toISOString(),\n ),\n };\n created = (stats?.birthtime ?? EPOCH).toISOString();\n modified = (stats?.mtime ?? EPOCH).toISOString();\n } else {\n const written = await ctx.fs.writeFile(path, content, {\n signal: ctx.signal,\n });\n validPath = written.validPath;\n const fileStats = (await ctx.fs.stat(path, { signal: ctx.signal })).stats;\n created = fileStats.birthtime.toISOString();\n modified = fileStats.mtime.toISOString();\n meta = buildWrittenFileMeta({\n validPath: written.validPath,\n content,\n lastModified: modified,\n });\n }\n\n const file: CreateFileResult = {\n path: validPath,\n size: meta.size,\n lineCount: meta.lineCount,\n mimeType: meta.mimeType,\n kind: meta.kind,\n resourceUri: meta.resourceUri,\n created,\n modified,\n };\n\n return meta.resourceLink ? { file, resourceLink: meta.resourceLink } : { file };\n });\n\n const results: CreateFileResult[] = [];\n const failures: CreateFailureItem[] = [];\n const skipped: string[] = [];\n const links: ContentBlock[] = [];\n for (const r of batch.results) {\n if ('error' in r) {\n failures.push({ path: r.path, error: r.error });\n continue;\n }\n if ('skipped' in r.value) {\n skipped.push(r.value.skipped);\n continue;\n }\n results.push(r.value.file);\n if (r.value.resourceLink) links.push(r.value.resourceLink);\n }\n\n const structured = {\n files: results,\n ...(failures.length > 0 ? { failures } : {}),\n ...(skipped.length > 0 ? { skipped } : {}),\n };\n // No `text` on purpose. The one-line roster this used to build named the\n // paths and dropped every per-file size, line count and error the caller\n // asked `create` for. Supplying no text makes this a data tool, so\n // `define.ts` renders the JSON and keeps it in `structuredContent`.\n if (links.length > 0) {\n return { structured, resources: links, isError: isTotalFailure(batch.summary) };\n }\n\n return { structured, isError: isTotalFailure(batch.summary) };\n },\n progress: (args) => ({\n label: 'Create',\n subject:\n args.files.length === 1\n ? basename(args.files[0]?.path ?? '')\n : `${String(args.files.length)} files`,\n }),\n});\n"]}
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../../src/tools/create.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE9C,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EACL,SAAS,EACT,yBAAyB,EACzB,OAAO,EAEP,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAwB,MAAM,qBAAqB,CAAC;AACnG,OAAO,EAAE,UAAU,EAAc,MAAM,eAAe,CAAC;AACvD,OAAO,EACL,UAAU,EACV,eAAe,EACf,gBAAgB,EAChB,kBAAkB,GACnB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,qBAAqB,EAAiB,MAAM,iBAAiB,CAAC;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,UAAU,EAAgB,MAAM,aAAa,CAAC;AAEvD,MAAM,KAAK,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC;AAE1B,MAAM,oBAAoB,GAAG,CAAC,CAAC,YAAY,CAAC;IAC1C,IAAI,EAAE,YAAY,CAAC,QAAQ,CAAC,8CAA8C,CAAC;IAC3E,OAAO,EAAE,CAAC;SACP,MAAM,EAAE;SACR,M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CAAC;AAC1D,CAAC","sourcesContent":["import type { ContentBlock } from '@modelcontextprotocol/server';\n\nimport { basename, dirname } from 'node:path';\n\nimport * as z from 'zod/v4';\n\nimport { processInParallel } from '../core/concurrency.ts';\nimport {\n ErrorCode,\n formatUnknownErrorMessage,\n FsError,\n type Problem,\n rethrowIfAborted,\n} from '../core/errors.ts';\nimport { buildWrittenFileMeta, writtenFileLinks, type WrittenFileMeta } from '../core/file-uri.ts';\nimport { destExists, type Stats } from '../core/fs.ts';\nimport {\n confirmKey,\n describeRefusal,\n pendingRoundTrip,\n readAcceptedChoice,\n} from '../core/input-required.ts';\nimport { detectMimeFromContent, type FileKind } from '../core/mime.ts';\nimport { isSamePath } from '../core/path-utils.ts';\nimport { RequiredPath } from '../core/schema.ts';\nimport { getMaxTextFileSize, PARALLEL_CONCURRENCY } from '../core/util.ts';\nimport { isTotalFailure, runOverPaths } from './batch.ts';\nimport { defineTool, type ToolCtx } from './define.ts';\n\nconst EPOCH = new Date(0);\n\nconst CreateFileItemSchema = z.strictObject({\n path: RequiredPath.describe('Absolute path where the file will be created'),\n content: z\n .string()\n .refine((val) => val.length <= getMaxTextFileSize(), {\n message: 'Content exceeds maximum allowed text file size',\n })\n .describe('Text content to write, verbatim.'),\n append: z\n .boolean()\n .optional()\n .describe(\n 'Append the content to the end of the file instead of overwriting; creates the file if it does not exist',\n ),\n overwrite: z\n .boolean()\n .optional()\n .describe(\n 'Replace an existing file without asking the user; without it an existing file prompts for confirmation',\n ),\n});\n\ntype CreateFileResult = Omit<WrittenFileMeta, 'resourceLink'> & {\n path: string;\n created: string;\n modified: string;\n};\n\nconst CreateInputSchema = z\n .strictObject({\n files: z\n .array(CreateFileItemSchema)\n .min(1)\n .max(100)\n .describe(\n 'List of files to create (max 100); each entry requires path and content; combined content is capped at the file-size limit',\n ),\n })\n .superRefine((value, ctx) => {\n // Entries write in parallel and each replaces its file whole, so two\n // entries for one path race: the last rename wins and the other's\n // content is lost while both report success. One entry per file.\n for (const [index, file] of value.files.entries()) {\n const first = value.files.findIndex((other) => isSamePath(other.path, file.path));\n if (first < index) {\n ctx.addIssue({\n code: 'custom',\n path: ['files', index, 'path'],\n message: `duplicate of files[${String(first)}].path; one entry per file`,\n input: value,\n });\n }\n }\n // One call's content is what one inbound message carries; the transports\n // size their message bound from this same limit (getMaxInboundMessageBytes),\n // so a batch that passes here always fits on the wire. Counted in the same\n // UTF-16 units as the per-file refine above.\n const total = value.files.reduce((sum, file) => sum + file.content.length, 0);\n const limit = getMaxTextFileSize();\n if (total > limit) {\n ctx.addIssue({\n code: 'custom',\n path: ['files'],\n message: `combined content is ${String(total)} characters; one create call carries at most ${String(limit)} (FS_MAX_FILE_SIZE). Split the batch.`,\n input: value,\n });\n }\n });\n\ninterface CreateFailureItem {\n path: string;\n error: Problem;\n}\n\ninterface CreateOutput {\n files: CreateFileResult[];\n failures?: CreateFailureItem[];\n skipped?: string[];\n}\n\nexport const CREATE = defineTool<typeof CreateInputSchema, CreateOutput>({\n name: 'create',\n title: 'Create Files',\n description:\n 'Write whole text files: create new ones, overwrite, or append. Missing parent directories are created. ' +\n 'edit or patch changes part of an existing file.',\n input: CreateInputSchema,\n readOnlyHint: false,\n accessPaths: (args) => args.files.map((f) => f.path),\n run: async (args, ctx) => {\n // Phase 1 (no mutation): which entries would replace a file that exists\n // right now? Those need the user's word before anything is written (R14).\n // append never destroys, and overwrite: true is that word given up front.\n // A path that fails validation here is left for runOverPaths to report.\n // A file that appears between rounds needs no re-stat before the write:\n // the retry re-plans, and the grown pending set fails the R9 check.\n const { results: planned } = await processInParallel(\n args.files,\n async (entry) => {\n if (entry.append || entry.overwrite) return undefined;\n try {\n const validPath = await ctx.fs.pathGuard.validatePathForWrite(entry.path);\n return (await destExists(ctx.fs, validPath, 'create')) ? validPath : undefined;\n } catch (error) {\n rethrowIfAborted(error);\n return undefined;\n }\n },\n PARALLEL_CONCURRENCY,\n ctx.signal,\n );\n const pendingByPath = new Map<string, string>();\n for (const { index, value } of planned) {\n const requested = args.files[index]?.path;\n if (value && requested) pendingByPath.set(requested, value);\n }\n const pendingSorted = [...new Set(pendingByPath.values())].sort();\n if (pendingSorted.length > 0) {\n // Round 1 returns input_required; a retry whose verified state does not\n // bind this overwrite set throws (R9) via `pendingRoundTrip`.\n const round = await pendingRoundTrip(ctx, {\n op: 'create',\n pending: pendingSorted,\n buildInputs: (paths) =>\n paths.map((target, i) => ({\n key: confirmKey(i),\n message: `\"${target}\" already exists. Overwrite it?`,\n choices: ['overwrite', 'skip'],\n })),\n });\n if (round !== undefined) return round;\n }\n\n const batch = await runOverPaths<\n z.infer<typeof CreateFileItemSchema>,\n { file: CreateFileResult; resourceLink?: ContentBlock } | { skipped: string }\n >(args.files, ctx, ErrorCode.UNKNOWN, async ({ path, content, append, overwrite }) => {\n const pendingPath = pendingByPath.get(path);\n if (pendingPath !== undefined) {\n const key = confirmKey(pendingSorted.indexOf(pendingPath));\n const choice = readAcceptedChoice(ctx.inputResponses, key);\n if (choice === 'skip') return { skipped: path };\n if (choice !== 'overwrite') {\n throw new FsError(\n ErrorCode.CANCELLED,\n `create cancelled: overwrite of \"${path}\" was ${describeRefusal(ctx, key)}`,\n path,\n );\n }\n }\n\n // Lock per resolved file so two entries naming one file (e.g. through a\n // symlinked parent) cannot clobber each other.\n return ctx.fs.lockForWrite(path, async (resolvedTarget) => {\n // The file may have appeared since the overwrite set was planned.\n if (\n !append &&\n !overwrite &&\n pendingPath === undefined &&\n (await destExists(ctx.fs, resolvedTarget, 'create'))\n ) {\n throw new FsError(\n ErrorCode.INVALID_INPUT,\n `create refused: \"${path}\" appeared after overwrite confirmation was checked`,\n path,\n );\n }\n return createOne(path, content, append, ctx);\n });\n });\n\n const results: CreateFileResult[] = [];\n const failures: CreateFailureItem[] = [];\n const skipped: string[] = [];\n const links: ContentBlock[] = [];\n for (const r of batch.results) {\n if ('error' in r) {\n failures.push({ path: r.path, error: r.error });\n continue;\n }\n if ('skipped' in r.value) {\n skipped.push(r.value.skipped);\n continue;\n }\n results.push(r.value.file);\n if (r.value.resourceLink) links.push(r.value.resourceLink);\n }\n\n const structured = {\n files: results,\n ...(failures.length > 0 ? { failures } : {}),\n ...(skipped.length > 0 ? { skipped } : {}),\n };\n // No `text` on purpose. The one-line roster this used to build named the\n // paths and dropped every per-file size, line count and error the caller\n // asked `create` for. Supplying no text makes this a data tool, so\n // `define.ts` renders the JSON and keeps it in `structuredContent`.\n return { structured, resources: links, isError: isTotalFailure(batch.summary) };\n },\n progress: (args) => ({\n label: 'Create',\n subject:\n args.files.length === 1\n ? basename(args.files[0]?.path ?? '')\n : `${String(args.files.length)} files`,\n }),\n});\n\nasync function createOne(\n path: string,\n content: string,\n append: boolean | undefined,\n ctx: ToolCtx,\n): Promise<{ file: CreateFileResult; resourceLink?: ContentBlock }> {\n await ctx.fs.mkdir(dirname(path), { recursive: true });\n\n // Overwrite: file content == `content`, so content-derived meta is\n // exact and the atomic temp+rename write protects the existing mode.\n // Append: the resulting file is everything that was there plus\n // `content`, so size/created/modified come from a real post-append\n // stat, MIME from the resulting file's leading bytes, and the line\n // count streams the file — reading a multi-GB log back whole is the\n // round-trip append exists to avoid.\n let validPath: string;\n let meta: WrittenFileMeta;\n let created: string;\n let modified: string;\n if (append) {\n const appended = await ctx.fs.appendFile(path, content, {\n signal: ctx.signal,\n });\n validPath = appended.validPath;\n // The append is the commit point: the bytes are on disk, so\n // everything below is best-effort RESULT METADATA, not part of the\n // write. It runs unwired to ctx.signal, and a metadata failure\n // degrades the result instead of failing it — either would report\n // the append as failed and a client retry would append twice. The\n // stat covers the committed validPath, never the input path: a\n // symlink there could have been swapped between write and stat,\n // and the result must describe the file the bytes landed in.\n let stats: Stats | undefined;\n let mimeType: string;\n let kind: FileKind;\n let lineCount = 0;\n try {\n stats = (await ctx.fs.stat(appended.validPath)).stats;\n // Sniff the resulting file's leading bytes, not the appended\n // chunk: text appended to a binary file is still a binary file.\n const sample = await ctx.fs.readLeadingSample(appended.validPath);\n const mimeInfo = detectMimeFromContent(appended.validPath, sample);\n mimeType = mimeInfo.mimeType;\n kind = mimeInfo.kind;\n lineCount = await ctx.fs.countFileLines(appended.validPath);\n } catch (error) {\n ctx.log(\n 'warning',\n `create append: result metadata degraded for ${appended.validPath}: ${formatUnknownErrorMessage(error)}`,\n 'create',\n );\n // A POSIX mode-0222 file appends fine but cannot be opened 'r';\n // fall back to the chunk for MIME (an approximation) and report\n // the line count as unknown-as-zero rather than fail the append.\n const mimeInfo = detectMimeFromContent(appended.validPath, content);\n mimeType = mimeInfo.mimeType;\n kind = mimeInfo.kind;\n }\n // A failed stat leaves the size unknown, so nothing is advertised.\n meta = {\n size: stats?.size ?? 0,\n lineCount,\n mimeType,\n kind,\n ...writtenFileLinks(\n appended.validPath,\n mimeType,\n stats?.size,\n false,\n stats?.mtime.toISOString(),\n ),\n };\n created = (stats?.birthtime ?? EPOCH).toISOString();\n modified = (stats?.mtime ?? EPOCH).toISOString();\n } else {\n const written = await ctx.fs.writeFile(path, content, {\n signal: ctx.signal,\n });\n validPath = written.validPath;\n const fileStats = (await ctx.fs.stat(path, { signal: ctx.signal })).stats;\n created = fileStats.birthtime.toISOString();\n modified = fileStats.mtime.toISOString();\n meta = buildWrittenFileMeta({\n validPath: written.validPath,\n content,\n lastModified: modified,\n });\n }\n\n const { resourceLink, ...fileMeta } = meta;\n const file: CreateFileResult = { path: validPath, ...fileMeta, created, modified };\n\n return resourceLink ? { file, resourceLink } : { file };\n}\n"]}
|
package/dist/tools/define.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ClientCapabilities, ContentBlock, InputRequiredResult, McpServer,
|
|
1
|
+
import type { ClientCapabilities, ContentBlock, InputRequiredResult, McpServer, RegisteredTool, RequestId, RequestStateAccessor, ServerContext } from '@modelcontextprotocol/server';
|
|
2
2
|
import type * as z from 'zod/v4';
|
|
3
3
|
import { ErrorCode } from '../core/errors.ts';
|
|
4
4
|
import type { ProgressCtx } from '../core/fmt.ts';
|
|
@@ -8,17 +8,13 @@ import type { PathGuard } from '../core/path.ts';
|
|
|
8
8
|
import type { PageSnapshotStore, ResourceStore } from '../core/store.ts';
|
|
9
9
|
export interface ToolCtx {
|
|
10
10
|
readonly signal: AbortSignal;
|
|
11
|
-
readonly _meta?: RequestMeta | undefined;
|
|
12
11
|
/** The JSON-RPC id of the `tools/call` this context serves; prefixes every log line. */
|
|
13
12
|
readonly requestId: RequestId;
|
|
14
|
-
/** W3C trace context the client put in `_meta`, when it did; rides the log prefix. */
|
|
15
|
-
readonly traceparent?: string | undefined;
|
|
16
13
|
readonly fs: GuardedFileSystem;
|
|
17
14
|
readonly pageStore: PageSnapshotStore;
|
|
18
15
|
readonly resourceStore: ResourceStore;
|
|
19
16
|
/** Emits a log line to stderr via `Logger.emit`, gated by `FS_LOG_LEVEL`. */
|
|
20
17
|
readonly log: (level: LoggingLevel, data: unknown, logger?: string) => void;
|
|
21
|
-
readonly sendNotification: (notification: Notification) => Promise<void>;
|
|
22
18
|
readonly onProgress: (params: {
|
|
23
19
|
current: number;
|
|
24
20
|
total?: number;
|
|
@@ -59,13 +55,16 @@ export interface ToolCtx {
|
|
|
59
55
|
readonly clientCapabilities?: ClientCapabilities | undefined;
|
|
60
56
|
/** The SDK request context this call runs under; `pendingRoundTrip` binds minted state to it. */
|
|
61
57
|
readonly serverCtx: ServerContext;
|
|
58
|
+
/** `stdio` alone has a 10 MiB frame limit; `http` on HTTP, unset on the in-memory test pair. */
|
|
59
|
+
readonly transport?: 'stdio' | 'http' | undefined;
|
|
62
60
|
}
|
|
63
|
-
interface ToolDeps {
|
|
61
|
+
export interface ToolDeps {
|
|
64
62
|
readonly server: McpServer;
|
|
65
63
|
readonly pathGuard: PathGuard;
|
|
66
64
|
readonly pageStore: PageSnapshotStore;
|
|
67
65
|
readonly resourceStore: ResourceStore;
|
|
68
66
|
readonly era?: 'legacy' | 'modern';
|
|
67
|
+
readonly transport?: 'stdio' | 'http';
|
|
69
68
|
}
|
|
70
69
|
interface RunResult<T> {
|
|
71
70
|
readonly structured: T;
|
|
@@ -99,6 +98,11 @@ export interface ToolDef<I extends z.ZodType, O extends object> {
|
|
|
99
98
|
*/
|
|
100
99
|
readonly accessPaths?: (args: z.infer<I>) => readonly string[];
|
|
101
100
|
readonly run: (args: z.infer<I>, ctx: ToolCtx) => Promise<RunResult<O> | InputRequiredResult>;
|
|
101
|
+
/**
|
|
102
|
+
* What the stdio size refusal tells the caller to do instead, for a tool
|
|
103
|
+
* that knows better than the generic advice (see `refuseOversized`).
|
|
104
|
+
*/
|
|
105
|
+
readonly oversizeRemedy?: (result: RunResult<O>) => string;
|
|
102
106
|
}
|
|
103
107
|
export interface DefinedTool {
|
|
104
108
|
readonly name: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../../src/tools/define.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,kBAAkB,EAClB,YAAY,EACZ,mBAAmB,EACnB,SAAS,
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../../src/tools/define.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,kBAAkB,EAClB,YAAY,EACZ,mBAAmB,EACnB,SAAS,EAET,cAAc,EACd,SAAS,EACT,oBAAoB,EACpB,aAAa,EAGd,MAAM,8BAA8B,CAAC;AAOtC,OAAO,KAAK,KAAK,CAAC,MAAM,QAAQ,CAAC;AAEjC,OAAO,EAAE,SAAS,EAA+C,MAAM,mBAAmB,CAAC;AAC3F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAQlD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAE7D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AA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AE,CAAC;IAC/D,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAAC,CAAC;IAC9F;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC;CAC5D;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;IAE/B,QAAQ,CAAC,IAAI,EAAE,QAAQ,GAAG,cAAc,CAAC;CAC1C;AA0RD,wBAAgB,UAAU,CAAC,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,WAAW,CA0CjG"}
|
package/dist/tools/define.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { CLIENT_CAPABILITIES_META_KEY, isInputRequiredResult, TRACEPARENT_META_KEY, } from '@modelcontextprotocol/server';
|
|
2
|
-
import { ErrorCode, formatUnknownErrorMessage, Problem } from "../core/errors.js";
|
|
2
|
+
import { ErrorCode, formatUnknownErrorMessage, FsError, Problem } from "../core/errors.js";
|
|
3
3
|
import { plainMessage } from "../core/fmt.js";
|
|
4
4
|
import { GuardedFileSystem } from "../core/fs.js";
|
|
5
5
|
import { confirmKey, pendingRoundTrip, readAcceptedConfirm, readAcceptedMultiChoice, } from "../core/input-required.js";
|
|
6
6
|
import { Logger, sanitizeLogField } from "../core/observability.js";
|
|
7
7
|
import { isSamePath } from "../core/path-utils.js";
|
|
8
8
|
import { withSplitArgsRecovery } from "../core/schema.js";
|
|
9
|
+
import { jsonBytesOver, MAX_OUTBOUND_RESULT_BYTES } from "../core/util.js";
|
|
9
10
|
import { ProgressSession } from "./progress.js";
|
|
10
11
|
function toToolCtx(ctx, deps) {
|
|
11
12
|
// Envelope first, accessor second — the two eras carry this differently.
|
|
@@ -33,20 +34,16 @@ function toToolCtx(ctx, deps) {
|
|
|
33
34
|
(deps.era === 'legacy' ? {} : undefined);
|
|
34
35
|
return {
|
|
35
36
|
signal: ctx.mcpReq.signal,
|
|
36
|
-
...(ctx.mcpReq._meta ? { _meta: ctx.mcpReq._meta } : {}),
|
|
37
37
|
requestId: ctx.mcpReq.id,
|
|
38
|
-
...(typeof ctx.mcpReq._meta?.[TRACEPARENT_META_KEY] === 'string'
|
|
39
|
-
? { traceparent: ctx.mcpReq._meta[TRACEPARENT_META_KEY] }
|
|
40
|
-
: {}),
|
|
41
38
|
fs: new GuardedFileSystem(deps.pathGuard),
|
|
42
39
|
pageStore: deps.pageStore,
|
|
43
40
|
resourceStore: deps.resourceStore,
|
|
44
|
-
sendNotification: async (notification) => ctx.mcpReq.notify(notification),
|
|
45
41
|
inputResponses: ctx.mcpReq.inputResponses,
|
|
46
42
|
droppedInputResponseKeys: ctx.mcpReq.droppedInputResponseKeys,
|
|
47
43
|
requestState: ctx.mcpReq.requestState,
|
|
48
44
|
serverCtx: ctx,
|
|
49
45
|
...(clientCapabilities ? { clientCapabilities } : {}),
|
|
46
|
+
...(deps.transport ? { transport: deps.transport } : {}),
|
|
50
47
|
};
|
|
51
48
|
}
|
|
52
49
|
/**
|
|
@@ -84,14 +81,21 @@ class ToolExecutor {
|
|
|
84
81
|
? AbortSignal.any([ctx.signal, AbortSignal.timeout(def.timeoutMs)])
|
|
85
82
|
: ctx.signal;
|
|
86
83
|
this.#progressCtx = def.progress ? def.progress(parsedArgs) : { label: def.title };
|
|
87
|
-
const
|
|
84
|
+
const { mcpReq } = ctx.serverCtx;
|
|
85
|
+
const token = mcpReq._meta?.progressToken;
|
|
86
|
+
const notify = async (notification) => mcpReq.notify(notification);
|
|
88
87
|
this.#progressSession = new ProgressSession({
|
|
89
88
|
label: this.#progressCtx.label,
|
|
90
|
-
...(token !== undefined ? { sink: { toolName, token, notify
|
|
89
|
+
...(token !== undefined ? { sink: { toolName, token, notify } } : {}),
|
|
91
90
|
});
|
|
91
|
+
const traceparent = mcpReq._meta?.[TRACEPARENT_META_KEY];
|
|
92
92
|
this.toolCtx = {
|
|
93
93
|
...ctx,
|
|
94
94
|
signal: this.signal,
|
|
95
|
+
// Per-call views, so a response refused after `run` can take back the
|
|
96
|
+
// cached result and page snapshot behind a link or cursor it never sent.
|
|
97
|
+
pageStore: ctx.pageStore.forCall(),
|
|
98
|
+
resourceStore: ctx.resourceStore.forCall(),
|
|
95
99
|
log: (level, data, logger) => {
|
|
96
100
|
const msg = typeof data === 'string' ? data : String(data);
|
|
97
101
|
// `[req <id>]` first so one grep finds every line of one call; the
|
|
@@ -101,7 +105,7 @@ class ToolExecutor {
|
|
|
101
105
|
// The message is flattened too (uncapped): it can carry a resolved
|
|
102
106
|
// path or OS error text, and input schemas reject CR/LF but not the
|
|
103
107
|
// Unicode line separators.
|
|
104
|
-
const trace =
|
|
108
|
+
const trace = typeof traceparent === 'string' ? ` ${sanitizeLogField(traceparent)}` : '';
|
|
105
109
|
const prefix = logger ? `[${logger}] ` : '';
|
|
106
110
|
const text = sanitizeLogField(msg, Infinity);
|
|
107
111
|
Logger.emit(level, `[req ${sanitizeLogField(String(ctx.requestId))}${trace}] ${prefix}${text}`);
|
|
@@ -119,17 +123,15 @@ class ToolExecutor {
|
|
|
119
123
|
};
|
|
120
124
|
this.#progressSession.set({ ...p, message: plainMessage('tick', tickCtx) });
|
|
121
125
|
}
|
|
122
|
-
|
|
126
|
+
completeProgress(result) {
|
|
123
127
|
const doneCtx = this.def.progressDone
|
|
124
128
|
? { ...this.#progressCtx, ...this.def.progressDone(this.parsedArgs, result) }
|
|
125
129
|
: this.#progressCtx;
|
|
126
130
|
this.#progressSession.complete(plainMessage('done', doneCtx));
|
|
127
|
-
await this.#progressSession.flush();
|
|
128
131
|
}
|
|
129
|
-
|
|
132
|
+
failProgress(error) {
|
|
130
133
|
const errMsg = formatUnknownErrorMessage(error);
|
|
131
134
|
this.#progressSession.fail(plainMessage('fail', { ...this.#progressCtx, error: errMsg }));
|
|
132
|
-
await this.#progressSession.flush();
|
|
133
135
|
const errorText = Problem.toText(error, this.def.defaultErrorCode ?? ErrorCode.UNKNOWN);
|
|
134
136
|
return {
|
|
135
137
|
content: [{ type: 'text', text: errorText }],
|
|
@@ -205,6 +207,35 @@ class ToolExecutor {
|
|
|
205
207
|
}
|
|
206
208
|
return undefined;
|
|
207
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* stdio's SDK reader drops the connection on a message past 10 MiB, so a
|
|
212
|
+
* response that would overflow it becomes TOO_LARGE instead. Only a call
|
|
213
|
+
* that changed nothing can be refused after `run` — a read-only tool, or a
|
|
214
|
+
* dry run (every tool with `dryRun` writes nothing under it); a mutation may
|
|
215
|
+
* already have committed. The whole encoded response counts, duplicated
|
|
216
|
+
* metadata and escaping included; the ceiling leaves room for the envelope.
|
|
217
|
+
*/
|
|
218
|
+
refuseOversized(result, response) {
|
|
219
|
+
if (this.toolCtx.transport !== 'stdio')
|
|
220
|
+
return;
|
|
221
|
+
const dryRun = this.parsedArgs.dryRun === true;
|
|
222
|
+
if (!this.def.readOnlyHint && !dryRun)
|
|
223
|
+
return;
|
|
224
|
+
const encodedBytes = jsonBytesOver(response, MAX_OUTBOUND_RESULT_BYTES);
|
|
225
|
+
if (encodedBytes === undefined)
|
|
226
|
+
return;
|
|
227
|
+
// The client never receives the link or cursor these back, so they would
|
|
228
|
+
// only push live entries out of the stores' caps.
|
|
229
|
+
this.toolCtx.resourceStore.discard();
|
|
230
|
+
this.toolCtx.pageStore.discard();
|
|
231
|
+
const remedy = this.def.oversizeRemedy?.(result) ??
|
|
232
|
+
(dryRun
|
|
233
|
+
? 'Nothing was written. Preview fewer files or a smaller file per call, or connect over HTTP.'
|
|
234
|
+
: 'Reduce the requested output or connect over HTTP.');
|
|
235
|
+
// The remedy stands in for TOO_LARGE's default head/tail advice, which
|
|
236
|
+
// fits neither media nor a dry run.
|
|
237
|
+
throw new FsError(ErrorCode.TOO_LARGE, `${this.def.name} result too large to return (${String(encodedBytes)} bytes once JSON-encoded, over the ${String(MAX_OUTBOUND_RESULT_BYTES)}-byte stdio message limit).`, undefined, undefined, remedy);
|
|
238
|
+
}
|
|
208
239
|
async execute() {
|
|
209
240
|
try {
|
|
210
241
|
// Access-grant pre-check before any filesystem touch (R7/R8/R9). A
|
|
@@ -223,11 +254,13 @@ class ToolExecutor {
|
|
|
223
254
|
if (isInputRequiredResult(result)) {
|
|
224
255
|
return result;
|
|
225
256
|
}
|
|
226
|
-
|
|
227
|
-
|
|
257
|
+
const response = buildSuccessResponse(result);
|
|
258
|
+
this.refuseOversized(result, response);
|
|
259
|
+
this.completeProgress(result.structured);
|
|
260
|
+
return response;
|
|
228
261
|
}
|
|
229
262
|
catch (error) {
|
|
230
|
-
return
|
|
263
|
+
return this.failProgress(error);
|
|
231
264
|
}
|
|
232
265
|
finally {
|
|
233
266
|
await this.#progressSession.flush();
|