From 4a6e2e29d2162be92cb6e24378c1844721a86dd9 Mon Sep 17 00:00:00 2001 From: re2zero Date: Thu, 20 Aug 2026 02:13:28 +0800 Subject: [PATCH] fix(filesystem): reject move_file when destination exists The move_file tool's documentation states it fails if the destination exists, but the implementation used fs.rename directly without checking, silently overwriting the destination. Add an existence check via fs.stat before the rename, matching the documented behavior. If the destination exists, throw an error immediately. Fixes #4628 --- src/filesystem/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/filesystem/index.ts b/src/filesystem/index.ts index 234605bb13..a5c9caf0e1 100644 --- a/src/filesystem/index.ts +++ b/src/filesystem/index.ts @@ -631,6 +631,20 @@ server.registerTool( async (args: z.infer) => { const validSourcePath = await validatePath(args.source); const validDestPath = await validatePath(args.destination); + + // Check if destination exists (documented behavior: fail if destination exists) + try { + await fs.stat(validDestPath); + throw new Error(`Destination already exists: ${args.destination}`); + } catch (err: any) { + if (err instanceof Error && err.message.startsWith("Destination already exists")) { + throw err; + } + if ((err as NodeJS.ErrnoException).code !== "ENOENT") { + throw err; + } + } + await fs.rename(validSourcePath, validDestPath); const text = `Successfully moved ${args.source} to ${args.destination}`; const contentBlock = { type: "text" as const, text };