Python MCP 天气服务器与 OAuth 2.1 认证 标志

Python MCP 天气服务器与 OAuth 2.1 认证

组织
Azure-Samples

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,…

发布者Azure-Samples
代码仓库remote-mcp-webapp-python-auth-oauth
语言Python
复刻数
12
星标数
27
可用工具
0
传输类型streamable-http
分类
链接
  • 将工具接入 AI 工作流

    Python MCP 天气服务器与 OAuth 2.1 认证 提供 MCP 功能,可供兼容的 AI 客户端和智能体使用。

  • 0 个可用工具

    浏览下方可调用的操作;如果服务器提供了名称和描述,也会一并显示。

  • 可直接复制的配置

    使用安装配置片段,在您首选的 MCP 客户端中配置此服务器。

  • 开源社区热度

    关联的代码仓库有 27 个 Star 和 12 个 Fork。

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:

    bash
    git clone <your-repo-url>
    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:

    bash
    cp .env.example .env
    # Edit .env with your Azure OAuth credentials from AUTH_SETUP.md
  4. Start the development server:

    bash
    .\start_server.ps1  # Windows
    # or manually:
    uvicorn main:app --host 0.0.0.0 --port 8000 --reload
  5. Access the server:

🔌 Connect to the Local MCP Server

Authentication Required

Before connecting any MCP client, you must authenticate:

  1. Get JWT Token: Visit http://localhost:8000/mcp_oauth_test.html
  2. Complete OAuth Flow: Use the built-in OAuth 2.1 test interface
  3. Copy JWT Token: Use the token in your MCP client configuration

Using MCP Inspector

  1. In a new terminal window, install and run MCP Inspector:

    bash
    npx @modelcontextprotocol/inspector
  2. CTRL+click the URL displayed by the app (e.g. http://localhost:5173/#resources)

  3. Configure authenticated connection:

    • Set transport type to HTTP

    • Set URL to: http://localhost:8000/

    • Add Authorization header: Bearer <your-jwt-token>

    💡 Getting your JWT token: Visit http://localhost:8000/mcp_oauth_test.html to complete the OAuth 2.1 flow and obtain your JWT token.

  4. Test the connection: List Tools, click on a tool, and Run Tool

Configuration for MCP Clients

json
{
  "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"
    }
  }
}
```   > 💡 **Replace `<your-jwt-token>`** with the actual JWT token obtained from the OAuth 2.1 flow at `/mcp_oauth_test.html`.

## 🚀 Quick Deploy to Azure

### Prerequisites

- [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli)
- [Azure Developer CLI (azd)](https://learn.microsoft.com/en-us/azure/developer/azure-developer-cli/install-azd)
- Active Azure subscription
- **Completed OAuth setup** (see [AUTH_SETUP.md](AUTH_SETUP.md))

### Deploy in 5 Commands

```bash
# 1. Login to Azure
azd auth login

# 2. Initialize the project  
azd init

# 3. Set OAuth environment variables (from your AUTH_SETUP.md)
azd env set AZURE_CLIENT_ID "your-client-id"
azd env set AZURE_TENANT_ID "your-tenant-id"
azd env set AZURE_CLIENT_SECRET "your-client-secret"
azd env set JWT_SECRET_KEY "your-secure-jwt-secret"

# 4. Deploy to Azure (first time to get the URL)
azd up

# 5. Update environment with deployed URL and redeploy
azd env set BASE_URL "https://app-web-[unique-id].azurewebsites.net"
azd env set AZURE_REDIRECT_URI "https://app-web-[unique-id].azurewebsites.net/auth/azure/callback"
azd env set ENVIRONMENT "production"
azd up

Post-Deployment Setup

⚠️ Critical: After deployment, you must update your Azure App Registration:

  1. Note your deployed URL: https://app-web-[unique-id].azurewebsites.net/
  2. Go to Azure Portal → Microsoft Entra ID → App registrations → Your App
  3. Click Authentication → Add redirect URI: https://app-web-[unique-id].azurewebsites.net/auth/azure/callback
  4. Click Save

💡 Note: The redirect URI must be /auth/azure/callback (not /auth/callback) for the MCP OAuth 2.1 flow to work correctly.

Test Your Deployment

