mirror of
https://github.com/samanhappy/mcphub.git
synced 2025-12-24 02:39:19 -05:00
feat: enhance OAuth token logging and add authentication error handling in tool calls (#512)
This commit is contained in:
@@ -325,7 +325,7 @@ export class MCPHubOAuthProvider implements OAuthClientProvider {
|
|||||||
return;
|
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, {
|
const updatedConfig = await persistTokens(this.serverName, {
|
||||||
accessToken: tokens.access_token,
|
accessToken: tokens.access_token,
|
||||||
|
|||||||
@@ -241,6 +241,8 @@ const callToolWithReconnect = async (
|
|||||||
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
||||||
try {
|
try {
|
||||||
const result = await serverInfo.client.callTool(toolParams, undefined, options || {});
|
const result = await serverInfo.client.callTool(toolParams, undefined, options || {});
|
||||||
|
// Check auth error
|
||||||
|
checkAuthError(result);
|
||||||
return result;
|
return result;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// Check if error message starts with "Error POSTing to endpoint (HTTP 40"
|
// 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
|
// Close server client and transport
|
||||||
function closeServer(name: string) {
|
function closeServer(name: string) {
|
||||||
const serverInfo = serverInfos.find((serverInfo) => serverInfo.name === name);
|
const serverInfo = serverInfos.find((serverInfo) => serverInfo.name === name);
|
||||||
|
|||||||
Reference in New Issue
Block a user