MCP Gateway Demo
A reference implementation of an MCP server built with Express that integrates full OAuth 2.1 authorization and RFC9728 protected resource metadata. It enables secure, authenticated communication between MCP clients and servers using streamable HTTP transport and built-in authorization flows.
README
MCP Gateway Demo
Express 实现的 MCP Server,带完整 OAuth 2.1 授权(符合 MCP Authorization 与 RFC9728 Protected Resource Metadata)。
功能
- MCP Server(资源服务器)
- Streamable HTTP 传输:
POST /mcp、GET /mcp(SSE) - 未认证请求返回
401,并带WWW-Authenticate: Bearer resource_metadata=..., scope=... - JWT 校验(audience = MCP 资源 URI),不足 scope 时返回
403 insufficient_scope
- Streamable HTTP 传输:
- Protected Resource Metadata(RFC9728)
GET /.well-known/oauth-protected-resource返回resource、authorization_servers、scopes_supported- 可选:
GET /.well-known/oauth-protected-resource/mcp
- 内置 Authorization Server(Demo)
GET /.well-known/oauth-authorization-server(RFC8414)GET /.well-known/openid-configuration(含code_challenge_methods_supported)GET /oauth/authorize(授权码 + PKCE S256)POST /oauth/token(换发 JWT,aud= 请求中的resource)
环境变量
| 变量 | 说明 | 示例 |
|---|---|---|
PORT |
HTTP 端口 | 3000 |
MCP_RESOURCE_URI |
MCP 资源 canonical URI(用于 aud 校验与 metadata) | http://localhost:3000 |
AUTH_ISSUER |
授权服务器 issuer(可与 MCP 同源) | http://localhost:3000 |
JWT_SECRET |
HS256 密钥(生产建议 RS256 + JWKS) | 至少 32 字符 |
复制 .env.example 为 .env 并修改。
启动
npm install
npm start
开发时:
npm run dev
联调(MCP 客户端,如 Cursor)
-
发现
客户端先请求 MCP 端点(无 token),收到401后从WWW-Authenticate取resource_metadata,再 GET 该 URL 得到authorization_servers、scopes_supported。 -
授权
客户端用authorization_servers中任一项作为 issuer,请求其/.well-known/oauth-authorization-server,得到authorization_endpoint、token_endpoint、code_challenge_methods_supported。
使用授权码 + PKCE(S256),并在授权与 token 请求中带上resource= MCP 的 canonical URI(如http://localhost:3000)。 -
访问 MCP
请求POST/GET /mcp时在 Header 中带Authorization: Bearer <access_token>,且Accept: application/json, text/event-stream。
本地测试时,可将 MCP 服务器 URL 设为 http://localhost:3000,授权服务器与资源服务器均为同源,使用上述发现与授权流程即可。
手动测试(curl)
# 1. 未认证 → 401
curl -i -X POST http://localhost:3000/mcp -H "Content-Type: application/json" -d '{}'
# 2. Protected Resource Metadata
curl -s http://localhost:3000/.well-known/oauth-protected-resource
# 3. Authorization Server 发现
curl -s http://localhost:3000/.well-known/oauth-authorization-server
# 4. 授权(浏览器或带 approve=yes 的 GET)
# 生成 PKCE:node -e "const c=require('crypto'); const v=c.randomBytes(32).toString('base64url'); console.log('verifier:', v, 'challenge:', c.createHash('sha256').update(v).digest('base64url'));"
# 在浏览器打开 /oauth/authorize?response_type=code&client_id=test&redirect_uri=http://localhost:3000/callback&scope=mcp:read%20mcp:write&state=1&code_challenge=<CHALLENGE>&code_challenge_method=S256&resource=http://localhost:3000
# 同意后从 redirect 的 URL 中取出 code
# 5. 换 Token
curl -s -X POST http://localhost:3000/oauth/token -H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code&code=<CODE>&redirect_uri=http://localhost:3000/callback&client_id=test&code_verifier=<VERIFIER>&resource=http://localhost:3000"
# 6. 带 Token 调用 MCP
curl -s -X POST http://localhost:3000/mcp -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}'
安全说明
- 生产环境请使用 HTTPS。
JWT_SECRET需足够强,或改用 RS256 + JWKS。- 本 Demo 允许的
redirect_uri包含 localhost;生产应严格限制为已注册的 redirect_uri。
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。