feat(mcp): add OAuth2 authorization for MCP services

Support connecting to MCP servers that require OAuth2, with a Cursor-like
zero-config flow: the user provides only a URL and the system performs
metadata discovery, dynamic client registration (DCR), and the
authorization code flow with PKCE on first connect.

- Persist per-user access/refresh tokens and per-service DCR client
  credentials, encrypted at rest (AES-256-GCM)
- Store the PKCE verifier/state during the flow via Redis with an
  in-memory fallback for Lite mode
- Cache MCP clients per (service, user) so tokens stay user-isolated
- Add endpoints for authorize-url, callback, status, and token revoke;
  the callback is allowlisted for unauthenticated access
- Surface a structured "authorization required" hint when a tool call
  hits an unauthorized MCP service
- Frontend: add an auth-type selector with OAuth config, inline
  authorization status/actions, and render the connection test result
  inline in the editor drawer instead of a stacked centered dialog
This commit is contained in:
wizardchen
2026-06-22 20:15:52 +08:00
committed by lyingbug
parent c25d762ed1
commit 7f3f219f58
27 changed files with 1920 additions and 686 deletions
+18
View File
@@ -4343,6 +4343,7 @@ export default {
connectionSection: "连接配置",
enableServiceDesc: "关闭后该服务不会被调用",
testAfterSaveHint: "保存后可测试连接",
testResultTitle: "测试结果",
unitSecond: "秒",
unitTimes: "次",
name: "服务名称",
@@ -4367,6 +4368,19 @@ export default {
addEnvVar: "添加环境变量",
enableService: "启用服务",
authConfig: "认证配置",
authType: "认证方式",
authTypeNone: "无 / 自定义 Header",
authTypeApiKey: "API Key",
authTypeBearer: "Bearer Token",
authTypeOAuth: "OAuth 2.0(首次连接授权)",
oauthScopes: "Scopes(可选,空格分隔)",
oauthAuthorization: "授权状态",
oauthAuthorized: "已授权",
oauthUnauthorized: "未授权",
oauthAuthorize: "去授权",
oauthReauthorize: "重新授权",
oauthRevoke: "撤销授权",
oauthSaveFirstHint: "保存服务后,可在编辑页发起首次授权(每个用户独立授权)。",
apiKey: "API Key",
bearerToken: "Bearer Token",
optional: "可选",
@@ -4387,6 +4401,10 @@ export default {
updated: "MCP 服务已更新",
createFailed: "创建 MCP 服务失败",
updateFailed: "更新 MCP 服务失败",
authorized: "授权成功",
authorizeFailed: "发起授权失败",
revoked: "已撤销授权",
revokeFailed: "撤销失败",
},
},
promptTemplate: {