When enabling an MCP server with OAuth 2.1 authentication, close tab after successful authentication

This commit is contained in:
Vladimir Lastik 2025-10-06 13:08:45 +02:00
parent 3f71fa641f
commit aec2cedb3c
2 changed files with 37 additions and 1 deletions

View File

@ -606,7 +606,25 @@ class OAuthClientManager:
redirect_url = f"{redirect_url}/?error={error_message}" redirect_url = f"{redirect_url}/?error={error_message}"
return RedirectResponse(url=redirect_url, headers=response.headers) return RedirectResponse(url=redirect_url, headers=response.headers)
response = RedirectResponse(url=redirect_url, headers=response.headers) from fastapi.responses import HTMLResponse
close_tab_html = f"""
<!DOCTYPE html>
<html>
<head>
<title>Authentication Complete</title>
</head>
<body>
<script>
const channel = new BroadcastChannel(`server:{client_id}:oauth`);
channel.postMessage('oauth_complete');
channel.close();
window.close();
</script>
<p>Authentication complete. You can close this tab.</p>
</body>
</html>
"""
response = HTMLResponse(content=close_tab_html, headers=response.headers)
return response return response

View File

@ -336,6 +336,24 @@
let parts = toolId.split(':'); let parts = toolId.split(':');
let serverId = parts?.at(-1) ?? toolId; let serverId = parts?.at(-1) ?? toolId;
const channel = new BroadcastChannel(`${toolId}:oauth`);
channel.onmessage = (event) => {
if (event.data === 'oauth_complete') {
_tools.update(currentTools => {
const currentTool = currentTools.find(t => t.id === toolId);
if (currentTool) {
currentTool.authenticated = true;
}
return currentTools;
});
tools[toolId].authenticated = true;
tools[toolId].enabled = true;
channel.close();
}
};
const authUrl = getOAuthClientAuthorizationUrl(serverId, 'mcp'); const authUrl = getOAuthClientAuthorizationUrl(serverId, 'mcp');
window.open(authUrl, '_blank', 'noopener'); window.open(authUrl, '_blank', 'noopener');