Skip to content

Commit a149ec5

Browse files
committed
fix(filesystem): verify write_file output landed on disk (modelcontextprotocol#4138)
write_file in the filesystem server resolved successfully even when no bytes reached disk on Windows. Callers (Claude Desktop and any other MCP client) received "File created successfully" and a normal structured result, with no way to detect that the file was never written. Reported in modelcontextprotocol#4138 against Windows 11 with both isUsingBuiltInNodeForMcp and system Node. Repro ruled out NTFS permissions, Defender, Controlled Folder Access, OneDrive, and paths-with-spaces. The same paths work for edit_file, read_file, and list_directory; only write_file silently no-ops. The host-side root cause varies by environment and isn't fully nailed down, but the failure mode is consistent: fs.writeFile resolves with no error and nothing is on disk afterward. Rather than chase the host layer, this change closes the gap at the boundary we control by adding a post-write verification step to writeFileContent: 1. fs.stat the target file after the write completes. 2. Compare on-disk size to Buffer.byteLength(content, 'utf-8'). 3. Throw an explicit error on miss / mismatch. This converts the silent-success failure mode into a clear "write reported success but the file is missing on disk" error that callers can surface to the user instead of returning a false-positive success. The verification runs for both the fresh-write (wx flag) and atomic- rename (EEXIST) branches. The fix is platform-agnostic: silent success is a bad failure mode on any OS, and the same verification protects against partial writes, antivirus quarantine mid-write, and rename-to-wrong-path bugs in addition to the original Windows symptom. Cost is one extra stat per write_file call. Rebased onto current main. The two modelcontextprotocol#4115 permission tests mock a single fs.stat for the permission read; they now also mock the verification stat, since writeFileContent stats the target a second time.
1 parent d73f99e commit a149ec5

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎src/filesystem/__tests__/lib.test.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,43 @@ describe('Lib Functions', () => {
345345
describe('writeFileContent', () => {
346346
it('writes file content', async () => {
347347
mockFs.writeFile.mockResolvedValueOnce(undefined);
348-
348+
mockFs.stat.mockResolvedValueOnce({ size: Buffer.byteLength('new content', 'utf-8') } as any);
349+
349350
await writeFileContent('/test/file.txt', 'new content');
350-
351+
351352
expect(mockFs.writeFile).toHaveBeenCalledWith('/test/file.txt', 'new content', { encoding: "utf-8", flag: 'wx' });
353+
expect(mockFs.stat).toHaveBeenCalledWith('/test/file.txt');
354+
});
355+
356+
// Regression for #4138: write_file silently returns success on Windows
357+
// even though no bytes reach disk. The post-write stat verification turns
358+
// that silent failure into an explicit error.
359+
it('throws when post-write stat finds the file missing (silent write failure)', async () => {
360+
mockFs.writeFile.mockResolvedValueOnce(undefined);
361+
const enoent = Object.assign(new Error('ENOENT: no such file or directory'), { code: 'ENOENT' });
362+
mockFs.stat.mockRejectedValueOnce(enoent);
363+
364+
await expect(writeFileContent('/test/file.txt', 'payload')).rejects.toThrow(
365+
/reported success but the file is missing on disk/
366+
);
367+
});
368+
369+
it('throws when post-write stat reports a size mismatch', async () => {
370+
mockFs.writeFile.mockResolvedValueOnce(undefined);
371+
// Content is 7 bytes ('payload'); pretend disk shows 0 bytes.
372+
mockFs.stat.mockResolvedValueOnce({ size: 0 } as any);
373+
374+
await expect(writeFileContent('/test/file.txt', 'payload')).rejects.toThrow(
375+
/on-disk size is 0 bytes; expected 7 bytes/
376+
);
377+
});
378+
379+
it('verifies size correctly for multibyte UTF-8 content', async () => {
380+
const content = 'héllo 🌍'; // 12 UTF-8 bytes
381+
mockFs.writeFile.mockResolvedValueOnce(undefined);
382+
mockFs.stat.mockResolvedValueOnce({ size: Buffer.byteLength(content, 'utf-8') } as any);
383+
384+
await expect(writeFileContent('/test/file.txt', content)).resolves.toBeUndefined();
352385
});
353386

354387
it('preserves file permissions when overwriting existing file', async () => {
@@ -360,6 +393,9 @@ describe('Lib Functions', () => {
360393
mockFs.writeFile.mockResolvedValueOnce(undefined);
361394
mockFs.rename.mockResolvedValueOnce(undefined);
362395
mockFs.chmod.mockResolvedValueOnce(undefined);
396+
// Post-write verification stats the target again; report the bytes
397+
// the write was expected to land.
398+
mockFs.stat.mockResolvedValueOnce({ size: Buffer.byteLength('new content', 'utf-8') });
363399

364400
await writeFileContent('/test/script.sh', 'new content');
365401

@@ -373,6 +409,7 @@ describe('Lib Functions', () => {
373409
mockFs.writeFile.mockResolvedValueOnce(undefined);
374410
mockFs.rename.mockResolvedValueOnce(undefined);
375411
mockFs.chmod.mockRejectedValueOnce(Object.assign(new Error('EPERM'), { code: 'EPERM' }));
412+
mockFs.stat.mockResolvedValueOnce({ size: Buffer.byteLength('new content', 'utf-8') });
376413

377414
await expect(writeFileContent('/test/script.sh', 'new content')).resolves.toBeUndefined();
378415
expect(mockFs.unlink).not.toHaveBeenCalled();

‎src/filesystem/lib.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,28 @@ export async function writeFileContent(filePath: string, content: string): Promi
234234
throw error;
235235
}
236236
}
237+
238+
// Post-write verification: stat the target and confirm size matches the
239+
// UTF-8 byte length of the written content. Catches silent write failures
240+
// where writeFile resolves successfully but the bytes never reach disk
241+
// (observed on Windows in #4138; the underlying cause varies by host but
242+
// the failure mode — silent success with no file on disk — is the same).
243+
const expectedSize = Buffer.byteLength(content, 'utf-8');
244+
let stats: Awaited<ReturnType<typeof fs.stat>>;
245+
try {
246+
stats = await fs.stat(filePath);
247+
} catch (statError) {
248+
throw new Error(
249+
`Write to ${filePath} reported success but the file is missing on disk. ` +
250+
`This indicates a silent write failure in the host filesystem layer.`
251+
);
252+
}
253+
if (stats.size !== expectedSize) {
254+
throw new Error(
255+
`Write to ${filePath} reported success but on-disk size is ${stats.size} bytes; ` +
256+
`expected ${expectedSize} bytes.`
257+
);
258+
}
237259
}
238260

239261

0 commit comments

Comments
 (0)