SSE MCP Server with JWT Authentication

by anisirji
Verified

Integrations

  • Manages environment variables for server configuration, including JWT secrets.

  • Serves as the web framework for the MCP server, handling HTTP requests and SSE connections for AI tool communication.

  • Used for version control and project setup via cloning.

🔐 带有 JWT 身份验证的 SSE MCP 服务器

这是一个基于 JWT 身份验证的模型上下文协议 (MCP) SSE 服务器。
它允许您通过 SSE 传输公开多个 AI 工具,并通过安全的 Bearer Token 流进行保护。

内置:

  • 🚀 Node.js + Express
  • 🧩@modelcontextprotocol/sdk
  • 🔒 JSON Web Tokens(JWT)用于身份验证
  • ⚙️ Zod 用于输入验证

✅ 已通过@modelcontextprotocol/inspector全面测试

📂 项目结构

server/ ├── index.ts # Main Express + MCP server ├── .env # Environment variables ├── package.json # Project metadata & scripts ├── tsconfig.json # TypeScript config └── README.md # You are here!

✨ 特点

  • ✅ 使用 Bearer JWT 令牌保护 SSE 连接
  • ✅ 动态工具注册(回声、时间、随机数等)
  • ✅ 经过 MCP Inspector 测试
  • ✅ 记录所有请求生命周期事件
  • ✅ /message 端点的会话管理
  • 🚀 准备扩展用于生产用途

⚙️ 设置

1. 克隆存储库

git clone https://github.com/anisirji/mcp-server-remote-setup-with-jwt-auth.git cd mcp-server-remote-setup-with-jwt-auth

2.安装依赖项

npm install

3.创建.env文件

echo "JWT_SECRET=your-secret-key" > .env

4.运行服务器

npm run dev

✅ 服务器将运行于:

http://localhost:3001/sse

🧪 使用 MCP Inspector 测试服务器

步骤 1 — 安装 MCP Inspector

📖 官方文档: MCP Inspector

npx @modelcontextprotocol/inspector

步骤 2 — 生成令牌

使用 cURL 获取您的 JWT 令牌:

curl "http://localhost:3001/auth/token?username=aniket&scope=mcp:access"

✅ 示例响应:

{ "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." }

步骤 3 — 连接 MCP Inspector

  1. 打开检查器用户界面
  2. 设置传输类型: SSE
  3. 网址:
    http://localhost:3001/sse
  4. 添加授权标头:
    Authorization: Bearer <your-token>
  5. 单击**“连接”**

🎉 成功!您的服务器现已连接。

步骤 4 — 测试工具

转到检查器中的**“工具”选项卡,然后单击“列出工具”**

您将看到:

  • test
  • echo
  • get-time
  • random-number

测试它们并享受吧!

📖 API 参考

🔑 生成令牌

GET /auth/token?username=<username>&scope=mcp:access

🔌 SSE 端点(需要令牌)

GET /sse Authorization: Bearer <token>

📩 向活动会话发送消息

POST /message?sessionId=<sessionId> Authorization: Bearer <token>

🧩 工具参考

工具名称描述
test测试连接(安全检查)
echo回显提供的消息
get-time返回当前服务器时间
random-number返回随机数(最小/最大)

🗓️ 即将发生的变化

  • [ ] 令牌撤销列表(黑名单)
  • [ ] 基于角色的工具访问(范围检查)
  • [ ] 会话心跳/保持活动
  • [ ] 速率限制和日志记录
  • [ ] Docker化部署

📚 有用的资源

👨‍💻 维护者

阿尼克特

📄 许可证

该项目是开源的,可以免费使用。

🚀 构建。 安全。 赋能。

ID: miwcfrgnmg