OAuth MCP服务器
支持PKCE的FastMCP的完整OAuth 2.1服务器实现。
⚠️ 安全警告
这是一种高级身份验证模式。 构建一个安全的OAuth服务器需要在身份验证协议、密码学和安全最佳实践方面拥有深厚的专业知识。FastMCP文档强烈建议使用远程OAuth或OAuth代理,除非您有强制要求。
看 OAUTH_README.md 以获取完整的文档。
快速开始
安装
# Install dependencies
uv sync运行服务器
python main.py服务器将于启动 http://localhost:8000 注册了演示OAuth客户端。
测试OAuth流
在单独的终端中:
python oauth_client_example.py这将演示完整的OAuth 2.1流程,包括:
- PKCE挑战/验证器生成
- 授权码交换
- 访问令牌使用情况
- 令牌刷新
测试动态客户端注册
在运行时动态注册新的OAuth客户端:
python test_dcr.py或者使用curl:
curl -X POST http://localhost:8000/oauth/register \
-H "Content-Type: application/json" \
-d '{
"redirect_uris": ["http://localhost:5000/callback"],
"client_name": "My App",
"scope": "read write"
}'服务器将以 client_id 和 client_secret 您可以立即使用。
测试新的OAuth端点
测试所有新的OAuth 2.0/2.1端点:
python test_new_endpoints.py该测试:
- OAuth授权服务器元数据发现
- 令牌撤销(RFC 7009)
- 令牌自检(RFC 7662)
- UserInfo端点
演示凭据
OAuth客户端:
- 客户端ID:
demo_client - 客户端密码:
demo_secret
演示用户:
- 用户名:
demo_user - 密码:
demo_password
项目结构
oauth_mcp/
├── main.py # FastMCP server with OAuth
├── oauth_provider.py # OAuth 2.1 server implementation
├── oauth_client_example.py # Complete OAuth flow demo
├── test_dcr.py # Dynamic Client Registration test
├── test_new_endpoints.py # Tests for all new OAuth endpoints
├── client.py # Original simple client (no auth)
├── OAUTH_README.md # Complete documentation
└── README.md # This file特性
✅ 完整的OAuth 2.1实现\ ✅ PKCE(代码交换证明密钥)\ ✅ 授权码流\ ✅ 令牌刷新与轮换\ ✅ 令牌撤销(RFC 7009)\ ✅ 令牌自检(RFC 7662)\ ✅ 范围验证\ ✅ CSRF保护状态参数\ ✅ 动态客户端注册(DCR)-RFC 7591\ ✅ OAuth授权服务器元数据(RFC 8414)\ ✅ OAuth保护资源元数据(RFC 9470)\ ✅ 用户配置文件的UserInfo端点
文档
看 OAUTH_README.md 用于:
- 详细架构
- 安全注意事项
- 生产部署指南
- 数据库模型
- 测试策略
- 故障排除
参考文献
许可证
版权所有Anysphere股份有限公司。
