bounded-filesystem-mcp 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Anthropic, PBC
4
+ Copyright (c) 2026 Shivam Singh
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,281 @@
1
- # Temporary Holding Version
1
+ # bounded-filesystem-mcp
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A fork of the [Filesystem MCP server](https://github.com/modelcontextprotocol/servers/tree/2026.1.14/src/filesystem) where every tool result has a size limit. Large files are read in windows instead of being returned in one piece.
4
+
5
+ ## Provenance
6
+
7
+ Vendored from [modelcontextprotocol/servers](https://github.com/modelcontextprotocol/servers), tag [`2026.1.14`](https://github.com/modelcontextprotocol/servers/tree/2026.1.14/src/filesystem) (commit `3e805376da81c063c2798410906b5fd134334a43`), directory `src/filesystem`. This repository's history starts from that snapshot. Upstream is MIT licensed, copyright Anthropic, PBC; see [LICENSE](LICENSE).
8
+
9
+ `lib.ts`, `path-validation.ts`, `path-utils.ts`, `roots-utils.ts` and their tests are unchanged from upstream (only `__tests__/structured-content.test.ts` is adapted, see below), so they can be synced by copying the files. The changes live in `index.ts` (tool registration) and the new `bounded.ts`.
10
+
11
+ ## Differences from upstream
12
+
13
+ - **Result cap.** A text result longer than the cap (default 50,000 characters) keeps its first 50,000 characters, never splitting a surrogate pair. A notice is added on its own line:
14
+ ```
15
+ [Result truncated: showing characters 1-50,000 of 1,234,567. Call again with offset=50000 to continue.]
16
+ ```
17
+ Tools that cannot page say how to narrow the request instead, for example:
18
+ ```
19
+ [Result truncated: showing characters 1-50,000 of 812,345. To see the rest, narrow the request: pass a deeper path, or add excludePatterns.]
20
+ ```
21
+ - **Paged reads.** `read_text_file` and `read_file` take `offset` and `limit`, both counted in characters. Follow the `offset` in each notice to walk the whole file; the last window has no notice. The file is streamed and only the window is kept in memory. `head` and `tail` work as before, but cannot be combined with `offset` or `limit`.
22
+ - **Capped listings.** `read_multiple_files`, `directory_tree`, `search_files`, `list_directory`, `list_directory_with_sizes` and `edit_file`'s diff all use the cap. `read_multiple_files` caps the combined text, and its notice says how many of the requested files were included in full. It reads the files one after another rather than in parallel.
23
+ - **Media size limit.** `read_media_file` refuses files over 5 MB (5,242,880 bytes) with an error naming the file size and the limit. It checks the size before reading any bytes.
24
+ - **Plain text results.** Tools have no `outputSchema` and return no `structuredContent`, only content blocks. Hosts that prefer `structuredContent` would otherwise see the text JSON-escaped and the payload doubled, so a capped result would not be the size the cap promises.
25
+ - The server reports itself as `bounded-filesystem` in `initialize`. Tool names and existing parameters are unchanged.
26
+
27
+ "Characters" here means UTF-16 code units, the same as a JavaScript string's `length`.
28
+
29
+ ## Options
30
+
31
+ | Flag | Environment variable | Default | Meaning |
32
+ | --- | --- | --- | --- |
33
+ | `--max-result-chars <n>` | `BOUNDED_FS_MAX_RESULT_CHARS` | `50000` | Largest text result, in characters, before the truncation notice |
34
+ | `--max-media-bytes <n>` | `BOUNDED_FS_MAX_MEDIA_BYTES` | `5242880` | Largest file `read_media_file` will return |
35
+
36
+ A flag wins over its environment variable. Both flags also accept the `--flag=<n>` form. Everything else on the command line is an allowed directory:
37
+
38
+ ```bash
39
+ bunx bounded-filesystem-mcp@0.1.0 --max-result-chars 40000 /path/to/dir1 /path/to/dir2
40
+ ```
41
+
42
+ ## Features
43
+
44
+ - Read/write files
45
+ - Create/list/delete directories
46
+ - Move files/directories
47
+ - Search files
48
+ - Get file metadata
49
+ - Dynamic directory access control via [Roots](https://modelcontextprotocol.io/docs/learn/client-concepts#roots)
50
+
51
+ ## Directory Access Control
52
+
53
+ The server uses a flexible directory access control system. Directories can be specified via command-line arguments or dynamically via [Roots](https://modelcontextprotocol.io/docs/learn/client-concepts#roots).
54
+
55
+ ### Method 1: Command-line Arguments
56
+ Specify Allowed directories when starting the server:
57
+ ```bash
58
+ bounded-filesystem-mcp /path/to/dir1 /path/to/dir2
59
+ ```
60
+
61
+ ### Method 2: MCP Roots (Recommended)
62
+ MCP clients that support [Roots](https://modelcontextprotocol.io/docs/learn/client-concepts#roots) can dynamically update the Allowed directories.
63
+
64
+ Roots notified by Client to Server, completely replace any server-side Allowed directories when provided.
65
+
66
+ **Important**: If server starts without command-line arguments AND client doesn't support roots protocol (or provides empty roots), the server will throw an error during initialization.
67
+
68
+ This is the recommended method, as this enables runtime directory updates via `roots/list_changed` notifications without server restart, providing a more flexible and modern integration experience.
69
+
70
+ ### How It Works
71
+
72
+ The server's directory access control follows this flow:
73
+
74
+ 1. **Server Startup**
75
+ - Server starts with directories from command-line arguments (if provided)
76
+ - If no arguments provided, server starts with empty allowed directories
77
+
78
+ 2. **Client Connection & Initialization**
79
+ - Client connects and sends `initialize` request with capabilities
80
+ - Server checks if client supports roots protocol (`capabilities.roots`)
81
+
82
+ 3. **Roots Protocol Handling** (if client supports roots)
83
+ - **On initialization**: Server requests roots from client via `roots/list`
84
+ - Client responds with its configured roots
85
+ - Server replaces ALL allowed directories with client's roots
86
+ - **On runtime updates**: Client can send `notifications/roots/list_changed`
87
+ - Server requests updated roots and replaces allowed directories again
88
+
89
+ 4. **Fallback Behavior** (if client doesn't support roots)
90
+ - Server continues using command-line directories only
91
+ - No dynamic updates possible
92
+
93
+ 5. **Access Control**
94
+ - All filesystem operations are restricted to allowed directories
95
+ - Use `list_allowed_directories` tool to see current directories
96
+ - Server requires at least ONE allowed directory to operate
97
+
98
+ **Note**: The server will only allow operations within directories specified either via `args` or via Roots.
99
+
100
+
101
+
102
+ ## API
103
+
104
+ ### Tools
105
+
106
+ - **read_text_file**
107
+ - Read complete contents of a file as text, or a window of it
108
+ - Inputs:
109
+ - `path` (string)
110
+ - `head` (number, optional): First N lines
111
+ - `tail` (number, optional): Last N lines
112
+ - `offset` (integer, optional): Character position to start from (default 0)
113
+ - `limit` (integer, optional): Number of characters to return, clamped to the result cap
114
+ - Always treats the file as UTF-8 text regardless of extension
115
+ - Cannot specify both `head` and `tail` simultaneously, or combine either with `offset`/`limit`
116
+ - Results over the cap end with a notice giving the `offset` to continue from
117
+ - `read_file` is the deprecated alias and behaves the same
118
+
119
+ - **read_media_file**
120
+ - Read an image or audio file
121
+ - Inputs:
122
+ - `path` (string)
123
+ - Streams the file and returns base64 data with the corresponding MIME type
124
+ - Files over `--max-media-bytes` (default 5 MB) return an error without being read
125
+
126
+ - **read_multiple_files**
127
+ - Read multiple files simultaneously
128
+ - Input: `paths` (string[])
129
+ - Failed reads won't stop the entire operation
130
+ - The combined text is capped; the notice says how many files were included in full
131
+
132
+ - **write_file**
133
+ - Create new file or overwrite existing (exercise caution with this)
134
+ - Inputs:
135
+ - `path` (string): File location
136
+ - `content` (string): File content
137
+
138
+ - **edit_file**
139
+ - Make selective edits using advanced pattern matching and formatting
140
+ - Features:
141
+ - Line-based and multi-line content matching
142
+ - Whitespace normalization with indentation preservation
143
+ - Multiple simultaneous edits with correct positioning
144
+ - Indentation style detection and preservation
145
+ - Git-style diff output with context
146
+ - Preview changes with dry run mode
147
+ - Inputs:
148
+ - `path` (string): File to edit
149
+ - `edits` (array): List of edit operations
150
+ - `oldText` (string): Text to search for (can be substring)
151
+ - `newText` (string): Text to replace with
152
+ - `dryRun` (boolean): Preview changes without applying (default: false)
153
+ - Returns detailed diff and match information for dry runs, otherwise applies changes
154
+ - Best Practice: Always use dryRun first to preview changes before applying them
155
+
156
+ - **create_directory**
157
+ - Create new directory or ensure it exists
158
+ - Input: `path` (string)
159
+ - Creates parent directories if needed
160
+ - Succeeds silently if directory exists
161
+
162
+ - **list_directory**
163
+ - List directory contents with [FILE] or [DIR] prefixes
164
+ - Input: `path` (string)
165
+
166
+ - **list_directory_with_sizes**
167
+ - List directory contents with [FILE] or [DIR] prefixes, including file sizes
168
+ - Inputs:
169
+ - `path` (string): Directory path to list
170
+ - `sortBy` (string, optional): Sort entries by "name" or "size" (default: "name")
171
+ - Returns detailed listing with file sizes and summary statistics
172
+ - Shows total files, directories, and combined size
173
+
174
+ - **move_file**
175
+ - Move or rename files and directories
176
+ - Inputs:
177
+ - `source` (string)
178
+ - `destination` (string)
179
+ - Fails if destination exists
180
+
181
+ - **search_files**
182
+ - Recursively search for files/directories that match or do not match patterns
183
+ - Inputs:
184
+ - `path` (string): Starting directory
185
+ - `pattern` (string): Search pattern
186
+ - `excludePatterns` (string[]): Exclude any patterns.
187
+ - Glob-style pattern matching
188
+ - Returns full paths to matches
189
+
190
+ - **directory_tree**
191
+ - Get recursive JSON tree structure of directory contents
192
+ - Inputs:
193
+ - `path` (string): Starting directory
194
+ - `excludePatterns` (string[]): Exclude any patterns. Glob formats are supported.
195
+ - Returns:
196
+ - JSON array where each entry contains:
197
+ - `name` (string): File/directory name
198
+ - `type` ('file'|'directory'): Entry type
199
+ - `children` (array): Present only for directories
200
+ - Empty array for empty directories
201
+ - Omitted for files
202
+ - Output is formatted with 2-space indentation for readability
203
+
204
+ - **get_file_info**
205
+ - Get detailed file/directory metadata
206
+ - Input: `path` (string)
207
+ - Returns:
208
+ - Size
209
+ - Creation time
210
+ - Modified time
211
+ - Access time
212
+ - Type (file/directory)
213
+ - Permissions
214
+
215
+ - **list_allowed_directories**
216
+ - List all directories the server is allowed to access
217
+ - No input required
218
+ - Returns:
219
+ - Directories that this server can read/write from
220
+
221
+ ### Tool annotations (MCP hints)
222
+
223
+ This server sets [MCP ToolAnnotations](https://modelcontextprotocol.io/specification/2025-03-26/server/tools#toolannotations)
224
+ on each tool so clients can:
225
+
226
+ - Distinguish **read‑only** tools from write‑capable tools.
227
+ - Understand which write operations are **idempotent** (safe to retry with the same arguments).
228
+ - Highlight operations that may be **destructive** (overwriting or heavily mutating data).
229
+
230
+ The mapping for filesystem tools is:
231
+
232
+ | Tool | readOnlyHint | idempotentHint | destructiveHint | Notes |
233
+ |-----------------------------|--------------|----------------|-----------------|--------------------------------------------------|
234
+ | `read_text_file` | `true` | – | – | Pure read |
235
+ | `read_media_file` | `true` | – | – | Pure read |
236
+ | `read_multiple_files` | `true` | – | – | Pure read |
237
+ | `list_directory` | `true` | – | – | Pure read |
238
+ | `list_directory_with_sizes` | `true` | – | – | Pure read |
239
+ | `directory_tree` | `true` | – | – | Pure read |
240
+ | `search_files` | `true` | – | – | Pure read |
241
+ | `get_file_info` | `true` | – | – | Pure read |
242
+ | `list_allowed_directories` | `true` | – | – | Pure read |
243
+ | `create_directory` | `false` | `true` | `false` | Re‑creating the same dir is a no‑op |
244
+ | `write_file` | `false` | `true` | `true` | Overwrites existing files |
245
+ | `edit_file` | `false` | `false` | `true` | Re‑applying edits can fail or double‑apply |
246
+ | `move_file` | `false` | `false` | `false` | Move/rename only; repeat usually errors |
247
+
248
+ > Note: `idempotentHint` and `destructiveHint` are meaningful only when `readOnlyHint` is `false`, as defined by the MCP spec.
249
+
250
+ ## Usage
251
+
252
+ MCP clients start the server over stdio:
253
+
254
+ ```json
255
+ {
256
+ "mcpServers": {
257
+ "filesystem": {
258
+ "command": "bunx",
259
+ "args": [
260
+ "bounded-filesystem-mcp@0.1.0",
261
+ "/Users/username/Desktop",
262
+ "/path/to/other/allowed/dir"
263
+ ]
264
+ }
265
+ }
266
+ }
267
+ ```
268
+
269
+ `npx -y bounded-filesystem-mcp@0.1.0 <dirs...>` works the same way.
270
+
271
+ ## Build
272
+
273
+ ```bash
274
+ npm install
275
+ npm run build
276
+ npm test
277
+ ```
278
+
279
+ ## License
280
+
281
+ MIT. The upstream code is copyright Anthropic, PBC; the changes in this fork are copyright Shivam Singh. See [LICENSE](LICENSE).
@@ -0,0 +1,184 @@
1
+ import { createReadStream } from "fs";
2
+ import { StringDecoder } from "string_decoder";
3
+ import { validatePath } from "./lib.js";
4
+ // "Characters" throughout are UTF-16 code units, i.e. JavaScript string length.
5
+ export const DEFAULT_MAX_RESULT_CHARS = 50_000;
6
+ export const DEFAULT_MAX_MEDIA_BYTES = 5 * 1024 * 1024;
7
+ const LIMIT_OPTIONS = {
8
+ "--max-result-chars": { key: "maxResultChars", env: "BOUNDED_FS_MAX_RESULT_CHARS" },
9
+ "--max-media-bytes": { key: "maxMediaBytes", env: "BOUNDED_FS_MAX_MEDIA_BYTES" },
10
+ };
11
+ function parsePositiveInteger(raw, source) {
12
+ if (!/^\d+$/.test(raw.trim()) || Number(raw) < 1 || !Number.isSafeInteger(Number(raw))) {
13
+ throw new Error(`${source} must be a positive integer, got "${raw}"`);
14
+ }
15
+ return Number(raw);
16
+ }
17
+ // Flags win over environment variables, which win over the defaults.
18
+ export function parseServerArgs(argv, env = process.env) {
19
+ const limits = {
20
+ maxResultChars: DEFAULT_MAX_RESULT_CHARS,
21
+ maxMediaBytes: DEFAULT_MAX_MEDIA_BYTES,
22
+ };
23
+ for (const { key, env: envName } of Object.values(LIMIT_OPTIONS)) {
24
+ const value = env[envName];
25
+ if (value !== undefined && value !== "") {
26
+ limits[key] = parsePositiveInteger(value, envName);
27
+ }
28
+ }
29
+ const directories = [];
30
+ for (let i = 0; i < argv.length; i++) {
31
+ const arg = argv[i];
32
+ const [name, inlineValue] = arg.split(/=(.*)/s, 2);
33
+ if (Object.hasOwn(LIMIT_OPTIONS, name)) {
34
+ const value = inlineValue ?? argv[++i];
35
+ if (value === undefined) {
36
+ throw new Error(`${name} needs a value`);
37
+ }
38
+ limits[LIMIT_OPTIONS[name].key] = parsePositiveInteger(value, name);
39
+ }
40
+ else {
41
+ directories.push(arg);
42
+ }
43
+ }
44
+ return { directories, limits };
45
+ }
46
+ const formatCount = (n) => n.toLocaleString("en-US");
47
+ function isHighSurrogate(code) {
48
+ return code >= 0xd800 && code <= 0xdbff;
49
+ }
50
+ function isLowSurrogate(code) {
51
+ return code >= 0xdc00 && code <= 0xdfff;
52
+ }
53
+ function splitsSurrogatePair(text, index) {
54
+ return index > 0 && index < text.length &&
55
+ isHighSurrogate(text.charCodeAt(index - 1)) && isLowSurrogate(text.charCodeAt(index));
56
+ }
57
+ export function pagingNotice(start, end, total) {
58
+ return `[Result truncated: showing characters ${formatCount(start + 1)}-${formatCount(end)} of ${formatCount(total)}. ` +
59
+ `Call again with offset=${end} to continue.]`;
60
+ }
61
+ export function narrowingNotice(end, total, hint, detail = "") {
62
+ return `[Result truncated: showing characters 1-${formatCount(end)} of ${formatCount(total)}. ` +
63
+ `${detail}To see the rest, narrow the request: ${hint}.]`;
64
+ }
65
+ // Where to cut `text` so that at most `maxChars` characters stay and no surrogate pair is split.
66
+ export function cutIndex(text, maxChars) {
67
+ if (text.length <= maxChars) {
68
+ return text.length;
69
+ }
70
+ if (!splitsSurrogatePair(text, maxChars)) {
71
+ return maxChars;
72
+ }
73
+ return maxChars > 1 ? maxChars - 1 : maxChars + 1;
74
+ }
75
+ // Caps a result from a tool that cannot page. `hint` names the parameters that narrow the request.
76
+ export function capText(text, maxChars, hint, detail = "") {
77
+ const end = cutIndex(text, maxChars);
78
+ if (end === text.length) {
79
+ return text;
80
+ }
81
+ return `${text.slice(0, end)}\n${narrowingNotice(end, text.length, hint, detail)}`;
82
+ }
83
+ // Streams the file once, keeping only the requested window in memory, and counts its total length.
84
+ // A window edge that would split a surrogate pair moves so the pair stays whole: the start moves
85
+ // back to include it, the end moves back to leave it for the next window (or forward when the
86
+ // window would otherwise be empty, so paging always advances).
87
+ export async function readTextWindow(filePath, offset, limit) {
88
+ const keepFrom = Math.max(0, offset - 1);
89
+ const keepTo = offset + limit + 1;
90
+ const decoder = new StringDecoder("utf8");
91
+ const kept = [];
92
+ let position = 0;
93
+ const keep = (chunk) => {
94
+ const from = Math.max(keepFrom, position);
95
+ const to = Math.min(keepTo, position + chunk.length);
96
+ if (from < to) {
97
+ kept.push(chunk.slice(from - position, to - position));
98
+ }
99
+ position += chunk.length;
100
+ };
101
+ for await (const chunk of createReadStream(filePath, { highWaterMark: 64 * 1024 })) {
102
+ keep(decoder.write(chunk));
103
+ }
104
+ keep(decoder.end());
105
+ const total = position;
106
+ if (offset > total) {
107
+ throw new Error(`offset ${offset} is past the end of the file, which has ${formatCount(total)} characters`);
108
+ }
109
+ const region = kept.join("");
110
+ let start = offset;
111
+ if (splitsSurrogatePair(region, start - keepFrom)) {
112
+ start -= 1;
113
+ }
114
+ let end = Math.min(start + limit, total);
115
+ if (splitsSurrogatePair(region, end - keepFrom)) {
116
+ end = end - 1 > start ? end - 1 : end + 1;
117
+ }
118
+ return { text: region.slice(start - keepFrom, end - keepFrom), start, end, total };
119
+ }
120
+ export function windowResult(window) {
121
+ if (window.end >= window.total) {
122
+ return window.text;
123
+ }
124
+ return `${window.text}\n${pagingNotice(window.start, window.end, window.total)}`;
125
+ }
126
+ const ENTRY_SEPARATOR = "\n---\n";
127
+ function entryText(entry) {
128
+ if ("error" in entry) {
129
+ const text = `${entry.path}: Error - ${entry.error}`;
130
+ return { shown: text, fullLength: text.length };
131
+ }
132
+ const header = `${entry.path}:\n`;
133
+ const complete = entry.window.end >= entry.window.total;
134
+ return {
135
+ shown: header + entry.window.text + (complete ? "\n" : ""),
136
+ fullLength: header.length + entry.window.total + 1,
137
+ };
138
+ }
139
+ // Joins entries the way upstream's read_multiple_files does, capped as a whole. Entries may hold
140
+ // only the start of a file's text, as long as each one holds at least as much as can still fit.
141
+ export function joinMultipleFiles(entries, maxChars) {
142
+ const parts = entries.map(entryText);
143
+ const joined = parts.map((p) => p.shown).join(ENTRY_SEPARATOR);
144
+ const total = parts.reduce((sum, p) => sum + p.fullLength, 0) + ENTRY_SEPARATOR.length * Math.max(0, parts.length - 1);
145
+ if (total <= maxChars) {
146
+ return joined;
147
+ }
148
+ const end = cutIndex(joined, maxChars);
149
+ let entryEnd = 0;
150
+ let complete = 0;
151
+ for (const [i, part] of parts.entries()) {
152
+ entryEnd += (i > 0 ? ENTRY_SEPARATOR.length : 0) + part.fullLength;
153
+ if (entryEnd > end)
154
+ break;
155
+ complete++;
156
+ }
157
+ const detail = `${complete} of ${entries.length} requested files were included in full. `;
158
+ const hint = "pass fewer paths, or read the remaining files one at a time with read_text_file using offset";
159
+ return `${joined.slice(0, end)}\n${narrowingNotice(end, total, hint, detail)}`;
160
+ }
161
+ // Reads the files in order, each only as far as the cap can still show, then joins them.
162
+ export async function readMultipleFiles(paths, maxChars) {
163
+ const entries = [];
164
+ let used = 0;
165
+ for (const filePath of paths) {
166
+ if (entries.length > 0) {
167
+ used += ENTRY_SEPARATOR.length;
168
+ }
169
+ try {
170
+ const validPath = await validatePath(filePath);
171
+ const header = `${filePath}:\n`;
172
+ const room = Math.max(0, maxChars - used - header.length);
173
+ const window = await readTextWindow(validPath, 0, room + 1);
174
+ entries.push({ path: filePath, window });
175
+ used += header.length + window.total + 1;
176
+ }
177
+ catch (error) {
178
+ const entry = { path: filePath, error: error instanceof Error ? error.message : String(error) };
179
+ entries.push(entry);
180
+ used += entryText(entry).fullLength;
181
+ }
182
+ }
183
+ return joinMultipleFiles(entries, maxChars);
184
+ }