AS

azure-samples/remote-mcp-webapp-python-auth-oauth

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

28 stars 13 forks 质量 53 趋势 11

一台基于FastAPI构建的生产准备模型上下文协议(MCP)服务器,利用国家气象局API提供天气信息。

概览

一台基于 FastAPI 构建的生产准备模型上下文协议(MCP)服务器,利用国家气象服务 API 提供天气信息。具备 PKCE、动态客户端注册和 Azure AD 集成的功能。准备部署至 Azure 应用服务,使用 Azure Developer CLI(azd)。 - : MCP 授权规范的完整实现(2025-03-26) - : 带验证密钥进行代码交换的安全授权(RFC 7636,S256 方法) - : 根据 RFC 7591 自动注册客户端 - : 根据 RFC 8414 进行发现端点 - : 使用 Azure AD 作为授权服务器 - : 完全支持 MCP 协议 - 版本:2025-03-26 - : 基于安全令牌的认证 - : - get_alerts: 获取美国任何州的天气警报 - get_forecast: 获取任意地点的详细天气预报 - : 预配置为 Azure 应用服务部署 - : 内置 OAuth 2.1 流测试 该服务器实现了完整的MCP授权规范(2025-03-26): - 去 /。

概述(AI 翻译)

一台配备FastAPI的生产准备模型上下文协议(MCP)服务器,利用国家气象局API提供天气信息。功能完全符合MCP OAuth 2.1标准,支持PKCE、动态客户端注册和Azure AD集成。准备部署到Azure App Service,支持Azure Developer CLI(azd)。

🌟 特色

  • MCP OAuth 2.1 规范合规:MCP 授权规范的完整实现(2025-03-26)
  • PKCE 要求:带证明密钥的安全授权用于代码交换(RFC 7636,S256 方法)
  • 动态客户端注册:根据RFC 7591自动注册客户端
  • 授权服务器元数据:根据RFC 8414进行发现端点
  • 第三方授权:使用 Azure AD 作为授权服务器
  • MCP 协议头部:完全支持 MCP-Protocol-Version: 2025-03-26
  • JWT 令牌管理:基于令牌的安全认证
  • 天气工具:
  • get_alerts:获取美国任何州的天气警报
  • get_forecast:获取任意地点的详细天气预报
  • Azure Ready:已预配置用于Azure App Service部署
  • Web 测试接口:内置 OAuth 2.1 流程测试

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

Python MCP Weather Server with OAuth 2.1 Authentication

A production-ready Model Context Protocol (MCP) server built with FastAPI that provides weather information using the National Weather Service API. Features full MCP OAuth 2.1 compliance with PKCE, dynamic client registration, and Azure AD integration. Ready for deployment to Azure App Service with Azure Developer CLI (azd).

🌟 Features

  • MCP OAuth 2.1 Specification Compliant: Complete implementation of MCP Authorization Specification (2025-03-26)
  • PKCE Required: Secure authorization with Proof Key for Code Exchange (RFC 7636, S256 method)
  • Dynamic Client Registration: Automatic client registration per RFC 7591
  • Authorization Server Metadata: Discovery endpoint per RFC 8414
  • Third-Party Authorization: Uses Azure AD as authorization server
  • MCP Protocol Headers: Full support for MCP-Protocol-Version: 2025-03-26
  • JWT Token Management: Secure token-based authentication
  • Weather Tools:
    • get_alerts: Get weather alerts for any US state
    • get_forecast: Get detailed weather forecast for any location
  • Azure Ready: Pre-configured for Azure App Service deployment
  • Web Test Interface: Built-in OAuth 2.1 flow testing

🔐 MCP Authorization Implementation

This server implements the complete MCP Authorization Specification (2025-03-26):

OAuth 2.1 Endpoints

  • GET /.well-known/oauth-authorization-server - Authorization server metadata (RFC 8414)
  • POST /register - Dynamic client registration (RFC 7591)
  • GET /authorize - Authorization endpoint with PKCE (RFC 7636)
  • POST /token - Token endpoint for code exchange and refresh
  • GET /auth/azure/callback - Third-party authorization callback

MCP Protocol Features

  • ✅ Protocol Version Headers: MCP-Protocol-Version: 2025-03-26
  • ✅ PKCE Required: All clients must use S256 method
  • ✅ Dynamic Registration: Automatic client onboarding
  • ✅ JWT Authentication: Bearer token validation on MCP endpoints
  • ✅ Proper Error Handling: 401/403/400 responses with details
  • ✅ Azure AD Integration: Enterprise-grade authorization server

⚠️ Important: Complete OAuth setup required before use. See AUTH_SETUP.md for Azure AD configuration instructions.

💻 Local Development

Prerequisites

  • Python 3.8+
  • Azure account with completed OAuth setup (see AUTH_SETUP.md)

Setup & Run

  1. Complete OAuth setup first: Follow the instructions in AUTH_SETUP.md to create your Azure App Registration.

  2. Clone and install dependencies:

    git clone 
    cd remote-mcp-webapp-python-auth-oauth
    python -m venv venv
    .\venv\Scripts\Activate.ps1  # Windows
    # source venv/bin/activate   # macOS/Linux
    pip install -r requirements.txt
    
  3. Configure environment variables:

在 GitHub 查看完整 README

安装

npx @modelcontextprotocol/inspector
新手?先看 mcpskill.org 教程 →

配置

{ "mcpServers": { "weather-mcp-server-local": { "transport": { "type": "http", "url": "http://localhost:8000/", "headers": { "Authorization": "Bearer <your-jwt-token>" } }, "name": "Weather MCP Server (Local with Auth)", "description": "Authenticated MCP Server with weather tools" } } }