-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathProgram.cs
More file actions
100 lines (86 loc) · 3.66 KB
/
Program.cs
File metadata and controls
100 lines (86 loc) · 3.66 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
using System.Net.Http;
using System.Threading.Tasks;
using ModelContextProtocol.Auth;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
namespace ProtectedMCPClient;
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("MCP Secure Weather Client with OAuth Authentication");
Console.WriteLine("==================================================");
Console.WriteLine();
// Create the authorization config with HTTP listener
var authConfig = new AuthorizationConfig
{
ClientId = "04f79824-ab56-4511-a7cb-d7deaea92dc0",
Scopes = ["User.Read"]
}.UseHttpListener(hostname: "localhost", listenPort: 1170);
// Create an HTTP client with OAuth handling
var oauthHandler = new OAuthDelegatingHandler(
redirectUri: authConfig.RedirectUri,
clientId: authConfig.ClientId,
clientName: authConfig.ClientName,
scopes: authConfig.Scopes,
authorizationHandler: authConfig.AuthorizationHandler)
{
// The OAuth handler needs an inner handler
InnerHandler = new HttpClientHandler()
};
var httpClient = new HttpClient(oauthHandler);
var serverUrl = "http://localhost:7071/sse"; // Default server URL
// Allow the user to specify a different server URL
Console.WriteLine($"Server URL (press Enter for default: {serverUrl}):");
var userInput = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(userInput))
{
serverUrl = userInput;
}
Console.WriteLine();
Console.WriteLine($"Connecting to weather server at {serverUrl}...");
Console.WriteLine("When prompted for authorization, a browser window will open automatically.");
Console.WriteLine("Complete the authentication in the browser, and this application will continue automatically.");
Console.WriteLine();
try
{
// Create SseClientTransportOptions with the server URL
var transportOptions = new SseClientTransportOptions
{
Endpoint = new Uri(serverUrl),
Name = "Secure Weather Client"
};
// Create SseClientTransport with our authenticated HTTP client
var transport = new SseClientTransport(transportOptions, httpClient);
// Create an MCP client using the factory method with our transport
var client = await McpClientFactory.CreateAsync(transport);
// Get the list of available tools
var tools = await client.ListToolsAsync();
if (tools.Count == 0)
{
Console.WriteLine("No tools available on the server.");
return;
}
Console.WriteLine($"Found {tools.Count} tools on the server.");
Console.WriteLine();
// Call the protected-data tool which requires authentication
if (tools.Any(t => t.Name == "protected-data"))
{
Console.WriteLine("Calling protected-data tool...");
var result = await client.CallToolAsync("protected-data");
Console.WriteLine("Result: " + result.Content[0].Text);
Console.WriteLine();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
if (ex.InnerException != null)
{
Console.WriteLine($"Inner error: {ex.InnerException.Message}");
}
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}