11package com .github .mcp .server .filesystem .official ;
22
3- import com .github .codeboyzhou .mcp .declarative .util .StringHelper ;
43import io .modelcontextprotocol .server .McpServerFeatures ;
54import io .modelcontextprotocol .spec .McpSchema ;
65import java .util .List ;
@@ -29,16 +28,16 @@ public final class Prompts {
2928 */
3029 public static McpServerFeatures .SyncPromptSpecification find () {
3130 // Step 1: Create a prompt argument with name, description, and required flag.
32- McpSchema .PromptArgument start =
31+ McpSchema .PromptArgument argumentStart =
3332 new McpSchema .PromptArgument ("start" , "The starting path to search, required." , true );
34- McpSchema .PromptArgument name =
33+ McpSchema .PromptArgument argumentName =
3534 new McpSchema .PromptArgument (
3635 "name" ,
37- "The name of the target file or directory to search, supports fuzzy matching, required." ,
36+ "The name of the target file or dir to search, supports fuzzy matching, required." ,
3837 true );
3938
4039 // Step 2: Create a prompt with name, description, and arguments.
41- List <McpSchema .PromptArgument > args = List .of (start , name );
40+ List <McpSchema .PromptArgument > args = List .of (argumentStart , argumentName );
4241 McpSchema .Prompt prompt =
4342 new McpSchema .Prompt (
4443 "find" , "Start from the specified path and recursively search subitems." , args );
@@ -49,25 +48,22 @@ public static McpServerFeatures.SyncPromptSpecification find() {
4948 (exchange , request ) -> {
5049 // Step 4: Create a prompt message with role and content.
5150 Map <String , Object > arguments = request .arguments ();
52- final String startPath = arguments .getOrDefault ("start" , StringHelper .EMPTY ).toString ();
53- final String nameToFind = arguments .getOrDefault ("name" , StringHelper .EMPTY ).toString ();
54- String result ;
55-
56- if (startPath .isBlank ()) {
57- result = "Please provide a valid start path to find." ;
58- } else if (nameToFind .isBlank ()) {
59- result = "Please provide a valid file/directory name to find." ;
60- } else {
61- result =
62- String .format (
63- "Call the MCP tool 'find' to search for files or directories whose name matches: '%s', starting from the specified start path: '%s'" ,
64- nameToFind , startPath );
51+ Object start = arguments .get (argumentStart .name ());
52+ Object name = arguments .get (argumentName .name ());
53+
54+ if (start == null || start .toString ().isBlank ()) {
55+ return result (prompt , "Please provide a valid start path to find." );
56+ }
57+
58+ if (name == null || name .toString ().isBlank ()) {
59+ return result (prompt , "Please provide a valid file/dir name to find." );
6560 }
6661
67- McpSchema .TextContent content = new McpSchema .TextContent (result );
68- McpSchema .PromptMessage message =
69- new McpSchema .PromptMessage (McpSchema .Role .USER , content );
70- return new McpSchema .GetPromptResult (prompt .description (), List .of (message ));
62+ final String result =
63+ String .format (
64+ "Call the MCP tool 'find' to search for files or dirs whose name matches: '%s', starting from the specified path: '%s'" ,
65+ name , start );
66+ return result (prompt , result );
7167 });
7268 }
7369
@@ -80,35 +76,28 @@ public static McpServerFeatures.SyncPromptSpecification find() {
8076 */
8177 public static McpServerFeatures .SyncPromptSpecification read () {
8278 // Step 1: Create a prompt argument with name, description, and required flag.
83- McpSchema .PromptArgument path =
79+ McpSchema .PromptArgument argumentPath =
8480 new McpSchema .PromptArgument (
85- "path" , "The path to read, can be a file or directory , required." , true );
81+ "path" , "The path to read, can be a file or dir , required." , true );
8682
8783 // Step 2: Create a prompt with name, description, and arguments.
88- List <McpSchema .PromptArgument > args = List .of (path );
84+ List <McpSchema .PromptArgument > args = List .of (argumentPath );
8985 McpSchema .Prompt prompt =
90- new McpSchema .Prompt (
91- "read" , "Read a file or list directory contents non-recursively." , args );
86+ new McpSchema .Prompt ("read" , "Read a file or list dir contents non-recursively." , args );
9287
9388 // Step 3: Create a prompt specification with the prompt and the prompt handler.
9489 return new McpServerFeatures .SyncPromptSpecification (
9590 prompt ,
9691 (exchange , request ) -> {
9792 // Step 4: Create a prompt message with role and content.
9893 Map <String , Object > arguments = request .arguments ();
99- final String filepath = arguments .getOrDefault ("path" , StringHelper .EMPTY ).toString ();
100- String result ;
94+ Object path = arguments .get (argumentPath .name ());
10195
102- if (filepath .isBlank ()) {
103- result = "Please provide a valid path to read." ;
104- } else {
105- result = "Call the MCP tool 'read' to read the file or directory: " + filepath ;
96+ if (path == null || path .toString ().isBlank ()) {
97+ return result (prompt , "Please provide a valid path to read." );
10698 }
10799
108- McpSchema .TextContent content = new McpSchema .TextContent (result );
109- McpSchema .PromptMessage message =
110- new McpSchema .PromptMessage (McpSchema .Role .USER , content );
111- return new McpSchema .GetPromptResult (prompt .description (), List .of (message ));
100+ return result (prompt , "Call the MCP tool 'read' to read the file or dir: " + path );
112101 });
113102 }
114103
@@ -121,34 +110,41 @@ public static McpServerFeatures.SyncPromptSpecification read() {
121110 */
122111 public static McpServerFeatures .SyncPromptSpecification delete () {
123112 // Step 1: Create a prompt argument with name, description, and required flag.
124- McpSchema .PromptArgument path =
113+ McpSchema .PromptArgument argumentPath =
125114 new McpSchema .PromptArgument (
126- "path" , "The path to delete, can be a file or directory , required." , true );
115+ "path" , "The path to delete, can be a file or dir , required." , true );
127116
128117 // Step 2: Create a prompt with name, description, and arguments.
129- List <McpSchema .PromptArgument > args = List .of (path );
118+ List <McpSchema .PromptArgument > args = List .of (argumentPath );
130119 McpSchema .Prompt prompt =
131- new McpSchema .Prompt ("delete" , "Delete a file or directory from the filesystem." , args );
120+ new McpSchema .Prompt ("delete" , "Delete a file or dir from the filesystem." , args );
132121
133122 // Step 3: Create a prompt specification with the prompt and the prompt handler.
134123 return new McpServerFeatures .SyncPromptSpecification (
135124 prompt ,
136125 (exchange , request ) -> {
137126 // Step 4: Create a prompt message with role and content.
138127 Map <String , Object > arguments = request .arguments ();
139- final String filepath = arguments .getOrDefault ("path" , StringHelper .EMPTY ).toString ();
140- String result ;
128+ Object path = arguments .get (argumentPath .name ());
141129
142- if (filepath .isBlank ()) {
143- result = "Please provide a valid path to delete." ;
144- } else {
145- result = "Call the MCP tool 'delete' to delete the file or directory: " + filepath ;
130+ if (path == null || path .toString ().isBlank ()) {
131+ return result (prompt , "Please provide a valid path to delete." );
146132 }
147133
148- McpSchema .TextContent content = new McpSchema .TextContent (result );
149- McpSchema .PromptMessage message =
150- new McpSchema .PromptMessage (McpSchema .Role .USER , content );
151- return new McpSchema .GetPromptResult (prompt .description (), List .of (message ));
134+ return result (prompt , "Call the MCP tool 'delete' to delete the file or dir: " + path );
152135 });
153136 }
137+
138+ /**
139+ * Create a prompt result with the given prompt and result.
140+ *
141+ * @param prompt The prompt to use.
142+ * @param result The result to use.
143+ * @return The prompt result.
144+ */
145+ private static McpSchema .GetPromptResult result (McpSchema .Prompt prompt , String result ) {
146+ McpSchema .TextContent content = new McpSchema .TextContent (result );
147+ McpSchema .PromptMessage message = new McpSchema .PromptMessage (McpSchema .Role .USER , content );
148+ return new McpSchema .GetPromptResult (prompt .description (), List .of (message ));
149+ }
154150}
0 commit comments