@t2nagi/filebrowser-quantum-mcp 0.0.0-stage → 0.1.0-rc.2

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.
Files changed (49) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +21 -0
  3. package/README.ja.md +335 -0
  4. package/README.md +328 -2
  5. package/dist/client/api.js +222 -0
  6. package/dist/client/errors.js +120 -0
  7. package/dist/client/http.js +172 -0
  8. package/dist/client/types.js +2 -0
  9. package/dist/config.js +168 -0
  10. package/dist/core/approvals.js +75 -0
  11. package/dist/core/confirm.js +66 -0
  12. package/dist/core/content.js +28 -0
  13. package/dist/core/display.js +65 -0
  14. package/dist/core/inspect.js +180 -0
  15. package/dist/core/lock.js +54 -0
  16. package/dist/core/messages.js +131 -0
  17. package/dist/core/names.js +114 -0
  18. package/dist/core/paths.js +41 -0
  19. package/dist/core/reads.js +39 -0
  20. package/dist/core/recovery.js +55 -0
  21. package/dist/core/replace.js +131 -0
  22. package/dist/core/shares.js +46 -0
  23. package/dist/core/sources.js +18 -0
  24. package/dist/core/state.js +154 -0
  25. package/dist/core/text.js +44 -0
  26. package/dist/core/writeflow.js +497 -0
  27. package/dist/index.js +83 -0
  28. package/dist/log.js +32 -0
  29. package/dist/server.js +117 -0
  30. package/dist/tools/archive.js +159 -0
  31. package/dist/tools/common.js +68 -0
  32. package/dist/tools/create-folder.js +94 -0
  33. package/dist/tools/delete.js +195 -0
  34. package/dist/tools/find-duplicates.js +59 -0
  35. package/dist/tools/get-info.js +53 -0
  36. package/dist/tools/index.js +45 -0
  37. package/dist/tools/list-dir.js +68 -0
  38. package/dist/tools/list-sources.js +45 -0
  39. package/dist/tools/move-copy.js +231 -0
  40. package/dist/tools/read-file.js +69 -0
  41. package/dist/tools/rename.js +99 -0
  42. package/dist/tools/search.js +93 -0
  43. package/dist/tools/shares.js +219 -0
  44. package/dist/tools/unarchive.js +147 -0
  45. package/dist/tools/write-common.js +89 -0
  46. package/dist/tools/write-file.js +160 -0
  47. package/dist/version.js +5 -0
  48. package/npm-shrinkwrap.json +1235 -0
  49. package/package.json +57 -4
