-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (80 loc) · 3.56 KB
/
Program.cs
File metadata and controls
93 lines (80 loc) · 3.56 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
using System.Diagnostics;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Auth;
using ModelContextProtocol.Protocol.Transport;
namespace AuthorizationExample;
/// <summary>
/// Example demonstrating how to use the MCP C# SDK with OAuth authorization.
/// </summary>
public class Program
{
public static async Task Main(string[] args)
{
// Define the MCP server endpoint that requires OAuth authentication
var serverEndpoint = new Uri("http://localhost:7071/sse");
// Configuration values for OAuth redirect
string hostname = "localhost";
int port = 13261;
string callbackPath = "/oauth/callback/";
// Set up the SSE transport with authorization support
var transportOptions = new SseClientTransportOptions
{
Endpoint = serverEndpoint,
AuthorizationOptions = new AuthorizationOptions
{
// Pre-registered client credentials (if applicable)
ClientId = "04f79824-ab56-4511-a7cb-d7deaea92dc0",
// Setting some pre-defined scopes the client requests.
Scopes = ["User.Read"],
// Specify the exact same redirect URIs that are registered with the OAuth server
RedirectUris = new[]
{
$"http://{hostname}:{port}{callbackPath}"
},
// Configure the authorize callback with the same hostname, port, and path
AuthorizeCallback = SseClientTransport.CreateHttpListenerAuthorizeCallback(
openBrowser: async (url) =>
{
Console.WriteLine($"Opening browser to authorize at: {url}");
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
},
hostname: hostname,
listenPort: port,
redirectPath: callbackPath
)
}
};
Console.WriteLine("Connecting to MCP server...");
try
{
// Create the client with authorization-enabled transport
var transport = new SseClientTransport(transportOptions);
var client = await McpClientFactory.CreateAsync(transport);
Console.WriteLine("Successfully connected and authorized!");
// Print the list of tools available from the server.
Console.WriteLine("\nAvailable tools:");
foreach (var tool in await client.ListToolsAsync())
{
Console.WriteLine($" - {tool.Name}: {tool.Description}");
}
// Execute a tool (this would normally be driven by LLM tool invocations).
Console.WriteLine("\nCalling 'echo' tool...");
var result = await client.CallToolAsync(
"echo",
new Dictionary<string, object?>() { ["message"] = "Hello MCP!" },
cancellationToken: CancellationToken.None);
// echo always returns one and only one text content object
Console.WriteLine($"Tool response: {result.Content.First(c => c.Type == "text").Text}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
if (ex.InnerException != null)
{
Console.WriteLine($"Inner Error: {ex.InnerException.Message}");
}
// Print the stack trace for debugging
Console.WriteLine($"Stack Trace:\n{ex.StackTrace}");
}
}
}