Skip to content

Commit e9a50ad

Browse files
committed
Update OAuth callback code
1 parent eb6af47 commit e9a50ad

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

client/src/components/OAuthCallback.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useRef } from "react";
2-
import { handleOAuthCallback } from "../lib/auth";
2+
import { authProvider } from "../lib/auth";
33
import { SESSION_KEYS } from "../lib/constants";
4+
import { auth } from "@modelcontextprotocol/sdk/client/auth.js";
45

56
const OAuthCallback = () => {
67
const hasProcessedRef = useRef(false);
@@ -24,15 +25,11 @@ const OAuthCallback = () => {
2425
}
2526

2627
try {
27-
const tokens = await handleOAuthCallback(serverUrl, code);
28-
// Store both access and refresh tokens
29-
sessionStorage.setItem(SESSION_KEYS.ACCESS_TOKEN, tokens.access_token);
30-
if (tokens.refresh_token) {
31-
sessionStorage.setItem(
32-
SESSION_KEYS.REFRESH_TOKEN,
33-
tokens.refresh_token,
34-
);
28+
const result = await auth(authProvider, { serverUrl, authorizationCode: code });
29+
if (result !== "AUTHORIZED") {
30+
throw new Error(`Expected to be authorized after providing auth code, got: ${result}`);
3531
}
32+
3633
// Redirect back to the main app with server URL to trigger auto-connect
3734
window.location.href = `/?serverUrl=${encodeURIComponent(serverUrl)}`;
3835
} catch (error) {

client/src/lib/auth.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { OAuthClientInformation, OAuthClientInformationSchema, OAuthClientProvider, OAuthTokens, OAuthTokensSchema } from "@modelcontextprotocol/sdk/client/auth.js";
22
import { SESSION_KEYS } from "./constants";
33

4-
export class InspectorOAuthClientProvider implements OAuthClientProvider {
4+
class InspectorOAuthClientProvider implements OAuthClientProvider {
55
get redirectUrl() {
66
return window.location.origin + "/oauth/callback";
77
}
@@ -69,3 +69,5 @@ export class InspectorOAuthClientProvider implements OAuthClientProvider {
6969
return verifier;
7070
}
7171
}
72+
73+
export const authProvider = new InspectorOAuthClientProvider();

client/src/lib/hooks/useConnection.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { z } from "zod";
1919
import { SESSION_KEYS } from "../constants";
2020
import { Notification, StdErrNotificationSchema } from "../notificationTypes";
2121
import { auth } from "@modelcontextprotocol/sdk/client/auth.js";
22-
import { InspectorOAuthClientProvider } from "../auth";
22+
import { authProvider } from "../auth";
2323

2424
const DEFAULT_REQUEST_TIMEOUT_MSEC = 10000;
2525

@@ -122,12 +122,11 @@ export function useConnection({
122122
}
123123
};
124124

125-
const authProvider = new InspectorOAuthClientProvider();
126125
const handleAuthError = async (error: unknown) => {
127126
if (error instanceof SseError && error.code === 401) {
128127
sessionStorage.setItem(SESSION_KEYS.SERVER_URL, sseUrl);
129128

130-
const result = await auth(authProvider, { serverUrl: sseUrl })
129+
const result = await auth(authProvider, { serverUrl: sseUrl });
131130
return result === "AUTHORIZED";
132131
}
133132

0 commit comments

Comments
 (0)