@@ -0,0 +1,160 @@
1
+ import { z } from "zod";
2
+ import { ToolError } from "../client/errors.js";
3
+ import { approvalCodeArg } from "../core/approvals.js";
4
+ import { Inspector } from "../core/inspect.js";
5
+ import { texts } from "../core/messages.js";
6
+ import { normalizePath } from "../core/paths.js";
7
+ import { replaceViaTemp } from "../core/replace.js";
8
+ import { resolveSource } from "../core/sources.js";
9
+ import { assertWritable, onConflictArg, runWriteFlow } from "../core/writeflow.js";
10
+ import { defineTool, pathArg, sourceArg } from "./common.js";
11
+ import { callInfo, commonFields, commonOutput, conflictsOutput, flowDeps, result, WRITE_ANNOTATIONS, WRITE_NOTE, } from "./write-common.js";
12
+ const inputSchema = z.strictObject({
13
+ approvalCode: approvalCodeArg,
14
+ source: sourceArg,
15
+ path: pathArg("File to write, starting with /. Missing parent folders are created."),
16
+ content: z.string().describe("Text to write (saved as UTF-8)."),
17
+ onConflict: onConflictArg,
18
+ expectedModified: z
19
+ .string()
20
+ .max(64)
21
+ .optional()
22
+ .describe("When overwriting, pass the `modified` value from fb_read_file. If the file changed since, nothing is written."),
23
+ });
24
+ const outputSchema = z.strictObject({
25
+ ...commonOutput,
26
+ ...conflictsOutput,
27
+ source: z.string(),
28
+ path: z.string().describe("The path actually written (a new name if renamed)."),
29
+ bytes: z.number().int().optional(),
30
+ });
31
+ const ISO = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,9}))?(Z|[+-]\d{2}:\d{2})$/;
32
+ /** Comparable key with nanosecond precision ("time as a value", not as text). */
33
+ function timeKey(value) {
34
+ const m = ISO.exec(value);
35
+ if (!m)
36
+ return undefined;
37
+ const seconds = Date.parse(`${m[1]}${m[3]}`) / 1000;
38
+ return Number.isNaN(seconds) ? undefined : `${seconds}.${(m[2] ?? "").padEnd(9, "0")}`;
39
+ }
40
+ export const registerWriteFile = (server, ctx) => defineTool(server, ctx.log, {
41
+ name: "fb_write_file",
42
+ title: "Write text file",
43
+ description: `Create or overwrite a UTF-8 text file. Do not overwrite a file you have not read in full (nextOffset must have been null); pass its \`modified\` as expectedModified. Overwriting replaces the file safely through a temporary name. ${WRITE_NOTE}`,
44
+ inputSchema,
45
+ outputSchema,
46
+ annotations: WRITE_ANNOTATIONS,
47
+ previous: ctx.recovery,
48
+ handler: async (args, extra) => {
49
+ const facts = await ctx.state.ready();
50
+ const source = resolveSource(facts, args.source).name;
51
+ const [info] = assertWritable(ctx.config, facts, [source]);
52
+ const path = normalizePath(args.path);
53
+ const bytes = Buffer.from(args.content, "utf8");
54
+ if (bytes.length > ctx.config.maxWriteBytes) {
55
+ throw new ToolError("too_large", `The content is ${bytes.length} bytes; the limit is ${ctx.config.maxWriteBytes} (FILEBROWSER_MAX_WRITE_BYTES).`, { source, path });
56
+ }
57
+ const expected = args.expectedModified;
58
+ if (expected !== undefined && timeKey(expected) === undefined) {
59
+ throw new ToolError("invalid_argument", "expectedModified must be an ISO 8601 date-time such as 2026-11-29T10:00:00.123456789Z.");
60
+ }
61
+ const canAsk = ctx.elicitor.supported();
62
+ const perms = info?.permissions;
63
+ const fullyRead = (modified) => ctx.reads.isFullyRead(source, path, modified);
64
+ const outcome = await runWriteFlow(flowDeps(ctx, facts), {
65
+ approval: { code: args.approvalCode, args },
66
+ tool: "fb_write_file",
67
+ verb: "write",
68
+ items: [{ toSource: source, to: path, newType: "file", newSize: bytes.length }],
69
+ onConflict: args.onConflict,
70
+ crossSource: false,
71
+ renameStyle: () => "file",
72
+ tempMayRemain: perms?.delete !== true,
73
+ classify: (p) => {
74
+ if (p.existing.kind === "present" && p.existing.type === "directory") {
75
+ throw new ToolError("invalid_argument", "The path is an existing folder.", {
76
+ source,
77
+ path: p.dest,
78
+ });
79
+ }
80
+ return "conflict";
81
+ },
82
+ precheck: (prepared) => {
83
+ const p = prepared[0];
84
+ if (!p || expected === undefined || args.onConflict === "rename")
85
+ return;
86
+ if (p.existing.kind !== "present")
87
+ return;
88
+ if (timeKey(p.existing.modified) !== timeKey(expected)) {
89
+ throw new ToolError("conflict", "The file changed after it was read, so nothing was written. Read it again before writing.", {
90
+ source,
91
+ path: p.dest,
92
+ details: {
93
+ reason: "modified_since_read",
94
+ conflicts: [],
95
+ allowedResolutions: ["rename"],
96
+ currentModified: p.existing.modified,
97
+ },
98
+ });
99
+ }
100
+ },
101
+ blockOverwrite: (p) => p.existing.kind !== "present" ||
102
+ p.existing.size > ctx.config.maxWriteBytes ||
103
+ perms?.create !== true ||
104
+ perms?.modify !== true ||
105
+ (!canAsk && !fullyRead(p.existing.modified)),
106
+ itemWarnings: (p) => p.existing.kind === "present" && !fullyRead(p.existing.modified)
107
+ ? [texts(ctx.config.lang).notFullyRead]
108
+ : [],
109
+ execute: async (finals, run) => {
110
+ const f = finals[0];
111
+ if (!f)
112
+ return;
113
+ if (f.resolution === "overwrite") {
114
+ await replaceViaTemp({
115
+ client: ctx.client,
116
+ facts,
117
+ run,
118
+ source,
119
+ target: f.finalTo,
120
+ approved: f.existing,
121
+ create: (temp) => ctx.client.createFile(source, temp, bytes),
122
+ });
123
+ return;
124
+ }
125
+ // New file (or a free alternative name): POST without override, so an item
126
+ // that appeared meanwhile is refused (409) instead of replaced.
127
+ try {
128
+ await run.send([{ toSource: source, to: f.finalTo }], [], (signal) => ctx.client.createFile(source, f.finalTo, bytes, { signal }));
129
+ }
130
+ catch (err) {
131
+ // A failed upload can leave a partly written file under the new name.
132
+ if (err instanceof ToolError &&
133
+ err.kind !== "timeout" &&
134
+ err.kind !== "already_exists") {
135
+ const left = await new Inspector(ctx.client, facts, {})
136
+ .probe(source, f.finalTo)
137
+ .catch(() => undefined);
138
+ if (left && left.kind !== "absent") {
139
+ run.leftovers.push({
140
+ source,
141
+ path: f.finalTo,
142
+ kind: "partial_copy",
143
+ originalStillExists: false,
144
+ });
145
+ }
146
+ }
147
+ throw err;
148
+ }
149
+ run.createdFolders.push(...f.missing); // the server creates missing parents
150
+ },
151
+ }, callInfo(extra));
152
+ const finalPath = outcome.status === "completed" ? (outcome.finals[0]?.finalTo ?? path) : path;
153
+ return result({
154
+ ...commonFields(outcome),
155
+ source,
156
+ path: finalPath,
157
+ ...(outcome.status === "completed" ? { bytes: bytes.length } : {}),
158
+ }, outcome);
159
+ },
160
+ });
@@ -0,0 +1,5 @@
1
+ // The package version (design §2.1: initialize reports package.json's version).
2
+ // src/ and dist/ both sit directly under the package root, so "../package.json" works for both.
3
+ import { createRequire } from "node:module";
4
+ const require = createRequire(import.meta.url);
5
+ export const VERSION = require("../package.json").version;