|
| 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 | +} |
0 commit comments