@@ -93,7 +93,6 @@ router.get('/:serverName/oauth/callback', async (req, res) => {
9393 return res . redirect ( '/oauth/error?error=missing_state' ) ;
9494 }
9595
96- // Extract flow ID from state
9796 const flowId = state ;
9897 logger . debug ( '[MCP OAuth] Using flow ID from state' , { flowId } ) ;
9998
@@ -116,22 +115,17 @@ router.get('/:serverName/oauth/callback', async (req, res) => {
116115 hasCodeVerifier : ! ! flowState . codeVerifier ,
117116 } ) ;
118117
119- // Complete the OAuth flow
120118 logger . debug ( '[MCP OAuth] Completing OAuth flow' ) ;
121119 const tokens = await MCPOAuthHandler . completeOAuthFlow ( flowId , code , flowManager ) ;
122120 logger . info ( '[MCP OAuth] OAuth flow completed, tokens received in callback route' ) ;
123121
124- // Try to establish the MCP connection with the new tokens
125122 try {
126123 const mcpManager = getMCPManager ( flowState . userId ) ;
127124 logger . debug ( `[MCP OAuth] Attempting to reconnect ${ serverName } with new OAuth tokens` ) ;
128125
129- // For user-level OAuth, try to establish the connection
130126 if ( flowState . userId !== 'system' ) {
131- // We need to get the user object - in this case we'll need to reconstruct it
132127 const user = { id : flowState . userId } ;
133128
134- // Try to establish connection with the new tokens
135129 const userConnection = await mcpManager . getUserConnection ( {
136130 user,
137131 serverName,
@@ -148,18 +142,15 @@ router.get('/:serverName/oauth/callback', async (req, res) => {
148142 `[MCP OAuth] Successfully reconnected ${ serverName } for user ${ flowState . userId } ` ,
149143 ) ;
150144
151- // Fetch and cache tools now that we have a successful connection
152145 const userTools = ( await getCachedTools ( { userId : flowState . userId } ) ) || { } ;
153146
154- // Remove any old tools from this server in the user's cache
155147 const mcpDelimiter = Constants . mcp_delimiter ;
156148 for ( const key of Object . keys ( userTools ) ) {
157149 if ( key . endsWith ( `${ mcpDelimiter } ${ serverName } ` ) ) {
158150 delete userTools [ key ] ;
159151 }
160152 }
161153
162- // Add the new tools from this server
163154 const tools = await userConnection . fetchTools ( ) ;
164155 for ( const tool of tools ) {
165156 const name = `${ tool . name } ${ Constants . mcp_delimiter } ${ serverName } ` ;
@@ -173,7 +164,6 @@ router.get('/:serverName/oauth/callback', async (req, res) => {
173164 } ;
174165 }
175166
176- // Save the updated user tool cache
177167 await setCachedTools ( userTools , { userId : flowState . userId } ) ;
178168
179169 logger . debug (
@@ -183,7 +173,6 @@ router.get('/:serverName/oauth/callback', async (req, res) => {
183173 logger . debug ( `[MCP OAuth] System-level OAuth completed for ${ serverName } ` ) ;
184174 }
185175 } catch ( error ) {
186- // Don't fail the OAuth callback if reconnection fails - the tokens are still saved
187176 logger . warn (
188177 `[MCP OAuth] Failed to reconnect ${ serverName } after OAuth, but tokens are saved:` ,
189178 error ,
@@ -219,7 +208,6 @@ router.get('/oauth/tokens/:flowId', requireJwtAuth, async (req, res) => {
219208 return res . status ( 401 ) . json ( { error : 'User not authenticated' } ) ;
220209 }
221210
222- // Allow system flows or user-owned flows
223211 if ( ! flowId . startsWith ( `${ user . id } :` ) && ! flowId . startsWith ( 'system:' ) ) {
224212 return res . status ( 403 ) . json ( { error : 'Access denied' } ) ;
225213 }
@@ -287,11 +275,7 @@ router.post('/oauth/cancel/:serverName', requireJwtAuth, async (req, res) => {
287275
288276 const flowsCache = getLogStores ( CacheKeys . FLOWS ) ;
289277 const flowManager = getFlowStateManager ( flowsCache ) ;
290-
291- // Generate the flow ID for this user/server combination
292278 const flowId = MCPOAuthHandler . generateFlowId ( user . id , serverName ) ;
293-
294- // Check if flow exists
295279 const flowState = await flowManager . getFlowState ( flowId , 'mcp_oauth' ) ;
296280
297281 if ( ! flowState ) {
@@ -302,7 +286,6 @@ router.post('/oauth/cancel/:serverName', requireJwtAuth, async (req, res) => {
302286 } ) ;
303287 }
304288
305- // Cancel the flow by marking it as failed
306289 await flowManager . failFlow ( flowId , 'mcp_oauth' , 'User cancelled OAuth flow' ) ;
307290
308291 logger . info ( `[MCP OAuth Cancel] Successfully cancelled OAuth flow for ${ serverName } ` ) ;
@@ -379,8 +362,7 @@ router.post('/:serverName/reinitialize', requireJwtAuth, async (req, res) => {
379362 createToken,
380363 deleteTokens,
381364 } ,
382- returnOnOAuth : true , // Return immediately when OAuth is initiated
383- // Add OAuth handlers to capture the OAuth URL when needed
365+ returnOnOAuth : true ,
384366 oauthStart : async ( authURL ) => {
385367 logger . info ( `[MCP Reinitialize] OAuth URL received: ${ authURL } ` ) ;
386368 oauthUrl = authURL ;
@@ -395,7 +377,6 @@ router.post('/:serverName/reinitialize', requireJwtAuth, async (req, res) => {
395377 `[MCP Reinitialize] OAuth state - oauthRequired: ${ oauthRequired } , oauthUrl: ${ oauthUrl ? 'present' : 'null' } ` ,
396378 ) ;
397379
398- // Check if this is an OAuth error - if so, the flow state should be set up now
399380 const isOAuthError =
400381 err . message ?. includes ( 'OAuth' ) ||
401382 err . message ?. includes ( 'authentication' ) ||
@@ -408,7 +389,6 @@ router.post('/:serverName/reinitialize', requireJwtAuth, async (req, res) => {
408389 `[MCP Reinitialize] OAuth required for ${ serverName } (isOAuthError: ${ isOAuthError } , oauthRequired: ${ oauthRequired } , isOAuthFlowInitiated: ${ isOAuthFlowInitiated } )` ,
409390 ) ;
410391 oauthRequired = true ;
411- // Don't return error - continue so frontend can handle OAuth
412392 } else {
413393 logger . error (
414394 `[MCP Reinitialize] Error initializing MCP server ${ serverName } for user:` ,
@@ -418,19 +398,16 @@ router.post('/:serverName/reinitialize', requireJwtAuth, async (req, res) => {
418398 }
419399 }
420400
421- // Only fetch and cache tools if we successfully connected (no OAuth required)
422401 if ( userConnection && ! oauthRequired ) {
423402 const userTools = ( await getCachedTools ( { userId : user . id } ) ) || { } ;
424403
425- // Remove any old tools from this server in the user's cache
426404 const mcpDelimiter = Constants . mcp_delimiter ;
427405 for ( const key of Object . keys ( userTools ) ) {
428406 if ( key . endsWith ( `${ mcpDelimiter } ${ serverName } ` ) ) {
429407 delete userTools [ key ] ;
430408 }
431409 }
432410
433- // Add the new tools from this server
434411 const tools = await userConnection . fetchTools ( ) ;
435412 for ( const tool of tools ) {
436413 const name = `${ tool . name } ${ Constants . mcp_delimiter } ${ serverName } ` ;
@@ -444,7 +421,6 @@ router.post('/:serverName/reinitialize', requireJwtAuth, async (req, res) => {
444421 } ;
445422 }
446423
447- // Save the updated user tool cache
448424 await setCachedTools ( userTools , { userId : user . id } ) ;
449425 }
450426
@@ -593,19 +569,16 @@ router.get('/:serverName/auth-values', requireJwtAuth, async (req, res) => {
593569 const pluginKey = `${ Constants . mcp_prefix } ${ serverName } ` ;
594570 const authValueFlags = { } ;
595571
596- // Check existence of saved values for each custom user variable (don't fetch actual values)
597572 if ( serverConfig . customUserVars && typeof serverConfig . customUserVars === 'object' ) {
598573 for ( const varName of Object . keys ( serverConfig . customUserVars ) ) {
599574 try {
600575 const value = await getUserPluginAuthValue ( user . id , varName , false , pluginKey ) ;
601- // Only store boolean flag indicating if value exists
602576 authValueFlags [ varName ] = ! ! ( value && value . length > 0 ) ;
603577 } catch ( err ) {
604578 logger . error (
605579 `[MCP Auth Value Flags] Error checking ${ varName } for user ${ user . id } :` ,
606580 err ,
607581 ) ;
608- // Default to false if we can't check
609582 authValueFlags [ varName ] = false ;
610583 }
611584 }
0 commit comments