diff --git a/src/services/mcpOAuthProvider.ts b/src/services/mcpOAuthProvider.ts index e92b293..10c9935 100644 --- a/src/services/mcpOAuthProvider.ts +++ b/src/services/mcpOAuthProvider.ts @@ -325,7 +325,7 @@ export class MCPHubOAuthProvider implements OAuthClientProvider { return; } - console.log(`Saving OAuth tokens for server: ${this.serverName}`); + console.log(`Saving OAuth tokens: ${JSON.stringify(tokens)} for server: ${this.serverName}`); const updatedConfig = await persistTokens(this.serverName, { accessToken: tokens.access_token, diff --git a/src/services/mcpService.ts b/src/services/mcpService.ts index 1cfdea4..967193f 100644 --- a/src/services/mcpService.ts +++ b/src/services/mcpService.ts @@ -241,6 +241,8 @@ const callToolWithReconnect = async ( for (let attempt = 0; attempt <= maxRetries; attempt++) { try { const result = await serverInfo.client.callTool(toolParams, undefined, options || {}); + // Check auth error + checkAuthError(result); return result; } catch (error: any) { // Check if error message starts with "Error POSTing to endpoint (HTTP 40" @@ -830,6 +832,25 @@ export const addOrUpdateServer = async ( } }; +// Check for authentication error in tool call result +function checkAuthError(result: any) { + if (Array.isArray(result.content) && result.content.length > 0) { + const text = result.content[0]?.text; + if (typeof text === 'string') { + let errorContent; + try { + errorContent = JSON.parse(text); + } catch (e) { + // Ignore JSON parse errors and continue + return; + } + if (errorContent.code === 401) { + throw new Error('Error POSTing to endpoint (HTTP 401 Unauthorized)'); + } + } + } +} + // Close server client and transport function closeServer(name: string) { const serverInfo = serverInfos.find((serverInfo) => serverInfo.name === name);