本项目可在 Azure Functions 上运行一个远程 MCP 服务器。借助此快速入门模板,你能轻松使用 Azure Functions 和 Python 构建并部署自定义远程 MCP 服务器到云端。MCP 服务器通过实验性功能支持,可作为工具提供给语言模型(LLM)。
这是一个快速入门模板,用于轻松构建和部署自定义远程 MCP 服务器到云中使用 Azure Functions 和 Python。MCP 服务器通过实验性功能支持,可以作为工具提供给语言模型(LLM)。MCP 功能需要在函数应用中启用,并且可以在 host.json 中引用实验性捆绑包。
{
"extensions": {
"httpExtension": {},
"mcpExtension": {}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle.Experimental",
"version": "[4.*, 5.0.0)"
}
}
使用 azd 命令创建一个新的 Azure Functions 应用,并指定运行时为 Python:
azd init --name --location --runtime python
进入项目目录并初始化 mcp.json 和其他必要文件:
cd . && azd up
在 host.json 中启用实验性功能和 MCP 扩展:
{
"extensions": {
"httpExtension": {},
"mcpExtension": {}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle.Experimental",
"version": "[4.*, 5.0.0)"
}
}
在 mcp.json 中定义 MCP 工具:
{
"myMCPTool": {
"type": "functionTrigger",
"description": "Retrieve attractions at a search location.",
"properties": {},
"trigger": {
"funcName": "get_attractions"
}
}
}
在 src 目录下创建一个 Python 函数文件(例如 function_app.py),并添加以下代码:
import logging
import json
def get_attractions(context):
"""
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()
# Add your logic to call RapidAPI here
return json.dumps({
"result": "Success",
"message": f"Successfully retrieved attractions for {query}"
})
def hello_mcp(context):
"""
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!"
使用 azd 命令将项目部署到 Azure:
azd up
这会创建所有必要的 Azure 资源,包括函数应用、存储账户和 API 管理实例。
部署完成后,可以从 VS Code 或任何兼容的客户端访问 MCP 服务器。通过 mcp.json 配置工具,并使用以下 URL 访问:
https://.azurewebsites.net/runtime/webhooks/mcp/sse
VNET_ENABLED=true 标志