@@ -7,6 +7,14 @@ import {
77 generateOAuthErrorDescription ,
88 parseOAuthCallbackParams ,
99} from "@/utils/oauthUtils.ts" ;
10+ import { createOAuthProviderForServer } from "../lib/oauth/provider-factory" ;
11+ import { OAuthStateMachine } from "../lib/oauth-state-machine" ;
12+ import { AuthDebuggerState } from "../lib/auth-types" ;
13+ import {
14+ getMCPProxyAddress ,
15+ getMCPProxyAuthToken ,
16+ initializeInspectorConfig ,
17+ } from "@/utils/configUtils" ;
1018
1119interface OAuthCallbackProps {
1220 onConnect : ( serverUrl : string ) => void ;
@@ -41,24 +49,97 @@ const OAuthCallback = ({ onConnect }: OAuthCallbackProps) => {
4149 return notifyError ( "Missing Server URL" ) ;
4250 }
4351
44- let result ;
45- try {
46- // Create an auth provider with the current server URL
47- const serverAuthProvider = new InspectorOAuthClientProvider ( serverUrl ) ;
52+ // Check if there's stored auth state (for proxy mode from Connect button)
53+ const storedAuthState = sessionStorage . getItem (
54+ SESSION_KEYS . AUTH_STATE_FOR_CONNECT ,
55+ ) ;
4856
49- result = await auth ( serverAuthProvider , {
50- serverUrl,
51- authorizationCode : params . code ,
52- } ) ;
53- } catch ( error ) {
54- console . error ( "OAuth callback error:" , error ) ;
55- return notifyError ( `Unexpected error occurred: ${ error } ` ) ;
56- }
57+ if ( storedAuthState ) {
58+ // Proxy mode: Complete the OAuth flow using the state machine
59+ try {
60+ let restoredState : AuthDebuggerState = JSON . parse ( storedAuthState ) ;
61+
62+ // Restore URL objects
63+ if (
64+ restoredState . resource &&
65+ typeof restoredState . resource === "string"
66+ ) {
67+ restoredState . resource = new URL ( restoredState . resource ) ;
68+ }
69+ if (
70+ restoredState . authorizationUrl &&
71+ typeof restoredState . authorizationUrl === "string"
72+ ) {
73+ restoredState . authorizationUrl = new URL (
74+ restoredState . authorizationUrl ,
75+ ) ;
76+ }
77+
78+ // Set up state with the authorization code
79+ let currentState : AuthDebuggerState = {
80+ ...restoredState ,
81+ authorizationCode : params . code ,
82+ oauthStep : "token_request" ,
83+ } ;
84+
85+ // Get config and create provider
86+ // Use the same config key and initialization as App.tsx
87+ const config = initializeInspectorConfig ( "inspectorConfig_v1" ) ;
88+
89+ const proxyAddress = getMCPProxyAddress ( config ) ;
90+ const proxyAuthObj = getMCPProxyAuthToken ( config ) ;
91+
92+ const oauthProvider = createOAuthProviderForServer (
93+ serverUrl ,
94+ proxyAddress ,
95+ proxyAuthObj . token ,
96+ ) ;
97+
98+ const stateMachine = new OAuthStateMachine (
99+ serverUrl ,
100+ ( updates ) => {
101+ currentState = { ...currentState , ...updates } ;
102+ } ,
103+ oauthProvider ,
104+ false , // use regular redirect URL
105+ ) ;
106+
107+ // Complete the token exchange
108+ await stateMachine . executeStep ( currentState ) ;
109+
110+ if ( currentState . oauthStep !== "complete" ) {
111+ return notifyError ( "Failed to complete OAuth token exchange" ) ;
112+ }
113+
114+ // Clean up stored state
115+ sessionStorage . removeItem ( SESSION_KEYS . AUTH_STATE_FOR_CONNECT ) ;
116+ } catch ( error ) {
117+ console . error ( "Proxy OAuth callback error:" , error ) ;
118+ sessionStorage . removeItem ( SESSION_KEYS . AUTH_STATE_FOR_CONNECT ) ;
119+ return notifyError ( `Failed to complete proxy OAuth: ${ error } ` ) ;
120+ }
121+ } else {
122+ // Direct mode: Use SDK's auth() function
123+ let result ;
124+ try {
125+ const serverAuthProvider = new InspectorOAuthClientProvider (
126+ serverUrl ,
127+ ) ;
128+
129+ result = await auth ( serverAuthProvider , {
130+ serverUrl,
131+ authorizationCode : params . code ,
132+ } ) ;
133+ } catch ( error ) {
134+ console . error ( "OAuth callback error:" , error ) ;
135+ return notifyError ( `Unexpected error occurred: ${ error } ` ) ;
136+ }
57137
58- if ( result !== "AUTHORIZED" ) {
59- return notifyError (
60- `Expected to be authorized after providing auth code, got: ${ result } ` ,
61- ) ;
138+ if ( result !== "AUTHORIZED" ) {
139+ return notifyError (
140+ `Expected to be authorized after providing auth code, got: ${ result } ` ,
141+ ) ;
142+ }
62143 }
63144
64145 // Finally, trigger auto-connect
0 commit comments