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:
@@ -10,6 +10,10 @@ export interface MCPService {
|
||||
url?: string // Optional: required for SSE/HTTP Streamable
|
||||
headers?: Record<string, string>
|
||||
auth_config?: {
|
||||
// Authentication strategy. Empty/absent means none. "oauth" enables the
|
||||
// per-user OAuth2 authorization-code flow (zero-config: discovery +
|
||||
// dynamic client registration).
|
||||
auth_type?: '' | 'api_key' | 'bearer' | 'oauth'
|
||||
// Secret fields (api_key, token) are NEVER returned by the server in
|
||||
// this shape — they live behind the /credentials subresource. The
|
||||
// optional-property typing remains so create-mode payloads can still
|
||||
@@ -17,6 +21,9 @@ export interface MCPService {
|
||||
api_key?: string
|
||||
token?: string
|
||||
custom_headers?: Record<string, string>
|
||||
// OAuth-only, non-secret configuration.
|
||||
scopes?: string[]
|
||||
auth_server_metadata_url?: string
|
||||
}
|
||||
advanced_config?: {
|
||||
timeout?: number
|
||||
@@ -166,6 +173,39 @@ export async function deleteMCPCredentialField(
|
||||
await del(`/api/v1/mcp-services/${serviceId}/credentials/${field}`)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Per-user OAuth2 authorization-code flow.
|
||||
//
|
||||
// The user authorizes a service once; the backend stores their access/refresh
|
||||
// token (per tenant + user + service) and refreshes it transparently. The
|
||||
// callback is a public backend route that the third-party authorization
|
||||
// server redirects to.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Path of the public backend OAuth callback (registered outside /mcp-services
|
||||
// to avoid a route conflict, and allow-listed for no-auth in the backend).
|
||||
export const MCP_OAUTH_CALLBACK_PATH = '/api/v1/mcp-oauth/callback'
|
||||
|
||||
// Begin authorization for the current user. Returns the URL to open in a popup.
|
||||
export async function getMCPOAuthAuthorizeURL(
|
||||
serviceId: string,
|
||||
body: { redirect_uri: string; frontend_redirect?: string }
|
||||
): Promise<string> {
|
||||
const response: any = await post(`/api/v1/mcp-services/${serviceId}/oauth/authorize-url`, body)
|
||||
return (response.data ?? response)?.authorization_url ?? ''
|
||||
}
|
||||
|
||||
// Whether the current user has authorized this service.
|
||||
export async function getMCPOAuthStatus(serviceId: string): Promise<boolean> {
|
||||
const response: any = await get(`/api/v1/mcp-services/${serviceId}/oauth/status`)
|
||||
return Boolean((response.data ?? response)?.authorized)
|
||||
}
|
||||
|
||||
// Revoke the current user's token (forces re-authorization).
|
||||
export async function revokeMCPOAuthToken(serviceId: string): Promise<void> {
|
||||
await del(`/api/v1/mcp-services/${serviceId}/oauth/token`)
|
||||
}
|
||||
|
||||
export async function resolveToolApproval(
|
||||
pendingId: string,
|
||||
body: { decision: 'approve' | 'reject'; modified_args?: Record<string, unknown>; reason?: string }
|
||||
|
||||
Reference in New Issue
Block a user