diff --git a/src/filesystem/__tests__/structured-content.test.ts b/src/filesystem/__tests__/structured-content.test.ts index 4605b72a8f..9eab649397 100644 --- a/src/filesystem/__tests__/structured-content.test.ts +++ b/src/filesystem/__tests__/structured-content.test.ts @@ -122,6 +122,29 @@ describe('structuredContent schema compliance', () => { // The content should contain success message expect(structuredContent.content).toContain('Successfully moved'); }); + + it('should reject an existing destination without replacing it', async () => { + const sourcePath = path.join(testDir, 'source.txt'); + const destPath = path.join(testDir, 'existing.txt'); + await fs.writeFile(sourcePath, 'source content'); + await fs.writeFile(destPath, 'existing content'); + + const result = await client.callTool({ + name: 'move_file', + arguments: { + source: sourcePath, + destination: destPath + } + }); + + expect(result.isError).toBe(true); + expect(result.content[0]).toMatchObject({ type: 'text' }); + expect((result.content[0] as { text: string }).text).toContain( + 'Destination already exists' + ); + await expect(fs.readFile(sourcePath, 'utf8')).resolves.toBe('source content'); + await expect(fs.readFile(destPath, 'utf8')).resolves.toBe('existing content'); + }); }); describe('list_directory (control - already working)', () => { diff --git a/src/filesystem/index.ts b/src/filesystem/index.ts index 234605bb13..fc43bbffa0 100644 --- a/src/filesystem/index.ts +++ b/src/filesystem/index.ts @@ -631,6 +631,16 @@ server.registerTool( async (args: z.infer) => { const validSourcePath = await validatePath(args.source); const validDestPath = await validatePath(args.destination); + + try { + await fs.lstat(validDestPath); + throw new Error(`Destination already exists: ${args.destination}`); + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") { + throw error; + } + } + await fs.rename(validSourcePath, validDestPath); const text = `Successfully moved ${args.source} to ${args.destination}`; const contentBlock = { type: "text" as const, text };