diff --git a/client/src/components/OAuthCallback.tsx b/client/src/components/OAuthCallback.tsx index 869eef1..fce470a 100644 --- a/client/src/components/OAuthCallback.tsx +++ b/client/src/components/OAuthCallback.tsx @@ -1,6 +1,7 @@ import { useEffect, useRef } from "react"; -import { handleOAuthCallback } from "../lib/auth"; +import { authProvider } from "../lib/auth"; import { SESSION_KEYS } from "../lib/constants"; +import { auth } from "@modelcontextprotocol/sdk/client/auth.js"; const OAuthCallback = () => { const hasProcessedRef = useRef(false); @@ -24,15 +25,11 @@ const OAuthCallback = () => { } try { - const tokens = await handleOAuthCallback(serverUrl, code); - // Store both access and refresh tokens - sessionStorage.setItem(SESSION_KEYS.ACCESS_TOKEN, tokens.access_token); - if (tokens.refresh_token) { - sessionStorage.setItem( - SESSION_KEYS.REFRESH_TOKEN, - tokens.refresh_token, - ); + const result = await auth(authProvider, { serverUrl, authorizationCode: code }); + if (result !== "AUTHORIZED") { + throw new Error(`Expected to be authorized after providing auth code, got: ${result}`); } + // Redirect back to the main app with server URL to trigger auto-connect window.location.href = `/?serverUrl=${encodeURIComponent(serverUrl)}`; } catch (error) { diff --git a/client/src/lib/auth.ts b/client/src/lib/auth.ts index 1ae0efd..a38f929 100644 --- a/client/src/lib/auth.ts +++ b/client/src/lib/auth.ts @@ -1,7 +1,7 @@ import { OAuthClientInformation, OAuthClientInformationSchema, OAuthClientProvider, OAuthTokens, OAuthTokensSchema } from "@modelcontextprotocol/sdk/client/auth.js"; import { SESSION_KEYS } from "./constants"; -export class InspectorOAuthClientProvider implements OAuthClientProvider { +class InspectorOAuthClientProvider implements OAuthClientProvider { get redirectUrl() { return window.location.origin + "/oauth/callback"; } @@ -69,3 +69,5 @@ export class InspectorOAuthClientProvider implements OAuthClientProvider { return verifier; } } + +export const authProvider = new InspectorOAuthClientProvider(); \ No newline at end of file diff --git a/client/src/lib/hooks/useConnection.ts b/client/src/lib/hooks/useConnection.ts index e23a532..158bb76 100644 --- a/client/src/lib/hooks/useConnection.ts +++ b/client/src/lib/hooks/useConnection.ts @@ -19,7 +19,7 @@ import { z } from "zod"; import { SESSION_KEYS } from "../constants"; import { Notification, StdErrNotificationSchema } from "../notificationTypes"; import { auth } from "@modelcontextprotocol/sdk/client/auth.js"; -import { InspectorOAuthClientProvider } from "../auth"; +import { authProvider } from "../auth"; const DEFAULT_REQUEST_TIMEOUT_MSEC = 10000; @@ -122,12 +122,11 @@ export function useConnection({ } }; - const authProvider = new InspectorOAuthClientProvider(); const handleAuthError = async (error: unknown) => { if (error instanceof SseError && error.code === 401) { sessionStorage.setItem(SESSION_KEYS.SERVER_URL, sseUrl); - const result = await auth(authProvider, { serverUrl: sseUrl }) + const result = await auth(authProvider, { serverUrl: sseUrl }); return result === "AUTHORIZED"; }