Skip to content

Commit 20e961c

Browse files
committed
resumption working
1 parent 0dbf75a commit 20e961c

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

client/src/App.tsx

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { OAuthTokensSchema } from "@modelcontextprotocol/sdk/shared/auth.js";
2121
import { SESSION_KEYS, getServerSpecificKey } from "./lib/constants";
2222
import { AuthDebuggerState, EMPTY_DEBUGGER_STATE } from "./lib/auth-types";
23+
import { OAuthStateMachine } from "./lib/oauth-state-machine";
2324
import { cacheToolOutputSchemas } from "./utils/schemaUtils";
2425
import React, {
2526
Suspense,
@@ -231,7 +232,7 @@ const App = () => {
231232

232233
// Update OAuth debug state during debug callback
233234
const onOAuthDebugConnect = useCallback(
234-
({
235+
async ({
235236
authorizationCode,
236237
errorMsg,
237238
restoredState,
@@ -241,29 +242,64 @@ const App = () => {
241242
restoredState?: AuthDebuggerState;
242243
}) => {
243244
setIsAuthDebuggerVisible(true);
244-
245-
if (restoredState) {
246-
// Restore the previous auth state
245+
246+
if (errorMsg) {
247247
updateAuthState({
248-
...restoredState,
249-
// Update with the new authorization code if provided
250-
authorizationCode: authorizationCode || restoredState.authorizationCode,
251-
oauthStep: authorizationCode ? "token_request" : restoredState.oauthStep,
252-
latestError: errorMsg ? new Error(errorMsg) : restoredState.latestError,
248+
latestError: new Error(errorMsg),
253249
});
254-
} else {
255-
// Fallback to the original behavior if no state was restored
256-
if (authorizationCode) {
257-
updateAuthState({
258-
authorizationCode,
259-
oauthStep: "token_request",
250+
return;
251+
}
252+
253+
if (restoredState && authorizationCode) {
254+
// Restore the previous auth state and continue the OAuth flow
255+
let currentState: AuthDebuggerState = {
256+
...restoredState,
257+
authorizationCode,
258+
oauthStep: "token_request",
259+
isInitiatingAuth: true,
260+
statusMessage: null,
261+
latestError: null,
262+
};
263+
264+
try {
265+
// Create a new state machine instance to continue the flow
266+
const stateMachine = new OAuthStateMachine(sseUrl, (updates) => {
267+
currentState = { ...currentState, ...updates };
260268
});
261-
}
262-
if (errorMsg) {
269+
270+
// Continue stepping through the OAuth flow from where we left off
271+
while (currentState.oauthStep !== "complete" && currentState.oauthStep !== "authorization_code") {
272+
await stateMachine.executeStep(currentState);
273+
}
274+
275+
if (currentState.oauthStep === "complete") {
276+
// After the flow completes or reaches a user-input step, update the app state
277+
updateAuthState({
278+
...currentState,
279+
statusMessage: {
280+
type: "success",
281+
message: "Authentication completed successfully",
282+
},
283+
isInitiatingAuth: false,
284+
});
285+
}
286+
} catch (error) {
287+
console.error("OAuth continuation error:", error);
263288
updateAuthState({
264-
latestError: new Error(errorMsg),
289+
latestError: error instanceof Error ? error : new Error(String(error)),
290+
statusMessage: {
291+
type: "error",
292+
message: `Failed to complete OAuth flow: ${error instanceof Error ? error.message : String(error)}`,
293+
},
294+
isInitiatingAuth: false,
265295
});
266296
}
297+
} else if (authorizationCode) {
298+
// Fallback to the original behavior if no state was restored
299+
updateAuthState({
300+
authorizationCode,
301+
oauthStep: "token_request",
302+
});
267303
}
268304
},
269305
[],

0 commit comments

Comments
 (0)