
由Azure API Management认证的远程MCP服务器
概览
本示例展示了如何在Azure上部署受Entra ID保护的MCP服务器。 该示例还采用了一种授权模式,客户端先为MCP服务器获取令牌,然后通过代流将该令牌交换为可与Microsoft Graph共用的令牌。这一切都完全无秘密地完成。 本示例演示如何通过实现OAuth 2.0动态客户端注册和PKCE流程模式,构建一个受保护的Entra IDMCP服务器,以弥补Entra ID对这些标准的原生支持中的不足。虽然展示了技术可能性,但仅供教育和概念验证目的。在生产场景中,考虑使用已建立的认证模式和预注册应用和标准OAuth流程。
概述(AI 翻译)
本示例展示了如何在Azure上部署受Entra ID保护的MCP服务器。
该示例还采用了授权模式,客户端先_for server_MCP获取一个令牌,然后通过代流将其交换成可用于图Microsoft的令牌。这一切都完全无秘密地完成。
⚠️ 重要:实验性实现
[!重要] 这是一个实验性实现,不应在生产场景中使用。
本示例演示如何通过实现 OAuth 2.0 动态客户端注册和 PKCE 流程模式,构建一个受保护的 Entra ID MCP 服务器,以弥补 Entra ID 原生支持这些标准的不足。虽然展示了技术可能性,但仅供教育和概念验证目的。
对于生产场景,考虑使用已建立的认证模式,配合预注册的应用程序和标准OAuth流程。
它用了什么
- ⚡ Azure 函数
- 🕸️ Azure API 管理
- 💪 肱二头肌
- 🐍 Python
- 🚀 Azure Developer CLI
[!注]
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
🤫 Authenticated Remote MCP Server
This sample shows how to deploy an Entra ID-protected MCP server on Azure.
The sample also uses an authorization pattern where the client acquires a token for the MCP server first, and then uses on-behalf-of flow to exchange it for a token that can be used with Microsoft Graph. It does all this in an entirely secretless manner too.
⚠️ Important: Experimental Implementation
[!IMPORTANT] This is an experimental implementation and should NOT be used in production scenarios.
This sample demonstrates how to build a protected MCP server with Entra ID by implementing OAuth 2.0 Dynamic Client Registration and PKCE flow patterns that work around current gaps in Entra ID’s native support for these standards. While it showcases the technical possibilities, it’s intended for educational and proof-of-concept purposes only.
For production scenarios, consider using established authentication patterns with pre-registered applications and standard OAuth flows.
What it uses
[!NOTE] You can use the Model Context Protocol Inspector or Visual Studio Code to test this MCP server.
How the Authorization Flow Works
This implementation uses a sophisticated OAuth 2.0 flow with PKCE (Proof Key for Code Exchange) to securely authenticate MCP clients. Here’s how it all fits together:
sequenceDiagram
participant Client as MCP Client
participant Browser as User's Browser
participant APIM as API Management
participant Entra as Entra ID
participant Function as Azure Function
participant Graph as Microsoft Graph
Note over Client,Graph: 1. Discovery & Registration
Client->>APIM: GET /.well-known/oauth-authorization-server
APIM->>Client: OAuth metadata (authorization_endpoint, token_endpoint, etc.)
Client->>APIM: POST /register (Dynamic Client Registration)
Note over APIM: Store client info in CosmosDBGenerate unique client_id
APIM->>Client: Client credentials (client_id, no secret)
Note over Client,Graph: 2. Authorization with PKCE
Note over Client: Generate code_verifier & code_challenge
Client->>Browser: Open authorization URL with PKCE params
Browser->>APIM: GET /authorize?client_id=...&code_challenge=...
安装
npx @modelcontextprotocol/[email protected]