After deployment, your authenticated MCP server will be available at:

  • OAuth 2.1 Test Interface: https://<your-app>.azurewebsites.net/mcp_oauth_test.html
  • Health Check: https://<your-app>.azurewebsites.net/health
  • MCP Capabilities: https://<your-app>.azurewebsites.net/mcp/capabilities
  • API Docs: https://<your-app>.azurewebsites.net/docs

🔌 Connect to the Remote MCP Server

Follow the same guidance as the local setup, but use your Azure App Service URL and ensure you have a valid JWT token from the deployed authentication endpoint.

Configuration for deployed server:

json
{
  "mcpServers": {
    "weather-mcp-server-azure": {
      "transport": {
        "type": "http", 
        "url": "https://<your-app>.azurewebsites.net/",
        "headers": {
          "Authorization": "Bearer <your-jwt-token>"
        }
      },
      "name": "Weather MCP Server (Azure with Auth)",
      "description": "Authenticated MCP Server hosted on Azure"
    }
  }
}

🧪 Testing

Interactive OAuth 2.1 Testing

The test interface provides:

  1. Complete OAuth 2.1 Flow: Dynamic client registration → Authorization → Token exchange
  2. PKCE Validation: Test the full Proof Key for Code Exchange flow
  3. MCP Endpoint Testing: Test authenticated weather tools
  4. JWT Token Display: View and validate your authentication tokens
  5. Client Callback Testing: Includes /client-callback endpoint for OAuth flow validation

OAuth Flow Architecture

The server implements a complete OAuth 2.1 flow:

  • Client Registration: Dynamic client registration with auto-generated credentials
  • Authorization: User redirected to Azure AD for authentication
  • Azure Callback: Server receives Azure auth code at /auth/azure/callback
  • Client Callback: Server redirects to client's callback (e.g., /client-callback) with authorization code
  • Token Exchange: Client exchanges authorization code for JWT access token

MCP Client Testing

Test with any MCP-compatible client using the authenticated endpoints and your JWT token.

🌦️ Data Source

This server uses the National Weather Service (NWS) API:

  • Real-time weather alerts and warnings
  • Detailed weather forecasts
  • Official US government weather data
  • No API key required
  • High reliability and accuracy

🔒 Security Features

  • ✅ OAuth 2.1 Compliance: Full MCP Authorization Specification implementation
  • ✅ PKCE Required: S256 method for all authorization flows
  • ✅ Dynamic Client Registration: Secure automatic client onboarding
  • ✅ Azure AD Integration: Enterprise-grade authorization server
  • ✅ JWT Token Security: Configurable expiration and secure validation
  • ✅ Protocol Version Enforcement: MCP-Protocol-Version header validation
  • ✅ Request Logging: Full audit trail with user identification
  • ✅ CORS Protection: Proper cross-origin resource sharing policies

安装

TypingMind
前提条件:

Node.js 18+

{
  "mcpServers": {
    "azure-samples-remote-mcp-webapp-python-auth-oauth": {
      "command": "",
      "args": []
    }
  }
}

将 Python MCP 天气服务器与 OAuth 2.1 认证 MCP 与多个 AI 模型配合使用

TypingMind 在工作区级别连接 MCP 工具,因此连接 Python MCP 天气服务器与 OAuth 2.1 认证 后,您就可以在 TypingMind 中搭配不同的 AI 模型使用,无需为每个模型单独设置。 此 MCP 通过远程托管的 MCP 服务器 URL 在 TypingMind 中连接。

添加 MCP 服务器 URL

如果 Python MCP 天气服务器与 OAuth 2.1 认证 已托管在远程服务器上,或您的团队希望让多个用户共用一个连接器,请选择此方式。

1

打开 MCP 连接器

在 TypingMind 中,前往“插件”,打开“MCP 连接器”,然后选择“添加 URL”。

  1. 在您的浏览器中打开 TypingMind。
  2. 进入“插件”。
  3. 打开“MCP 连接器”。
  4. 点击“添加 URL”。
TypingMind 的“添加自定义 MCP 服务器 URL”表单
2

粘贴服务器 URL

