When enabling an MCP server with OAuth 2.1 authentication, close tab after successful authentication
This commit is contained in:
parent
3f71fa641f
commit
aec2cedb3c
|
@ -606,7 +606,25 @@ class OAuthClientManager:
|
|||
redirect_url = f"{redirect_url}/?error={error_message}"
|
||||
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
|
||||
|
||||
|
||||
|
|
|
@ -337,6 +337,24 @@
|
|||
let parts = toolId.split(':');
|
||||
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');
|
||||
window.open(authUrl, '_blank', 'noopener');
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue