2025-08-13 09:45:06 -06:00
|
|
|
import { logger } from '@librechat/data-schemas';
|
|
|
|
|
import type { OAuthClientInformation } from '@modelcontextprotocol/sdk/shared/auth.js';
|
|
|
|
|
import type { TokenMethods } from '@librechat/data-schemas';
|
2025-09-11 00:53:34 +02:00
|
|
|
import type { MCPOAuthTokens, MCPOAuthFlowMetadata, OAuthMetadata } from '~/mcp/oauth';
|
2025-08-13 09:45:06 -06:00
|
|
|
import type { FlowStateManager } from '~/flow/manager';
|
|
|
|
|
import type { FlowMetadata } from '~/flow/types';
|
2025-08-13 14:41:38 -04:00
|
|
|
import type * as t from './types';
|
2025-08-13 09:45:06 -06:00
|
|
|
import { MCPTokenStorage, MCPOAuthHandler } from '~/mcp/oauth';
|
2025-09-14 18:55:32 -04:00
|
|
|
import { sanitizeUrlForLogging } from './utils';
|
2025-11-12 08:44:45 -05:00
|
|
|
import { withTimeout } from '~/utils/promise';
|
2025-08-13 09:45:06 -06:00
|
|
|
import { MCPConnection } from './connection';
|
|
|
|
|
import { processMCPEnv } from '~/utils';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Factory for creating MCP connections with optional OAuth authentication.
|
|
|
|
|
* Handles OAuth flows, token management, and connection retry logic.
|
|
|
|
|
* NOTE: Much of the OAuth logic was extracted from the old MCPManager class as is.
|
|
|
|
|
*/
|
|
|
|
|
export class MCPConnectionFactory {
|
|
|
|
|
protected readonly serverName: string;
|
|
|
|
|
protected readonly serverConfig: t.MCPOptions;
|
|
|
|
|
protected readonly logPrefix: string;
|
|
|
|
|
protected readonly useOAuth: boolean;
|
|
|
|
|
|
|
|
|
|
// OAuth-related properties (only set when useOAuth is true)
|
|
|
|
|
protected readonly userId?: string;
|
|
|
|
|
protected readonly flowManager?: FlowStateManager<MCPOAuthTokens | null>;
|
|
|
|
|
protected readonly tokenMethods?: TokenMethods;
|
|
|
|
|
protected readonly signal?: AbortSignal;
|
|
|
|
|
protected readonly oauthStart?: (authURL: string) => Promise<void>;
|
|
|
|
|
protected readonly oauthEnd?: () => Promise<void>;
|
|
|
|
|
protected readonly returnOnOAuth?: boolean;
|
2025-08-23 03:27:05 -04:00
|
|
|
protected readonly connectionTimeout?: number;
|
2025-08-13 09:45:06 -06:00
|
|
|
|
|
|
|
|
/** Creates a new MCP connection with optional OAuth support */
|
|
|
|
|
static async create(
|
2025-08-16 20:45:55 -04:00
|
|
|
basic: t.BasicConnectionOptions,
|
|
|
|
|
oauth?: t.OAuthConnectionOptions,
|
2025-08-13 09:45:06 -06:00
|
|
|
): Promise<MCPConnection> {
|
|
|
|
|
const factory = new this(basic, oauth);
|
|
|
|
|
return factory.createConnection();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-16 20:45:55 -04:00
|
|
|
protected constructor(basic: t.BasicConnectionOptions, oauth?: t.OAuthConnectionOptions) {
|
|
|
|
|
this.serverConfig = processMCPEnv({
|
|
|
|
|
options: basic.serverConfig,
|
|
|
|
|
user: oauth?.user,
|
|
|
|
|
customUserVars: oauth?.customUserVars,
|
|
|
|
|
body: oauth?.requestBody,
|
|
|
|
|
});
|
2025-08-13 09:45:06 -06:00
|
|
|
this.serverName = basic.serverName;
|
|
|
|
|
this.useOAuth = !!oauth?.useOAuth;
|
2025-08-23 03:27:05 -04:00
|
|
|
this.connectionTimeout = oauth?.connectionTimeout;
|
2025-08-13 09:45:06 -06:00
|
|
|
this.logPrefix = oauth?.user
|
|
|
|
|
? `[MCP][${basic.serverName}][${oauth.user.id}]`
|
|
|
|
|
: `[MCP][${basic.serverName}]`;
|
|
|
|
|
|
|
|
|
|
if (oauth?.useOAuth) {
|
|
|
|
|
this.userId = oauth.user.id;
|
|
|
|
|
this.flowManager = oauth.flowManager;
|
|
|
|
|
this.tokenMethods = oauth.tokenMethods;
|
|
|
|
|
this.signal = oauth.signal;
|
|
|
|
|
this.oauthStart = oauth.oauthStart;
|
|
|
|
|
this.oauthEnd = oauth.oauthEnd;
|
|
|
|
|
this.returnOnOAuth = oauth.returnOnOAuth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Creates the base MCP connection with OAuth tokens */
|
|
|
|
|
protected async createConnection(): Promise<MCPConnection> {
|
|
|
|
|
const oauthTokens = this.useOAuth ? await this.getOAuthTokens() : null;
|
|
|
|
|
const connection = new MCPConnection({
|
|
|
|
|
serverName: this.serverName,
|
|
|
|
|
serverConfig: this.serverConfig,
|
|
|
|
|
userId: this.userId,
|
|
|
|
|
oauthTokens,
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-11 18:54:43 -04:00
|
|
|
let cleanupOAuthHandlers: (() => void) | null = null;
|
|
|
|
|
if (this.useOAuth) {
|
|
|
|
|
cleanupOAuthHandlers = this.handleOAuthEvents(connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await this.attemptToConnect(connection);
|
|
|
|
|
if (cleanupOAuthHandlers) {
|
|
|
|
|
cleanupOAuthHandlers();
|
|
|
|
|
}
|
|
|
|
|
return connection;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (cleanupOAuthHandlers) {
|
|
|
|
|
cleanupOAuthHandlers();
|
|
|
|
|
}
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2025-08-13 09:45:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Retrieves existing OAuth tokens from storage or returns null */
|
|
|
|
|
protected async getOAuthTokens(): Promise<MCPOAuthTokens | null> {
|
|
|
|
|
if (!this.tokenMethods?.findToken) return null;
|
|
|
|
|
|
|
|
|
|
try {
|
2025-08-23 03:27:05 -04:00
|
|
|
const flowId = MCPOAuthHandler.generateFlowId(this.userId!, this.serverName);
|
2025-08-13 09:45:06 -06:00
|
|
|
const tokens = await this.flowManager!.createFlowWithHandler(
|
2025-08-23 03:27:05 -04:00
|
|
|
flowId,
|
2025-08-13 09:45:06 -06:00
|
|
|
'mcp_get_tokens',
|
|
|
|
|
async () => {
|
|
|
|
|
return await MCPTokenStorage.getTokens({
|
|
|
|
|
userId: this.userId!,
|
|
|
|
|
serverName: this.serverName,
|
|
|
|
|
findToken: this.tokenMethods!.findToken!,
|
|
|
|
|
createToken: this.tokenMethods!.createToken,
|
|
|
|
|
updateToken: this.tokenMethods!.updateToken,
|
|
|
|
|
refreshTokens: this.createRefreshTokensFunction(),
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
this.signal,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (tokens) logger.info(`${this.logPrefix} Loaded OAuth tokens`);
|
|
|
|
|
return tokens;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.debug(`${this.logPrefix} No existing tokens found or error loading tokens`, error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Creates a function to refresh OAuth tokens when they expire */
|
|
|
|
|
protected createRefreshTokensFunction(): (
|
|
|
|
|
refreshToken: string,
|
|
|
|
|
metadata: {
|
|
|
|
|
userId: string;
|
|
|
|
|
serverName: string;
|
|
|
|
|
identifier: string;
|
|
|
|
|
clientInfo?: OAuthClientInformation;
|
|
|
|
|
},
|
|
|
|
|
) => Promise<MCPOAuthTokens> {
|
|
|
|
|
return async (refreshToken, metadata) => {
|
|
|
|
|
return await MCPOAuthHandler.refreshOAuthTokens(
|
|
|
|
|
refreshToken,
|
|
|
|
|
{
|
|
|
|
|
serverUrl: (this.serverConfig as t.SSEOptions | t.StreamableHTTPOptions).url,
|
|
|
|
|
serverName: metadata.serverName,
|
|
|
|
|
clientInfo: metadata.clientInfo,
|
|
|
|
|
},
|
2025-10-11 17:17:12 +02:00
|
|
|
this.serverConfig.oauth_headers ?? {},
|
2025-08-13 09:45:06 -06:00
|
|
|
this.serverConfig.oauth,
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Sets up OAuth event handlers for the connection */
|
2025-09-11 18:54:43 -04:00
|
|
|
protected handleOAuthEvents(connection: MCPConnection): () => void {
|
|
|
|
|
const oauthHandler = async (data: { serverUrl?: string }) => {
|
2025-08-13 09:45:06 -06:00
|
|
|
logger.info(`${this.logPrefix} oauthRequired event received`);
|
|
|
|
|
|
|
|
|
|
// If we just want to initiate OAuth and return, handle it differently
|
|
|
|
|
if (this.returnOnOAuth) {
|
|
|
|
|
try {
|
|
|
|
|
const config = this.serverConfig;
|
|
|
|
|
const { authorizationUrl, flowId, flowMetadata } =
|
|
|
|
|
await MCPOAuthHandler.initiateOAuthFlow(
|
|
|
|
|
this.serverName,
|
|
|
|
|
data.serverUrl || '',
|
|
|
|
|
this.userId!,
|
2025-10-11 17:17:12 +02:00
|
|
|
config?.oauth_headers ?? {},
|
2025-08-13 09:45:06 -06:00
|
|
|
config?.oauth,
|
|
|
|
|
);
|
|
|
|
|
|
⚠️ fix: OAuth Error and Token Expiry Detection and Reporting Improvements (#10922)
* fix: create new flows on invalid_grant errors
* chore: fix failing test
* chore: keep isOAuthError test function in sync with implementation
* test: add tests for OAuth error detection on invalid grant errors
* test: add tests for creating new flows when token expires
* test: add test for flow clean up prior to creation
* refactor: consolidate token expiration handling in FlowStateManager
- Removed the old token expiration checks and replaced them with a new method, `isTokenExpired`, to streamline the logic.
- Introduced `normalizeExpirationTimestamp` to handle timestamp normalization for both seconds and milliseconds.
- Updated tests to ensure proper functionality of flow management with token expiration scenarios.
* fix: conditionally setup cleanup handlers in FlowStateManager
- Updated the FlowStateManager constructor to only call setupCleanupHandlers if the ci parameter is not set, improving flexibility in flow management.
* chore: enhance OAuth token refresh logging
- Introduced a new method, `processRefreshResponse`, to streamline the processing of token refresh responses from the OAuth server.
- Improved logging to provide detailed information about token refresh operations, including whether new tokens were received and if the refresh token was rotated.
- Updated existing token handling logic to utilize the new method, ensuring consistency and clarity in token management.
* chore: enhance logging for MCP server reinitialization
- Updated the logging in the reinitMCPServer function to provide more detailed information about the response, including success status, OAuth requirements, presence of the OAuth URL, and the count of tools involved. This improves the clarity and usefulness of logs for debugging purposes.
---------
Co-authored-by: Danny Avila <danny@librechat.ai>
2025-12-12 10:51:28 -08:00
|
|
|
// Delete any existing flow state to ensure we start fresh
|
|
|
|
|
// This prevents stale codeVerifier issues when re-authenticating
|
|
|
|
|
await this.flowManager!.deleteFlow(flowId, 'mcp_oauth');
|
|
|
|
|
|
2025-08-13 09:45:06 -06:00
|
|
|
// Create the flow state so the OAuth callback can find it
|
|
|
|
|
// We spawn this in the background without waiting for it
|
|
|
|
|
this.flowManager!.createFlow(flowId, 'mcp_oauth', flowMetadata).catch(() => {
|
|
|
|
|
// The OAuth callback will resolve this flow, so we expect it to timeout here
|
|
|
|
|
// which is fine - we just need the flow state to exist
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (this.oauthStart) {
|
|
|
|
|
logger.info(`${this.logPrefix} OAuth flow started, issuing authorization URL`);
|
|
|
|
|
await this.oauthStart(authorizationUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Emit oauthFailed to signal that connection should not proceed
|
|
|
|
|
// but OAuth was successfully initiated
|
|
|
|
|
connection.emit('oauthFailed', new Error('OAuth flow initiated - return early'));
|
|
|
|
|
return;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(`${this.logPrefix} Failed to initiate OAuth flow`, error);
|
|
|
|
|
connection.emit('oauthFailed', new Error('OAuth initiation failed'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normal OAuth handling - wait for completion
|
|
|
|
|
const result = await this.handleOAuthRequired();
|
|
|
|
|
|
|
|
|
|
if (result?.tokens && this.tokenMethods?.createToken) {
|
|
|
|
|
try {
|
|
|
|
|
connection.setOAuthTokens(result.tokens);
|
|
|
|
|
await MCPTokenStorage.storeTokens({
|
|
|
|
|
userId: this.userId!,
|
|
|
|
|
serverName: this.serverName,
|
|
|
|
|
tokens: result.tokens,
|
|
|
|
|
createToken: this.tokenMethods.createToken,
|
|
|
|
|
updateToken: this.tokenMethods.updateToken,
|
|
|
|
|
findToken: this.tokenMethods.findToken,
|
|
|
|
|
clientInfo: result.clientInfo,
|
2025-09-11 00:53:34 +02:00
|
|
|
metadata: result.metadata,
|
2025-08-13 09:45:06 -06:00
|
|
|
});
|
|
|
|
|
logger.info(`${this.logPrefix} OAuth tokens saved to storage`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(`${this.logPrefix} Failed to save OAuth tokens to storage`, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only emit oauthHandled if we actually got tokens (OAuth succeeded)
|
|
|
|
|
if (result?.tokens) {
|
|
|
|
|
connection.emit('oauthHandled');
|
|
|
|
|
} else {
|
|
|
|
|
// OAuth failed, emit oauthFailed to properly reject the promise
|
|
|
|
|
logger.warn(`${this.logPrefix} OAuth failed, emitting oauthFailed event`);
|
|
|
|
|
connection.emit('oauthFailed', new Error('OAuth authentication failed'));
|
|
|
|
|
}
|
2025-09-11 18:54:43 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connection.on('oauthRequired', oauthHandler);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
connection.removeListener('oauthRequired', oauthHandler);
|
|
|
|
|
};
|
2025-08-13 09:45:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Attempts to establish connection with timeout handling */
|
|
|
|
|
protected async attemptToConnect(connection: MCPConnection): Promise<void> {
|
2025-08-23 03:27:05 -04:00
|
|
|
const connectTimeout = this.connectionTimeout ?? this.serverConfig.initTimeout ?? 30000;
|
2025-10-31 13:00:21 -06:00
|
|
|
await withTimeout(
|
|
|
|
|
this.connectTo(connection),
|
|
|
|
|
connectTimeout,
|
|
|
|
|
`Connection timeout after ${connectTimeout}ms`,
|
2025-08-13 09:45:06 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (await connection.isConnected()) return;
|
|
|
|
|
logger.error(`${this.logPrefix} Failed to establish connection.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handles connection attempts with retry logic and OAuth error handling
|
|
|
|
|
private async connectTo(connection: MCPConnection): Promise<void> {
|
|
|
|
|
const maxAttempts = 3;
|
|
|
|
|
let attempts = 0;
|
|
|
|
|
let oauthHandled = false;
|
|
|
|
|
|
|
|
|
|
while (attempts < maxAttempts) {
|
|
|
|
|
try {
|
|
|
|
|
await connection.connect();
|
|
|
|
|
if (await connection.isConnected()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
throw new Error('Connection attempt succeeded but status is not connected');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
attempts++;
|
|
|
|
|
|
|
|
|
|
if (this.useOAuth && this.isOAuthError(error)) {
|
2025-12-04 19:52:32 -05:00
|
|
|
// For returnOnOAuth mode, let the event handler (handleOAuthEvents) deal with OAuth
|
|
|
|
|
// We just need to stop retrying and let the error propagate
|
|
|
|
|
if (this.returnOnOAuth) {
|
|
|
|
|
logger.info(
|
|
|
|
|
`${this.logPrefix} OAuth required (return on OAuth mode), stopping retries`,
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normal flow - wait for OAuth to complete
|
2025-08-13 09:45:06 -06:00
|
|
|
if (this.oauthStart && !oauthHandled) {
|
2025-12-04 19:52:32 -05:00
|
|
|
oauthHandled = true;
|
|
|
|
|
logger.info(`${this.logPrefix} Handling OAuth`);
|
|
|
|
|
await this.handleOAuthRequired();
|
2025-08-13 09:45:06 -06:00
|
|
|
}
|
|
|
|
|
// Don't retry on OAuth errors - just throw
|
|
|
|
|
logger.info(`${this.logPrefix} OAuth required, stopping connection attempts`);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (attempts === maxAttempts) {
|
|
|
|
|
logger.error(`${this.logPrefix} Failed to connect after ${maxAttempts} attempts`, error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 2000 * attempts));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determines if an error indicates OAuth authentication is required
|
|
|
|
|
private isOAuthError(error: unknown): boolean {
|
|
|
|
|
if (!error || typeof error !== 'object') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for error code
|
|
|
|
|
if ('code' in error) {
|
|
|
|
|
const code = (error as { code?: number }).code;
|
2025-12-04 19:52:32 -05:00
|
|
|
if (code === 401 || code === 403) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check message for various auth error indicators
|
|
|
|
|
if ('message' in error && typeof error.message === 'string') {
|
|
|
|
|
const message = error.message.toLowerCase();
|
|
|
|
|
// Check for 401 status
|
|
|
|
|
if (message.includes('401') || message.includes('non-200 status code (401)')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// Check for invalid_token (OAuth servers return this for expired/revoked tokens)
|
|
|
|
|
if (message.includes('invalid_token')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// Check for authentication required
|
|
|
|
|
if (message.includes('authentication required') || message.includes('unauthorized')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-08-13 09:45:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Manages OAuth flow initiation and completion */
|
|
|
|
|
protected async handleOAuthRequired(): Promise<{
|
|
|
|
|
tokens: MCPOAuthTokens | null;
|
|
|
|
|
clientInfo?: OAuthClientInformation;
|
2025-09-11 00:53:34 +02:00
|
|
|
metadata?: OAuthMetadata;
|
2025-08-13 09:45:06 -06:00
|
|
|
} | null> {
|
|
|
|
|
const serverUrl = (this.serverConfig as t.SSEOptions | t.StreamableHTTPOptions).url;
|
2025-09-14 18:55:32 -04:00
|
|
|
logger.debug(
|
|
|
|
|
`${this.logPrefix} \`handleOAuthRequired\` called with serverUrl: ${serverUrl ? sanitizeUrlForLogging(serverUrl) : 'undefined'}`,
|
|
|
|
|
);
|
2025-08-13 09:45:06 -06:00
|
|
|
|
|
|
|
|
if (!this.flowManager || !serverUrl) {
|
|
|
|
|
logger.error(
|
|
|
|
|
`${this.logPrefix} OAuth required but flow manager not available or server URL missing for ${this.serverName}`,
|
|
|
|
|
);
|
|
|
|
|
logger.warn(`${this.logPrefix} Please configure OAuth credentials for ${this.serverName}`);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
logger.debug(`${this.logPrefix} Checking for existing OAuth flow for ${this.serverName}...`);
|
|
|
|
|
|
|
|
|
|
/** Flow ID to check if a flow already exists */
|
|
|
|
|
const flowId = MCPOAuthHandler.generateFlowId(this.userId!, this.serverName);
|
|
|
|
|
|
|
|
|
|
/** Check if there's already an ongoing OAuth flow for this flowId */
|
|
|
|
|
const existingFlow = await this.flowManager.getFlowState(flowId, 'mcp_oauth');
|
2025-11-11 13:51:20 +13:00
|
|
|
|
2025-08-13 09:45:06 -06:00
|
|
|
if (existingFlow && existingFlow.status === 'PENDING') {
|
|
|
|
|
logger.debug(
|
|
|
|
|
`${this.logPrefix} OAuth flow already exists for ${flowId}, waiting for completion`,
|
|
|
|
|
);
|
|
|
|
|
/** Tokens from existing flow to complete */
|
|
|
|
|
const tokens = await this.flowManager.createFlow(flowId, 'mcp_oauth');
|
|
|
|
|
if (typeof this.oauthEnd === 'function') {
|
|
|
|
|
await this.oauthEnd();
|
|
|
|
|
}
|
|
|
|
|
logger.info(
|
|
|
|
|
`${this.logPrefix} OAuth flow completed, tokens received for ${this.serverName}`,
|
|
|
|
|
);
|
|
|
|
|
|
2025-11-12 08:44:45 -05:00
|
|
|
/** Client information from the existing flow metadata */
|
2025-08-13 09:45:06 -06:00
|
|
|
const existingMetadata = existingFlow.metadata as unknown as MCPOAuthFlowMetadata;
|
2025-11-12 08:44:45 -05:00
|
|
|
const clientInfo = existingMetadata?.clientInfo;
|
2025-08-13 09:45:06 -06:00
|
|
|
|
|
|
|
|
return { tokens, clientInfo };
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-12 08:44:45 -05:00
|
|
|
// Clean up old completed/failed flows, but only if they're actually stale
|
|
|
|
|
// This prevents race conditions where we delete a flow that's still being processed
|
2025-11-11 13:51:20 +13:00
|
|
|
if (existingFlow && existingFlow.status !== 'PENDING') {
|
2025-11-12 08:44:45 -05:00
|
|
|
const STALE_FLOW_THRESHOLD = 2 * 60 * 1000; // 2 minutes
|
|
|
|
|
const { isStale, age, status } = await this.flowManager.isFlowStale(
|
|
|
|
|
flowId,
|
|
|
|
|
'mcp_oauth',
|
|
|
|
|
STALE_FLOW_THRESHOLD,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (isStale) {
|
|
|
|
|
try {
|
|
|
|
|
await this.flowManager.deleteFlow(flowId, 'mcp_oauth');
|
|
|
|
|
logger.debug(
|
|
|
|
|
`${this.logPrefix} Cleared stale ${status} OAuth flow (age: ${Math.round(age / 1000)}s)`,
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.warn(`${this.logPrefix} Failed to clear stale OAuth flow`, error);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-11-11 13:51:20 +13:00
|
|
|
logger.debug(
|
2025-11-12 08:44:45 -05:00
|
|
|
`${this.logPrefix} Skipping cleanup of recent ${status} flow (age: ${Math.round(age / 1000)}s, threshold: ${STALE_FLOW_THRESHOLD / 1000}s)`,
|
2025-11-11 13:51:20 +13:00
|
|
|
);
|
2025-11-12 08:44:45 -05:00
|
|
|
// If flow is recent but not pending, something might be wrong
|
|
|
|
|
if (status === 'FAILED') {
|
|
|
|
|
logger.warn(
|
|
|
|
|
`${this.logPrefix} Recent OAuth flow failed, will retry after ${Math.round((STALE_FLOW_THRESHOLD - age) / 1000)}s`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-11-11 13:51:20 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 09:45:06 -06:00
|
|
|
logger.debug(`${this.logPrefix} Initiating new OAuth flow for ${this.serverName}...`);
|
|
|
|
|
const {
|
|
|
|
|
authorizationUrl,
|
|
|
|
|
flowId: newFlowId,
|
|
|
|
|
flowMetadata,
|
|
|
|
|
} = await MCPOAuthHandler.initiateOAuthFlow(
|
|
|
|
|
this.serverName,
|
|
|
|
|
serverUrl,
|
|
|
|
|
this.userId!,
|
2025-10-11 17:17:12 +02:00
|
|
|
this.serverConfig.oauth_headers ?? {},
|
2025-08-13 09:45:06 -06:00
|
|
|
this.serverConfig.oauth,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (typeof this.oauthStart === 'function') {
|
|
|
|
|
logger.info(`${this.logPrefix} OAuth flow started, issued authorization URL to user`);
|
|
|
|
|
await this.oauthStart(authorizationUrl);
|
|
|
|
|
} else {
|
2025-08-13 14:41:38 -04:00
|
|
|
logger.info(
|
|
|
|
|
`${this.logPrefix} OAuth flow started, no \`oauthStart\` handler defined, relying on callback endpoint`,
|
|
|
|
|
);
|
2025-08-13 09:45:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Tokens from the new flow */
|
|
|
|
|
const tokens = await this.flowManager.createFlow(
|
|
|
|
|
newFlowId,
|
|
|
|
|
'mcp_oauth',
|
|
|
|
|
flowMetadata as FlowMetadata,
|
2025-08-23 03:27:05 -04:00
|
|
|
this.signal,
|
2025-08-13 09:45:06 -06:00
|
|
|
);
|
|
|
|
|
if (typeof this.oauthEnd === 'function') {
|
|
|
|
|
await this.oauthEnd();
|
|
|
|
|
}
|
|
|
|
|
logger.info(`${this.logPrefix} OAuth flow completed, tokens received for ${this.serverName}`);
|
|
|
|
|
|
2025-11-12 08:44:45 -05:00
|
|
|
/** Client information from the flow metadata */
|
|
|
|
|
const clientInfo = flowMetadata?.clientInfo;
|
|
|
|
|
const metadata = flowMetadata?.metadata;
|
2025-08-13 09:45:06 -06:00
|
|
|
|
2025-09-11 00:53:34 +02:00
|
|
|
return {
|
|
|
|
|
tokens,
|
|
|
|
|
clientInfo,
|
|
|
|
|
metadata,
|
|
|
|
|
};
|
2025-08-13 09:45:06 -06:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(`${this.logPrefix} Failed to complete OAuth flow for ${this.serverName}`, error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|