Skip to content

Commit 59db6d9

Browse files
committed
feat(mcp-server-filesystem): Introduce MCP prompts for file operations
1 parent 57fc811 commit 59db6d9

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.github.mcp.examples.server.filesystem;
2+
3+
import io.modelcontextprotocol.server.McpServerFeatures;
4+
import io.modelcontextprotocol.server.McpSyncServer;
5+
import io.modelcontextprotocol.spec.McpSchema;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* A class that defines MCP prompts to the MCP server.
12+
*
13+
* @author codeboyzhou
14+
*/
15+
public class McpPrompts {
16+
17+
/**
18+
* Create a MCP prompt to read complete contents of a file.
19+
* --------------------------------------------------------
20+
* Prompt Arguments:
21+
* path (string): The filepath to read, required.
22+
* --------------------------------------------------------
23+
* Prompt Return:
24+
* "What is the complete contents of the file: /path/to/file.txt"
25+
*
26+
* @return {@link McpServerFeatures.SyncPromptSpecification}
27+
*/
28+
public static McpServerFeatures.SyncPromptSpecification readFile() {
29+
// Step 1: Create a prompt argument with name, description, and required flag.
30+
McpSchema.PromptArgument path = new McpSchema.PromptArgument("path", "The filepath to read, required.", true);
31+
32+
// Step 2: Create a prompt with name, description, and arguments.
33+
McpSchema.Prompt prompt = new McpSchema.Prompt("read_file", "Read complete contents of a file.", List.of(path));
34+
35+
// Step 3: Create a prompt specification with the prompt and the prompt handler.
36+
return new McpServerFeatures.SyncPromptSpecification(prompt, (exchange, request) -> {
37+
// Step 4: Create a prompt message with role and content.
38+
Map<String, Object> arguments = request.arguments();
39+
final String filepath = arguments.get("path").toString();
40+
McpSchema.TextContent content = new McpSchema.TextContent(
41+
String.format("What is the complete contents of the file: %s", filepath)
42+
);
43+
McpSchema.PromptMessage message = new McpSchema.PromptMessage(McpSchema.Role.USER, content);
44+
return new McpSchema.GetPromptResult(prompt.description(), List.of(message));
45+
});
46+
}
47+
48+
/**
49+
* Create a MCP prompt to list files of a directory.
50+
* -------------------------------------------------
51+
* Prompt Arguments:
52+
* path (string): The directory path to list files, required.
53+
* pattern (string): The file name pattern to match, optional, default is {@code ""}.
54+
* recursive (boolean): Whether to list files recursively, optional, default is {@code false}.
55+
* -------------------------------------------------
56+
* Prompt Return:
57+
* "List files in the directory: /path/to/directory, with file name pattern: *.txt, recursively: true"
58+
*
59+
* @return {@link McpServerFeatures.SyncPromptSpecification}
60+
*/
61+
public static McpServerFeatures.SyncPromptSpecification listFiles() {
62+
// Step 1: Create a prompt argument with name, description, and required flag.
63+
McpSchema.PromptArgument path = new McpSchema.PromptArgument("path", "The directory path to list files, required.", true);
64+
McpSchema.PromptArgument pattern = new McpSchema.PromptArgument("pattern", "The file name pattern to match, optional.", false);
65+
McpSchema.PromptArgument recursive = new McpSchema.PromptArgument("recursive", "Whether to list files recursively, optional.", false);
66+
67+
// Step 2: Create a prompt with name, description, and arguments.
68+
McpSchema.Prompt prompt = new McpSchema.Prompt("list_files", "List files of a directory.", List.of(path, pattern, recursive));
69+
70+
// Step 3: Create a prompt specification with the prompt and the prompt handler.
71+
return new McpServerFeatures.SyncPromptSpecification(prompt, (exchange, request) -> {
72+
// Step 4: Create a prompt message with role and content.
73+
Map<String, Object> arguments = request.arguments();
74+
McpSchema.TextContent content = new McpSchema.TextContent(
75+
String.format("List files in the directory: %s, with file name pattern: %s, recursively: %s",
76+
arguments.get("path"), arguments.getOrDefault("pattern", ""), arguments.getOrDefault("recursive", false)
77+
)
78+
);
79+
McpSchema.PromptMessage message = new McpSchema.PromptMessage(McpSchema.Role.USER, content);
80+
return new McpSchema.GetPromptResult(prompt.description(), List.of(message));
81+
});
82+
}
83+
84+
/**
85+
* Add all prompts to the MCP server.
86+
*
87+
* @param server The MCP server to add prompts to.
88+
*/
89+
public static void addAllTo(McpSyncServer server) {
90+
server.addPrompt(readFile());
91+
server.addPrompt(listFiles());
92+
}
93+
94+
}

mcp-server-filesystem/src/main/java/com/github/mcp/examples/server/filesystem/McpSseServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ private void initialize() {
7070
.capabilities(serverCapabilities)
7171
.build();
7272

73-
// Add resources and tools to the MCP server
73+
// Add resources, prompts, and tools to the MCP server
7474
McpResources.addAllTo(server);
75+
McpPrompts.addAllTo(server);
7576
McpTools.addAllTo(server);
7677

7778
// Start the HTTP server

mcp-server-filesystem/src/main/java/com/github/mcp/examples/server/filesystem/McpStdioServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ public static void main(String[] args) {
5252
// Initialize MCP server
5353
McpStdioServer mcpStdioServer = new McpStdioServer();
5454
mcpStdioServer.initialize();
55-
// Add resources and tools to the MCP server
55+
// Add resources, prompts, and tools to the MCP server
5656
McpResources.addAllTo(mcpStdioServer.server);
57+
McpPrompts.addAllTo(mcpStdioServer.server);
5758
McpTools.addAllTo(mcpStdioServer.server);
5859
}
5960

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<!--==================== maven plugin versions ====================-->
2323
<sortpom-maven-plugin.version>4.0.0</sortpom-maven-plugin.version>
2424
<!--==================== dependency versions ======================-->
25-
<mcp-sdk.version>0.8.1</mcp-sdk.version>
25+
<mcp-sdk.version>0.9.0</mcp-sdk.version>
2626
<jetty.version>12.0.18</jetty.version>
2727
<logback.version>1.5.18</logback.version>
2828
</properties>

0 commit comments

Comments
 (0)