-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathAuthDebugger.tsx
More file actions
320 lines (290 loc) · 10.5 KB
/
AuthDebugger.tsx
File metadata and controls
320 lines (290 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { useCallback, useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import { AlertCircle } from "lucide-react";
import type { InspectorClient } from "@modelcontextprotocol/inspector-core/mcp/index.js";
import { silentLogger } from "@modelcontextprotocol/inspector-core/logging/browser";
import type { AuthGuidedState } from "@modelcontextprotocol/inspector-core/auth/types.js";
import type { WebEnvironmentResult } from "@/lib/adapters/environmentFactory";
import { OAuthFlowProgress } from "./OAuthFlowProgress";
import { useToast } from "@/lib/hooks/useToast";
export interface AuthDebuggerProps {
inspectorClient: InspectorClient | null;
ensureInspectorClient: () => InspectorClient | null;
canCreateInspectorClient: () => boolean;
/** Logger from the same env as InspectorClient (for OAuth/auth logging). */
logger?: WebEnvironmentResult["logger"] | null;
onBack: () => void;
}
interface StatusMessageProps {
message: { type: "error" | "success" | "info"; message: string };
}
const StatusMessage = ({ message }: StatusMessageProps) => {
let bgColor: string;
let textColor: string;
let borderColor: string;
switch (message.type) {
case "error":
bgColor = "bg-red-50";
textColor = "text-red-700";
borderColor = "border-red-200";
break;
case "success":
bgColor = "bg-green-50";
textColor = "text-green-700";
borderColor = "border-green-200";
break;
case "info":
default:
bgColor = "bg-blue-50";
textColor = "text-blue-700";
borderColor = "border-blue-200";
break;
}
return (
<div
className={`p-3 rounded-md border ${bgColor} ${borderColor} ${textColor} mb-4`}
>
<div className="flex items-center gap-2">
<AlertCircle className="h-4 w-4" />
<p className="text-sm">{message.message}</p>
</div>
</div>
);
};
const AuthDebugger = ({
inspectorClient,
ensureInspectorClient,
canCreateInspectorClient,
logger,
onBack,
}: AuthDebuggerProps) => {
const log = logger ?? silentLogger;
const { toast } = useToast();
const [oauthState, setOauthState] = useState<AuthGuidedState | undefined>(
undefined,
);
const [isInitiatingAuth, setIsInitiatingAuth] = useState(false);
// Sync oauthState from InspectorClient (TUI pattern)
useEffect(() => {
if (!inspectorClient) {
setOauthState(undefined);
return;
}
const update = () => setOauthState(inspectorClient.getOAuthState());
update();
const onStepChange = () => update();
inspectorClient.addEventListener("oauthStepChange", onStepChange);
inspectorClient.addEventListener("oauthComplete", onStepChange);
inspectorClient.addEventListener("oauthError", onStepChange);
return () => {
inspectorClient.removeEventListener("oauthStepChange", onStepChange);
inspectorClient.removeEventListener("oauthComplete", onStepChange);
inspectorClient.removeEventListener("oauthError", onStepChange);
};
}, [inspectorClient]);
// Check for existing tokens on mount
useEffect(() => {
if (inspectorClient && !oauthState?.oauthTokens) {
inspectorClient.getOAuthTokens().then((tokens) => {
if (tokens) {
// State will be updated via getOAuthState() in sync effect
setOauthState(inspectorClient.getOAuthState());
}
});
}
}, [inspectorClient, oauthState]);
const handleQuickOAuth = useCallback(async () => {
const client = ensureInspectorClient();
if (!client) {
return; // Error already shown in ensureInspectorClient
}
setIsInitiatingAuth(true);
try {
// Quick Auth: normal flow (automatic redirect via BrowserNavigation)
const authUrl = await client.authenticate();
// Log via app-provided logger (same as InspectorClient's env logger)
log.info(
{
component: "AuthDebugger",
action: "authenticate",
authorizationUrl: authUrl.href,
redirectUri: authUrl.searchParams.get("redirect_uri"),
expectedRedirectUri: `${window.location.origin}/oauth/callback`,
currentOrigin: window.location.origin,
currentPathname: window.location.pathname,
},
"OAuth authorization URL generated - about to redirect",
);
// BrowserNavigation handles redirect automatically
} catch (error) {
log.error({ err: error }, "Quick OAuth failed");
toast({
title: "OAuth Error",
description: error instanceof Error ? error.message : String(error),
variant: "destructive",
});
} finally {
setIsInitiatingAuth(false);
}
}, [ensureInspectorClient, log, toast]);
const handleGuidedOAuth = useCallback(async () => {
const client = ensureInspectorClient();
if (!client) {
return; // Error already shown in ensureInspectorClient
}
setIsInitiatingAuth(true);
try {
// Start guided flow
await client.beginGuidedAuth();
// State updates via oauthStepChange events (handled in useEffect above)
} catch (error) {
log.error({ err: error }, "Guided OAuth start failed");
toast({
title: "OAuth Error",
description: error instanceof Error ? error.message : String(error),
variant: "destructive",
});
} finally {
setIsInitiatingAuth(false);
}
}, [ensureInspectorClient, log, toast]);
const proceedToNextStep = useCallback(async () => {
const client = ensureInspectorClient();
if (!client || !oauthState) {
if (!client) {
// Error already shown in ensureInspectorClient
return;
}
return; // No oauthState, nothing to proceed
}
setIsInitiatingAuth(true);
try {
await client.proceedOAuthStep();
// Note: For guided flow, users manually copy the authorization code.
// There's a manual button in OAuthFlowProgress to open the URL if needed.
// Quick auth handles redirects automatically via BrowserNavigation.
} catch (error) {
log.error({ err: error }, "OAuth step failed");
toast({
title: "OAuth Error",
description: error instanceof Error ? error.message : String(error),
variant: "destructive",
});
} finally {
setIsInitiatingAuth(false);
}
}, [ensureInspectorClient, log, oauthState, toast]);
const handleClearOAuth = useCallback(async () => {
const client = ensureInspectorClient();
if (!client) {
return; // Error already shown in ensureInspectorClient
}
try {
client.clearOAuthTokens();
toast({
title: "OAuth Cleared",
description: "OAuth tokens cleared successfully",
variant: "default",
});
} catch (error) {
log.error({ err: error }, "Clear OAuth failed");
toast({
title: "Error",
description: error instanceof Error ? error.message : String(error),
variant: "destructive",
});
}
}, [ensureInspectorClient, log, toast]);
return (
<div className="w-full p-4">
<div className="flex justify-between items-center mb-6">
<h2 className="text-2xl font-bold">Authentication Settings</h2>
<Button variant="outline" onClick={onBack}>
Back to Connect
</Button>
</div>
<div className="w-full space-y-6">
<div className="flex flex-col gap-6">
<div className="grid w-full gap-2">
<p className="text-muted-foreground mb-4">
Configure authentication settings for your MCP server connection.
</p>
<div className="rounded-md border p-6 space-y-6">
<h3 className="text-lg font-medium">OAuth Authentication</h3>
<p className="text-sm text-muted-foreground mb-2">
Use OAuth to securely authenticate with the MCP server.
</p>
{oauthState?.latestError && (
<StatusMessage
message={{
type: "error",
message: oauthState.latestError.message,
}}
/>
)}
<div className="space-y-4">
{oauthState?.oauthTokens && (
<div className="space-y-2">
<p className="text-sm font-medium">Access Token:</p>
<div className="bg-muted p-2 rounded-md text-xs overflow-x-auto">
{oauthState.oauthTokens.access_token.substring(0, 25)}...
</div>
</div>
)}
<div className="flex gap-4">
<Button
variant="outline"
onClick={handleGuidedOAuth}
disabled={
isInitiatingAuth ||
(!inspectorClient && !canCreateInspectorClient())
}
>
{oauthState?.oauthTokens
? "Guided Token Refresh"
: "Guided OAuth Flow"}
</Button>
<Button
onClick={handleQuickOAuth}
disabled={
isInitiatingAuth ||
(!inspectorClient && !canCreateInspectorClient())
}
>
{isInitiatingAuth
? "Initiating..."
: oauthState?.oauthTokens
? "Quick Refresh"
: "Quick OAuth Flow"}
</Button>
<Button
variant="outline"
onClick={handleClearOAuth}
disabled={!inspectorClient && !canCreateInspectorClient()}
>
Clear OAuth State
</Button>
</div>
{!inspectorClient && !canCreateInspectorClient() && (
<p className="text-sm text-destructive">
API Token is required. Please set it in Configuration.
</p>
)}
<p className="text-xs text-muted-foreground">
Choose "Guided" for step-by-step instructions or "Quick" for
the standard automatic flow.
</p>
</div>
</div>
<OAuthFlowProgress
oauthState={oauthState}
proceedToNextStep={proceedToNextStep}
ensureInspectorClient={ensureInspectorClient}
/>
</div>
</div>
</div>
</div>
);
};
export default AuthDebugger;