|
| 1 | +--- |
| 2 | +title: MCP trigger for Azure Functions |
| 3 | +description: Learn how you can use a trigger endpoint to expose a functions as a model content protocol (MCP) server tool in Azure Functions. |
| 4 | +ms.topic: reference |
| 5 | +ms.date: 05/03/2025 |
| 6 | +ms.custom: |
| 7 | + - build-2025 |
| 8 | +ai-usage: ai-assisted |
| 9 | +ms.collection: |
| 10 | + - ce-skilling-ai-copilot |
| 11 | +zone_pivot_groups: programming-languages-set-functions-lang-workers |
| 12 | +--- |
| 13 | + |
| 14 | +# MCP trigger for Azure Functions |
| 15 | + |
| 16 | +Use the MCP trigger to create endpoints in a [Model Content Protocol (MCP)](https://github.com/modelcontextprotocol) server that are accessed by client LLMs and agents to do specific tasks, such as storing or accessing code snippets. These MCP clients can also subscribe to your MCP server (your function app) to receive notifications about changes to the exposed APIs. |
| 17 | + |
| 18 | +[!INCLUDE [functions-mcp-extension-preview-note](../../includes/functions-mcp-extension-preview-note.md)] |
| 19 | + |
| 20 | +For information on setup and configuration details, see the [overview](functions-bindings-mcp.md). |
| 21 | + |
| 22 | +## Example |
| 23 | + |
| 24 | +::: zone pivot="programming-language-csharp" |
| 25 | +>[!TIP] |
| 26 | +>The Azure Functions MCP extension supports only the [isolated worker model](dotnet-isolated-process-guide.md). |
| 27 | +
|
| 28 | +This code creates an endpoint to expose a tool named `GetSnippet` that tries to retrieve a code snippet by name from blob storage. |
| 29 | + |
| 30 | +:::code language="csharp" source="~/remote-mcp-functions-dotnet/src/SnippetsTool.cs" range="10-20" ::: |
| 31 | + |
| 32 | +This code creates an endpoint to expose a tool named `SaveSnippet` that tries to persist a named code snippet to blob storage. |
| 33 | + |
| 34 | +:::code language="csharp" source="~/remote-mcp-functions-dotnet/src/SnippetsTool.cs" range="10, 22-34" ::: |
| 35 | + |
| 36 | +For the complete code example, see [SnippetTool.cs](https://github.com/Azure-Samples/remote-mcp-functions-dotnet/blob/main/src/SnippetsTool.cs). |
| 37 | +::: zone-end |
| 38 | +::: zone pivot="programming-language-java" |
| 39 | +This code creates an endpoint to expose a tool named `GetSnippets` that tries to retrieve a code snippet by name from blob storage. |
| 40 | + |
| 41 | +:::code language="java" source="~/remote-mcp-functions-java/src/main/java/com/function/Snippets.java" range="128-155" ::: |
| 42 | + |
| 43 | +This code creates an endpoint to expose a tool named `SaveSnippets` that tries to persist a named code snippet to blob storage. |
| 44 | + |
| 45 | +:::code language="java" source="~/remote-mcp-functions-java/src/main/java/com/function/Snippets.java" range="85-114" ::: |
| 46 | + |
| 47 | +For the complete code example, see [Snippets.java](https://github.com/Azure-Samples/remote-mcp-functions-java/blob/main/src/main/java/com/function/Snippets.java). |
| 48 | +::: zone-end |
| 49 | +::: zone pivot="programming-language-javascript" |
| 50 | +Example code for JavaScript isn't currently available. |
| 51 | +::: zone-end |
| 52 | +::: zone pivot="programming-language-typescript" |
| 53 | +This code creates an endpoint to expose a tool named `getsnippet` that tries to retrieve a code snippet by name from blob storage. |
| 54 | + |
| 55 | +:::code language="typescript" source="~/remote-mcp-functions-typescript/src/functions/snippetsMcpTool.ts" range="79-91" ::: |
| 56 | + |
| 57 | +This is the code that handles the `getsnippet` trigger: |
| 58 | + |
| 59 | +:::code language="typescript" source="~/remote-mcp-functions-typescript/src/functions/snippetsMcpTool.ts" range="26-48" ::: |
| 60 | + |
| 61 | +This code creates an endpoint to expose a tool named `savesnippet` that tries to persist a named code snippet to blob storage. |
| 62 | + |
| 63 | +:::code language="typescript" source="~/remote-mcp-functions-typescript/src/functions/snippetsMcpTool.ts" range="94-111" ::: |
| 64 | + |
| 65 | +This is the code that handles the `savesnippet` trigger: |
| 66 | + |
| 67 | +:::code language="typescript" source="~/remote-mcp-functions-typescript/src/functions/snippetsMcpTool.ts" range="51-76" ::: |
| 68 | + |
| 69 | +For the complete code example, see [snippetsMcpTool.ts](https://github.com/Azure-Samples/remote-mcp-functions-typescript/blob/main/src/functions/snippetsMcpTool.ts). |
| 70 | +::: zone-end |
| 71 | +::: zone pivot="programming-language-python" |
| 72 | +This code uses the `generic_trigger` decorator to create an endpoint to expose a tool named `get_snippet` that tries to retrieve a code snippet by name from blob storage. |
| 73 | + |
| 74 | +:::code language="java" source="~/remote-mcp-functions-python/src/function_app.py" range="61-82" ::: |
| 75 | + |
| 76 | +This code uses the `generic_trigger` decorator to create an endpoint to expose a tool named `save_snippet` that tries to persist a named code snippet to blob storage. |
| 77 | + |
| 78 | +:::code language="java" source="~/remote-mcp-functions-python/src/function_app.py" range="85-106" ::: |
| 79 | + |
| 80 | +For the complete code example, see [function_app.py](https://github.com/Azure-Samples/remote-mcp-functions-python/blob/main/src/function_app.py). |
| 81 | +::: zone-end |
| 82 | +[!INCLUDE [functions-mcp-extension-powershell-note](../../includes/functions-mcp-extension-powershell-note.md)] |
| 83 | +::: zone pivot="programming-language-csharp" |
| 84 | +## Attributes |
| 85 | + |
| 86 | +C# libraries use `McpToolTriggerAttribute` to define the function trigger. |
| 87 | + |
| 88 | +The attribute's constructor takes the following parameters: |
| 89 | + |
| 90 | +|Parameter | Description| |
| 91 | +|---------|----------------------| |
| 92 | +|**ToolName**| (Required) name of the tool that's being exposed by the MCP trigger endpoint. | |
| 93 | +|**Description**| (Optional) friendly description of the tool endpoint for clients. | |
| 94 | + |
| 95 | +See [Usage](#usage) to learn how to define properties of the endpoint as input parameters. |
| 96 | + |
| 97 | +::: zone-end |
| 98 | +::: zone pivot="programming-language-java" |
| 99 | + |
| 100 | +## Annotations |
| 101 | + |
| 102 | +The `McpTrigger` annotation creates a function that exposes a tool endpoint in your MCP server. |
| 103 | + |
| 104 | +The annotation supports the following configuration options: |
| 105 | + |
| 106 | +|Parameter | Description| |
| 107 | +|---------|----------------------| |
| 108 | +| **toolName**| (Required) name of the tool that's being exposed by the MCP trigger endpoint. | |
| 109 | +| **description**| (Optional) friendly description of the tool endpoint for clients. | |
| 110 | +| **toolProperties** | The JSON string representation of one or more property objects that expose properties of the tool to clients. | |
| 111 | + |
| 112 | +::: zone-end |
| 113 | +::: zone pivot="programming-language-python" |
| 114 | +## Decorators |
| 115 | + |
| 116 | +_Applies only to the Python v2 programming model._ |
| 117 | + |
| 118 | +>[!NOTE] |
| 119 | +>At this time, you must use a generic decorator to define an MCP trigger. |
| 120 | +
|
| 121 | +The following MCP trigger properties are supported on `generic_trigger`: |
| 122 | + |
| 123 | +| Property | Description | |
| 124 | +|-------------|-----------------------------| |
| 125 | +| **type** | (Required) Must be set to `mcpToolTrigger` in the `generic_trigger` decorator. | |
| 126 | +| **arg_name** | The variable name (usually `context`) used in function code to access the execution context. | |
| 127 | +| **toolName** | (Required) The name of the MCP server tool exposed by the function endpoint. | |
| 128 | +| **description** | A description of the MCP server tool exposed by the function endpoint. | |
| 129 | +| **toolProperties** | The JSON string representation of one or more property objects that expose properties of the tool to clients. | |
| 130 | + |
| 131 | +::: zone-end |
| 132 | +::: zone pivot="programming-language-javascript,programming-language-typescript" |
| 133 | +## Configuration |
| 134 | + |
| 135 | +The trigger supports these binding options, which are defined in your code: |
| 136 | + |
| 137 | +| Options | Description | |
| 138 | +|-----------------------|-------------| |
| 139 | +| **type** | Must be `mcpToolTrigger`. Only used with generic definitions. | |
| 140 | +| **toolName** | (Required) The name of the MCP server tool exposed by the function endpoint. | |
| 141 | +| **description** | A description of the MCP server tool exposed by the function endpoint. | |
| 142 | +| **toolProperties** | An array of `toolProperty` objects that expose properties of the tool to clients. | |
| 143 | +| **extraOutputs** | When defined, sends function output to another binding. | |
| 144 | +| **handler** | The method that contains the actual function code. | |
| 145 | + |
| 146 | +::: zone-end |
| 147 | + |
| 148 | +See the [Example section](#example) for complete examples. |
| 149 | + |
| 150 | +## Usage |
| 151 | + |
| 152 | +::: zone pivot="programming-language-csharp" |
| 153 | + |
| 154 | +The MCP protocol enables an MCP server to make known to clients additional properties of a tool endpoint. In C#, you can define one or more tool properties by applying the `McpToolProperty` attribute to input binding-style parameters in your function. |
| 155 | + |
| 156 | +The `McpToolPropertyAttribute` type supports these properties: |
| 157 | + |
| 158 | +| Property | Description | |
| 159 | +| ---- | ----- | |
| 160 | +| **PropertyName** | Name of the tool property that gets exposed to clients. | |
| 161 | +| **PropertyType** | The data type of the tool property, such as `string`. | |
| 162 | +| **Description** | (Optional) Description of what the tool property does. | |
| 163 | + |
| 164 | +::: zone-end |
| 165 | +::: zone pivot="programming-language-java,pprogramming-language-python" |
| 166 | +Properties of a tool exposed by your MCP server are defined using tool properties. These properties are returned by the `toolProperties` field, which is a string representation of an array of `ToolProperty` objects. |
| 167 | + |
| 168 | +A `ToolProperty` object has this structure: |
| 169 | + |
| 170 | +```json |
| 171 | +{ |
| 172 | + "propertyName": "Name of the property", |
| 173 | + "propertyType": "Type of the property", |
| 174 | + "description": "Optional property description", |
| 175 | +} |
| 176 | +``` |
| 177 | +::: zone-end |
| 178 | +::: zone pivot="programming-language-javascript,programming-language-typescript" |
| 179 | +Properties of a tool exposed by your MCP server are defined using tool properties. These properties are returned by the `toolProperties` field, which is a string representation of an array of `ToolProperty` objects. |
| 180 | + |
| 181 | +A `ToolProperty` object has this structure: |
| 182 | + |
| 183 | +```json |
| 184 | +{ |
| 185 | + "propertyName": "Name of the property", |
| 186 | + "propertyValue": "Type of the property", |
| 187 | + "description": "Optional property description", |
| 188 | +} |
| 189 | +``` |
| 190 | +::: zone-end |
| 191 | + |
| 192 | +For more information, see [Examples](#example). |
| 193 | + |
| 194 | +## host.json settings |
| 195 | + |
| 196 | +The host.json file contains settings that control MCP trigger behaviors. See the [host.json settings](functions-bindings-mcp.md#host-json) section for details regarding available settings. |
| 197 | + |
| 198 | +## Related articles |
| 199 | + |
| 200 | +[Azure OpenAI extension for Azure Functions](functions-bindings-openai.md) |
0 commit comments