|
1 | | -//using ModelContextProtocol.Server; |
2 | | -//using System; |
3 | | -//using System.Collections.Generic; |
4 | | -//using System.ComponentModel; |
5 | | -//using System.Linq; |
6 | | -//using System.Text; |
7 | | -//using System.Threading.Tasks; |
| 1 | +using ModelContextProtocol.Protocol.Messages; |
| 2 | +using ModelContextProtocol.Protocol.Types; |
| 3 | +using ModelContextProtocol.Server; |
| 4 | +using System.ComponentModel; |
8 | 5 |
|
9 | | -//namespace EverythingServer.Tools; |
| 6 | +namespace EverythingServer.Tools; |
10 | 7 |
|
11 | | -//[McpServerToolType] |
12 | | -//public static class LongRunningTool |
13 | | -//{ |
14 | | -// [McpServerTool, Description("Demonstrates a long running operation with progress updates")] |
15 | | -// public static async Task<string> LongRunningOperation(int duration, int steps) |
16 | | -// { |
17 | | -// var stepDuration = duration / steps; |
| 8 | +[McpServerToolType] |
| 9 | +public static class LongRunningTool |
| 10 | +{ |
| 11 | + [McpServerTool("longRunningOperation"), Description("Demonstrates a long running operation with progress updates")] |
| 12 | + public static async Task<string> LongRunningOperation( |
| 13 | + IMcpServer server, |
| 14 | + RequestContext<CallToolRequestParams> context, |
| 15 | + int duration = 10, |
| 16 | + int steps = 5) |
| 17 | + { |
| 18 | + var progressToken = context.Params?.Meta?.ProgressToken; |
| 19 | + var stepDuration = duration / steps; |
18 | 20 |
|
19 | | -// for (int i = 1; i <= steps + 1; i++) |
20 | | -// { |
21 | | -// // Simulate a long-running operation |
22 | | -// await Task.Delay(stepDuration); |
23 | | -// // Report progress |
24 | | -// var progress = (i * 100) / steps; |
25 | | -// Console.WriteLine($"Progress: {progress}%"); |
26 | | -// } |
27 | | -// } |
28 | | -//} |
| 21 | + for (int i = 1; i <= steps + 1; i++) |
| 22 | + { |
| 23 | + await Task.Delay(stepDuration * 1000); |
| 24 | + |
| 25 | + if (progressToken is not null) |
| 26 | + { |
| 27 | + await server.SendMessageAsync(new JsonRpcNotification |
| 28 | + { |
| 29 | + Method = "notifications/progress", |
| 30 | + Params = new |
| 31 | + { |
| 32 | + Progress = i, |
| 33 | + Total = steps, |
| 34 | + progressToken |
| 35 | + } |
| 36 | + }); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return $"Long running operation completed. Duration: {duration} seconds. Steps: {steps}."; |
| 41 | + } |
| 42 | +} |
0 commit comments