2025-02-25 14:57:22 +00:00
|
|
|
using BotSharp.Abstraction.Functions.Models;
|
2025-03-29 02:05:52 +00:00
|
|
|
using ModelContextProtocol.Client;
|
2025-03-21 05:14:19 +00:00
|
|
|
using ModelContextProtocol.Protocol.Types;
|
2025-02-23 14:55:35 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.MCP;
|
|
|
|
|
|
|
|
|
|
internal static class AIFunctionUtilities
|
|
|
|
|
{
|
2025-03-29 02:05:52 +00:00
|
|
|
public static FunctionDef MapToFunctionDef(McpClientTool tool)
|
2025-02-23 14:55:35 +00:00
|
|
|
{
|
|
|
|
|
if (tool == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(tool));
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-29 02:05:52 +00:00
|
|
|
var properties = tool.JsonSchema.GetProperty("properties");
|
|
|
|
|
var required = tool.JsonSchema.GetProperty("required");
|
2025-02-23 14:55:35 +00:00
|
|
|
|
|
|
|
|
FunctionDef funDef = new FunctionDef
|
|
|
|
|
{
|
|
|
|
|
Name = tool.Name,
|
2025-03-21 05:14:19 +00:00
|
|
|
Description = tool.Description ?? string.Empty,
|
2025-02-23 14:55:35 +00:00
|
|
|
Type = "function",
|
|
|
|
|
Parameters = new FunctionParametersDef
|
|
|
|
|
{
|
|
|
|
|
Type = "object",
|
2025-03-21 05:14:19 +00:00
|
|
|
Properties = JsonDocument.Parse(properties.GetRawText()),
|
|
|
|
|
Required = JsonSerializer.Deserialize<List<string>>(required.GetRawText())
|
2025-02-23 14:55:35 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return funDef;
|
|
|
|
|
}
|
|
|
|
|
}
|