MCP Auth Server

MCP Auth Server

A Model Context Protocol (MCP) server with HTTP transport that implements JWT authentication, allowing secure tool execution and prompt handling with role-based access control.

Category
访问服务器

README

MCP Auth Step by Step

This repository demonstrates building an MCP (Model Context Protocol) server with HTTP transport and JWT authentication, progressing through iterative steps.

Overview

The project shows how to build a secure MCP server with:

  • FastAPI-based HTTP transport
  • JWT token authentication
  • OAuth 2.0 metadata endpoints
  • Scope-based authorization
  • Role-based access control

Step-by-Step Progression

Step 1: Basic FastAPI Skeleton

  • File: http-transport-steps/src/mcp_http/step1.py
  • What it adds: Basic FastAPI application with health endpoint
  • Key features:
    • FastAPI server setup
    • Basic health check endpoint (/health)
    • Foundation for MCP HTTP transport

Step 2: Basic MCP Request Handling

  • File: http-transport-steps/src/mcp_http/step2.py
  • What it adds: MCP protocol request/response handling
  • Key features:
    • MCP request parsing and validation
    • Basic MCP response structure
    • /mcp endpoint for MCP protocol communication
    • JSON-RPC style request handling

Step 3: MCP Tools and Prompts Definitions

  • File: http-transport-steps/src/mcp_http/step3.py
  • What it adds: MCP tools and prompts without dispatching
  • Key features:
    • Tool definitions (echo, get_time)
    • Prompt definitions (greeting, help)
    • MCP protocol compliance for tools and prompts
    • No actual tool execution yet

Step 4: MCP Tools Dispatching

  • File: http-transport-steps/src/mcp_http/step4.py
  • What it adds: Actual tool execution and prompt handling
  • Key features:
    • Tool dispatching and execution
    • Prompt retrieval and handling
    • Working MCP server with functional tools
    • Error handling for invalid requests

Step 5: Basic JWT Infrastructure

  • File: http-transport-steps/src/mcp_http/step5.py
  • What it adds: JWT public key loading and JWKS endpoint
  • Key features:
    • Public key loading from file
    • JWKS (JSON Web Key Set) endpoint (/.well-known/jwks.json)
    • External token generation script (generate_token.py)
    • JWT infrastructure foundation

Step 6: JWT Token Validation

  • File: http-transport-steps/src/mcp_http/step6.py
  • What it adds: JWT authentication middleware and enforcement
  • Key features:
    • JWT token validation middleware
    • Authentication enforcement on /mcp endpoint
    • User context extraction from tokens
    • Proper error responses for invalid/missing tokens

Step 7: OAuth 2.0 Metadata Endpoints

  • File: http-transport-steps/src/mcp_http/step7.py
  • What it adds: OAuth 2.0 metadata for protected resource and authorization server
  • Key features:
    • /.well-known/oauth-protected-resource endpoint
    • /.well-known/oauth-authorization-server endpoint
    • Enhanced health endpoint with OAuth metadata
    • OAuth metadata in MCP responses

Step 8: Scope-Based Authorization

  • File: http-transport-steps/src/mcp_http/step8.py
  • What it adds: Permission checking and role-based access control
  • Key features:
    • check_permission method for scope validation
    • Role-based access control (admin, user, guest)
    • 403 Forbidden responses for insufficient permissions
    • Scope enforcement for MCP operations

Step 9: Enhanced MCP Integration (Planned)

  • What it will add: User context in responses and authenticated tools
  • Planned features:
    • User context in MCP response headers/metadata
    • Authenticated tools with user-aware behavior
    • Enhanced MCP protocol integration
    • Personalized responses based on user identity

JWT Token Structure

The JWT tokens include:

  • User ID: Unique identifier for the user
  • Scopes: Permissions (e.g., mcp:read, mcp:tools, mcp:prompts)
  • Roles: User roles (e.g., admin, user, guest)
  • Expiration: Token validity period

Testing

Each step includes a corresponding test script (test_stepX.sh) that validates:

  • Basic functionality
  • JWT authentication (steps 5+)
  • Authorization (steps 6+)
  • OAuth metadata (steps 7+)
  • Access control (steps 8+)

Usage

Prerequisites

  1. Install uv: https://docs.astral.sh/uv/getting-started/installation/
  2. Navigate to the http-transport-steps directory

Running Steps with uv

# Run any step using uv run
uv run step1
uv run step2
uv run step3
# ... etc

Token Generation

For steps 5-8 that require JWT authentication, you can generate tokens using the generate_token.py script:

uv run python generate_token.py --username alice --scopes mcp:read,mcp:tools
uv run python generate_token.py --username bob --scopes mcp:read,mcp:prompts
uv run python generate_token.py --username admin --scopes mcp:read,mcp:tools,mcp:prompts
uv run python generate_token.py --username guest --scopes ""

Keycloak Token Generation

To quickly get a token for testing step9/keycloak:

curl -X POST "http://localhost:8080/realms/mcp-realm/protocol/openid-connect/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=password" \
  -d "client_id=mcp-test-client" \
  -d "username=mcp-admin" \
  -d "password=admin123" \
  -d "scope=openid profile email mcp:read mcp:tools mcp:prompts" | jq -r '.access_token'

The script will output a JWT token that can be used in the Authorization: Bearer <token> header for authenticated requests.

Dependencies

  • FastAPI
  • PyJWT
  • cryptography
  • uvicorn

The project uses uv for dependency management with pyproject.toml configuration.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选