@@ -480,9 +480,9 @@ func (fs *FilesystemHandler) handleReadFile(
480
480
ctx context.Context ,
481
481
request mcp.CallToolRequest ,
482
482
) (* mcp.CallToolResult , error ) {
483
- path , ok := request .Params . Arguments [ "path" ].( string )
484
- if ! ok {
485
- return nil , fmt . Errorf ( "path must be a string" )
483
+ path , err := request .RequireString ( "path" )
484
+ if err != nil {
485
+ return nil , err
486
486
}
487
487
488
488
// Handle empty or relative paths like "." or "./" by converting to absolute path
@@ -685,13 +685,13 @@ func (fs *FilesystemHandler) handleWriteFile(
685
685
ctx context.Context ,
686
686
request mcp.CallToolRequest ,
687
687
) (* mcp.CallToolResult , error ) {
688
- path , ok := request .Params . Arguments [ "path" ].( string )
689
- if ! ok {
690
- return nil , fmt . Errorf ( "path must be a string" )
688
+ path , err := request .RequireString ( "path" )
689
+ if err != nil {
690
+ return nil , err
691
691
}
692
- content , ok := request .Params . Arguments [ "content" ].( string )
693
- if ! ok {
694
- return nil , fmt . Errorf ( "content must be a string" )
692
+ content , err := request .RequireString ( "content" )
693
+ if err != nil {
694
+ return nil , err
695
695
}
696
696
697
697
// Handle empty or relative paths like "." or "./" by converting to absolute path
@@ -801,9 +801,9 @@ func (fs *FilesystemHandler) handleListDirectory(
801
801
ctx context.Context ,
802
802
request mcp.CallToolRequest ,
803
803
) (* mcp.CallToolResult , error ) {
804
- path , ok := request .Params . Arguments [ "path" ].( string )
805
- if ! ok {
806
- return nil , fmt . Errorf ( "path must be a string" )
804
+ path , err := request .RequireString ( "path" )
805
+ if err != nil {
806
+ return nil , err
807
807
}
808
808
809
809
// Handle empty or relative paths like "." or "./" by converting to absolute path
@@ -920,9 +920,9 @@ func (fs *FilesystemHandler) handleCreateDirectory(
920
920
ctx context.Context ,
921
921
request mcp.CallToolRequest ,
922
922
) (* mcp.CallToolResult , error ) {
923
- path , ok := request .Params . Arguments [ "path" ].( string )
924
- if ! ok {
925
- return nil , fmt . Errorf ( "path must be a string" )
923
+ path , err := request .RequireString ( "path" )
924
+ if err != nil {
925
+ return nil , err
926
926
}
927
927
928
928
// Handle empty or relative paths like "." or "./" by converting to absolute path
@@ -1023,13 +1023,13 @@ func (fs *FilesystemHandler) handleCopyFile(
1023
1023
ctx context.Context ,
1024
1024
request mcp.CallToolRequest ,
1025
1025
) (* mcp.CallToolResult , error ) {
1026
- source , ok := request .Params . Arguments [ "source" ].( string )
1027
- if ! ok {
1028
- return nil , fmt . Errorf ( "source must be a string" )
1026
+ source , err := request .RequireString ( "source" )
1027
+ if err != nil {
1028
+ return nil , err
1029
1029
}
1030
- destination , ok := request .Params . Arguments [ "destination" ].( string )
1031
- if ! ok {
1032
- return nil , fmt . Errorf ( "destination must be a string" )
1030
+ destination , err := request .RequireString ( "destination" )
1031
+ if err != nil {
1032
+ return nil , err
1033
1033
}
1034
1034
1035
1035
// Handle empty or relative paths for source
@@ -1259,13 +1259,13 @@ func (fs *FilesystemHandler) handleMoveFile(
1259
1259
ctx context.Context ,
1260
1260
request mcp.CallToolRequest ,
1261
1261
) (* mcp.CallToolResult , error ) {
1262
- source , ok := request .Params . Arguments [ "source" ].( string )
1263
- if ! ok {
1264
- return nil , fmt . Errorf ( "source must be a string" )
1262
+ source , err := request .RequireString ( "source" )
1263
+ if err != nil {
1264
+ return nil , err
1265
1265
}
1266
- destination , ok := request .Params . Arguments [ "destination" ].( string )
1267
- if ! ok {
1268
- return nil , fmt . Errorf ( "destination must be a string" )
1266
+ destination , err := request .RequireString ( "destination" )
1267
+ if err != nil {
1268
+ return nil , err
1269
1269
}
1270
1270
1271
1271
// Handle empty or relative paths for source
@@ -1396,13 +1396,13 @@ func (fs *FilesystemHandler) handleSearchFiles(
1396
1396
ctx context.Context ,
1397
1397
request mcp.CallToolRequest ,
1398
1398
) (* mcp.CallToolResult , error ) {
1399
- path , ok := request .Params . Arguments [ "path" ].( string )
1400
- if ! ok {
1401
- return nil , fmt . Errorf ( "path must be a string" )
1399
+ path , err := request .RequireString ( "path" )
1400
+ if err != nil {
1401
+ return nil , err
1402
1402
}
1403
- pattern , ok := request .Params . Arguments [ "pattern" ].( string )
1404
- if ! ok {
1405
- return nil , fmt . Errorf ( "pattern must be a string" )
1403
+ pattern , err := request .RequireString ( "pattern" )
1404
+ if err != nil {
1405
+ return nil , err
1406
1406
}
1407
1407
1408
1408
// Handle empty or relative paths like "." or "./" by converting to absolute path
@@ -1520,9 +1520,9 @@ func (fs *FilesystemHandler) handleTree(
1520
1520
ctx context.Context ,
1521
1521
request mcp.CallToolRequest ,
1522
1522
) (* mcp.CallToolResult , error ) {
1523
- path , ok := request .Params . Arguments [ "path" ].( string )
1524
- if ! ok {
1525
- return nil , fmt . Errorf ( "path must be a string" )
1523
+ path , err := request .RequireString ( "path" )
1524
+ if err != nil {
1525
+ return nil , err
1526
1526
}
1527
1527
1528
1528
// Handle empty or relative paths like "." or "./" by converting to absolute path
@@ -1545,18 +1545,14 @@ func (fs *FilesystemHandler) handleTree(
1545
1545
1546
1546
// Extract depth parameter (optional, default: 3)
1547
1547
depth := 3 // Default value
1548
- if depthParam , ok := request .Params .Arguments ["depth" ]; ok {
1549
- if d , ok := depthParam .(float64 ); ok {
1550
- depth = int (d )
1551
- }
1548
+ if depthParam , err := request .RequireFloat ("depth" ); err != nil {
1549
+ depth = int (depthParam )
1552
1550
}
1553
1551
1554
1552
// Extract follow_symlinks parameter (optional, default: false)
1555
1553
followSymlinks := false // Default value
1556
- if followParam , ok := request .Params .Arguments ["follow_symlinks" ]; ok {
1557
- if f , ok := followParam .(bool ); ok {
1558
- followSymlinks = f
1559
- }
1554
+ if followParam , err := request .RequireBool ("follow_symlinks" ); err != nil {
1555
+ followSymlinks = followParam
1560
1556
}
1561
1557
1562
1558
// Validate the path is within allowed directories
@@ -1653,9 +1649,9 @@ func (fs *FilesystemHandler) handleGetFileInfo(
1653
1649
ctx context.Context ,
1654
1650
request mcp.CallToolRequest ,
1655
1651
) (* mcp.CallToolResult , error ) {
1656
- path , ok := request .Params . Arguments [ "path" ].( string )
1657
- if ! ok {
1658
- return nil , fmt . Errorf ( "path must be a string" )
1652
+ path , err := request .RequireString ( "path" )
1653
+ if err != nil {
1654
+ return nil , err
1659
1655
}
1660
1656
1661
1657
// Handle empty or relative paths like "." or "./" by converting to absolute path
@@ -1756,15 +1752,9 @@ func (fs *FilesystemHandler) handleReadMultipleFiles(
1756
1752
ctx context.Context ,
1757
1753
request mcp.CallToolRequest ,
1758
1754
) (* mcp.CallToolResult , error ) {
1759
- pathsParam , ok := request .Params .Arguments ["paths" ]
1760
- if ! ok {
1761
- return nil , fmt .Errorf ("paths parameter is required" )
1762
- }
1763
-
1764
- // Convert the paths parameter to a string slice
1765
- pathsSlice , ok := pathsParam .([]any )
1766
- if ! ok {
1767
- return nil , fmt .Errorf ("paths must be an array of strings" )
1755
+ pathsSlice , err := request .RequireStringSlice ("paths" )
1756
+ if err != nil {
1757
+ return nil , err
1768
1758
}
1769
1759
1770
1760
if len (pathsSlice ) == 0 {
@@ -1795,12 +1785,7 @@ func (fs *FilesystemHandler) handleReadMultipleFiles(
1795
1785
1796
1786
// Process each file
1797
1787
var results []mcp.Content
1798
- for _ , pathInterface := range pathsSlice {
1799
- path , ok := pathInterface .(string )
1800
- if ! ok {
1801
- return nil , fmt .Errorf ("each path must be a string" )
1802
- }
1803
-
1788
+ for _ , path := range pathsSlice {
1804
1789
// Handle empty or relative paths like "." or "./" by converting to absolute path
1805
1790
if path == "." || path == "./" {
1806
1791
// Get current working directory
@@ -1941,9 +1926,9 @@ func (fs *FilesystemHandler) handleDeleteFile(
1941
1926
ctx context.Context ,
1942
1927
request mcp.CallToolRequest ,
1943
1928
) (* mcp.CallToolResult , error ) {
1944
- path , ok := request .Params . Arguments [ "path" ].( string )
1945
- if ! ok {
1946
- return nil , fmt . Errorf ( "path must be a string" )
1929
+ path , err := request .RequireString ( "path" )
1930
+ if err != nil {
1931
+ return nil , err
1947
1932
}
1948
1933
1949
1934
// Handle empty or relative paths like "." or "./" by converting to absolute path
@@ -2003,10 +1988,8 @@ func (fs *FilesystemHandler) handleDeleteFile(
2003
1988
2004
1989
// Extract recursive parameter (optional, default: false)
2005
1990
recursive := false
2006
- if recursiveParam , ok := request .Params .Arguments ["recursive" ]; ok {
2007
- if r , ok := recursiveParam .(bool ); ok {
2008
- recursive = r
2009
- }
1991
+ if recursiveParam , err := request .RequireBool ("recursive" ); err != nil {
1992
+ recursive = recursiveParam
2010
1993
}
2011
1994
2012
1995
// Check if it's a directory and handle accordingly
0 commit comments