@@ -9,66 +9,27 @@ import (
99)
1010
1111// HandleMoveFile moves or renames a file or directory.
12- // Can move files between directories and rename them in a single operation.
13- // Fails if the destination already exists.
1412func (h * Handler ) HandleMoveFile (ctx context.Context , req * mcp.CallToolRequest , input MoveFileInput ) (* mcp.CallToolResult , MoveFileOutput , error ) {
15- // Validate source path
16- if input .Source == "" {
17- return & mcp.CallToolResult {
18- Content : []mcp.Content {& mcp.TextContent {Text : "source is required and must be a non-empty string" }},
19- IsError : true ,
20- }, MoveFileOutput {}, nil
13+ src , dst := h .ValidateSourceDest (input .Source , input .Destination )
14+ if ! src .Ok () {
15+ return src .Result , MoveFileOutput {}, nil
2116 }
22-
23- // Validate destination path
24- if input .Destination == "" {
25- return & mcp.CallToolResult {
26- Content : []mcp.Content {& mcp.TextContent {Text : "destination is required and must be a non-empty string" }},
27- IsError : true ,
28- }, MoveFileOutput {}, nil
29- }
30-
31- // Validate source path against allowed directories
32- validatedSource , err := h .validatePath (input .Source )
33- if err != nil {
34- return & mcp.CallToolResult {
35- Content : []mcp.Content {& mcp.TextContent {Text : err .Error ()}},
36- IsError : true ,
37- }, MoveFileOutput {}, nil
38- }
39-
40- // Validate destination path against allowed directories
41- validatedDest , err := h .validatePath (input .Destination )
42- if err != nil {
43- return & mcp.CallToolResult {
44- Content : []mcp.Content {& mcp.TextContent {Text : err .Error ()}},
45- IsError : true ,
46- }, MoveFileOutput {}, nil
17+ if ! dst .Ok () {
18+ return dst .Result , MoveFileOutput {}, nil
4719 }
4820
4921 // Check if source exists
50- if _ , err := os .Stat (validatedSource ); os .IsNotExist (err ) {
51- return & mcp.CallToolResult {
52- Content : []mcp.Content {& mcp.TextContent {Text : fmt .Sprintf ("source does not exist: %s" , input .Source )}},
53- IsError : true ,
54- }, MoveFileOutput {}, nil
22+ if _ , err := os .Stat (src .Path ); os .IsNotExist (err ) {
23+ return errorResult (fmt .Sprintf ("source does not exist: %s" , input .Source )), MoveFileOutput {}, nil
5524 }
5625
5726 // Check if destination already exists
58- if _ , err := os .Stat (validatedDest ); err == nil {
59- return & mcp.CallToolResult {
60- Content : []mcp.Content {& mcp.TextContent {Text : fmt .Sprintf ("destination already exists: %s" , input .Destination )}},
61- IsError : true ,
62- }, MoveFileOutput {}, nil
27+ if _ , err := os .Stat (dst .Path ); err == nil {
28+ return errorResult (fmt .Sprintf ("destination already exists: %s" , input .Destination )), MoveFileOutput {}, nil
6329 }
6430
65- // Perform the move/rename operation
66- err = os .Rename (validatedSource , validatedDest )
67- if err != nil {
68- return & mcp.CallToolResult {
69- Content : []mcp.Content {& mcp.TextContent {Text : fmt .Sprintf ("failed to move file: %v" , err )}},
70- IsError : true ,
71- }, MoveFileOutput {}, nil
31+ if err := os .Rename (src .Path , dst .Path ); err != nil {
32+ return errorResult (fmt .Sprintf ("failed to move file: %v" , err )), MoveFileOutput {}, nil
7233 }
7334
7435 message := fmt .Sprintf ("Successfully moved %s to %s" , input .Source , input .Destination )
0 commit comments