csharp-sdk/samples/anthropic/tools/ToolsConsole/McpToolExtensions.cs
Stephen Toub 3de19d38e4
Change type of Tool.InputSchema to be JsonElement (#4)
* Change type of Tool.InputSchema to be JsonElement

* Add schema validation in the InputSchema property setter.

* Simplify AIFunction tool adapter.

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
2025-03-20 19:16:41 +02:00

37 lines
No EOL
1.1 KiB
C#

using Anthropic.SDK.Common;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace ModelContextProtocol;
public static class McpToolExtensions
{
public static IList<Anthropic.SDK.Common.Tool> ToAnthropicTools(this IEnumerable<ModelContextProtocol.Protocol.Types.Tool> tools)
{
if (tools is null)
{
throw new ArgumentNullException(nameof(tools));
}
List<Anthropic.SDK.Common.Tool> result = [];
foreach (var tool in tools)
{
var function = new Function(tool.Name, tool.Description, JsonSerializer.SerializeToNode(tool.InputSchema));
result.Add(function);
}
return result;
}
public static Dictionary<string, object>? ToMCPArguments(this JsonNode jsonNode)
{
if (jsonNode == null)
return null;
// Convert JsonNode to Dictionary<string, object>
return jsonNode.AsObject()
.ToDictionary(
prop => prop.Key,
prop => JsonSerializer.Deserialize<object>(prop.Value)!
);
}
}