使用Azure函数(.NET/C#)的保险计算器MCP服务器
这是一个远程MCP服务器,通过Azure Functions提供保险计算功能。该服务器与外部Subabase API集成,根据个人信息计算保险费。您可以在本地运行它进行开发,并使用以下命令部署到Azure azd up.
观看视频概述
如果您正在寻找更多语言的此示例,请查看 和 python 样品。
](https://codespaces.new/Azure-Samples/remote-mcp-functions-dotnet)
先决条件
- .NET 8 SDK
- Azure功能核心工具 >=
4.0.7030 - Azure开发者命令行界面
- 要使用Visual Studio在本地运行和调试,请执行以下操作:
- Visual Studio 2022. - 确保选择 Azure开发 安装期间的工作量。
- 要使用Visual Studio代码在本地运行和调试,请执行以下操作:
- Visual Studio Code - Azure功能扩展
以下是使用Azure Functions的远程MCP服务器的架构图:
准备好当地环境
本地开发不需要额外的设置,因为保险计算器只对外部API进行HTTP调用。
从终端本地运行MCP服务器
- 从
src文件夹中,运行此命令以在本地启动Functions主机:
cd src
func start注意,默认情况下,这将使用webhooks路由: /runtime/webhooks/mcp稍后,我们将在Azure中使用它来设置客户端/主机调用的密钥: /runtime/webhooks/mcp?code=
连接到 *本地* 客户端/主机内的MCP服务器
VS代码-副本编辑
- 添加MCP服务器 从命令面板中,将URL添加到正在运行的Function应用程序的MCP端点:
http://localhost:7071/runtime/webhooks/mcp- 列出MCP服务器 从命令面板启动服务器
- 在Copilot聊天代理模式下,输入提示以触发保险计算器工具:
Calculate insurance for a 45 year old with salary 750000, degree disabled 100, degree medical disabled 100, marital status enslig, children ages [10, 10], external group id abbas1, and formatter innmeldt- 当系统提示运行该工具时,点击同意 继续
- 完成后,在终端窗口中按Ctrl+C停止
func.exe主机进程。
MCP检查员
- 在其中 新终端窗口,安装并运行MCP检查器
npx @modelcontextprotocol/inspector node build/index.js- 按CTRL键单击可从应用程序显示的URL加载MCP Inspector web应用程序(例如。http://0.0.0.0:5173/#resources)
- 将运输类型设置为
Streamable HTTP
- 将URL设置为正在运行的Function应用程序的MCP端点,然后 连接:
http://0.0.0.0:7071/runtime/webhooks/mcp- 列出工具。单击工具,然后 运行工具.
测试保险计算器
保险计算器工具接受以下参数:
- 年龄:人的年龄(人数)
- 薪水:年薪(人)
- 程度已停用:残疾程度0-100(数字)
- 程度医疗残疾:医疗残疾程度0-100(数字)
- 婚姻状况:婚姻状况,如“enslig”或“gift”(字符串)
- 孩子:儿童年龄的JSON数组,如“\[10,12\]”(字符串)
- externalGroupId:外部组标识符(字符串)
- 格式化程序:格式化程序类型,如“innmeldt”(字符串)
该工具将调用外部Subabase API并返回详细的保险计算。
部署到Azure以实现远程MCP
运行这个 雅匝德 命令为功能应用程序配置任何所需的Azure资源,并部署您的代码:
azd up您可以选择加入示例中使用的ExpressRoute。为此,请先执行此操作 azd up
azd env set VNET_ENABLED true另外, API管理 可用于提高MCP服务器的安全性和策略,以及 App Service内置身份验证 可用于设置您最喜欢的OAuth提供程序,包括Entra。
从客户端连接到您的\*remote()MCP服务器功能应用程序
您的客户端需要一个密钥来调用新的托管MCP端点,该密钥的格式为 https://.azurewebsites.net/runtime/webhooks/mcp默认情况下,托管功能需要一个系统密钥,可以从 门户 或CLI(az functionapp keys list --resource-group --name ).获取名为的系统密钥 mcp_extension.
在MCP Inspector中连接到远程MCP服务器
对于MCP检查器,您可以在URL中包含密钥:
https://.azurewebsites.net/runtime/webhooks/mcp?code=在VS Code中连接到远程MCP服务器-GitHub Copilot
对于VS Code中的GitHub Copilot,您应该将密钥设置为 x-functions-key 标题在 mcp.json,你只需使用 https://.azurewebsites.net/runtime/webhooks/mcp 以下示例使用输入,并在从VS Code启动服务器时提示您提供密钥。备注 mcp.json 已包含在此仓库中,并将由VS Code获取。单击服务器上的“开始”,系统将提示输入以下值 functionapp-name (在您的/.alue/\*/env文件中)和 functions-mcp-extension-system-key 可以从上面的CLI命令或Function App门户中的API密钥中获取。
{
"inputs": [
{
"type": "promptString",
"id": "functions-mcp-extension-system-key",
"description": "Azure Functions MCP Extension System Key",
"password": true
},
{
"type": "promptString",
"id": "functionapp-name",
"description": "Azure Functions App Name"
}
],
"servers": {
"remote-mcp-function": {
"type": "http",
"url": "https://${input:functionapp-name}.azurewebsites.net/runtime/webhooks/mcp",
"headers": {
"x-functions-key": "${input:functions-mcp-extension-system-key}"
}
},
"local-mcp-function": {
"type": "http",
"url": "http://0.0.0.0:7071/runtime/webhooks/mcp"
}
}
}单击服务器上的“开始”,系统将提示输入以下值 functionapp-name (在您的/.alue/\*/env文件中)和 functions-mcp-extension-system-key 可以从上面的CLI命令或Function App门户中的API密钥中获取。
重新部署代码
你可以运行 azd up 根据需要多次执行命令,以配置Azure资源并将代码更新部署到功能应用程序。
\[!注意\] 部署的代码文件总是被最新的部署包覆盖。
清理资源
使用完功能应用程序和相关资源后,您可以使用此命令从Azure中删除功能应用程序及其相关资源,从而避免产生任何进一步的成本:
azd down源代码
保险计算器功能在中定义 InsuranceCalculatorTool.csThe McpToolTrigger 属性将函数作为MCP工具公开,以及 McpToolProperty 属性定义了所需的参数。
[Function(nameof(CalculateInsurance))]
public async Task CalculateInsurance(
[McpToolTrigger(InsuranceCalculatorToolName, InsuranceCalculatorToolDescription)] ToolInvocationContext context,
[McpToolProperty(AgePropertyName, NumberType, AgePropertyDescription, Required = true)] int age,
[McpToolProperty(SalaryPropertyName, NumberType, SalaryPropertyDescription, Required = true)] int salary,
[McpToolProperty(DegreeDisabledPropertyName, NumberType, DegreeDisabledPropertyDescription, Required = true)] int degreeDisabled,
[McpToolProperty(DegreeMedicalDisabledPropertyName, NumberType, DegreeMedicalDisabledPropertyDescription, Required = true)] int degreeMedicalDisabled,
[McpToolProperty(MaritalStatusPropertyName, PropertyType, MaritalStatusPropertyDescription, Required = true)] string maritalStatus,
[McpToolProperty(ChildrenPropertyName, PropertyType, ChildrenPropertyDescription, Required = true)] string children,
[McpToolProperty(ExternalGroupIdPropertyName, PropertyType, ExternalGroupIdPropertyDescription, Required = true)] string externalGroupId,
[McpToolProperty(FormatterPropertyName, PropertyType, FormatterPropertyDescription, Required = true)] string formatter
)该函数使用提供的参数向外部Suabase API发出HTTP POST请求,并返回保险计算结果。
后续步骤
- 添加 API管理 到您的MCP服务器
- 添加 EasyAuth 到您的MCP服务器
- 使用VNET_ENABLED=true标志启用VNET
- 更多了解 微软的相关MCP工作
