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
7 changed files with 629 additions and 5 deletions
22
docs/concepts/progress/samples/server/Program.cs
Normal file
22
docs/concepts/progress/samples/server/Program.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using Progress.Tools;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddMcpServer()
|
||||
.WithHttpTransport()
|
||||
.WithTools<LongRunningTools>();
|
||||
|
||||
builder.Logging.AddConsole(options =>
|
||||
{
|
||||
options.LogToStandardErrorThreshold = LogLevel.Information;
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.MapMcp();
|
||||
|
||||
app.Run();
|
||||
13
docs/concepts/progress/samples/server/Progress.csproj
Normal file
13
docs/concepts/progress/samples/server/Progress.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>
|
||||
18
docs/concepts/progress/samples/server/Progress.http
Normal file
18
docs/concepts/progress/samples/server/Progress.http
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
@HostAddress = http://localhost:3001
|
||||
|
||||
POST {{HostAddress}}/
|
||||
Accept: application/json, text/event-stream
|
||||
Content-Type: application/json
|
||||
MCP-Protocol-Version: 2025-06-18
|
||||
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 2,
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"_meta": {
|
||||
"progressToken": "abc123"
|
||||
},
|
||||
"name": "long_running_tool"
|
||||
}
|
||||
}
|
||||
|
|
@ -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:7175;http://localhost:3001",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
using System.ComponentModel;
|
||||
using ModelContextProtocol;
|
||||
using ModelContextProtocol.Protocol;
|
||||
using ModelContextProtocol.Server;
|
||||
|
||||
namespace Progress.Tools;
|
||||
|
||||
[McpServerToolType]
|
||||
public class LongRunningTools
|
||||
{
|
||||
[McpServerTool, Description("Demonstrates a long running tool with progress updates")]
|
||||
public static async Task<string> LongRunningTool(
|
||||
IMcpServer server,
|
||||
RequestContext<CallToolRequestParams> context,
|
||||
int duration = 10,
|
||||
int steps = 5)
|
||||
{
|
||||
var progressToken = context.Params?.ProgressToken;
|
||||
var stepDuration = duration / steps;
|
||||
|
||||
for (int i = 1; i <= steps; i++)
|
||||
{
|
||||
await Task.Delay(stepDuration * 1000);
|
||||
|
||||
// <snippet_SendProgress >
|
||||
if (progressToken is not null)
|
||||
{
|
||||
await server.SendNotificationAsync("notifications/progress", new ProgressNotificationParams
|
||||
{
|
||||
ProgressToken = progressToken.Value,
|
||||
Progress = new ProgressNotificationValue
|
||||
{
|
||||
Progress = i,
|
||||
Total = steps,
|
||||
Message = $"Step {i} of {steps} completed.",
|
||||
},
|
||||
});
|
||||
}
|
||||
// </snippet_SendProgress >
|
||||
}
|
||||
|
||||
return $"Long running tool completed. Duration: {duration} seconds. Steps: {steps}.";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
docs/concepts/progress/samples/server/appsettings.json
Normal file
9
docs/concepts/progress/samples/server/appsettings.json
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Loading…
Reference in a new issue