在“服务器 URL”字段中输入 您的服务器 URL。根据服务器要求,添加连接名称、描述、图标、自定义 HTTP 请求头或 OAuth 客户端设置。

  1. 将 您的服务器 URL 粘贴到“服务器 URL”字段中。
  2. 为 Python MCP 天气服务器与 OAuth 2.1 认证 输入连接名称。
  3. 您可以添加描述和图标,以便更容易识别此连接。
  4. 如果服务器需要身份验证,请添加自定义 HTTP 请求头或 OAuth 客户端信息。
3

创建连接

点击“创建连接”,然后返回插件列表,确认新的 MCP 连接显示为“有效”。

  1. 点击“创建连接”。
  2. 返回 MCP 连接器列表。
  3. 确认 Python MCP 天气服务器与 OAuth 2.1 认证 连接显示为“有效”。
  4. 如果连接未立即出现,请刷新插件列表。
4

切换模型,无需重新连接

使用您偏好的模型开始聊天,在“插件”中启用 Python MCP 天气服务器与 OAuth 2.1 认证 工具,并根据需要随时切换到其他模型。该 MCP 连接在 TypingMind 工作区中仍然可用。

  1. 在 TypingMind 中开始新聊天。
  2. 选择您想使用的 AI 模型。
  3. 在“插件”中启用 Python MCP 天气服务器与 OAuth 2.1 认证 工具。
  4. 需要时,请让模型使用该工具。
  5. 切换到其他 AI 模型,继续使用同一个 MCP 连接。
在 TypingMind 聊天中使用已启用的 MCP 工具和选定的 AI 模型
您能使用 Python MCP 天气服务器与 OAuth 2.1 认证 帮我完成这项任务吗?
Python MCP 天气服务器与 OAuth 2.1 认证
当然。我已阅读。
以下是我使用 Python MCP 天气服务器与 OAuth 2.1 认证 得到的结果。

常见问题

Python MCP 天气服务器与 OAuth 2.1 认证 MCP 服务器有什么用途?

Python MCP 天气服务器与 OAuth 2.1 认证 是一个 MCP 服务器,可让兼容的 AI 客户端连接外部工具并获取上下文。在 TypingMind 中,只需添加一次此 MCP 服务器,即可在您的 AI 工作区中使用其工具。

可以在 TypingMind 中让多个 AI 模型使用 Python MCP 天气服务器与 OAuth 2.1 认证 MCP 吗?

可以。TypingMind 在工作区层面连接 MCP 工具,因此您可以让 Claude、ChatGPT、Gemini 等不同 AI 模型,或您在 TypingMind 中配置的其他模型使用 Python MCP 天气服务器与 OAuth 2.1 认证,而无需为每个模型单独配置 MCP 服务器。

为什么在 TypingMind 中使用 Python MCP 天气服务器与 OAuth 2.1 认证 MCP?

TypingMind 将多个 AI 模型、提示词、插件、AI 智能体、API 密钥和 MCP 工具整合到同一个工作区中,是优秀的大语言模型聊天前端之一。连接 Python MCP 天气服务器与 OAuth 2.1 认证 后,您可以在偏好的不同模型中使用其 MCP 工具,同时在 TypingMind 中井然有序地管理聊天工作流程。

如何将 Python MCP 天气服务器与 OAuth 2.1 认证 MCP 连接到 TypingMind?

在 TypingMind 中添加 Python MCP 天气服务器与 OAuth 2.1 认证 托管的 MCP 服务器 URL,即可连接。如果您希望在 TypingMind 工作区中使用远程 MCP 连接,这种方式很适合。

Python MCP 天气服务器与 OAuth 2.1 认证 MCP 在 TypingMind 中提供哪些工具?

Python MCP 天气服务器与 OAuth 2.1 认证 提供 MCP 功能,您可以在 TypingMind 的插件页面启用这些功能,然后在聊天中使用,或将其分配给 AI 智能体。

使用 Python MCP 天气服务器与 OAuth 2.1 认证 MCP 需要向 TypingMind 提供我的 API 密钥吗?

不需要。TypingMind 采用本地优先的设计,让您自行掌控模型提供商、API 密钥、提示词和 MCP 配置。如果 Python MCP 天气服务器与 OAuth 2.1 认证 需要身份验证,请在创建连接时,为该 MCP 服务器添加所需的请求头、OAuth 设置或本地配置。

相关 MCP 服务器

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