Getting Started with Remote MCP Servers using Azure Functions (Python)
MCP 服务器示例旅行伙伴应用,托管于 Azure 容器应用上的 Azure Functions 和 MCP 扩展。 (MCP fúwùqì shìlì lǚxíng huǒbàn yìngyòng, tuōguǎn yú Azure róngqì yìngyòng shàng de Azure Functions hé MCP kuòzhǎn.)
raorugan
README
Azure Functions (Python) 入门:远程 MCP 服务器
这是一个快速入门模板,可用于使用 Azure Functions 和 Python 轻松构建和部署自定义远程 MCP 服务器到云端。MCP 服务器在设计上通过密钥和 HTTPS 进行保护,并允许使用内置身份验证和/或 API 管理以及使用 VNET 的网络隔离来实现更多 OAuth 选项。
注意
大型语言模型 (LLM) 是非确定性的,重要的是要注意它们可能存在不准确之处和实时变化。因此,建议在做出决策之前仔细检查信息并进行验证。
前提条件
- Python 3.11 或更高版本
- Azure Functions Core Tools
- 使用 Visual Studio Code 在本地运行和调试:
注意:这里我使用了 Rapid API marketplace Booking COM 旅行 API 来进行调用和处理旅行详细信息。该 API 需要一个密钥,因此您需要订阅 Booking COM API 才能获取密钥并使用它。
准备本地环境
此特定示例需要 Azure 存储模拟器,因为我们将从 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 编辑
-
从命令面板 添加 MCP 服务器,并将 URL 添加到正在运行的 Function app 的 SSE 终结点:
http://0.0.0.0:7071/runtime/webhooks/mcp/sse OR http://localhost:7071/runtime/webhooks/mcp/sse
-
从命令面板 列出 MCP 服务器 并启动服务器
-
在 Copilot 聊天代理模式下,输入提示以触发该工具,例如,选择一些代码并输入此提示
Hey what are the attractions in <place of your choice>
Please share reviews of the tour <tour_name>
-
当提示运行该工具时,单击 继续 表示同意
-
完成后,在终端窗口中按 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 app 的 SSE 终结点并连接:
http://0.0.0.0:7071/runtime/webhooks/mcp/sse
-
列出工具。单击一个工具并运行工具。
部署到 Azure 以进行远程 MCP
使用此链接中记录的说明,使用适用于 Azure Functions 的全新 MCP 扩展在 Azure 容器应用资源上预配 Azure Functions,以托管 MCP Azure Functions 服务器。
或者
运行此 azd 命令以预配函数应用以及任何必需的 Azure 资源,并部署您的代码:
azd up
此外,API 管理 可用于提高 MCP 服务器的安全性和策略,并且 App Service 内置身份验证 可用于设置您喜欢的 OAuth 提供程序,包括 Entra。
从客户端连接到您的函数应用
您的客户端需要一个密钥才能调用新的托管 SSE 终结点,该终结点的格式为 https://<function-name>.*****-****.<location>.azurecontainerapps.io
。默认情况下,托管函数需要一个系统密钥,该密钥可以从存储帐户 -> Blob 容器 -> azure-webjobs-secrets -> host.json -> 查看/编辑 获取名为 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
标头,并且您只需使用 https://<function-name>.*****-****.<location>.azurecontainerapps.io/runtime/webhooks/mcp/sse
作为 URL。以下示例使用一个输入,并在您从 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 管理 添加到您的 MCP 服务器
- 将 内置身份验证 添加到您的 MCP 服务器
- 使用 VNET_ENABLED=true 标志启用 VNET
- 了解有关 Microsoft 相关 MCP 工作的更多信息
推荐服务器
Crypto Price & Market Analysis MCP Server
一个模型上下文协议 (MCP) 服务器,它使用 CoinCap API 提供全面的加密货币分析。该服务器通过一个易于使用的界面提供实时价格数据、市场分析和历史趋势。 (Alternative, slightly more formal and technical translation): 一个模型上下文协议 (MCP) 服务器,利用 CoinCap API 提供全面的加密货币分析服务。该服务器通过用户友好的界面,提供实时价格数据、市场分析以及历史趋势数据。
MCP PubMed Search
用于搜索 PubMed 的服务器(PubMed 是一个免费的在线数据库,用户可以在其中搜索生物医学和生命科学文献)。 我是在 MCP 发布当天创建的,但当时正在度假。 我看到有人在您的数据库中发布了类似的服务器,但还是决定发布我的服务器。
mixpanel
连接到您的 Mixpanel 数据。 从 Mixpanel 分析查询事件、留存和漏斗数据。

Sequential Thinking MCP Server
这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。

Nefino MCP Server
为大型语言模型提供访问德国可再生能源项目新闻和信息的能力,允许按地点、主题(太阳能、风能、氢能)和日期范围进行筛选。
Vectorize
将 MCP 服务器向量化以实现高级检索、私有深度研究、Anything-to-Markdown 文件提取和文本分块。
Mathematica Documentation MCP server
一个服务器,通过 FastMCP 提供对 Mathematica 文档的访问,使用户能够从 Wolfram Mathematica 检索函数文档和列出软件包符号。
kb-mcp-server
一个 MCP 服务器,旨在实现便携性、本地化、简易性和便利性,以支持对 txtai “all in one” 嵌入数据库进行基于语义/图的检索。任何 tar.gz 格式的 txtai 嵌入数据库都可以被加载。
Research MCP Server
这个服务器用作 MCP 服务器,与 Notion 交互以检索和创建调查数据,并与 Claude Desktop Client 集成以进行和审查调查。

Cryo MCP Server
一个API服务器,实现了模型补全协议(MCP),用于Cryo区块链数据提取,允许用户通过任何兼容MCP的客户端查询以太坊区块链数据。