-
Notifications
You must be signed in to change notification settings - Fork 680
Expand file tree
/
Copy pathLongRunningTools.cs
More file actions
44 lines (39 loc) · 1.38 KB
/
LongRunningTools.cs
File metadata and controls
44 lines (39 loc) · 1.38 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
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(
McpServer 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}.";
}
}