1+ using ModelContextProtocol . Authentication ;
12using ModelContextProtocol . Client ;
23using ModelContextProtocol . Protocol . Transport ;
4+ using System . Diagnostics ;
35
46namespace ProtectedMCPClient ;
57
68class Program
79{
810 static async Task Main ( string [ ] args )
911 {
10- Console . WriteLine ( "MCP Secure Weather Client with OAuth Authentication" ) ;
11- Console . WriteLine ( "================================================== " ) ;
12+ Console . WriteLine ( "MCP Secure Weather Client with Authentication" ) ;
13+ Console . WriteLine ( "==============================================" ) ;
1214 Console . WriteLine ( ) ;
1315
14- //// Create the authorization config with HTTP listener
15- //var authConfig = new AuthorizationConfig
16- //{
17- // ClientId = "6ad97b5f-7a7b-413f-8603-7a3517d4adb8",
18- // Scopes = ["api://167b4284-3f92-4436-92ed-38b38f83ae08/weather.read"]
19- //}.UseHttpListener(hostname: "localhost", listenPort: 1170);
20-
21- //// Create an HTTP client with OAuth handling
22- //var oauthHandler = new OAuthDelegatingHandler(
23- // redirectUri: authConfig.RedirectUri,
24- // clientId: authConfig.ClientId,
25- // clientName: authConfig.ClientName,
26- // scopes: authConfig.Scopes,
27- // authorizationHandler: authConfig.AuthorizationHandler)
28- //{
29- // // The OAuth handler needs an inner handler
30- // InnerHandler = new HttpClientHandler()
31- //};
32-
33- var httpClient = new HttpClient ( ) ;
16+ // Create a standard HttpClient with authentication configured
3417 var serverUrl = "http://localhost:7071/sse" ; // Default server URL
3518
19+ // Ask for the API key
20+ Console . WriteLine ( "Enter your API key (or press Enter to use default):" ) ;
21+ var apiKey = Console . ReadLine ( ) ;
22+ if ( string . IsNullOrWhiteSpace ( apiKey ) )
23+ {
24+ apiKey = "demo-api-key-12345" ; // Default API key for demonstration
25+ Console . WriteLine ( $ "Using default API key: { apiKey } ") ;
26+ }
27+
3628 // Allow the user to specify a different server URL
3729 Console . WriteLine ( $ "Server URL (press Enter for default: { serverUrl } ):") ;
3830 var userInput = Console . ReadLine ( ) ;
@@ -41,10 +33,14 @@ static async Task Main(string[] args)
4133 serverUrl = userInput ;
4234 }
4335
36+ // Create a single HttpClient with authentication configured
37+ var tokenProvider = new SimpleAccessTokenProvider ( apiKey , new Uri ( serverUrl ) ) ;
38+ var httpClient = new HttpClient ( ) . UseAuthenticationProvider ( tokenProvider ) ;
39+
4440 Console . WriteLine ( ) ;
4541 Console . WriteLine ( $ "Connecting to weather server at { serverUrl } ...") ;
46- Console . WriteLine ( "When prompted for authorization, a browser window will open automatically." ) ;
47- Console . WriteLine ( "Complete the authentication in the browser, and this application will continue automatically ." ) ;
42+ Console . WriteLine ( "When prompted for authorization, the challenge will be verified automatically." ) ;
43+ Console . WriteLine ( "If required, you'll be guided through any necessary authentication steps ." ) ;
4844 Console . WriteLine ( ) ;
4945
5046 try
@@ -82,13 +78,27 @@ static async Task Main(string[] args)
8278 Console . WriteLine ( ) ;
8379 }
8480 }
81+ catch ( HttpRequestException ex ) when ( ex . StatusCode == System . Net . HttpStatusCode . Unauthorized )
82+ {
83+ // Handle authentication failures specifically
84+ Console . WriteLine ( "Authentication failed. The server returned a 401 Unauthorized response." ) ;
85+ Console . WriteLine ( $ "Details: { ex . Message } ") ;
86+
87+ // Additional handling for 401 - could add manual authentication retry here
88+ Console . WriteLine ( "You might need to provide a different API key or authentication credentials." ) ;
89+ }
8590 catch ( Exception ex )
8691 {
8792 Console . WriteLine ( $ "Error: { ex . Message } ") ;
8893 if ( ex . InnerException != null )
8994 {
9095 Console . WriteLine ( $ "Inner error: { ex . InnerException . Message } ") ;
9196 }
97+
98+ // Print stack trace in debug builds
99+ #if DEBUG
100+ Console . WriteLine ( $ "Stack trace: { ex . StackTrace } ") ;
101+ #endif
92102 }
93103
94104 Console . WriteLine ( "Press any key to exit..." ) ;
0 commit comments