关于 Getting Started with Remote MCP Servers using Azure Functions (Python)
此 Python 快速入门模板演示如何使用 Azure Functions 构建和部署远程 MCP 服务,包含旅行相关工具及本地和云端配置说明,并提供 HTTPS、密钥访问,以及 OAuth 和网络隔离选项。
主要功能
- 01使用 Python 构建远程 MCP 服务
- 02通过 Azure Functions 本地运行
- 03部署到 Azure Container Apps
- 04提供景点和评论相关功能
- 05配置密钥、OAuth 和网络隔离
适用场景
- 01搭建旅行信息 MCP 服务原型
- 02通过客户端或 Inspector 本地测试 MCP 工具
- 03部署受保护的远程 MCP 端点
MCP 相关
Azure Functions MCP 扩展将带注释的函数作为 MCP 工具,通过 SSE 端点公开。客户端可连接本地实例或已部署的函数应用,访问云端服务时需使用密钥。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
使用 Azure Functions(Python)快速入门:远程 MCP 服务
这是一个快速入门模板,可帮助你使用 Azure Functions 和 Python 轻松构建自定义远程 MCP 服务并将其部署到云端。该 MCP 服务在设计上使用密钥和 HTTPS 进行保护,并支持更多 OAuth 选项,可使用内置身份验证和/或 API Management,以及通过 VNET 实现网络隔离。
注意
大语言模型(LLM)具有非确定性,了解它们可能存在的不准确之处以及实时变化非常重要。因此,建议在做出决策前仔细核查并验证信息。
前提条件
- Python 版本 3.11 或更高版本
- Azure Functions Core Tools
-
- 使用 Visual Studio Code 在本地运行和调试:
- Visual Studio Code
- Azure Functions 扩展
注意:此处我使用了 Rapid API marketplace Booking COM 旅行 API 来调用并处理旅行详情。该 API 需要密钥,因此你需要订阅 Booking COM API 以获取并使用密钥。
准备本地环境
此示例需要 Azure Storage Emulator,因为我们将从 Blob 存储中保存和获取片段。
-
启动 Azurite
docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 \ mcr.microsoft.com/azure-storage/azurite
注意 如果你使用的是 VS Code 扩展提供的 Azurite,现在需要运行
Azurite: Start,否则会看到错误。
在终端中本地运行 MCP 服务
-
在新的终端窗口中切换到 src 文件夹:
cd src -
安装 Python 依赖项:
pip install -r requirements.txt -
在本地启动 Functions 主机:
func start
注意 默认情况下,此操作将使用 webhooks 路由:
/runtime/webhooks/mcp/sse。之后我们将在 Azure 中使用此路由,为客户端/主机调用设置密钥:/runtime/webhooks/mcp/sse?code=<system_key>
在客户端/主机中使用 MCP 服务
VS Code - Copilot 编辑
-
从命令面板中选择 Add MCP Server,并添加正在运行的 Function 应用的 SSE 终结点 URL:
http://0.0.0.0:7071/runtime/webhooks/mcp/sse OR http://localhost:7071/runtime/webhooks/mcp/sse -
从命令面板中选择 List MCP Servers,并启动服务
-
在 Copilot 聊天的 Agent 模式下输入提示词来触发工具,例如,选择一些代码并输入以下提示词
Hey what are the attractions in <place of your choice>Please share reviews of the tour <tour_name> -
提示你运行工具时,点击 Continue 表示同意
-
完成后,在终端窗口中按 Ctrl+C,停止 Functions 主机进程。
MCP Inspector
-
在新的终端窗口中安装并运行 MCP Inspector
npx @modelcontextprotocol/inspector -
按住 CTRL 并点击应用显示的 URL,以打开 MCP Inspector Web 应用(例如 http://0.0.0.0:5173/#resources)
-
将传输类型设为
SSE -
将 URL 设为正在运行的 Function 应用的 SSE 终结点,并点击 Connect:
http://0.0.0.0:7071/runtime/webhooks/mcp/sse -
选择 List Tools。点击某个工具,再点击 Run Tool。
部署到 Azure 以提供远程 MCP 服务
按照此链接中记录的说明,使用全新的 Azure Functions MCP 扩展,在 Azure Container Apps 资源上预配托管 MCP Azure Functions 服务的 Azure Functions
或者
运行此 azd 命令,预配函数应用及所需的任何 Azure 资源,并部署代码:
azd up
此外,可以使用 API Management 增强 MCP 服务的安全性并应用策略,也可以使用 App Service 内置身份验证设置你喜欢的 OAuth 提供商,包括 Entra。
从客户端连接到函数应用
客户端需要一个密钥才能调用新的托管 SSE 终结点,其格式为 https://<function-name>.*****-****.<location>.azurecontainerapps.io。默认情况下,托管函数需要系统密钥。你可以从 Storage account-> Blob containers -> azure-webjobs-secrets->host.json -> View/edit 中获取名为 mcp_extension 的系统密钥值。
对于 MCP Inspector,可以将密钥包含在 URL 中:
`https://<function-name>.*****-****.<location>.azurecontainerapps.io/runtime/webhooks/mcp/sse?code=<your-mcp-extension-system-key>`.
对于 VS Code 中的 GitHub Copilot,则应在 mcp.json 中将密钥设置为 x-functions-key 标头,URL 只需使用 https://<function-name>.*****-****.<location>.azurecontainerapps.io/runtime/webhooks/mcp/sse。以下示例使用输入项,当你从 VS Code 启动服务时,系统会提示你提供密钥:
{
"inputs": [
{
"type": "promptString",
"id": "functions-mcp-extension-system-key",
"description": "Azure Functions MCP Extension System Key",
"password": true
}
],
"servers": {
"my-mcp-server": {
"type": "sse",
"url": "<funcappname>.azurewebsites.net/runtime/webhooks/mcp/sse",
"headers": {
"x-functions-key": "${input:functions-mcp-extension-system-key}"
}
}
}
}
源代码
get_attractions 和 get_attractions_reviews 终结点的函数代码定义在 src 目录中的 Python 文件里。MCP 函数注解会将这些函数公开为 MCP 服务工具。
以下是 function_app.py 文件中的实际代码:
@app.generic_trigger(arg_name="context", type="mcpToolTrigger", toolName="hello",
description="Hello world.",
toolProperties="[]")
def hello_mcp(context) -> None:
"""
A simple function that returns a greeting message.
Args:
context: The trigger context (not used in this function).
Returns:
str: A greeting message.
"""
return "Hello I am MCPTool!"
@app.generic_trigger(
arg_name="context",
type="mcpToolTrigger",
toolName="get_attractions",
description="Retrieve attractions at a search location.",
toolProperties=tool_properties_get_attractions_json,
)
def get_attractions(context) -> str:
"""
Retrieves attractions at a search location using the RapidAPI endpoint.
Args:
context: The trigger context containing the input arguments.
Returns:
str: A JSON string containing the attractions or an error message.
"""
content = json.loads(context)
query = content["arguments"].get("query", "").strip()
language_code = content["arguments"].get("languagecode", "en-us").strip()
请注意,host.json 文件还包含对实验性捆绑包的引用,使用此功能的应用必须包含该引用:
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle.Experimental",
"version": "[4.*, 5.0.0)"
}
后续步骤
- 将 API Management 添加到 MCP 服务
- 将内置身份验证添加到 MCP 服务
- 使用 VNET_ENABLED=true 标志启用 VNET
- 详细了解 Microsoft 的相关 MCP 工作
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。