|
1 |
| -using System.Threading.Tasks; |
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
2 | 4 | using Microsoft.Extensions.DependencyInjection;
|
3 | 5 | using Microsoft.Extensions.Logging;
|
| 6 | +using ModelContextProtocol.NET.Core.Models.Protocol.Client.Responses; |
4 | 7 | using ModelContextProtocol.NET.Core.Models.Protocol.Common;
|
| 8 | +using ModelContextProtocol.NET.Core.Models.Protocol.Shared.Content; |
5 | 9 | using ModelContextProtocol.NET.Demo.Calculator.Handlers;
|
6 | 10 | using ModelContextProtocol.NET.Server.Builder;
|
7 | 11 | using Serilog;
|
|
25 | 29 | builder.SetMinimumLevel(LogLevel.Trace);
|
26 | 30 | })
|
27 | 31 | )
|
28 |
| - .ConfigureTools(tools => tools.AddHandler<CalculatorToolHandler>()) |
| 32 | + .ConfigureTools(tools => |
| 33 | + tools |
| 34 | + //.AddHandler<CalculatorToolHandler>() |
| 35 | + .AddFunction( |
| 36 | + name: "Calculator_function_flavor", |
| 37 | + description: "Still performs basic arithmetic operations, but implemented in functional style", |
| 38 | + parameterTypeInfo: CalculatorParametersJsonContext.Default.CalculatorParameters, |
| 39 | + handler: (CalculatorParameters parameters, CancellationToken ct) => |
| 40 | + Task.FromResult( |
| 41 | + new CallToolResult |
| 42 | + { |
| 43 | + Content = (TextContent) |
| 44 | + ( |
| 45 | + (double)( |
| 46 | + parameters.Operation switch |
| 47 | + { |
| 48 | + CalculatorOperation.Add => parameters.A + parameters.B, |
| 49 | + CalculatorOperation.Subtract => parameters.A - parameters.B, |
| 50 | + CalculatorOperation.Multiply => parameters.A * parameters.B, |
| 51 | + CalculatorOperation.Divide when parameters.B != 0 |
| 52 | + => parameters.A / parameters.B, |
| 53 | + CalculatorOperation.Divide |
| 54 | + => throw new DivideByZeroException( |
| 55 | + "Cannot divide by zero" |
| 56 | + ), |
| 57 | + _ |
| 58 | + => throw new ArgumentException( |
| 59 | + $"Unknown operation: {parameters.Operation}" |
| 60 | + ), |
| 61 | + } |
| 62 | + ) |
| 63 | + ).ToString() |
| 64 | + } |
| 65 | + ) |
| 66 | + ) |
| 67 | + ) |
29 | 68 | .Build();
|
30 | 69 |
|
31 | 70 | try
|
|
0 commit comments