OAuth认证演示

一个展示MCP协议下OAuth 2.0认证流程的演示项目,包含授权服务器、资源服务器和客户端的完整实现。

  • by 22f2000147
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 devloper-hs/oauth-demo

使用说明

MCP OAuth演示

使用模型上下文协议(MCP)演示OAuth 2.0身份验证,展示使用oAuths的独立授权服务器/资源服务器架构


概述

本项目演示了如何使用以下方法为MCP服务器和客户端实现OAuth 2.0身份验证:

  • 授权服务器(AS):处理OAuth流、客户端注册和令牌颁发
  • 资源服务器(RS):通过自检验证令牌,并为受保护的MCP资源提供服务
  • MCP客户端:使用OAuth进行身份验证并连接到受保护的MCP服务器

如何跑步

确保你有 python3 & uv 安装。然后,

  1. 克隆仓库
git clone https://github.com/devloper-hs/oauth-demo.git
  1. 更改目录
cd oauth-demo
  1. 启动授权服务器
cd simple-auth
uv run mcp-simple-auth-as --port=9000
  1. 启动资源服务器
cd simple-auth
uv run mcp-simple-auth-rs --port=8001 --auth-server=http://localhost:9000 --transport=streamable-http
  1. 运行客户端
cd simple-auth-client
MCP_SERVER_PORT=8001 MCP_TRANSPORT_TYPE=streamable_http uv run mcp-simple-auth-client
  1. 示例视频

https://github.com/user-attachments/assets/d4993185-ff07-4808-afe3-20267be9914c


项目结构

oauth-demo/
├── simple-auth/                    # Authorization & Resource Server
│   ├── mcp_simple_auth/
│   │   ├── auth_server.py         # Authorization Server
│   │   ├── server.py              # Resource Server  
│   │   ├── simple_auth_provider.py # OAuth provider (demo provider)
│   │   └── token_verifier.py      # Token introspection
│   └── pyproject.toml
├── simple-auth-client/             # MCP Client
│   ├── mcp_simple_auth_client/
│   │   └── main.py                # OAuth client
│   └── pyproject.toml
└── README.md

建筑

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   MCP Client    │─── │ Authorization   │─── │  Resource       │
│                 │    │ Server (AS)     │    │  Server (RS)    │
│ - OAuth flow    │    │ - User auth     │    │ - Token verify  │
│ - Token storage │    │ - Token issue   │    │ - MCP tools     │
│ - MCP calls     │    │ - Introspection │    │ - Protected API │
└─────────────────┘    └─────────────────┘    └─────────────────┘

注意:此oAuth流包含用于演示的硬编码变量。不要将其用作生产工具!