Skip to content

Commit bc133dd

Browse files
committed
test tokens at the end
1 parent 60f6b68 commit bc133dd

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

bin/test-auth-flow.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ class CLIAuthFlowTester {
152152
scope: this.state.oauthTokens?.scope,
153153
});
154154
break;
155+
156+
case 'validate_token':
157+
this.success('TOKEN VALIDATION', 'Token validated successfully', {
158+
message: this.state.statusMessage?.message || 'Access token is valid',
159+
});
160+
break;
155161
}
156162

157163
return true;
@@ -283,6 +289,10 @@ class CLIAuthFlowTester {
283289
// Step 6: Exchange Code for Tokens
284290
await this.executeStep('token_request');
285291
console.log("");
292+
293+
// Step 7: Validate the token by calling tools/list
294+
await this.executeStep('validate_token');
295+
console.log("");
286296

287297
// Final summary
288298
console.log("🎉 AUTHORIZATION FLOW COMPLETED SUCCESSFULLY!");

client/src/lib/auth-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type OAuthStep =
1313
| "authorization_redirect"
1414
| "authorization_code"
1515
| "token_request"
16+
| "validate_token"
1617
| "complete";
1718

1819
// Message types for inline feedback

client/src/lib/oauth-state-machine.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
OAuthMetadataSchema,
1212
OAuthProtectedResourceMetadata,
1313
} from "@modelcontextprotocol/sdk/shared/auth.js";
14+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
15+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
1416

1517
export interface StateMachineContext {
1618
state: AuthDebuggerState;
@@ -168,10 +170,55 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
168170
context.provider.saveTokens(tokens);
169171
context.updateState({
170172
oauthTokens: tokens,
171-
oauthStep: "complete",
173+
oauthStep: "validate_token",
172174
});
173175
},
174176
},
177+
178+
validate_token: {
179+
canTransition: async (context) => {
180+
return !!context.state.oauthTokens && !!context.state.oauthTokens.access_token;
181+
},
182+
execute: async (context) => {
183+
if (!context.state.oauthTokens?.access_token) {
184+
throw new Error("No access token available for validation");
185+
}
186+
187+
try {
188+
// Create a simple client with the StreamableHTTP transport
189+
const transport = new StreamableHTTPClientTransport(
190+
new URL(context.serverUrl),
191+
{
192+
requestInit: {
193+
headers: {
194+
Authorization: `Bearer ${context.state.oauthTokens.access_token}`
195+
}
196+
}
197+
}
198+
);
199+
200+
const client = new Client(
201+
{ name: "mcp-auth-validator", version: "1.0.0" },
202+
{ capabilities: {} }
203+
);
204+
205+
// Connect and list tools to validate the token
206+
await client.connect(transport);
207+
const response = await client.listTools();
208+
209+
// Successfully validated token
210+
context.updateState({
211+
oauthStep: "complete",
212+
statusMessage: {
213+
type: "success",
214+
message: `Token validated successfully! Found ${response.tools?.length || 0} tools.`,
215+
},
216+
});
217+
} catch (error) {
218+
throw new Error(`Token validation failed: ${error instanceof Error ? error.message : String(error)}`);
219+
}
220+
},
221+
},
175222

176223
complete: {
177224
canTransition: async () => false,

0 commit comments

Comments
 (0)