MCP プロンプト トリガーを使用して、 モデル コンテキスト プロトコル (MCP) サーバーでプロンプト エンドポイントを定義します。 クライアントは、言語モデルを操作するときに、プロンプトを使用して構造化されたメッセージと命令を生成できます。 プロンプトはユーザーが制御します。つまり、ユーザーが使用するために選択できるように、サーバーからクライアントに公開されます。
セットアップと構成の詳細については、overviewを参照してください。
例
このバインドに関しては現在、Goのサポートは利用できません。
Note
C# の場合、Azure Functions MCP 拡張機能では、isolated worker モデルのみがサポートされます。
このコードは、コード レビュー プロンプトを公開するエンドポイントを作成します。
[Function(nameof(CodeReviewChecklist))]
public string CodeReviewChecklist(
[McpPromptTrigger(CodeReviewPromptName, Description = CodeReviewPromptDescription)]
PromptInvocationContext context)
{
logger.LogInformation("Code review checklist prompt invoked.");
return """
You are a senior software engineer performing a code review.
Use the following checklist to evaluate the code:
1. **Correctness** — Does the code do what it's supposed to?
2. **Error Handling** — Are edge cases and failures handled?
3. **Security** — Are there any vulnerabilities (injection, auth, secrets)?
4. **Performance** — Are there obvious inefficiencies?
5. **Readability** — Is the code clear and well-named?
6. **Tests** — Are there adequate tests for the changes?
Provide your feedback in a structured format with a severity level
(critical, warning, suggestion) for each finding.
""";
}
このコードは、 topic と audienceの 2 つの引数を受け取る要約プロンプトを公開するエンドポイントを作成します。
[Function(nameof(SummarizeContent))]
public string SummarizeContent(
[McpPromptTrigger(SummarizePromptName, Description = SummarizePromptDescription)]
PromptInvocationContext context,
[McpPromptArgument("topic", "The topic or content to summarize.", isRequired: true)]
string topic,
[McpPromptArgument("audience", "Target audience (e.g., 'executive', 'developer', 'beginner').")]
string? audience)
{
logger.LogInformation("Summarize prompt invoked for topic: {Topic}", topic);
var audienceInstruction = audience is not null
? $"Tailor the summary for a **{audience}** audience."
: "Write the summary for a general technical audience.";
return $"""
Summarize the following topic concisely and accurately:
**Topic:** {topic}
{audienceInstruction}
Guidelines:
- Start with a one-sentence overview.
- Include 3–5 key points as bullet items.
- End with a brief conclusion or recommendation.
- Keep the total length under 300 words.
""";
}
Program.cs ビルダーを使用して、プロンプトのプロンプト引数を ConfigureMcpPrompt で構成することもできます。
var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();
builder
.ConfigureMcpPrompt(SummarizePromptName)
.WithArgument("topic", "The topic or content to summarize.", required: true)
.WithArgument("audience", "Target audience (e.g., 'executive', 'developer', 'beginner').");
builder.Build().Run();
完全なコード例については、GitHubの FunctionsMcpPrompts サンプルを参照してください。
Tip
上記の例では、 Program.cs と関数の両方で "code_review" プロンプトの名前などにリテラル文字列を使用しています。 代わりに、共有定数文字列を使用して、プロジェクト間で同期を維持することを検討してください。
このコードは、複数の引数 (1 つは必須、1 つは省略可能) を含むコード レビュー プロンプトを公開するエンドポイントを作成します。
@FunctionName("CodeReviewPrompt")
public String codeReviewPrompt(
@McpPromptTrigger(
name = "code_review",
description = "Generates a code review prompt for the given code snippet",
title = "Code Review")
String context,
@McpPromptArgument(
name = "code",
description = "The code to review",
isRequired = true)
String code,
@McpPromptArgument(
name = "language",
description = "The programming language")
String language,
final ExecutionContext executionContext) {
executionContext.getLogger().info("Generating code review prompt");
String lang = (language != null && !language.isEmpty()) ? language : "unknown";
String snippet = (code != null && !code.isEmpty()) ? code : "// no code provided";
return "Please review the following " + lang + " code and suggest improvements:\n\n```"
+ lang + "\n" + snippet + "\n```";
}
このコードでは、1 つの必須引数を含む集計プロンプトを公開するエンドポイントを作成します。
@FunctionName("SummarizePrompt")
public String summarizePrompt(
@McpPromptTrigger(
name = "summarize",
description = "Summarizes the provided text",
title = "Summarize Text")
String context,
@McpPromptArgument(
name = "text",
description = "The text to summarize",
isRequired = true)
String text,
final ExecutionContext executionContext) {
executionContext.getLogger().info("Generating summarize prompt");
String input = (text != null && !text.isEmpty()) ? text : "No text provided";
return "Please provide a concise summary of the following text:\n\n" + input;
}
完全なコード例については、GitHubの PromptExamples.java サンプルを参照してください。
Note
MCP プロンプトのサポートには、バージョン 3.3.0 以降とバージョン 1.42.0 以降azure-functions-java-libraryazure-functions-maven-plugin必要があります。 プレビュー拡張機能バンドルを使用するように pom.xml を更新します。
<extensionBundle>
<id>Microsoft.Azure.Functions.ExtensionBundle.Preview</id>
<version>[4.41, 5.0.0)</version>
</extensionBundle>
JavaScript のコード例は現在使用できません。 Node.jsを使用した一般的なガイダンスについては、TypeScript の例を参照してください。
このコードは、コード レビュー プロンプトを公開するエンドポイントを作成します。
app.mcpPrompt('CodeReviewChecklist', {
promptName: CodeReviewPromptName,
description: CodeReviewPromptDescription,
handler: async (_ctx: PromptInvocationContext, context: InvocationContext) => {
context.log('Code review checklist prompt invoked.');
return [
"You are a senior software engineer performing a code review.",
'Use the following checklist to evaluate the code:',
'',
"1. **Correctness** \u2014 Does the code do what it's supposed to?",
'2. **Error Handling** \u2014 Are edge cases and failures handled?',
'3. **Security** \u2014 Are there any vulnerabilities (injection, auth, secrets)?',
'4. **Performance** \u2014 Are there obvious inefficiencies?',
'5. **Readability** \u2014 Is the code clear and well-named?',
'6. **Tests** \u2014 Are there adequate tests for the changes?',
'',
'Provide your feedback in a structured format with a severity level',
'(critical, warning, suggestion) for each finding.',
].join('\n');
},
});
このコードでは、引数を含むドキュメント生成プロンプトを公開するエンドポイントを作成します。
app.mcpPrompt('GenerateDocumentation', {
promptName: GenerateDocsPromptName,
description: GenerateDocsPromptDescription,
promptArguments: {
function_name: promptArg.describe("The function to document.").isRequired(),
style: promptArg.describe("Documentation style (e.g., 'concise', 'verbose')."),
},
handler: async (ctx: PromptInvocationContext, context: InvocationContext) => {
const functionName = ctx.arguments.function_name ?? '(unknown)';
const style = ctx.arguments.style ?? 'concise';
context.log(`Generate docs prompt invoked for function: ${functionName}`);
return [
`Generate API documentation for the function named **${functionName}**.`,
'',
`Documentation style: **${style}**`,
'',
'Include the following sections:',
'- **Description** \u2014 What the function does.',
'- **Parameters** \u2014 List each parameter with its type and purpose.',
'- **Return Value** \u2014 What the function returns.',
'- **Example Usage** \u2014 A short code example showing how to call it.',
].join('\n');
},
});
完全なコード例については、GitHubの mcp-prompts サンプルを参照してください。
Note
MCP プロンプトのサポートには、プレビュー拡張機能バンドルとバージョン 4.14.0 以降 @azure/functions が必要です。 プレビュー バンドルを使用するように host.json を更新します。
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle.Preview",
"version": "[4.41, 5.0.0)"
}
また、 package.json が "@azure/functions": "^4.14.0"参照していることを確認します。
このコードでは、 mcp_prompt_trigger デコレーターを使用してエンドポイントを作成し、 code_review_checklistという名前のプロンプトを公開します。
@app.mcp_prompt_trigger(
arg_name="context",
prompt_name="code_review_checklist",
description="Returns a structured code review checklist prompt for evaluating code changes."
)
def code_review_checklist(context: func.PromptInvocationContext) -> str:
logging.info("Code review checklist prompt invoked.")
return """You are a senior software engineer performing a code review.
Use the following checklist to evaluate the code:
1. **Correctness** — Does the code do what it's supposed to?
2. **Error Handling** — Are edge cases and failures handled?
3. **Security** — Are there any vulnerabilities (injection, auth, secrets)?
4. **Performance** — Are there obvious inefficiencies?
5. **Readability** — Is the code clear and well-named?
6. **Tests** — Are there adequate tests for the changes?
Provide your feedback in a structured format with a severity level
(critical, warning, suggestion) for each finding."""
このコードでは、API ドキュメントを生成するための引数を含むプロンプトを公開するエンドポイントを作成します。
@app.mcp_prompt_trigger(
arg_name="context",
prompt_name="generate_documentation",
prompt_arguments=[
func.PromptArgument("function_name", "The name of the function to document.", required=False),
func.PromptArgument("style", "Documentation style: 'concise', 'detailed', or 'tutorial'.", required=False)
],
description="Generates API documentation for a function. Arguments are configured in Program.cs."
)
def generate_documentation(context: func.PromptInvocationContext) -> str:
function_name = context.arguments.get("function_name", "(unknown)")
style = context.arguments.get("style", "concise")
logging.info(f"Generate docs prompt invoked for function: {function_name}")
return f"""Generate API documentation for the function named **{function_name}**.
Documentation style: **{style}**
Include the following sections:
- **Description** — What the function does.
- **Parameters** — List each parameter with its type and purpose.
- **Return Value** — What the function returns.
- **Example Usage** — A short code example showing how to call it."""
完全なコード例については、GitHubの FunctionsMcpPrompts サンプルを参照してください。
Note
MCP プロンプトのサポートには、プレビュー拡張機能バンドルとバージョン 2.2.0b2 以降 azure-functions が必要です。 プレビュー バンドルを使用するように host.json を更新します。
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle.Preview",
"version": "[4.41, 5.0.0)"
}
また、 requirements.txt に azure-functions>=2.2.0b2が含まれていることを確認します。
Important
MCP 拡張機能は現在、PowerShell アプリをサポートしていません。
属性
C# ライブラリでは、 McpPromptTriggerAttribute を使用して関数トリガーを定義します。
この属性のコンストラクターは、次のパラメーターを受け取ります。
| パラメーター | Description |
|---|---|
| PromptName | (必須)MCP トリガー エンドポイントが公開するプロンプトの名前。 |
この属性は、次の名前付きプロパティもサポートしています。
| 財産 | Description |
|---|---|
| Title | (省略可能)MCP クライアント インターフェイスでの表示用の人間が判読できるタイトル。 |
| 説明 | (省略可能)クライアントのプロンプト エンドポイントのわかりやすい説明。 |
| PromptArguments | (省略可能)プロンプト引数スキーマの JSON シリアル化された文字列表現。 引数を指定する別の方法として、 McpPromptArgument 属性を使用することもできます。 |
| Metadata | (省略可能)プロンプトのメタデータの JSON シリアル化文字列。 |
| アイコン | (省略可能)クライアント インターフェイスに表示するためのアイコン定義の JSON シリアル化文字列。 |
入力パラメーターとしてプロンプトの引数を定義する方法については、「 使用法 」を参照してください。
Annotations
@McpPromptTrigger注釈を使用して、リモート MCP サーバーでプロンプト エンドポイントを公開する関数を作成します。
注釈は、次の構成オプションをサポートしています。
| パラメーター | Description |
|---|---|
| name | (必須)バインド パラメーター名と一意のプロンプト識別子。 |
| 説明 | (省略可能)クライアントのプロンプト エンドポイントのわかりやすい説明。 |
| title | (省略可能)MCP クライアント インターフェイスでの表示用の人間が判読できるタイトル。 |
| promptArguments | (省略可能) McpPromptArgument 注釈の代わりに、引数定義のインライン JSON 配列。 |
| メタデータ | (省略可能)プロンプトのメタデータの JSON シリアル化文字列。 |
| アイコン | (省略可能)クライアント インターフェイスに表示するためのアイコン定義の JSON シリアル化文字列。 |
@McpPromptArgument注釈を使用して、個々のプロンプト引数を定義します。 関数内の各引数パラメーターにこの注釈を付けます。
@McpPromptArgument注釈では、次の構成オプションがサポートされています。
| パラメーター | Description |
|---|---|
| name | (必須)バインド パラメーター名と MCP プロトコル引数識別子の両方として使用される引数名。 |
| 説明 | (省略可能)引数が表す内容の説明。 |
| isRequired | (省略可能) trueに設定されている場合は、プロンプトを呼び出すときに引数が必要です。 既定値は false です。 |
デコレータ
Python v2 プログラミング モデルにのみ適用されます。
mcp_prompt_triggerでは、次の MCP プロンプト トリガー プロパティがサポートされています。
| 財産 | Description |
|---|---|
| arg_name | プロンプト呼び出しコンテキストにアクセスするために関数コードで使用される変数名 (通常は context)。 |
| prompt_name | (必須)関数エンドポイントによって公開される MCP サーバー プロンプトの名前。 |
| 説明 | 関数エンドポイントが公開する MCP サーバー プロンプトの説明。 |
| title | MCP クライアント インターフェイスでの表示用のオプションのタイトル。 |
| prompt_arguments | プロンプトがクライアントから受け入れる引数を定義する PromptArgument オブジェクトの一覧。 |
コンフィギュレーション
コードでトリガーのバインド オプションを定義します。 次の表では、各オプションについて説明します。
| Option | Description |
|---|---|
| type |
mcpPromptTrigger に設定します。 ジェネリック定義でのみ使用します。 |
| promptName | (必須)関数エンドポイントが公開する MCP サーバー プロンプトの名前。 |
| 説明 | 関数エンドポイントが公開する MCP サーバー プロンプトの説明。 |
| promptArguments |
promptArg ヘルパーを使用してプロンプト引数を定義するオブジェクト。 各キーは引数名であり、値は引数を記述して構成します。 |
| ハンドラー | 実際の関数コードを含むメソッド。 |
完全な例については、「例」 セクションを参照してください。
使用方法
MCP プロンプト トリガーは、次の種類にバインドできます。
| タイプ | Description |
|---|---|
| PromptInvocationContext | プロンプト名、引数、セッション ID、トランスポート情報など、プロンプト呼び出しを表すオブジェクト。 |
PromptInvocationContext型には、次のプロパティがあります。
| 財産 | タイプ | Description |
|---|---|---|
| 氏名 | string |
呼び出されるプロンプトの名前。 |
| 引数 | Dictionary<string, string>? |
プロンプト呼び出しに指定された引数。 |
| SessionId | string? |
現在のプロンプト呼び出しに関連付けられているセッション ID。 |
| 輸送 | Transport? |
現在の呼び出しのトランスポート情報。 |
@McpPromptTrigger注釈は、プロンプト呼び出しコンテキストを JSON 文字列として含むString パラメーターにバインドされます。 トリガー関数は、 @McpPromptArgumentで注釈が付けられたパラメーターを使用して引数の値を受け取ります。
プロンプト ハンドラー関数には、次の 2 つのパラメーターがあります。
| パラメーター | タイプ | Description |
|---|---|---|
| Ctx | PromptInvocationContext |
プロンプト呼び出しコンテキスト。プロンプトの name、 arguments、 sessionId、および transport 情報が含まれます。 |
| context | InvocationContext |
Azure Functions呼び出しコンテキスト。ログやその他のランタイム情報を提供します。 |
Prompt 引数
MCP クライアントは、プロンプト メッセージを生成するためのデータとコンテキストを提供するために、引数を指定してプロンプトを呼び出します。 クライアントは、プロンプトがプロトコルの一部としてアドバタイズする引数定義に基づいて、これらの引数を収集して渡す方法を知っています。 プロンプトの引数は、関数コードで定義します。
プロンプト引数を定義するときは、既定で省略可能にします。 クライアントは、プロンプトを呼び出すときに省略できます。 プロンプトなしでは操作できない場合は、引数を必須として明示的にマークします。
C# では、プロンプトの引数を複数の方法で定義できます。 どの方法を使用するかは、コード スタイルの好みの問題です。 オプションは次のとおりです。
- 関数は、
McpPromptArgument属性を使用して入力パラメーターを受け取ります。 -
FunctionsApplicationBuilderを使用して、Program.csファイルで引数を定義します。
McpPromptArgument属性を関数の入力バインド スタイルパラメーターに適用して、1 つ以上のプロンプト引数を定義します。
McpPromptArgumentAttribute型では、次のプロパティがサポートされています。
| 財産 | Description |
|---|---|
| ArgumentName | クライアントに公開されるプロンプト引数の名前。 |
| 説明 | 引数が表す内容の説明。 |
| 必須 | (省略可能) trueに設定すると、プロンプトを呼び出すときに prompt 引数が必要になります。 既定値は false です。 |
これらの属性は、例の CodeReviewPrompt で使用 されています。
プロンプト引数は、トリガー定義の prompt_arguments フィールド ( PromptArgument オブジェクトの一覧) で構成できます。
PromptArgumentは次のように構築されます。
func.PromptArgument("argument_name", "Description of the argument", required=True)
PromptArgumentのフィールドは次のとおりです。
| 財産 | Description |
|---|---|
| name | クライアントに公開するプロンプト引数の名前。 |
| 説明 | 引数が表す内容の説明。 |
| required | (省略可能) Trueに設定されている場合は、プロンプトを呼び出すときに引数が必要です。 既定値は False です。 |
Javaでは、個々の関数パラメーターに対して @McpPromptArgument 注釈を使用してプロンプト引数を定義します。 この注釈を使用してプロンプト引数を表す各パラメーターに注釈を付けます。 引数の名前、説明、および必須かどうかを指定します。
これらの注釈は 、例で使用されています。
promptArguments: {
code: promptArg.describe("The code to review").isRequired(),
language: promptArg.describe("The programming language"),
}
戻り値の型
MCP プロンプト トリガーでは、次の戻り値の型がサポートされます。
| タイプ | Description |
|---|---|
string |
MCP GetPromptResultで単一のユーザー ロール テキスト メッセージとして返されます。 |
MCP プロンプト トリガーでは、次の戻り値の型がサポートされます。
| タイプ | Description |
|---|---|
String |
MCP GetPromptResultで単一のユーザー ロール テキスト メッセージとして返されます。 |
MCP プロンプト トリガーでは、次の戻り値の型がサポートされます。
| タイプ | Description |
|---|---|
str |
MCP GetPromptResultで単一のユーザー ロール テキスト メッセージとして返されます。 |
この関数は、プロンプト メッセージ テキストを含む string を返す必要があります。 文字列は、MCP GetPromptResultで単一のユーザー ロール テキスト メッセージとしてラップされます。
プロンプトの検出
関数アプリが起動すると、すべてのプロンプト トリガー関数が MCP サーバーに登録されます。 クライアントは、MCP prompts/list メソッドを呼び出すことによって、使用可能なプロンプトを検出します。 このメソッドは、各プロンプトの名前、タイトル、説明、引数、アイコン、およびメタデータ ( meta フィールドを通じて) を返します。 クライアントは、プロンプト名と引数を使用して prompts/get を呼び出すことによってプロンプトを呼び出します。
セッション
SessionIdの PromptInvocationContext プロパティは、要求を行っている MCP セッションを識別します。 このプロパティを使用して、セッションごとの状態を維持するか、プロンプトを生成するときにセッション固有のロジックを適用します。
host.json 設定
host.json ファイルには、MCP トリガーの動作を制御する設定が含まれています。 使用可能な設定の詳細については、「host.json 設定」を参照してください。
関連資料
Azure FunctionsMCP リソース トリガー>