|
| 1 | +package com.function; |
| 2 | + |
| 3 | +import com.microsoft.azure.functions.ExecutionContext; |
| 4 | +import com.microsoft.azure.functions.annotation.FunctionName; |
| 5 | +import com.microsoft.azure.functions.annotation.McpPromptArgument; |
| 6 | +import com.microsoft.azure.functions.annotation.McpPromptTrigger; |
| 7 | + |
| 8 | +/** |
| 9 | + * Demonstrates MCP Prompt functions that expose prompt templates to MCP clients. |
| 10 | + * Clients discover prompts via prompts/list and invoke them via prompts/get. |
| 11 | + * |
| 12 | + * Return a plain string (auto-wrapped into a single user message by the host) |
| 13 | + * or a JSON-serialized GetPromptResult for multi-message / rich content responses. |
| 14 | + */ |
| 15 | +public class PromptExamples { |
| 16 | + |
| 17 | + /** |
| 18 | + * A code review prompt with multiple arguments (1 required, 1 optional). |
| 19 | + * Uses McpPromptArgument annotations to define arguments in a strongly-typed way. |
| 20 | + */ |
| 21 | + @FunctionName("CodeReviewPrompt") |
| 22 | + public String codeReviewPrompt( |
| 23 | + @McpPromptTrigger( |
| 24 | + name = "code_review", |
| 25 | + description = "Generates a code review prompt for the given code snippet", |
| 26 | + title = "Code Review") |
| 27 | + String context, |
| 28 | + @McpPromptArgument( |
| 29 | + name = "code", |
| 30 | + argumentName = "code", |
| 31 | + description = "The code to review", |
| 32 | + isRequired = true) |
| 33 | + String code, |
| 34 | + @McpPromptArgument( |
| 35 | + name = "language", |
| 36 | + argumentName = "language", |
| 37 | + description = "The programming language") |
| 38 | + String language, |
| 39 | + final ExecutionContext executionContext) { |
| 40 | + |
| 41 | + executionContext.getLogger().info("Generating code review prompt"); |
| 42 | + |
| 43 | + String lang = (language != null && !language.isEmpty()) ? language : "unknown"; |
| 44 | + String snippet = (code != null && !code.isEmpty()) ? code : "// no code provided"; |
| 45 | + |
| 46 | + return "Please review the following " + lang + " code and suggest improvements:\n\n```" |
| 47 | + + lang + "\n" + snippet + "\n```"; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * A summarize prompt with a single required argument and plain string return. |
| 52 | + * The host auto-wraps the returned string into a PromptMessage with role "user". |
| 53 | + */ |
| 54 | + @FunctionName("SummarizePrompt") |
| 55 | + public String summarizePrompt( |
| 56 | + @McpPromptTrigger( |
| 57 | + name = "summarize", |
| 58 | + description = "Summarizes the provided text", |
| 59 | + title = "Summarize Text") |
| 60 | + String context, |
| 61 | + @McpPromptArgument( |
| 62 | + name = "text", |
| 63 | + argumentName = "text", |
| 64 | + description = "The text to summarize", |
| 65 | + isRequired = true) |
| 66 | + String text, |
| 67 | + final ExecutionContext executionContext) { |
| 68 | + |
| 69 | + executionContext.getLogger().info("Generating summarize prompt"); |
| 70 | + |
| 71 | + String input = (text != null && !text.isEmpty()) ? text : "No text provided"; |
| 72 | + return "Please provide a concise summary of the following text:\n\n" + input; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * A prompt with no arguments. Tests the edge case of a prompt |
| 77 | + * that takes no user input. |
| 78 | + */ |
| 79 | + @FunctionName("NoArgsPrompt") |
| 80 | + public String noArgsPrompt( |
| 81 | + @McpPromptTrigger( |
| 82 | + name = "no_args_prompt", |
| 83 | + description = "A prompt that requires no arguments", |
| 84 | + title = "No Arguments Prompt") |
| 85 | + String context, |
| 86 | + final ExecutionContext executionContext) { |
| 87 | + |
| 88 | + executionContext.getLogger().info("Generating no-args prompt"); |
| 89 | + return "This prompt requires no arguments. Please provide general guidance."; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * A prompt with inline promptArguments JSON instead of McpPromptArgument annotations. |
| 94 | + * This is the alternative approach — useful when arguments are static/known at compile time. |
| 95 | + */ |
| 96 | + @FunctionName("InlineArgsPrompt") |
| 97 | + public String inlineArgsPrompt( |
| 98 | + @McpPromptTrigger( |
| 99 | + name = "inline_args_prompt", |
| 100 | + description = "A prompt with arguments defined via inline JSON", |
| 101 | + title = "Inline Arguments Prompt", |
| 102 | + promptArguments = "[{\"name\":\"topic\",\"description\":\"The topic to discuss\",\"required\":false}," |
| 103 | + + "{\"name\":\"style\",\"description\":\"The writing style\",\"required\":false}]") |
| 104 | + String context, |
| 105 | + final ExecutionContext executionContext) { |
| 106 | + |
| 107 | + executionContext.getLogger().info("Generating inline-args prompt"); |
| 108 | + return "Please write about the specified topic in the requested style."; |
| 109 | + } |
| 110 | +} |
0 commit comments