使用说明
MCP授权概念证明
一个完整的实现,演示了具有动态客户端注册(DCR)和OAuth 2.0流程的模型上下文协议(MCP)授权规范。
概述
本项目展示了MCP客户如何:
- 注册 使用动态客户端注册(RFC 7591)的MCP服务器
- 验证 使用OAuth 2.0授权代码流(RFC 6749)
- 访问受保护的工具 使用获得的访问令牌
这种方法实现了对企业工具和数据集的动态、可扩展访问,而不需要预先配置的客户端凭据。
项目结构
mcp-auth-poc/
├── server/ # MCP server with DCR and OAuth provider
│ ├── src/
│ │ ├── index.ts # Main server entry point
│ │ ├── dcr.ts # Dynamic Client Registration handler
│ │ ├── oauth.ts # OAuth 2.0 authorization & token endpoints
│ │ ├── mcp-server.ts # MCP protocol server with tools
│ │ ├── storage.ts # Token and client storage
│ │ └── file-storage.ts # File-based persistence
│ └── package.json
├── client/ # MCP client with self-registration
│ ├── src/
│ │ ├── index.ts # Main client orchestration
│ │ ├── dcr-client.ts # DCR registration client
│ │ ├── oauth-client.ts # OAuth authorization flow
│ │ └── mcp-client.ts # MCP protocol client
│ └── package.json
├── shared/ # Shared TypeScript types
│ └── src/types.ts
└── README.md
先决条件
- Node.js 20+和npm
- web浏览器(用于OAuth授权流)
安装
# Install all dependencies
npm install
# Build all packages
npm run build
用法
运行完整流程
终端1-启动HTTP服务器 (适用于DCR和OAuth):
cd server
npm start
您应该看到:
🎯 MCP Authorization Server POC
================================
✅ HTTP server running on http://localhost:3000
Available endpoints:
- POST http://localhost:3000/register (DCR)
- GET http://localhost:3000/oauth/authorize
- POST http://localhost:3000/oauth/token
终端2-运行客户端:
cd client
npm start
客户将:
- ✅ 在服务器(DCR)上注册自己
- 🌐 打开浏览器进行OAuth授权
- ✅ 将身份验证码替换为访问令牌
- 🔌 通过stdio连接到MCP服务器
- 📋 列出可用工具
- 🔓 调用公共工具
- 🔐 使用访问令牌调用受身份验证保护的回显工具
期望输出
🎯 MCP Authorization Client POC
=================================
Step 1: Dynamic Client Registration
------------------------------------
📝 Registering client with server...
✅ Client registration successful!
Client ID: client_f343ef7a-cab6-4b24-a4f5-f925ac931848
Step 2: OAuth Authorization Flow
--------------------------------
🔐 Starting OAuth authorization flow...
Callback server listening on port 3001
🌐 Opening browser for authorization...
✅ Authorization code received
Step 3: Token Exchange
----------------------
🔄 Exchanging authorization code for access token...
✅ Access token received!
Token type: Bearer
Expires in: 3600 seconds
Step 4: MCP Tool Access
-----------------------
🔌 Connecting to MCP server...
✅ Connected to MCP server
📋 Fetching available tools...
📋 Available tools: echo, public_info
🔓 Testing public tool...
Response: { server: 'MCP Auth Server', version: '1.0.0', ... }
🔐 Testing auth-protected tool...
Response: { echo: 'Hello from authenticated client!', authorized: true }
✅ Authorization flow complete!
快速演示:测试单个工具
如果您想使用访问令牌直接测试MCP工具,请使用演示脚本:
第一步: 使用上述步骤(或下面的curl命令)获取访问令牌
第二步: 运行演示脚本:
cd client
npx tsx src/demo-tools.ts <your-access-token>
这将证明:
- 📋 工具/列表 -列出可用工具(无身份验证)
- 🔓 public_info -调用公共工具(无身份验证)
- ❌ 无标记回声 -因未经授权的错误而失败
- ✅ 带令牌的回声 -使用有效令牌成功
看 client/src/demo-tools.ts 为了实施。
建筑
服务器组件
1.HTTP服务器(端口3000)
- DCR端点 (
POST /register):根据RFC 7591处理客户端注册 - OAuth授权 (
GET /oauth/authorize):授权端点(POC自动批准) - OAuth令牌 (
POST /oauth/token):令牌交换端点
2.MCP服务器(stdio模式)
- 工具:
public_info:公共工具,无需身份验证echo:受身份验证保护的工具,需要有效的访问令牌
- 协议:MCP通过stdio传输
- 验证:根据基于文件的存储检查访问令牌
3.存储层
- 内存+文件持久性:在HTTP和stdio服务器进程之间共享令牌
- 商店:注册客户端、授权码、访问令牌
客户端组件
1.DCR客户端
- 向服务器发送注册请求
- 接收
client_id和client_secret
2.OAuth客户端
- 启动本地回叫服务器(端口3001)
- 打开浏览器进行用户授权
- 通过重定向接收授权码
- 访问令牌的交换代码
3.MCP客户端
- 通过stdio连接到MCP服务器
- 列出可用工具
- 在参数中调用带有访问令牌的工具
授权流程
┌─────────┐ ┌─────────┐
│ Client │ │ Server │
└────┬────┘ └────┬────┘
│ │
│ 1. POST /register (DCR) │
├──────────────────────────────────────────►│
│ client_id, client_secret │
│◄──────────────────────────────────────────┤
│ │
│ 2. GET /oauth/authorize │
├──────────────────────────────────────────►│
│ (opens browser) │
│ │
│ 3. Redirect with auth code │
│◄──────────────────────────────────────────┤
│ │
│ 4. POST /oauth/token │
├──────────────────────────────────────────►│
│ access_token │
│◄──────────────────────────────────────────┤
│ │
│ 5. MCP connection (stdio) │
├═══════════════════════════════════════════┤
│ tools/call (with access_token) │
├──────────────────────────────────────────►│
│ response (authorized) │
│◄──────────────────────────────────────────┤
主要特点
✅ 动态客户端注册(DCR) -符合RFC 7591 ✅ OAuth 2.0授权代码流 -符合RFC 6749 ✅ MCP协议 -全面实施stdio传输 ✅ 受身份验证保护的工具 -工具调用的令牌验证 ✅ 基于文件的令牌持久化 -跨进程共享的令牌 ✅ 自动审批OAuth -简化POC演示流程
发展
# Run server in development mode
npm run dev:server
# Run client in development mode
npm run dev:client
# Build all packages
npm run build
# Build specific package
npm run build -w server
npm run build -w client
手动测试端点
注册客户
curl -X POST http://localhost:3000/register \
-H "Content-Type: application/json" \
-d '{
"client_name": "Test Client",
"redirect_uris": ["http://localhost:3001/callback"]
}'
启动OAuth流
在浏览器中打开:
http://localhost:3000/oauth/authorize?response_type=code&client_id=<CLIENT_ID>&redirect_uri=http://localhost:3001/callback
交换令牌
curl -X POST http://localhost:3000/oauth/token \
-H "Content-Type: application/json" \
-d '{
"grant_type": "authorization_code",
"code": "<AUTH_CODE>",
"redirect_uri": "http://localhost:3001/callback",
"client_id": "<CLIENT_ID>",
"client_secret": "<CLIENT_SECRET>"
}'
安全说明
这是一个 概念验证 用于演示目的。生产中:
- ⚠️ 在OAuth授权之前添加适当的用户身份验证
- ⚠️ 为公共客户端实现PKCE
- ⚠️ 对所有端点使用HTTPS
- ⚠️ 添加适当的同意屏幕
- ⚠️ 实现令牌刷新流程
- ⚠️ 使用适当的数据库而不是文件存储
- ⚠️ 添加速率限制和安全标头
- ⚠️ 严格验证重定向URI
- ⚠️ 添加适当的错误处理
要点
何时将MCP与DCR+OAuth结合使用
✅ 合身:
- 与许多企业SaaS工具动态集成
- 客户自行注册的多租户应用程序
- 需要用户特定数据访问的场景
- 需要发现和授权的工具
❌ 不适合:
- 简单的单一服务集成(使用直接OAuth)
- 需要复杂权限模型的场景(使用命令式方法)
- 实时、高通量操作(协议开销)
这种方法的好处
- 无预配置:客户端可以动态注册
- 标准协议:基于经过验证的OAuth 2.0规范构建
- 可扩展的:适用于多种服务,无需自定义集成
- 用户控件:OAuth提供明确的授权时刻
- 工具发现:MCP协议支持动态能力发现
许可证
麻省理工学院
