@milaboratories/pl-mcp-server 54.0.0 → 55.0.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 +15 -25
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/agent-tables.cjs +116 -0
- package/dist/agent-tables.cjs.map +1 -0
- package/dist/agent-tables.js +115 -0
- package/dist/agent-tables.js.map +1 -0
- package/dist/instructions.cjs +15 -12
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +15 -12
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +58 -8
- package/dist/project-holds.cjs.map +1 -1
- package/dist/project-holds.js +58 -8
- package/dist/project-holds.js.map +1 -1
- package/dist/project-list.cjs +16 -0
- package/dist/project-list.cjs.map +1 -0
- package/dist/project-list.js +16 -0
- package/dist/project-list.js.map +1 -0
- package/dist/server-log.cjs +40 -18
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +40 -19
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +48 -18
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +21 -20
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +48 -18
- package/dist/server.js.map +1 -1
- package/dist/tools/agent-tables.cjs +424 -0
- package/dist/tools/agent-tables.cjs.map +1 -0
- package/dist/tools/agent-tables.js +417 -0
- package/dist/tools/agent-tables.js.map +1 -0
- package/dist/tools/await.cjs +216 -73
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +212 -75
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +3 -0
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +3 -0
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-api.cjs +17 -2
- package/dist/tools/block-api.cjs.map +1 -1
- package/dist/tools/block-api.js +17 -3
- package/dist/tools/block-api.js.map +1 -1
- package/dist/tools/block-params.cjs +272 -4
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +270 -6
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +323 -42
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +317 -46
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/block-storage.cjs +153 -0
- package/dist/tools/block-storage.cjs.map +1 -0
- package/dist/tools/block-storage.js +147 -0
- package/dist/tools/block-storage.js.map +1 -0
- package/dist/tools/block-updates.cjs +405 -0
- package/dist/tools/block-updates.cjs.map +1 -0
- package/dist/tools/block-updates.js +397 -0
- package/dist/tools/block-updates.js.map +1 -0
- package/dist/tools/blocks.cjs +183 -70
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +177 -70
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +451 -113
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +446 -118
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/errors.cjs +21 -0
- package/dist/tools/errors.cjs.map +1 -0
- package/dist/tools/errors.js +20 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/files.cjs +643 -0
- package/dist/tools/files.cjs.map +1 -0
- package/dist/tools/files.js +613 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/ids.cjs +56 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +51 -2
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/imports.cjs +195 -0
- package/dist/tools/imports.cjs.map +1 -0
- package/dist/tools/imports.js +186 -0
- package/dist/tools/imports.js.map +1 -0
- package/dist/tools/logs.cjs +98 -19
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +95 -20
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/paging.cjs +27 -0
- package/dist/tools/paging.cjs.map +1 -0
- package/dist/tools/paging.js +26 -0
- package/dist/tools/paging.js.map +1 -0
- package/dist/tools/params-merge.cjs +106 -0
- package/dist/tools/params-merge.cjs.map +1 -0
- package/dist/tools/params-merge.js +106 -0
- package/dist/tools/params-merge.js.map +1 -0
- package/dist/tools/project-template.cjs +75 -0
- package/dist/tools/project-template.cjs.map +1 -0
- package/dist/tools/project-template.js +72 -0
- package/dist/tools/project-template.js.map +1 -0
- package/dist/tools/projects.cjs +66 -19
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +66 -20
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/readiness.cjs +104 -0
- package/dist/tools/readiness.cjs.map +1 -0
- package/dist/tools/readiness.js +99 -0
- package/dist/tools/readiness.js.map +1 -0
- package/dist/tools/result-pool.cjs +260 -0
- package/dist/tools/result-pool.cjs.map +1 -0
- package/dist/tools/result-pool.js +252 -0
- package/dist/tools/result-pool.js.map +1 -0
- package/dist/tools/sandbox.cjs +112 -14
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +110 -15
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/sdk-types.cjs +24 -0
- package/dist/tools/sdk-types.cjs.map +1 -0
- package/dist/tools/sdk-types.js +24 -0
- package/dist/tools/sdk-types.js.map +1 -0
- package/dist/tools/settle.cjs +155 -0
- package/dist/tools/settle.cjs.map +1 -0
- package/dist/tools/settle.js +148 -0
- package/dist/tools/settle.js.map +1 -0
- package/dist/tools/tokens.cjs +49 -8
- package/dist/tools/tokens.cjs.map +1 -1
- package/dist/tools/tokens.js +45 -9
- package/dist/tools/tokens.js.map +1 -1
- package/dist/tools/types.cjs +62 -11
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +60 -12
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/value-outline.cjs +119 -0
- package/dist/tools/value-outline.cjs.map +1 -0
- package/dist/tools/value-outline.js +117 -0
- package/dist/tools/value-outline.js.map +1 -0
- package/package.json +9 -8
- package/src/agent-tables.test.ts +246 -0
- package/src/agent-tables.ts +145 -0
- package/src/instructions.ts +15 -12
- package/src/project-holds.test.ts +179 -0
- package/src/project-holds.ts +114 -12
- package/src/project-list.test.ts +38 -0
- package/src/project-list.ts +25 -0
- package/src/server-log.test.ts +54 -1
- package/src/server-log.ts +49 -27
- package/src/server.logging.test.ts +38 -46
- package/src/server.test.ts +151 -17
- package/src/server.ts +62 -34
- package/src/tools/agent-tables.test.ts +650 -0
- package/src/tools/agent-tables.ts +673 -0
- package/src/tools/await.test.ts +287 -0
- package/src/tools/await.ts +393 -123
- package/src/tools/block-action-context.ts +3 -0
- package/src/tools/block-api.ts +19 -4
- package/src/tools/block-params.test.ts +828 -0
- package/src/tools/block-params.ts +623 -8
- package/src/tools/block-state.test.ts +480 -0
- package/src/tools/block-state.ts +659 -66
- package/src/tools/block-storage.test.ts +270 -0
- package/src/tools/block-storage.ts +195 -0
- package/src/tools/block-updates.test.ts +605 -0
- package/src/tools/block-updates.ts +751 -0
- package/src/tools/blocks.test.ts +169 -22
- package/src/tools/blocks.ts +336 -105
- package/src/tools/data-query.test.ts +641 -0
- package/src/tools/data-query.ts +807 -157
- package/src/tools/errors.test.ts +33 -0
- package/src/tools/errors.ts +23 -0
- package/src/tools/files.test.ts +842 -0
- package/src/tools/files.ts +990 -0
- package/src/tools/ids.test.ts +87 -0
- package/src/tools/ids.ts +67 -0
- package/src/tools/imports.ts +304 -0
- package/src/tools/logs.test.ts +70 -0
- package/src/tools/logs.ts +130 -21
- package/src/tools/paging.test.ts +39 -0
- package/src/tools/paging.ts +39 -0
- package/src/tools/params-merge.test.ts +274 -0
- package/src/tools/params-merge.ts +149 -0
- package/src/tools/project-template.test.ts +230 -0
- package/src/tools/project-template.ts +135 -0
- package/src/tools/projects.test.ts +122 -0
- package/src/tools/projects.ts +135 -25
- package/src/tools/readiness.test.ts +195 -0
- package/src/tools/readiness.ts +189 -0
- package/src/tools/result-pool.test.ts +377 -0
- package/src/tools/result-pool.ts +483 -0
- package/src/tools/sandbox.test.ts +128 -10
- package/src/tools/sandbox.ts +132 -18
- package/src/tools/sdk-types.ts +54 -0
- package/src/tools/settle.test.ts +340 -0
- package/src/tools/settle.ts +312 -0
- package/src/tools/tokens.ts +74 -9
- package/src/tools/types.ts +77 -12
- package/src/tools/value-outline.ts +143 -0
- package/dist/tools/screenshot.cjs +0 -44
- package/dist/tools/screenshot.cjs.map +0 -1
- package/dist/tools/screenshot.js +0 -44
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/ui-interaction.cjs +0 -140
- package/dist/tools/ui-interaction.cjs.map +0 -1
- package/dist/tools/ui-interaction.js +0 -140
- package/dist/tools/ui-interaction.js.map +0 -1
- package/src/tools/screenshot.ts +0 -61
- package/src/tools/ui-interaction.ts +0 -184
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
import { RESULT_CHAR_LIMIT, errorResult, hints, readSignal, textResult, transformFailedResult } from "./types.js";
|
|
2
|
+
import { projectIdInput } from "./ids.js";
|
|
3
|
+
import { isImportHandle } from "./imports.js";
|
|
4
|
+
import { SandboxError, expressionTimeoutText, safeEval } from "./sandbox.js";
|
|
5
|
+
import { clamp, isPlainObject, trimChars } from "@milaboratories/helpers";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { getFilePathFromHandle, isImportFileHandleIndex, isImportFileHandleUpload } from "@milaboratories/pl-model-common";
|
|
8
|
+
import * as fsp from "node:fs/promises";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
/** Bytes `read_file` reads by default. */
|
|
11
|
+
const READ_DEFAULT_BYTES = 16384;
|
|
12
|
+
/** The most bytes `read_file` returns. */
|
|
13
|
+
const READ_MAX_BYTES = 65536;
|
|
14
|
+
/** The most bytes `read_file` reads for a `transform`. */
|
|
15
|
+
const READ_MAX_TRANSFORM_BYTES = 4194304;
|
|
16
|
+
/** Default timeout of a `transform`, in milliseconds. */
|
|
17
|
+
const DEFAULT_TRANSFORM_TIMEOUT = 5e3;
|
|
18
|
+
const LOCAL_BLOB_RE = /^blob\+local:\/\/download\/.*#.*$/;
|
|
19
|
+
const REMOTE_BLOB_RE = /^blob\+remote:\/\/download\/.+\/(\d+)#.*$/;
|
|
20
|
+
/** Parses a block-output blob handle (`blob+local://` or `blob+remote://`); else `undefined`. */
|
|
21
|
+
function parseBlobHandle(s) {
|
|
22
|
+
if (LOCAL_BLOB_RE.test(s)) return {
|
|
23
|
+
kind: "local",
|
|
24
|
+
handle: s
|
|
25
|
+
};
|
|
26
|
+
const remote = REMOTE_BLOB_RE.exec(s);
|
|
27
|
+
if (remote !== null) return {
|
|
28
|
+
kind: "remote",
|
|
29
|
+
handle: s,
|
|
30
|
+
size: Number(remote[1])
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Whether a storage is on this computer (`local://` handle) or a remote data library. */
|
|
34
|
+
function storageKind(entry) {
|
|
35
|
+
return entry.handle.startsWith("local://") ? "local" : "remote";
|
|
36
|
+
}
|
|
37
|
+
/** The root folder of a local storage, from its handle (`local://<id>/<encoded root>`). */
|
|
38
|
+
function localStorageRoot(entry) {
|
|
39
|
+
return decodeURIComponent(entry.handle.slice(entry.handle.lastIndexOf("/") + 1));
|
|
40
|
+
}
|
|
41
|
+
/** Finds a storage by its id, then by its name. */
|
|
42
|
+
function findStorage(storages, ref) {
|
|
43
|
+
return storages.find((s) => s.id === ref) ?? storages.find((s) => s.name === ref);
|
|
44
|
+
}
|
|
45
|
+
function unknownStorage(ref, storages) {
|
|
46
|
+
return errorResult(`No storage "${ref}".`, `Storages: ${storages.map((s) => s.id).join(", ") || "none"}. list_files with no arguments lists them.`);
|
|
47
|
+
}
|
|
48
|
+
/** A path of a local storage made absolute: a relative one is under the storage's root. */
|
|
49
|
+
function localPath(entry, p) {
|
|
50
|
+
if (path.isAbsolute(p)) return p;
|
|
51
|
+
return path.join(entry === void 0 ? "/" : localStorageRoot(entry), p);
|
|
52
|
+
}
|
|
53
|
+
/** A `pattern` of `list_files` that does not parse, or takes too long to match. */
|
|
54
|
+
var PatternError = class extends Error {
|
|
55
|
+
hint;
|
|
56
|
+
constructor(message, hint) {
|
|
57
|
+
super(message);
|
|
58
|
+
this.hint = hint;
|
|
59
|
+
this.name = "PatternError";
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* The indices of the names `pattern` matches. The caller's regular expression runs in the
|
|
64
|
+
* sandbox, all names in one evaluation under `budgetMs`: a pattern that backtracks
|
|
65
|
+
* catastrophically is stopped there instead of blocking the server. Throws a
|
|
66
|
+
* {@link PatternError} when the pattern does not parse or runs out of time.
|
|
67
|
+
*/
|
|
68
|
+
async function matchNames(pattern, names, budgetMs = 500) {
|
|
69
|
+
try {
|
|
70
|
+
return await safeEval("(() => { const re = new RegExp(pattern); const hits = []; for (let i = 0; i < names.length; i++) if (re.test(names[i])) hits.push(i); return hits; })()", {
|
|
71
|
+
pattern,
|
|
72
|
+
names
|
|
73
|
+
}, budgetMs);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
if (e instanceof SandboxError && e.errorClass === "timeout") throw new PatternError(`\`pattern\` took longer than ${budgetMs} ms to match ${names.length} ${names.length === 1 ? "name" : "names"}.`, "Simplify it: avoid nested repetition such as `(a+)+`, or anchor it with ^…$.");
|
|
76
|
+
if (e instanceof SandboxError && e.errorClass === "SyntaxError") throw new PatternError(`\`pattern\` is not a valid regular expression: ${e.message}`, "Give a JavaScript regular expression, e.g. `^S\\d+_R[12]\\.fastq\\.gz$`.");
|
|
77
|
+
throw e;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Filters entries by `type` and `pattern` (tested against the name), sorts them by name and
|
|
82
|
+
* returns the page `[offset, offset + limit)`.
|
|
83
|
+
*/
|
|
84
|
+
async function pageEntries(entries, options) {
|
|
85
|
+
let matching = entries.filter((e) => options.type === void 0 || e.type === options.type);
|
|
86
|
+
if (options.pattern !== void 0) {
|
|
87
|
+
const candidates = matching;
|
|
88
|
+
matching = (await matchNames(options.pattern, candidates.map((e) => e.name))).map((i) => candidates[i]);
|
|
89
|
+
}
|
|
90
|
+
matching.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
91
|
+
const page = matching.slice(options.offset, options.offset + options.limit);
|
|
92
|
+
const end = options.offset + page.length;
|
|
93
|
+
const hasMore = end < matching.length;
|
|
94
|
+
return {
|
|
95
|
+
total: matching.length,
|
|
96
|
+
page,
|
|
97
|
+
hasMore,
|
|
98
|
+
...hasMore ? { nextOffset: end } : {}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** The page size asked for, clamped to `[1, LIST_MAX_LIMIT]`. */
|
|
102
|
+
function clampLimit(limit, fallback, max) {
|
|
103
|
+
if (limit === void 0 || !Number.isFinite(limit)) return fallback;
|
|
104
|
+
return clamp(Math.floor(limit), 1, max);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Lists a local folder. Every entry's type comes from the folder listing (a symbolic link from a
|
|
108
|
+
* `stat` of its target); sizes are read with one `stat` per file of the returned page only, so a
|
|
109
|
+
* large folder costs one listing, not one `stat` per entry.
|
|
110
|
+
*/
|
|
111
|
+
async function listLocalFolder(dir, options) {
|
|
112
|
+
const dirents = await fsp.readdir(dir, { withFileTypes: true });
|
|
113
|
+
const entries = [];
|
|
114
|
+
for (const d of dirents) {
|
|
115
|
+
const full = path.join(dir, d.name);
|
|
116
|
+
let type;
|
|
117
|
+
if (d.isFile()) type = "file";
|
|
118
|
+
else if (d.isDirectory()) type = "dir";
|
|
119
|
+
else if (d.isSymbolicLink()) try {
|
|
120
|
+
const st = await fsp.stat(full);
|
|
121
|
+
type = st.isFile() ? "file" : st.isDirectory() ? "dir" : void 0;
|
|
122
|
+
} catch {
|
|
123
|
+
type = void 0;
|
|
124
|
+
}
|
|
125
|
+
if (type !== void 0) entries.push({
|
|
126
|
+
name: d.name,
|
|
127
|
+
type,
|
|
128
|
+
path: full
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
const { total, page, hasMore, nextOffset } = await pageEntries(entries, options);
|
|
132
|
+
const withSizes = await Promise.all(page.map(async (e) => {
|
|
133
|
+
if (e.type !== "file") return e;
|
|
134
|
+
try {
|
|
135
|
+
return {
|
|
136
|
+
...e,
|
|
137
|
+
size: (await fsp.stat(e.path)).size
|
|
138
|
+
};
|
|
139
|
+
} catch {
|
|
140
|
+
return e;
|
|
141
|
+
}
|
|
142
|
+
}));
|
|
143
|
+
return {
|
|
144
|
+
total,
|
|
145
|
+
offset: options.offset,
|
|
146
|
+
entries: withSizes,
|
|
147
|
+
hasMore,
|
|
148
|
+
...nextOffset !== void 0 ? { nextOffset } : {}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** Lists a folder of a remote storage; the listing gives sizes. */
|
|
152
|
+
async function listRemoteFolder(ls, storage, dir, options) {
|
|
153
|
+
const { total, page, hasMore, nextOffset } = await pageEntries((await ls.listRemoteFilesWithFileStats(storage.handle, dir)).entries.map((e) => ({
|
|
154
|
+
name: e.name,
|
|
155
|
+
type: e.type,
|
|
156
|
+
path: e.fullPath,
|
|
157
|
+
...e.type === "file" ? { size: e.size } : {}
|
|
158
|
+
})), options);
|
|
159
|
+
return {
|
|
160
|
+
total,
|
|
161
|
+
offset: options.offset,
|
|
162
|
+
entries: page,
|
|
163
|
+
hasMore,
|
|
164
|
+
...nextOffset !== void 0 ? { nextOffset } : {}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/** Makes the import handle of a local file; a folder or a relative path gets an error. */
|
|
168
|
+
async function localFileHandle(ls, p) {
|
|
169
|
+
if (!path.isAbsolute(p)) return {
|
|
170
|
+
path: p,
|
|
171
|
+
error: "Not an absolute path."
|
|
172
|
+
};
|
|
173
|
+
try {
|
|
174
|
+
const st = await fsp.stat(p);
|
|
175
|
+
if (!st.isFile()) return {
|
|
176
|
+
path: p,
|
|
177
|
+
error: "Not a file."
|
|
178
|
+
};
|
|
179
|
+
return {
|
|
180
|
+
path: p,
|
|
181
|
+
handle: await ls.getLocalFileHandle(p),
|
|
182
|
+
size: st.size
|
|
183
|
+
};
|
|
184
|
+
} catch (e) {
|
|
185
|
+
return {
|
|
186
|
+
path: p,
|
|
187
|
+
error: errorText(e)
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* The `index://` handles of files of a remote storage. A handle carries what the storage's
|
|
193
|
+
* listing gives, so the folder of each file is listed (once per folder) and the file looked up in
|
|
194
|
+
* it by its full path.
|
|
195
|
+
*/
|
|
196
|
+
async function remoteFileHandles(ls, storage, paths) {
|
|
197
|
+
const folderOf = (p) => {
|
|
198
|
+
const trimmed = trimChars(p, ["/"]);
|
|
199
|
+
const slash = trimmed.lastIndexOf("/");
|
|
200
|
+
return slash < 0 ? "" : trimmed.slice(0, slash);
|
|
201
|
+
};
|
|
202
|
+
const listings = /* @__PURE__ */ new Map();
|
|
203
|
+
return await Promise.all(paths.map(async (p) => {
|
|
204
|
+
const folder = folderOf(p);
|
|
205
|
+
let listing = listings.get(folder);
|
|
206
|
+
if (listing === void 0) {
|
|
207
|
+
listing = ls.listRemoteFilesWithFileStats(storage.handle, folder);
|
|
208
|
+
listings.set(folder, listing);
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
const { entries } = await listing;
|
|
212
|
+
const entry = entries.find((e) => e.fullPath === p || trimChars(e.fullPath, ["/"]) === trimChars(p, ["/"]));
|
|
213
|
+
if (entry === void 0) return {
|
|
214
|
+
path: p,
|
|
215
|
+
error: "No such file in the storage."
|
|
216
|
+
};
|
|
217
|
+
if (entry.type !== "file") return {
|
|
218
|
+
path: p,
|
|
219
|
+
error: "Not a file."
|
|
220
|
+
};
|
|
221
|
+
return {
|
|
222
|
+
path: p,
|
|
223
|
+
handle: entry.handle,
|
|
224
|
+
size: entry.size
|
|
225
|
+
};
|
|
226
|
+
} catch (e) {
|
|
227
|
+
return {
|
|
228
|
+
path: p,
|
|
229
|
+
error: errorText(e)
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
}));
|
|
233
|
+
}
|
|
234
|
+
/** A range clamped to a size: `[from, to)`, empty at or past the end. */
|
|
235
|
+
function clampRange(offset, length, size) {
|
|
236
|
+
const from = clamp(offset, 0, size);
|
|
237
|
+
return {
|
|
238
|
+
from,
|
|
239
|
+
to: clamp(from + length, from, size)
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
function isUtf8Continuation(b) {
|
|
243
|
+
return (b & 192) === 128;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Decodes `bytes`, a range of a file, as text of whole characters:
|
|
247
|
+
* - `skipLead` (the range does not start the file): up to 3 leading continuation bytes, the tail
|
|
248
|
+
* of a character begun before the range, are skipped (`skippedBytes`);
|
|
249
|
+
* - unless `atEnd` (the range ends the file), an incomplete character at the end is left for the
|
|
250
|
+
* next read: `usedBytes` stops before it.
|
|
251
|
+
*
|
|
252
|
+
* Content that is not UTF-8, or holds NUL bytes, is binary: all the bytes, none skipped.
|
|
253
|
+
*/
|
|
254
|
+
function decodeContent(bytes, options = {}) {
|
|
255
|
+
let start = 0;
|
|
256
|
+
if (options.skipLead === true) while (start < 3 && start < bytes.length && isUtf8Continuation(bytes[start])) start++;
|
|
257
|
+
const part = bytes.subarray(start);
|
|
258
|
+
if (part.includes(0)) return {
|
|
259
|
+
encoding: "binary",
|
|
260
|
+
usedBytes: bytes.length
|
|
261
|
+
};
|
|
262
|
+
try {
|
|
263
|
+
const text = new TextDecoder("utf-8", {
|
|
264
|
+
fatal: true,
|
|
265
|
+
ignoreBOM: true
|
|
266
|
+
}).decode(part, { stream: options.atEnd !== true });
|
|
267
|
+
return {
|
|
268
|
+
encoding: "utf-8",
|
|
269
|
+
text,
|
|
270
|
+
skippedBytes: start,
|
|
271
|
+
usedBytes: Buffer.byteLength(text, "utf8")
|
|
272
|
+
};
|
|
273
|
+
} catch {
|
|
274
|
+
return {
|
|
275
|
+
encoding: "binary",
|
|
276
|
+
usedBytes: bytes.length
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/** Characters of a `read_file` result the content may take; the rest is for the other fields. */
|
|
281
|
+
const READ_CONTENT_CHARS = RESULT_CHAR_LIMIT - 1e3;
|
|
282
|
+
/**
|
|
283
|
+
* Cuts decoded content so that the result stays under the result size limit: text to the
|
|
284
|
+
* characters whose JSON fits in {@link READ_CONTENT_CHARS}, base64 to the bytes whose encoding
|
|
285
|
+
* fits. `usedBytes` then counts the bytes kept, so `nextOffset` continues after them.
|
|
286
|
+
*/
|
|
287
|
+
function fitToResult(decoded, base64) {
|
|
288
|
+
if (decoded.encoding === "binary") {
|
|
289
|
+
if (!base64) return decoded;
|
|
290
|
+
const maxBytes = Math.floor(READ_CONTENT_CHARS / 4) * 3;
|
|
291
|
+
return decoded.usedBytes <= maxBytes ? decoded : {
|
|
292
|
+
encoding: "binary",
|
|
293
|
+
usedBytes: maxBytes
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
if (JSON.stringify(decoded.text).length <= READ_CONTENT_CHARS) return decoded;
|
|
297
|
+
let lo = 0;
|
|
298
|
+
let hi = decoded.text.length;
|
|
299
|
+
while (lo < hi) {
|
|
300
|
+
const mid = Math.ceil((lo + hi) / 2);
|
|
301
|
+
if (JSON.stringify(decoded.text.slice(0, mid)).length <= READ_CONTENT_CHARS) lo = mid;
|
|
302
|
+
else hi = mid - 1;
|
|
303
|
+
}
|
|
304
|
+
let cut = lo;
|
|
305
|
+
const code = decoded.text.charCodeAt(cut - 1);
|
|
306
|
+
if (cut > 0 && code >= 55296 && code <= 56319) cut--;
|
|
307
|
+
const text = decoded.text.slice(0, cut);
|
|
308
|
+
return {
|
|
309
|
+
...decoded,
|
|
310
|
+
text,
|
|
311
|
+
usedBytes: Buffer.byteLength(text, "utf8")
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Reads ranges of a local file through the ls driver, by its import handle. The driver takes no
|
|
316
|
+
* signal, so the wait for a read ends when `signal` aborts; the read itself goes on and closes its
|
|
317
|
+
* file.
|
|
318
|
+
*/
|
|
319
|
+
function localSource(ls, kind, handle, size, signal) {
|
|
320
|
+
return {
|
|
321
|
+
kind,
|
|
322
|
+
size,
|
|
323
|
+
read: (range) => raceAbort(() => ls.getLocalFileContent(handle, {
|
|
324
|
+
offset: range.from,
|
|
325
|
+
length: range.to - range.from
|
|
326
|
+
}), signal)
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* The result of `start()`, or a rejection with the reason of `signal` when it aborts first. An
|
|
331
|
+
* aborted signal does not start the work; a work that fails after the abort fails unobserved.
|
|
332
|
+
*/
|
|
333
|
+
function raceAbort(start, signal) {
|
|
334
|
+
if (signal.aborted) return Promise.reject(signal.reason);
|
|
335
|
+
const work = start();
|
|
336
|
+
work.catch(() => {});
|
|
337
|
+
return new Promise((resolve, reject) => {
|
|
338
|
+
const onAbort = () => reject(signal.reason);
|
|
339
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
340
|
+
work.then(resolve, reject).finally(() => signal.removeEventListener("abort", onAbort));
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
/** The error a `read_file` of an `index://` handle gets. */
|
|
344
|
+
const INDEX_HANDLE_ERROR = "An index:// handle names a file in a remote data library; MCP cannot read it from here.";
|
|
345
|
+
const INDEX_HANDLE_HINT = "Give the handle to the block (set_block_data) and read what the block makes of it: blocks that read such a file in their pre-run give its content, or a part of it, as an output blob (a blob+remote:// or blob+local:// handle in get_block_state outputs); read_file reads that handle.";
|
|
346
|
+
/** Resolves what `read_file` was given to a source; an error result when it cannot be read. */
|
|
347
|
+
async function resolveSource(ls, blob, input, signal) {
|
|
348
|
+
if (input.path !== void 0) {
|
|
349
|
+
const p = input.path;
|
|
350
|
+
if (!path.isAbsolute(p)) return errorResult(`Not an absolute path: ${p}`);
|
|
351
|
+
const st = await fsp.stat(p);
|
|
352
|
+
if (!st.isFile()) return errorResult(`Not a file: ${p}`);
|
|
353
|
+
return localSource(ls, "local", await ls.getLocalFileHandle(p), st.size, signal);
|
|
354
|
+
}
|
|
355
|
+
const handle = input.handle ?? "";
|
|
356
|
+
if (isImportHandle(handle)) {
|
|
357
|
+
if (isImportFileHandleIndex(handle)) return errorResult(INDEX_HANDLE_ERROR, INDEX_HANDLE_HINT);
|
|
358
|
+
const upload = handle;
|
|
359
|
+
return localSource(ls, "upload", upload, await ls.getLocalFileSize(upload), signal);
|
|
360
|
+
}
|
|
361
|
+
const parsed = parseBlobHandle(handle);
|
|
362
|
+
if (parsed !== void 0) return {
|
|
363
|
+
kind: "blob",
|
|
364
|
+
size: parsed.kind === "remote" ? parsed.size : (await fsp.stat(blob.getLocalPath(parsed.handle))).size,
|
|
365
|
+
read: (range) => blob.getContent(parsed.handle, {
|
|
366
|
+
range,
|
|
367
|
+
signal
|
|
368
|
+
})
|
|
369
|
+
};
|
|
370
|
+
return errorResult("Not a handle read_file reads.", "Give `path` (an absolute local path), an upload:// import handle (get_file_handles), or a blob+local:// / blob+remote:// handle from a block's outputs.");
|
|
371
|
+
}
|
|
372
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
373
|
+
/** Every import handle in `value`, with its path (`a.b[2]`, `(root)` for the value itself). */
|
|
374
|
+
function importHandlesIn(value) {
|
|
375
|
+
const found = [];
|
|
376
|
+
const walk = (v, p) => {
|
|
377
|
+
if (isImportHandle(v)) found.push({
|
|
378
|
+
path: p || "(root)",
|
|
379
|
+
handle: v
|
|
380
|
+
});
|
|
381
|
+
else if (Array.isArray(v)) v.forEach((item, i) => walk(item, `${p}[${i}]`));
|
|
382
|
+
else if (isPlainObject(v)) for (const [k, item] of Object.entries(v)) walk(item, IDENTIFIER.test(k) ? p === "" ? k : `${p}.${k}` : `${p}[${JSON.stringify(k)}]`);
|
|
383
|
+
};
|
|
384
|
+
walk(value, "");
|
|
385
|
+
return found;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Checks the import handles `next` carries that `current` does not: an `upload://` handle must be
|
|
389
|
+
* signed by this computer and name a file that exists unchanged since the handle was made; an
|
|
390
|
+
* `index://` handle must parse. Handles already stored are not checked: their file may have
|
|
391
|
+
* changed since, and that must not keep the rest of the data from being written.
|
|
392
|
+
*/
|
|
393
|
+
async function invalidNewImportHandles(ls, current, next) {
|
|
394
|
+
const stored = new Set(importHandlesIn(current).map((h) => h.handle));
|
|
395
|
+
const fresh = importHandlesIn(next).filter((h) => !stored.has(h.handle));
|
|
396
|
+
const unique = [...new Set(fresh.map((h) => h.handle))];
|
|
397
|
+
const problems = new Map(await Promise.all(unique.map(async (h) => [h, await handleProblem(ls, h)])));
|
|
398
|
+
return fresh.flatMap(({ path: p, handle }) => {
|
|
399
|
+
const error = problems.get(handle);
|
|
400
|
+
return error === void 0 ? [] : [{
|
|
401
|
+
path: p,
|
|
402
|
+
error
|
|
403
|
+
}];
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
/** What is wrong with an import handle a write would store; `undefined` when it checks out. */
|
|
407
|
+
async function handleProblem(ls, handle) {
|
|
408
|
+
if (isImportFileHandleUpload(handle)) try {
|
|
409
|
+
await ls.getLocalFileSize(handle);
|
|
410
|
+
return;
|
|
411
|
+
} catch (e) {
|
|
412
|
+
return errorText(e);
|
|
413
|
+
}
|
|
414
|
+
try {
|
|
415
|
+
getFilePathFromHandle(handle);
|
|
416
|
+
return;
|
|
417
|
+
} catch {
|
|
418
|
+
return "The index:// handle does not parse.";
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/** The most invalid handles a refusal names. */
|
|
422
|
+
const MAX_INVALID_NAMED = 10;
|
|
423
|
+
/** The refusal of a write that carries invalid new import handles. */
|
|
424
|
+
function invalidHandlesResult(invalid) {
|
|
425
|
+
const named = invalid.slice(0, MAX_INVALID_NAMED).map((h) => `${h.path}: ${h.error}`).join("; ");
|
|
426
|
+
const more = invalid.length > MAX_INVALID_NAMED ? ` (and ${invalid.length - MAX_INVALID_NAMED} more)` : "";
|
|
427
|
+
return errorResult(`The new data has ${invalid.length} import handle(s) that do not check out: ${named}${more}. Nothing was changed.`, "Make the handles with get_file_handles and copy them as they are. A handle of a local file stops checking out when the file is changed, moved or deleted: make a new one.");
|
|
428
|
+
}
|
|
429
|
+
function errorText(e) {
|
|
430
|
+
return e instanceof Error ? e.message : String(e);
|
|
431
|
+
}
|
|
432
|
+
/** The storages, or a page of a folder's entries (`list_files`). */
|
|
433
|
+
async function listFilesResult(ls, input) {
|
|
434
|
+
const { storage, path: dir, pattern, type, offset, limit } = input;
|
|
435
|
+
const storages = await ls.getStorageList();
|
|
436
|
+
if (storage === void 0 && dir === void 0) return textResult({ storages: storages.map((s) => ({
|
|
437
|
+
id: s.id,
|
|
438
|
+
name: s.name,
|
|
439
|
+
kind: storageKind(s),
|
|
440
|
+
initialPath: s.initialFullPath
|
|
441
|
+
})) });
|
|
442
|
+
const options = {
|
|
443
|
+
pattern: pattern === void 0 || pattern === "" ? void 0 : pattern,
|
|
444
|
+
type,
|
|
445
|
+
offset: Math.max(0, Math.floor(offset ?? 0)),
|
|
446
|
+
limit: clampLimit(limit, 100, 500)
|
|
447
|
+
};
|
|
448
|
+
const entry = storage === void 0 ? void 0 : findStorage(storages, storage);
|
|
449
|
+
if (storage !== void 0 && entry === void 0) return unknownStorage(storage, storages);
|
|
450
|
+
try {
|
|
451
|
+
if (entry !== void 0 && storageKind(entry) === "remote") {
|
|
452
|
+
const folder = dir ?? "";
|
|
453
|
+
return textResult({
|
|
454
|
+
storage: entry.id,
|
|
455
|
+
path: folder,
|
|
456
|
+
...await listRemoteFolder(ls, entry, folder, options)
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
const folder = localPath(entry, dir ?? entry?.initialFullPath ?? "/");
|
|
460
|
+
return textResult({
|
|
461
|
+
...entry !== void 0 ? { storage: entry.id } : {},
|
|
462
|
+
path: folder,
|
|
463
|
+
...await listLocalFolder(folder, options)
|
|
464
|
+
});
|
|
465
|
+
} catch (e) {
|
|
466
|
+
if (e instanceof PatternError) return errorResult(e.message, e.hint);
|
|
467
|
+
return errorResult(`Cannot list ${dir ?? "the folder"}: ${errorText(e)}`, "Check the path with list_files on its parent folder.");
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
/** Import handles for files (`get_file_handles`). */
|
|
471
|
+
async function fileHandlesResult(ls, input) {
|
|
472
|
+
const { paths, storage } = input;
|
|
473
|
+
if (paths.length === 0) return errorResult("`paths` is empty.");
|
|
474
|
+
if (paths.length > 200) return errorResult(`${paths.length} paths; at most 200 per call.`, "Split the paths over several calls.");
|
|
475
|
+
let results;
|
|
476
|
+
if (storage === void 0) results = await Promise.all(paths.map((p) => localFileHandle(ls, p)));
|
|
477
|
+
else {
|
|
478
|
+
const storages = await ls.getStorageList();
|
|
479
|
+
const entry = findStorage(storages, storage);
|
|
480
|
+
if (entry === void 0) return unknownStorage(storage, storages);
|
|
481
|
+
results = storageKind(entry) === "remote" ? await remoteFileHandles(ls, entry, paths) : await Promise.all(paths.map((p) => localFileHandle(ls, localPath(entry, p))));
|
|
482
|
+
}
|
|
483
|
+
const handles = results.filter((r) => "handle" in r);
|
|
484
|
+
const errors = results.filter((r) => "error" in r);
|
|
485
|
+
return textResult({
|
|
486
|
+
handles,
|
|
487
|
+
...errors.length > 0 ? { errors } : {}
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* A bounded read of a file (`read_file`). A blob handle is read inside `holdProject` on the
|
|
492
|
+
* project that gave it: while that project is held, it is not unloaded, so the block's downloaded
|
|
493
|
+
* file keeps its owner and is not deleted from the cache between two pages of a read.
|
|
494
|
+
*/
|
|
495
|
+
async function readFileResult(ls, blob, input, signal, holdProject) {
|
|
496
|
+
if (input.path === void 0 === (input.handle === void 0)) return errorResult("Give either `path` or `handle`.");
|
|
497
|
+
if (input.handle !== void 0 && parseBlobHandle(input.handle) !== void 0 && holdProject !== void 0) {
|
|
498
|
+
if (input.projectId === void 0) return errorResult("A blob handle is read with `projectId`: the project whose block outputs gave it.");
|
|
499
|
+
return await holdProject(input.projectId, () => readFileResult(ls, blob, input, signal));
|
|
500
|
+
}
|
|
501
|
+
let source;
|
|
502
|
+
try {
|
|
503
|
+
source = await resolveSource(ls, blob, input, signal);
|
|
504
|
+
} catch (e) {
|
|
505
|
+
return errorResult(`Cannot read ${input.path ?? "the handle"}: ${errorText(e)}`, input.path === void 0 ? "A handle of a local file stops working when the file changes; make a new one with get_file_handles. A blob handle comes from get_block_state outputs." : void 0);
|
|
506
|
+
}
|
|
507
|
+
if ("isError" in source) return source;
|
|
508
|
+
const max = input.transform !== void 0 ? READ_MAX_TRANSFORM_BYTES : READ_MAX_BYTES;
|
|
509
|
+
const min = input.base64 === true ? 1 : 4;
|
|
510
|
+
const length = clamp(Math.floor(input.length ?? 16384), min, max);
|
|
511
|
+
const range = clampRange(Math.floor(input.offset ?? 0), length, source.size);
|
|
512
|
+
let bytes;
|
|
513
|
+
try {
|
|
514
|
+
bytes = range.to > range.from ? await source.read(range) : /* @__PURE__ */ new Uint8Array(0);
|
|
515
|
+
} catch (e) {
|
|
516
|
+
return errorResult(`Read failed: ${errorText(e)}`, signal.aborted ? "The read took too long or was cancelled: try again, or read a smaller range." : void 0);
|
|
517
|
+
}
|
|
518
|
+
let decoded = input.base64 === true ? decodeContent(bytes, { atEnd: true }) : decodeContent(bytes, {
|
|
519
|
+
skipLead: range.from > 0,
|
|
520
|
+
atEnd: range.to >= source.size
|
|
521
|
+
});
|
|
522
|
+
if (input.transform === void 0) decoded = fitToResult(decoded, input.base64 === true);
|
|
523
|
+
const skipped = decoded.encoding === "utf-8" ? decoded.skippedBytes : 0;
|
|
524
|
+
const used = decoded.usedBytes;
|
|
525
|
+
bytes = bytes.subarray(skipped, skipped + used);
|
|
526
|
+
const start = range.from + skipped;
|
|
527
|
+
const end = start + used;
|
|
528
|
+
const meta = {
|
|
529
|
+
source: source.kind,
|
|
530
|
+
size: source.size,
|
|
531
|
+
offset: start,
|
|
532
|
+
bytesRead: used,
|
|
533
|
+
eof: end >= source.size,
|
|
534
|
+
...end < source.size ? { nextOffset: end } : {},
|
|
535
|
+
encoding: decoded.encoding
|
|
536
|
+
};
|
|
537
|
+
const base64 = decoded.encoding === "binary" ? Buffer.from(bytes).toString("base64") : void 0;
|
|
538
|
+
if (input.transform !== void 0) try {
|
|
539
|
+
const result = await safeEval(input.transform, {
|
|
540
|
+
text: decoded.encoding === "utf-8" ? decoded.text : void 0,
|
|
541
|
+
base64,
|
|
542
|
+
size: meta.size,
|
|
543
|
+
offset: meta.offset,
|
|
544
|
+
bytesRead: meta.bytesRead,
|
|
545
|
+
eof: meta.eof
|
|
546
|
+
}, input.transformTimeout ?? DEFAULT_TRANSFORM_TIMEOUT);
|
|
547
|
+
return textResult({
|
|
548
|
+
...meta,
|
|
549
|
+
result
|
|
550
|
+
});
|
|
551
|
+
} catch (e) {
|
|
552
|
+
return transformFailedResult(e, "Check the expression. Available variables: text, base64, size, offset, bytesRead, eof.");
|
|
553
|
+
}
|
|
554
|
+
if (decoded.encoding === "utf-8") return textResult({
|
|
555
|
+
...meta,
|
|
556
|
+
text: decoded.text
|
|
557
|
+
});
|
|
558
|
+
return textResult(input.base64 === true ? {
|
|
559
|
+
...meta,
|
|
560
|
+
base64
|
|
561
|
+
} : meta);
|
|
562
|
+
}
|
|
563
|
+
function registerFileTools(server, ctx) {
|
|
564
|
+
const drivers = () => {
|
|
565
|
+
const kit = ctx.requireMl().internalDriverKit;
|
|
566
|
+
return {
|
|
567
|
+
ls: kit.lsDriver,
|
|
568
|
+
blob: kit.blobDriver
|
|
569
|
+
};
|
|
570
|
+
};
|
|
571
|
+
server.registerTool("list_files", {
|
|
572
|
+
title: "List Files",
|
|
573
|
+
annotations: hints.read,
|
|
574
|
+
description: "List the storages files can be taken from, or the entries of a folder in one. With no arguments: the storages, `[{ id, name, kind: local|remote, initialPath }]` (local: this computer; remote: data libraries of the server). With `storage` (an id) and `path`: the folder's entries `{ name, type: file|dir, path, size? }`, sorted by name, a page at a time (`total`, `hasMore`, `nextOffset`). `path` alone lists a folder of this computer. Listing gives no handles: get_file_handles makes them for the files you pick, and that is how files are given to a block.",
|
|
575
|
+
inputSchema: z.object({
|
|
576
|
+
storage: z.string().optional().describe("Storage id (or name) from the storage list. Omit for this computer."),
|
|
577
|
+
path: z.string().optional().describe("Folder to list. Local: absolute, or relative to the storage's root; omitted, the storage's initial folder. Remote: the folder's path in the storage, '' for its top."),
|
|
578
|
+
pattern: z.string().optional().describe(`JavaScript regular expression the entry name must match (tested anywhere in the name: anchor it with ^…\$ for the whole name), e.g. \`^S\\d+_R[12]\\.fastq\\.gz\$\`. Matching one folder may take at most 500 ms.`),
|
|
579
|
+
type: z.enum(["file", "dir"]).optional().describe("Only files, or only folders."),
|
|
580
|
+
offset: z.number().optional().describe("Entries to skip (default 0)."),
|
|
581
|
+
limit: z.number().optional().describe(`Entries per page (default 100, at most 500).`)
|
|
582
|
+
})
|
|
583
|
+
}, async (input) => listFilesResult(drivers().ls, input));
|
|
584
|
+
server.registerTool("get_file_handles", {
|
|
585
|
+
title: "Get File Handles",
|
|
586
|
+
annotations: hints.read,
|
|
587
|
+
description: `Make import handles for files: this is how files are given to a block. Put a handle into the block's data where the block takes a file (set_block_data). When the block uses the file (its pre-run, which follows every change of the data, or its run), a file of this computer is uploaded to the server, a file of a data library is used where it is. get_block_state shows the progress under \`imports\`; await_block_done with waitForImports waits for it.
|
|
588
|
+
|
|
589
|
+
\`paths\`: absolute paths of files of this computer, or with \`storage\`, paths in that storage (as list_files gives them). Result: \`{ handles: [{ path, handle, size? }], errors?: [{ path, error }] }\`, in the order asked. At most 200 files per call. Copy handles as they are; a local file's handle stops working when the file changes.`,
|
|
590
|
+
inputSchema: z.object({
|
|
591
|
+
paths: z.array(z.string()).describe("File paths."),
|
|
592
|
+
storage: z.string().optional().describe("Storage id (or name) the paths are in. Omit for this computer.")
|
|
593
|
+
})
|
|
594
|
+
}, async (input) => fileHandlesResult(drivers().ls, input));
|
|
595
|
+
server.registerTool("read_file", {
|
|
596
|
+
title: "Read File",
|
|
597
|
+
annotations: hints.read,
|
|
598
|
+
description: `Read a part of a file: a local file (\`path\`), an upload:// import handle, or a blob handle from a block's outputs (blob+local://, blob+remote://). Reads \`length\` bytes from \`offset\` (default ${READ_DEFAULT_BYTES}, at most ${READ_MAX_BYTES}); a range past the end is cut to it. Result: \`{ size, offset, bytesRead, eof, nextOffset?, encoding: utf-8|binary, text? , base64? }\`: text when the bytes are UTF-8, otherwise only their count (base64 with \`base64: true\`). Text keeps whole characters: a range that starts inside one starts after it (\`offset\` says where) and one that ends inside one stops before it (\`nextOffset\`). With \`base64: true\` the range is returned as it is. With \`transform\` the server reads up to 4 MiB and returns only the expression's value. index:// handles (files of a data library) cannot be read here: read the block's output blob instead.`,
|
|
599
|
+
inputSchema: z.object({
|
|
600
|
+
path: z.string().optional().describe("Absolute path of a local file."),
|
|
601
|
+
handle: z.string().optional().describe("An upload:// import handle, or a blob handle from a block's outputs."),
|
|
602
|
+
projectId: projectIdInput.optional().describe("With a blob handle (required): the project whose block outputs gave it."),
|
|
603
|
+
offset: z.number().optional().describe("Byte to start at (default 0)."),
|
|
604
|
+
length: z.number().optional().describe(`Bytes to read (default ${READ_DEFAULT_BYTES}; at most ${READ_MAX_BYTES}, or ${READ_MAX_TRANSFORM_BYTES} with \`transform\`).`),
|
|
605
|
+
base64: z.boolean().optional().describe("Return binary content as base64 (default false: its byte count only)."),
|
|
606
|
+
transform: z.string().optional().describe("JS expression evaluated in the server against the content: `text` (UTF-8 content, or undefined), `base64` (binary content, or undefined), `size`, `offset`, `bytesRead`, `eof`. Example: `text.split('\\n').slice(0, 3)` for the first lines."),
|
|
607
|
+
transformTimeout: z.number().optional().describe(expressionTimeoutText("the transform", DEFAULT_TRANSFORM_TIMEOUT))
|
|
608
|
+
})
|
|
609
|
+
}, async (input, call) => readFileResult(drivers().ls, drivers().blob, input, readSignal(call?.mcpReq.signal), (projectId, read) => ctx.withProject(projectId, () => read())));
|
|
610
|
+
}
|
|
611
|
+
export { INDEX_HANDLE_ERROR, INDEX_HANDLE_HINT, PatternError, READ_CONTENT_CHARS, READ_DEFAULT_BYTES, READ_MAX_BYTES, READ_MAX_TRANSFORM_BYTES, clampLimit, clampRange, decodeContent, fileHandlesResult, findStorage, fitToResult, importHandlesIn, invalidHandlesResult, invalidNewImportHandles, listFilesResult, listLocalFolder, listRemoteFolder, localFileHandle, localStorageRoot, matchNames, pageEntries, parseBlobHandle, readFileResult, registerFileTools, remoteFileHandles, storageKind };
|
|
612
|
+
|
|
613
|
+
//# sourceMappingURL=files.js.map
|