@@ -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
1517export 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