55
66namespace NetContextServer . Tools ;
77
8+ /// <summary>
9+ /// Provides MCP tools for file system operations, including file listing, project discovery, and file access.
10+ /// </summary>
811[ McpToolType ]
912public static class FileTools
1013{
11- // Shared JsonSerializerOptions instance to improve performance
14+ /// <summary>
15+ /// Default JSON serializer options used across all file tools.
16+ /// </summary>
1217 private static readonly JsonSerializerOptions DefaultJsonOptions = new ( )
1318 {
1419 WriteIndented = true
1520 } ;
1621
22+ /// <summary>
23+ /// Standard error message for directory not found scenarios.
24+ /// </summary>
1725 private static readonly string [ ] ErrorDirectoryNotFound = [ "Error: Directory not found" ] ;
1826
27+ /// <summary>
28+ /// Lists all .NET source files in the specified project directory.
29+ /// </summary>
30+ /// <param name="projectPath">Absolute path to the project directory containing the .cs files.</param>
31+ /// <returns>A JSON string containing an array of file paths.</returns>
1932 [ McpTool ( "list_files" ) ]
2033 [ Description ( "Lists all .NET source files in the specified project directory." ) ]
2134 public static string ListFiles (
2235 [ Description ( "Absolute path to the project directory containing the .cs files" ) ]
2336 string projectPath ) =>
2437 JsonSerializer . Serialize ( FileService . ListFiles ( projectPath ) , DefaultJsonOptions ) ;
2538
39+ /// <summary>
40+ /// Scans the current solution and returns all .csproj files found.
41+ /// </summary>
42+ /// <returns>A JSON string containing an array of project file paths.</returns>
2643 [ McpTool ( "list_projects" ) ]
2744 [ Description ( "Scans the current solution and returns all .csproj files found." ) ]
2845 public static string ListProjects ( ) =>
2946 JsonSerializer . Serialize ( FileService . ListProjects ( ) , DefaultJsonOptions ) ;
3047
48+ /// <summary>
49+ /// Searches a specific directory for .csproj files.
50+ /// </summary>
51+ /// <param name="directory">Absolute path to the directory to search for .csproj files.</param>
52+ /// <returns>A JSON string containing an array of project file paths.</returns>
3153 [ McpTool ( "list_projects_in_dir" ) ]
3254 [ Description ( "Searches a specific directory for .csproj files." ) ]
3355 public static string ListProjectsInDirectory (
3456 [ Description ( "Absolute path to the directory to search for .csproj files" ) ]
3557 string directory ) =>
3658 JsonSerializer . Serialize ( FileService . ListProjectsInDirectory ( directory ) , DefaultJsonOptions ) ;
3759
60+ /// <summary>
61+ /// Returns all .sln files found in the base directory.
62+ /// </summary>
63+ /// <returns>A JSON string containing an array of solution file paths.</returns>
3864 [ McpTool ( "list_solutions" ) ]
3965 [ Description ( "Returns all .sln files found in the base directory." ) ]
4066 public static string ListSolutions ( ) =>
4167 JsonSerializer . Serialize ( FileService . ListSolutions ( ) , DefaultJsonOptions ) ;
4268
69+ /// <summary>
70+ /// Lists all source files in a project directory.
71+ /// </summary>
72+ /// <param name="projectDir">Absolute path to the project directory to scan for source files.</param>
73+ /// <returns>A JSON string containing an array of source file paths.</returns>
4374 [ McpTool ( "list_source_files" ) ]
4475 [ Description ( "Lists all source files in a project directory." ) ]
4576 public static string ListSourceFiles (
4677 [ Description ( "Absolute path to the project directory to scan for source files" ) ]
4778 string projectDir ) =>
4879 JsonSerializer . Serialize ( FileService . ListSourceFiles ( projectDir ) , DefaultJsonOptions ) ;
4980
81+ /// <summary>
82+ /// Reads and returns the contents of a specified file.
83+ /// </summary>
84+ /// <param name="filePath">Absolute path to the file to read.</param>
85+ /// <returns>The contents of the file, or an error message if the file cannot be read.</returns>
5086 [ McpTool ( "open_file" ) ]
5187 [ Description ( "Reads and returns the contents of a specified file." ) ]
5288 public static string OpenFile (
5389 [ Description ( "Absolute path to the file to read" ) ]
5490 string filePath ) =>
5591 FileService . OpenFile ( filePath ) ;
5692
93+ /// <summary>
94+ /// Sets the base directory for all file operations.
95+ /// </summary>
96+ /// <param name="directory">Absolute path to set as the new base directory. Must be a valid, existing directory.</param>
97+ /// <returns>A JSON string containing a confirmation message or an error message.</returns>
5798 [ McpTool ( "set_base_directory" ) ]
5899 [ Description ( "Sets the base directory for all file operations." ) ]
59100 public static string SetBaseDirectory (
@@ -70,6 +111,10 @@ public static string SetBaseDirectory(
70111 return JsonSerializer . Serialize ( new [ ] { $ "Base directory set to: { directory } " } , DefaultJsonOptions ) ;
71112 }
72113
114+ /// <summary>
115+ /// Returns the current base directory used for all file operations.
116+ /// </summary>
117+ /// <returns>A JSON string containing the current base directory and whether it exists.</returns>
73118 [ McpTool ( "get_base_directory" ) ]
74119 [ Description ( "Returns the current base directory used for all file operations." ) ]
75120 public static string GetBaseDirectory ( )
@@ -90,4 +135,4 @@ public static string GetBaseDirectory()
90135 return JsonSerializer . Serialize ( new { Error = ex . Message } , DefaultJsonOptions ) ;
91136 }
92137 }
93- }
138+ }
0 commit comments