|
| 1 | +--- |
| 2 | +title: MCP tool trigger for Azure Functions |
| 3 | +description: Learn how you can use a trigger endpoint to expose functions as a model content protocol (MCP) server tools in Azure Functions. |
| 4 | +ms.topic: reference |
| 5 | +ms.date: 05/06/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 |
| 12 | +--- |
| 13 | + |
| 14 | +# MCP tool trigger for Azure Functions |
| 15 | + |
| 16 | +Use the MCP tool trigger to define tool endpoints in a [Model Content Protocol (MCP)](https://github.com/modelcontextprotocol) server that are accessed by client language models and agents to do specific tasks, such as storing or accessing code snippets. MCP clients can also subscribe to your function app to receive notifications about changes to the exposed tools. |
| 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 | +>[!NOTE] |
| 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. See the TypeScript examples for general guidance using Node.js. |
| 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="python" 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="python" 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 remote 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 set to `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 other properties of a tool endpoint. In C#, you can define properties of your tools as either input parameters using the `McpToolProperty` attribute to your trigger function code or by using the `FunctionsApplicationBuilder` when the app starts. |
| 155 | + |
| 156 | +### [`McpToolPropertyAttribute`](#tab/attribute) |
| 157 | + |
| 158 | +You can define one or more tool properties by applying the `McpToolProperty` attribute to input binding-style parameters in your function. |
| 159 | + |
| 160 | +The `McpToolPropertyAttribute` type supports these properties: |
| 161 | + |
| 162 | +| Property | Description | |
| 163 | +| ---- | ----- | |
| 164 | +| **PropertyName** | Name of the tool property that gets exposed to clients. | |
| 165 | +| **PropertyType** | The data type of the tool property, such as `string`. | |
| 166 | +| **Description** | (Optional) Description of what the tool property does. | |
| 167 | + |
| 168 | +You can see these attributes used in the `SaveSnippet` tool in the [Examples](#example). |
| 169 | + |
| 170 | +### [`FunctionsApplicationBuilder`](#tab/builder) |
| 171 | + |
| 172 | +You can define tool properties in your entry point (program.cs) file by using the `McpToolBuilder` returned by the `ConfigureMcpTool` method on `FunctionsApplicationBuilder`. This example calls the `WithProperty` method on the builder for the `GetSnippet` tool to set the properties of the tool: |
| 173 | + |
| 174 | +:::code language="csharp" source="~/remote-mcp-functions-dotnet/src/Program.cs" range="5-15" ::: |
| 175 | + |
| 176 | +For the complete example, see the [program.cs file](https://github.com/Azure-Samples/remote-mcp-functions-dotnet/blob/main/src/Program.cs). |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +::: zone-end |
| 181 | +::: zone pivot="programming-language-java,programming-language-python" |
| 182 | +Properties of a tool exposed by your remote 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. |
| 183 | + |
| 184 | +A `ToolProperty` object has this structure: |
| 185 | + |
| 186 | +```json |
| 187 | +{ |
| 188 | + "propertyName": "Name of the property", |
| 189 | + "propertyType": "Type of the property", |
| 190 | + "description": "Optional property description", |
| 191 | +} |
| 192 | +``` |
| 193 | +::: zone-end |
| 194 | +::: zone pivot="programming-language-javascript,programming-language-typescript" |
| 195 | +Properties of a tool exposed by your remote 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. |
| 196 | + |
| 197 | +A `ToolProperty` object has this structure: |
| 198 | + |
| 199 | +```json |
| 200 | +{ |
| 201 | + "propertyName": "Name of the property", |
| 202 | + "propertyValue": "Type of the property", |
| 203 | + "description": "Optional property description", |
| 204 | +} |
| 205 | +``` |
| 206 | +::: zone-end |
| 207 | + |
| 208 | +For more information, see [Examples](#example). |
| 209 | + |
| 210 | +## host.json settings |
| 211 | + |
| 212 | +The host.json file contains settings that control MCP trigger behaviors. See the [host.json settings](functions-bindings-mcp.md#hostjson-settings) section for details regarding available settings. |
| 213 | + |
| 214 | +## Related articles |
| 215 | + |
| 216 | +[Azure OpenAI extension for Azure Functions](functions-bindings-openai.md) |
0 commit comments