项目简介
本项目是一个使用 Python 和 Azure Functions 构建的远程 MCP 服务器示例。它旨在帮助开发者快速搭建并部署自定义的 MCP 服务器到 Azure 云平台。该服务器通过安全的 HTTPS 和密钥进行保护,并支持使用 Azure API 管理和内置身份验证进行 OAuth 授权,以及使用 VNET 进行网络隔离,为 LLM 应用提供安全可扩展的上下文服务。
主要功能点
- 工具注册与执行: 实现了 'hello_mcp', 'get_snippet', 'save_snippet' 等工具,允许客户端通过 MCP 协议调用这些工具,扩展 LLM 的功能。
- 资源管理: 使用 Azure Blob Storage 存储和检索代码片段(snippets),作为一种简单的资源管理方式。
- Prompt 模板支持 (隐式): 虽然没有显式定义 Prompt 模板,但工具的描述和功能可以被视为 Prompt 模板的一部分,指导 LLM 如何与工具交互。
- SSE 协议支持: 通过 Azure Functions 的 HTTP 触发器和 MCP 扩展,实现了基于 Server-Sent Events (SSE) 的 MCP 通信协议。
- 安全机制: 支持通过 Function Key 进行身份验证,并可集成 Azure API 管理和内置身份验证以增强安全性。
- 云端部署: 提供 'azd' 部署脚本,可以快速部署到 Azure 云平台。
安装步骤
- 安装 Python 3.11+: 确保本地环境安装了 Python 3.11 或更高版本。
- 安装 Azure Functions Core Tools: 参考 Azure Functions Core Tools 安装文档 安装 Function Core Tools。
- 安装 Azure Developer CLI (azd): 参考 Azure Developer CLI 安装文档 安装 Azure Developer CLI。
- 安装 Visual Studio Code (可选): 如果需要在本地使用 VS Code 调试,请安装 Visual Studio Code 和 Azure Functions 扩展。
- 启动 Azurite (可选): 如果需要在本地运行,需要启动 Azurite 模拟 Azure Storage,用于代码片段的存储。可以使用 Docker 启动:
docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite - 克隆仓库: 将 GitHub 仓库 'remote-mcp-functions-python' 克隆到本地。
- 安装 Python 依赖: 进入 'src' 目录,运行 'pip install -r requirements.txt' 安装依赖。
服务器配置
MCP 客户端需要配置 MCP 服务器的连接信息。对于本示例,MCP 服务器的启动命令和参数由 Azure Functions 运行时环境管理,客户端主要需要配置服务器的 SSE Endpoint URL 和认证密钥(Function Key)。
以下是 MCP 客户端 (例如 VS Code Copilot) 'mcp.json' 的配置示例,用于连接到本地运行或部署到 Azure 的 MCP 服务器。
{ "servers": { "my-azure-functions-mcp-server": { "type": "sse", "url": "http://0.0.0.0:7071/runtime/webhooks/mcp/sse" // 本地运行时 URL,部署到 Azure 后需要替换为 Azure Function App 的 URL // 如果部署到 Azure Function App 且需要 Function Key 认证,则需要配置 headers 或 URL 参数 // 例如 (headers 方式,推荐 VS Code Copilot): // "headers": { // "x-functions-key": "<your-mcp-extension-system-key>" // 替换为你的 Function Key // } // 例如 (URL 参数方式,适用于 MCP Inspector): // "url": "https://<funcappname>.azurewebsites.net/runtime/webhooks/mcp/sse?code=<your-mcp-extension-system-key>" // 替换为你的 Function App URL 和 Function Key } } }
配置说明:
- 'servers': 定义 MCP 服务器列表。
- 'my-azure-functions-mcp-server': 自定义服务器名称,客户端用于引用。
- 'type: "sse"': 指定使用 SSE 协议进行通信。
- 'url': 本地运行: 默认为 'http://0.0.0.0:7071/runtime/webhooks/mcp/sse'。 Azure 部署: 需要替换为你的 Azure Function App 的 SSE Endpoint URL,格式通常为 'https://<funcappname>.azurewebsites.net/runtime/webhooks/mcp/sse'。
- 'headers' (可选, 用于 Function Key 认证): 如果 Azure Function App 配置了 Function Key 认证,则需要配置 'headers' 字段,将 Function Key 放入 'x-functions-key' 请求头中。 '<your-mcp-extension-system-key>' 需要替换为你在 Azure 门户或使用 Azure CLI 获取的 'mcp_extension' 系统密钥。
- 'url' 参数 (可选, 用于 Function Key 认证): 另一种 Function Key 认证方式是将密钥作为 URL 参数 'code' 传递。适用于一些不支持配置请求头的 MCP 客户端,例如 MCP Inspector。
基本使用方法
-
本地运行 MCP 服务器:
- 进入 'src' 目录,运行 'func start' 启动 Azure Functions host。
- 配置 MCP 客户端 (如 VS Code Copilot 或 MCP Inspector) 连接到 'http://0.0.0.0:7071/runtime/webhooks/mcp/sse'。
- 在 MCP 客户端中使用 'hello_mcp', 'get_snippet', 'save_snippet' 等工具。
-
部署 MCP 服务器到 Azure:
- 运行 'azd up' 命令部署到 Azure 云平台。
- 获取部署后的 Azure Function App 的 SSE Endpoint URL 和 'mcp_extension' 系统密钥。
- 配置 MCP 客户端连接到 Azure Function App 的 SSE Endpoint URL,并根据需要配置 Function Key 认证 (通过 headers 或 URL 参数)。
- 在 MCP 客户端中使用工具。
-
使用示例工具:
- 'hello_mcp': 一个简单的问候工具,无需参数。
- 'save_snippet': 保存代码片段到 Blob Storage。需要提供 'snippetname' (片段名称) 和 'snippet' (片段内容) 两个参数。
- 'get_snippet': 从 Blob Storage 检索代码片段。需要提供 'snippetname' (片段名称) 参数。
注意: 本示例主要演示了如何使用 Azure Functions 构建 MCP 服务器,资源管理和工具功能较为简单。实际应用中可以根据需求扩展工具功能和资源管理方式。
信息
分类
开发者工具