使用说明
带Auth0和Express的MCP服务器
一种使用以下组件构建的模型上下文协议(MCP)服务器 Express.js 和 Passport.js 通过Auth0对用户进行身份验证,并提供与Notes API交互的工具。
特性
- ✅ 我是谁:获取当前用户的Auth0配置文件和声明
- ✅ 列出注释:使用已验证用户的令牌从notes API获取注释
- ✅ Auth0 OAuth 2.0身份验证流程
- ✅ 使用Passport.js进行基于会话的身份验证
- ✅ MCP协议的SSE传输
- ✅ 平台无关(在Node.js工作的任何地方运行)
建筑
MCP Client → Express Server → Auth0 → Notes API
↓
Passport.js
↓
Session Storage
先决条件
- Node.js 18+和npm
- 已配置应用程序的Auth0帐户
- 注释API正在运行(默认值:http://localhost:3000)
快速设置
1.安装依赖项
npm install
2.配置Auth0
-
创建Auth0应用程序 (常规Web应用程序)
-
配置应用程序设置:
- 允许的回调URL:
http://localhost:3001/callback - 允许的注销URL:
http://localhost:3001 - 允许的Web来源:
http://localhost:3001
- 允许的回调URL:
-
创建Auth0 API 对于您的Notes API:
- 标识符:
https://notes-api(或您的自定义标识符) - 签名算法:RS256
- 权限:添加
read:notes范围
- 标识符:
3.配置环境
复制 .dev.vars.example 到 .env:
cp .dev.vars.example .env
编辑 .env 使用您的Auth0凭据:
AUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_CLIENT_ID=your_client_id_here
AUTH0_CLIENT_SECRET=your_client_secret_here
AUTH0_CALLBACK_URL=http://localhost:3001/callback
AUTH0_AUDIENCE=https://notes-api
SESSION_SECRET=generate-a-strong-random-secret
API_BASE_URL=http://localhost:3000
PORT=3001
NODE_ENV=development
生成会话_SECRET:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
4.启动服务器
发展 (自动重新加载):
npm run dev
生产:
npm run build
npm start
服务器将在以下时间启动: http://localhost:3001
用法
1.身份验证流程
- 访问
http://localhost:3001 - 点击“登录”以使用Auth0进行身份验证
- 成功登录后,您将被重定向回
2.MCP客户端连接
配置您的MCP客户端以连接到:
http://localhost:3001/mcp/sse
服务器将在允许MCP连接之前确保身份验证。
3.可用工具
whoami
返回当前用户的个人资料和Auth0声明:
{
"profile": {
"id": "auth0|...",
"displayName": "John Doe",
"emails": [{"value": "john@example.com"}]
},
"claims": {
"sub": "auth0|...",
"email": "john@example.com",
"aud": "...",
"iat": 1234567890
}
}
list-notes
使用用户的访问令牌从notes API获取注释:
[
{
"id": 1,
"title": "My Note",
"content": "Note content",
"userId": "auth0|..."
}
]
API终点
| 端点 | 方法 | 描述 |
|---|---|---|
/ |
GET | 主页(显示登录状态) |
/login |
GET | 启动Auth0登录 |
/callback |
GET | Auth0回调URL |
/logout |
GET | 注销用户并重定向到Auth0注销 |
/mcp/sse |
GET | MCP SSE传输(需要身份验证) |
/health |
GET | 健康检查端点 |
注释API要求
您的Notes API必须:
- 接受来自Auth0的JWT令牌
Authorization: Bearer <token>头球 - 根据您的Auth0租户验证令牌
- 端点位于
/api/notes返回用户特定的笔记
NestJS认证中间件示例
@Injectable()
export class Auth0JwtMiddleware implements NestMiddleware {
async use(req: Request, res: Response, next: NextFunction) {
const token = req.headers.authorization?.replace('Bearer ', '');
// Validate token with Auth0
const jwks = jose.createRemoteJWKSet(
new URL(`https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`)
);
const { payload } = await jose.jwtVerify(token, jwks, {
audience: process.env.AUTH0_AUDIENCE,
issuer: `https://${process.env.AUTH0_DOMAIN}/`,
});
req.user = payload;
next();
}
}
部署
本地开发
npm run dev
生产部署
选项1:Node.js服务器(铁路、Heroku、AWS等)
npm run build
npm start
选项2:Docker
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
CMD ["node", "dist/index.js"]
选项3:PM2
npm install -g pm2
npm run build
pm2 start dist/index.js --name mcp-notes
故障排除
“缺少必需的环境变量”
- 确保中的所有变量
.env已设置 - 检查一下
.env文件存在于项目根目录中
Notes API中的“无效压缩JWS”
- 验证
AUTH0_AUDIENCE与您的Auth0 API标识符匹配 - 确保Notes API正确验证令牌
- 检查是否在Authorization标头中发送访问令牌
MCP连接失败
- 确保您已通过身份验证(访问
/login第一) - 检查MCP客户端URL是否为
http://localhost:3001/mcp/sse - 验证服务器是否正在运行且可访问
会话问题
- 生成强大
SESSION_SECRET - 生产中,set
NODE_ENV=production用于安全Cookie - 在生产中使用Redis或数据库进行会话存储(当前在内存中)
项目结构
notes-mcp/
├── src/
│ ├── index.ts # Express app and MCP server setup
│ ├── auth.ts # Passport Auth0 configuration
│ └── types.ts # TypeScript type definitions
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── .env # Environment variables (create from .dev.vars.example)
└── README.md # This file
与Cloudflare版本的比较
| 功能 | Cloudflare Workers | Express.js |
|---|---|---|
| 部署 | 仅限Cloudflare | 任何平台 |
| 依赖关系 | 特定于Cloudflare | 标准Node.js |
| 会话存储 | 持久对象+KV | 内存或Redis中 |
| 设置复杂性 | 高 | 中 |
| 成本 | Cloudflare定价 | 服务器托管 |
许可证
麻省理工学院
