11using ModelContextProtocol . Authentication ;
22using ModelContextProtocol . Client ;
33using ModelContextProtocol . Protocol . Transport ;
4- using System . Diagnostics ;
54
65namespace ProtectedMCPClient ;
76
87class Program
98{
109 static async Task Main ( string [ ] args )
1110 {
12- Console . WriteLine ( "MCP Secure Weather Client with Authentication" ) ;
13- Console . WriteLine ( "==============================================" ) ;
11+ Console . WriteLine ( "Protected MCP Weather Server" ) ;
1412 Console . WriteLine ( ) ;
1513
16- // Create a standard HttpClient with authentication configured
17- var serverUrl = "http://localhost:7071/sse" ; // Default server URL
14+ var serverUrl = "http://localhost:7071/sse" ;
1815
19- // Allow the user to specify a different server URL
20- Console . WriteLine ( $ "Server URL (press Enter for default: { serverUrl } ):") ;
21- var userInput = Console . ReadLine ( ) ;
22- if ( ! string . IsNullOrWhiteSpace ( userInput ) )
23- {
24- serverUrl = userInput ;
25- }
26-
27- // Create a single HttpClient with authentication configured
2816 var tokenProvider = new SimpleAccessTokenProvider ( new Uri ( serverUrl ) ) ;
29- var httpClient = new HttpClient ( ) . UseAuthenticationProvider ( tokenProvider ) ;
17+ var httpClient = new HttpClient ( ) . UseMcpAuthorizationProvider ( tokenProvider ) ;
3018
3119 Console . WriteLine ( ) ;
3220 Console . WriteLine ( $ "Connecting to weather server at { serverUrl } ...") ;
33- Console . WriteLine ( "When prompted for authorization, the challenge will be verified automatically." ) ;
34- Console . WriteLine ( "If required, you'll be guided through any necessary authentication steps." ) ;
35- Console . WriteLine ( ) ;
3621
3722 try
3823 {
39- // Create SseClientTransportOptions with the server URL
4024 var transportOptions = new SseClientTransportOptions
4125 {
4226 Endpoint = new Uri ( serverUrl ) ,
4327 Name = "Secure Weather Client"
4428 } ;
4529
46- // Create SseClientTransport with our authenticated HTTP client
4730 var transport = new SseClientTransport ( transportOptions , httpClient ) ;
4831
49- // Create an MCP client using the factory method with our transport
5032 var client = await McpClientFactory . CreateAsync ( transport ) ;
5133
52- // Get the list of available tools
5334 var tools = await client . ListToolsAsync ( ) ;
5435 if ( tools . Count == 0 )
5536 {
@@ -60,7 +41,6 @@ static async Task Main(string[] args)
6041 Console . WriteLine ( $ "Found { tools . Count } tools on the server.") ;
6142 Console . WriteLine ( ) ;
6243
63- // Call the protected-data tool which requires authentication
6444 if ( tools . Any ( t => t . Name == "protected-data" ) )
6545 {
6646 Console . WriteLine ( "Calling protected-data tool..." ) ;
0 commit comments