Add articles on logging and progress (#717)
* Add articles on logging and progress * Address comments in PR review
This commit is contained in:
parent
52b1ed5fac
commit
7cb3a64c06
9 changed files with 629 additions and 5 deletions
14
docs/concepts/logging/samples/client/LoggingClient.csproj
Normal file
14
docs/concepts/logging/samples/client/LoggingClient.csproj
Normal 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>
|
||||
67
docs/concepts/logging/samples/client/Program.cs
Normal file
67
docs/concepts/logging/samples/client/Program.cs
Normal 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");
|
||||
|
||||
13
docs/concepts/logging/samples/server/Logging.csproj
Normal file
13
docs/concepts/logging/samples/server/Logging.csproj
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ModelContextProtocol.AspNetCore" Version="0.3.0-preview.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
40
docs/concepts/logging/samples/server/Logging.http
Normal file
40
docs/concepts/logging/samples/server/Logging.http
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
@HostAddress = http://localhost:3001
|
||||
|
||||
POST {{HostAddress}}/
|
||||
Accept: application/json, text/event-stream
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "initialize",
|
||||
"params": {
|
||||
"clientInfo": {
|
||||
"name": "RestClient",
|
||||
"version": "0.1.0"
|
||||
},
|
||||
"capabilities": {},
|
||||
"protocolVersion": "2025-06-18"
|
||||
}
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
@SessionId = JCo3W4Q7KA_evyWoFE5qwA
|
||||
|
||||
###
|
||||
|
||||
POST {{HostAddress}}/
|
||||
Accept: application/json, text/event-stream
|
||||
Content-Type: application/json
|
||||
MCP-Protocol-Version: 2025-06-18
|
||||
Mcp-Session-Id: {{SessionId}}
|
||||
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 2,
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": "logging_tool"
|
||||
}
|
||||
}
|
||||
20
docs/concepts/logging/samples/server/Program.cs
Normal file
20
docs/concepts/logging/samples/server/Program.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using Logging.Tools;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddMcpServer()
|
||||
.WithHttpTransport(options =>
|
||||
options.IdleTimeout = Timeout.InfiniteTimeSpan // Never timeout
|
||||
)
|
||||
.WithTools<LoggingTools>();
|
||||
// .WithSetLoggingLevelHandler(async (ctx, ct) => new EmptyResult());
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.MapMcp();
|
||||
|
||||
app.Run();
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:3001",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7207;http://localhost:3001",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
45
docs/concepts/logging/samples/server/Tools/LoggingTools.cs
Normal file
45
docs/concepts/logging/samples/server/Tools/LoggingTools.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using System.ComponentModel;
|
||||
using ModelContextProtocol.Protocol;
|
||||
using ModelContextProtocol.Server;
|
||||
|
||||
namespace Logging.Tools;
|
||||
|
||||
[McpServerToolType]
|
||||
public class LoggingTools
|
||||
{
|
||||
[McpServerTool, Description("Demonstrates a tool that produces log messages")]
|
||||
public static async Task<string> LoggingTool(
|
||||
RequestContext<CallToolRequestParams> context,
|
||||
int duration = 10,
|
||||
int steps = 10)
|
||||
{
|
||||
var progressToken = context.Params?.ProgressToken;
|
||||
var stepDuration = duration / steps;
|
||||
|
||||
// <snippet_LoggingConfiguration >
|
||||
ILoggerProvider loggerProvider = context.Server.AsClientLoggerProvider();
|
||||
ILogger logger = loggerProvider.CreateLogger("LoggingTools");
|
||||
// </snippet_LoggingConfiguration>
|
||||
|
||||
for (int i = 1; i <= steps; i++)
|
||||
{
|
||||
await Task.Delay(stepDuration * 1000);
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogCritical("A critial log message");
|
||||
logger.LogError("An error log message");
|
||||
logger.LogWarning("A warning log message");
|
||||
logger.LogInformation("An informational log message");
|
||||
logger.LogDebug("A debug log message");
|
||||
logger.LogTrace("A trace log message");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "An error occurred while logging messages");
|
||||
}
|
||||
}
|
||||
|
||||
return $"Long running tool completed. Duration: {duration} seconds. Steps: {steps}.";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
docs/concepts/logging/samples/server/appsettings.json
Normal file
9
docs/concepts/logging/samples/server/appsettings.json
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Loading…
Reference in a new issue