AA

alejandro-ao/mcp-fastapi-auth

开发工具

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

17 stars 18 forks 质量 43 趋势 21

与 OAuth 2.1 和 ScalKit 在 FastAPI 中的 MCP Auth 演示

概览

这是安全的"模式上下文协议"(MCP)服务器,使用Scalkit作为授权服务器执行OAuth 2.1认证. 该服务器通过Tavily API提供网络搜索能力,同时确保适当的认证和授权. MCP(Model Context Protocol)是设计用于LLMS与工具或资源之间通信的协议. 这一实施通过OAuth 2.1认证增加了企业级安全,使其适合在出入控制至关重要的地方进行生产部署。 - :使用 Scalkit 授权服务器安全访问 - : 执行 MCP 授权规格 - : 通过 Tavily API 提供网络搜索功能 - : 支持自动客户端注册 - : 验证访问令牌,并进行适当的受众检查 - : 执行 PKCE , 适当的错误处理, 以及令牌受众验证 1. : 在 Scalkit 2. 注册 : 从 Tavily 3. 获取您的 API 密钥 .

概述(AI 翻译)

这是安全的"模式上下文协议"(MCP)服务器,使用Scalkit作为授权服务器执行OAuth 2.1认证. 该服务器通过Tavily API提供网络搜索能力,同时确保适当的认证和授权.

□ 什么是MCP吗?

MCP(Model Context Protocol)是设计用于LLMS与工具或资源之间通信的协议. 这一实施通过OAuth 2.1认证增加了企业级安全,使其适合在出入控制至关重要的地方进行生产部署。

□ 特性

  • 2.1 认证: 使用 Scalkit 授权服务器安全访问
  • ** MCP合规情况**:执行MCP授权规定
  • 网页搜索**: 通过 Tavily API 提供网络搜索功能
  • ** 动态客户登记**: 支持自动客户端注册
  • ** 验证**: 通过适当的观众检查验证访问符
  • ** 安全最佳做法**:实施PKCE、适当的错误处理和象征性受众验证

□ 先决条件

  1. ** Scalekit账户**:在Scalekit注册
  2. Tavily API 密钥:从 Tavily 获取您的 API 密钥
  3. **P 3.

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

README

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

MCP Server with OAuth 2.1 Authentication

This is a secure Model Context Protocol (MCP) server that implements OAuth 2.1 authentication using Scalekit as the authorization server. The server provides web search capabilities through the Tavily API while ensuring proper authentication and authorization.

What is MCP?

MCP (Model Context Protocol) is a protocol designed for communication between LLMs and tools or resources. This implementation adds enterprise-grade security through OAuth 2.1 authentication, making it suitable for production deployments where access control is critical.

Features

  • OAuth 2.1 Authentication: Secure access using Scalekit authorization server
  • MCP Compliance: Implements the MCP authorization specification
  • Web Search: Provides web search functionality via Tavily API
  • Dynamic Client Registration: Supports automatic client registration
  • Token Validation: Validates access tokens with proper audience checking
  • Security Best Practices: Implements PKCE, proper error handling, and token audience validation

Prerequisites

  1. Scalekit Account: Sign up at Scalekit
  2. Tavily API Key: Get your API key from Tavily
  3. Python 3.11+: Required for running the server

Setup

1. Install Dependencies

pip install -e .

2. Configure Environment Variables

Copy the example environment file and configure your settings:

cp .env.example .env

Edit .env with your actual values:

# Your Scalekit environment URL
SCALEKIT_ENVIRONMENT_URL=https://your-company.scalekit.com

# Your MCP server's resource identifier
RESOURCE_IDENTIFIER=https://your-mcp-server.com

# Server port (optional)
PORT=10000

# Tavily API key
TAVILY_API_KEY=your_tavily_api_key_here

3. Configure Scalekit

In the Scalekit dashboard:

  1. Navigate to MCP servers and click Add MCP server
  2. Configure your server:
    • Server name: “Web Search MCP Server”
    • Resource identifier: Your server URL (e.g., https://your-mcp-server.com)
    • Allow dynamic client registration: Enable this
    • Access token lifetime: 300-3600 seconds
    • Scopes: web-search:read, web-search:write

Running the Server

python src/server.py

The server will start on http://localhost:10000 (or your configured port).

API Endpoints

OAuth Discovery Endpoints

  • GET /.well-known/oauth-protected-resource - Resource metadata for MCP client discovery
  • GET /.well-known/oauth-authorization-server - Authorization server metadata (proxied from Scalekit)

OAuth Endpoints

  • POST /oauth/register - Dynamic client registration (proxied to Scalekit)

MCP Endpoints

  • POST /web-search/mcp - Main MCP endpoint (requires authentication)

Authentication Flow

在 GitHub 查看完整 README

安装

This server does not publish a one-line install command.

Open the repository installation guide 新手?先看 mcpskill.org 教程 →