Skip to content

Commit 886d388

Browse files
committed
Add console logger to ProtectedMCPClient sample
1 parent 1a3d2c7 commit 886d388

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

samples/ProtectedMCPClient/Program.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.Logging;
12
using ModelContextProtocol.Authentication;
23
using ModelContextProtocol.Client;
34
using ModelContextProtocol.Protocol;
@@ -19,14 +20,20 @@
1920
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(1)
2021
};
2122

23+
var consoleLoggerFactory = LoggerFactory.Create(builder =>
24+
{
25+
builder.AddConsole();
26+
});
27+
2228
var httpClient = new HttpClient(sharedHandler);
2329
// Create the token provider with our custom HttpClient and authorization URL handler
2430
var tokenProvider = new GenericOAuthProvider(
2531
new Uri(serverUrl),
2632
httpClient,
2733
clientId: clientId,
2834
redirectUri: new Uri("http://localhost:1179/callback"),
29-
authorizationRedirectDelegate: HandleAuthorizationUrlAsync);
35+
authorizationRedirectDelegate: HandleAuthorizationUrlAsync,
36+
loggerFactory: consoleLoggerFactory);
3037

3138
Console.WriteLine();
3239
Console.WriteLine($"Connecting to weather server at {serverUrl}...");
@@ -38,9 +45,9 @@
3845
Endpoint = new Uri(serverUrl),
3946
Name = "Secure Weather Client",
4047
CredentialProvider = tokenProvider,
41-
});
48+
}, httpClient, consoleLoggerFactory);
4249

43-
var client = await McpClientFactory.CreateAsync(transport);
50+
var client = await McpClientFactory.CreateAsync(transport, loggerFactory: consoleLoggerFactory);
4451

4552
var tools = await client.ListToolsAsync();
4653
if (tools.Count == 0)

samples/ProtectedMCPClient/ProtectedMCPClient.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
</PropertyGroup>
@@ -11,4 +11,8 @@
1111
<ProjectReference Include="..\..\src\ModelContextProtocol.Core\ModelContextProtocol.Core.csproj" />
1212
</ItemGroup>
1313

14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
16+
</ItemGroup>
17+
1418
</Project>

src/ModelContextProtocol.Core/Authentication/GenericOAuthProvider.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ public GenericOAuthProvider(
7979
/// </summary>
8080
/// <param name="availableServers">List of available authorization servers.</param>
8181
/// <returns>The selected authorization server, or null if none are available.</returns>
82-
private static Uri? DefaultAuthServerSelector(IReadOnlyList<Uri> availableServers)
83-
{
84-
return availableServers.FirstOrDefault();
85-
}
82+
private static Uri? DefaultAuthServerSelector(IReadOnlyList<Uri> availableServers) => availableServers.FirstOrDefault();
8683

8784
/// <summary>
8885
/// Default authorization URL handler that displays the URL to the user for manual input.

0 commit comments

Comments
 (0)