Add articles on logging and progress (#717)

* Add articles on logging and progress
* Address comments in PR review
This commit is contained in:
Mike Kistler 2025-08-25 20:51:41 -05:00 • committed by GitHub
commit 7cb3a64c06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 629 additions and 5 deletions

View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ModelContextProtocol.Core" Version="0.3.0-preview.3" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,67 @@
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Client;
using System.Text.Json;
var endpoint = Environment.GetEnvironmentVariable("ENDPOINT") ?? "http://localhost:3001";
var clientTransport = new SseClientTransport(new()
{
Endpoint = new Uri(endpoint),
TransportMode = HttpTransportMode.StreamableHttp,
});
await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport);
// <snippet_LoggingCapabilities>
// Verify that the server supports logging
if (mcpClient.ServerCapabilities.Logging is null)
{
Console.WriteLine("Server does not support logging.");
return;
}
// </snippet_LoggingCapabilities>
// Get the first argument if it was specified
var firstArgument = args.Length > 0 ? args[0] : null;
if (firstArgument is not null)
{
// Set the logging level to the value from the first argument
if (Enum.TryParse<LoggingLevel>(firstArgument, true, out var loggingLevel))
{
// <snippet_LoggingLevel>
await mcpClient.SetLoggingLevel(loggingLevel);
// </snippet_LoggingLevel>
}
else
{
Console.WriteLine($"Invalid logging level: {firstArgument}");
// Print a list of valid logging levels
Console.WriteLine("Valid logging levels are:");
foreach (var level in Enum.GetValues<LoggingLevel>())
{
Console.WriteLine($" - {level}");
}
}
}
// <snippet_LoggingHandler>
mcpClient.RegisterNotificationHandler(NotificationMethods.LoggingMessageNotification,
(notification, ct) =>
{
if (JsonSerializer.Deserialize<LoggingMessageNotificationParams>(notification.Params) is { } ln)
{
Console.WriteLine($"[{ln.Level}] {ln.Logger} {ln.Data}");
}
else
{
Console.WriteLine($"Received unexpected logging notification: {notification.Params}");
}
return default;
});
// </snippet_LoggingHandler>
// Now call the "logging_tool" tool
await mcpClient.CallToolAsync("logging_tool");