refine mcp

This commit is contained in:
Jicheng Lu 2025-04-15 11:28:37 -05:00
parent 8acdbb7ba4
commit 7470953a8e
10 changed files with 60 additions and 14 deletions

View file

@ -2,6 +2,7 @@ namespace BotSharp.Abstraction.Functions;
public interface IFunctionCallback
{
string Provider => "Botsharp";
string Name { get; }
/// <summary>

View file

@ -0,0 +1,19 @@
namespace BotSharp.Abstraction.MCP.Models;
public class McpServerOptionModel : IdName
{
public IEnumerable<string> Tools { get; set; } = [];
public McpServerOptionModel() : base()
{
}
public McpServerOptionModel(
string id,
string name,
IEnumerable<string> tools) : base(id, name)
{
Tools = tools ?? [];
}
}

View file

@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.MCP.Services;
public interface IMcpService
{
IEnumerable<McpServerConfigModel> GetServerConfigs() => [];
IEnumerable<McpServerOptionModel> GetServerConfigs() => [];
}

View file

@ -8,6 +8,11 @@ public class IdName
[JsonPropertyName("name")]
public string Name { get; set; } = default!;
public IdName()
{
}
public IdName(string id, string name)
{
Id = id;

View file

@ -52,7 +52,7 @@ public static class BotSharpMcpExtensions
services.AddScoped<IFunctionCallback>(provider =>
{
var funcTool = new McpToolAdapter(provider, tool, clientManager);
var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager);
return funcTool;
});
}

View file

@ -6,17 +6,24 @@ namespace BotSharp.Core.MCP.Functions;
public class McpToolAdapter : IFunctionCallback
{
private readonly string _provider;
private readonly McpClientTool _tool;
private readonly McpClientManager _clientManager;
private readonly IServiceProvider _services;
public McpToolAdapter(IServiceProvider services, McpClientTool tool, McpClientManager client)
public McpToolAdapter(
IServiceProvider services,
string serverName,
McpClientTool tool,
McpClientManager client)
{
_services = services ?? throw new ArgumentNullException(nameof(services));
_tool = tool ?? throw new ArgumentNullException(nameof(tool));
_clientManager = client ?? throw new ArgumentNullException(nameof(client));
_provider = serverName;
}
public string Provider => _provider;
public string Name => _tool.Name;
public async Task<bool> Execute(RoleDialogModel message)

View file

@ -16,14 +16,26 @@ public class McpService : IMcpService
_logger = logger;
}
public IEnumerable<McpServerConfigModel> GetServerConfigs()
public IEnumerable<McpServerOptionModel> GetServerConfigs()
{
var options = new List<McpServerOptionModel>();
var settings = _services.GetRequiredService<McpSettings>();
var configs = settings?.McpServerConfigs ?? [];
return configs.Select(x => new McpServerConfigModel
foreach (var config in configs)
{
Id = x.Id,
Name = x.Name
});
var tools = _services.GetServices<IFunctionCallback>()
.Where(x => x.Provider == config.Name)
.Select(x => x.Name);
options.Add(new McpServerOptionModel
{
Id = config.Id,
Name = config.Name,
Tools = tools
});
}
return options;
}
}

View file

@ -13,7 +13,7 @@ public class McpController : ControllerBase
}
[HttpGet("/mcp/server-configs")]
public IEnumerable<McpServerConfigModel> GetMcpServerConfigs()
public IEnumerable<McpServerOptionModel> GetMcpServerConfigs()
{
var mcp = _services.GetRequiredService<IMcpService>();
return mcp.GetServerConfigs();

View file

@ -1,3 +1,4 @@
using ModelContextProtocol;
using ModelContextProtocol.Server;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
@ -16,11 +17,11 @@ public static class PizzaPrices
{
if (pizza_type is null)
{
throw new McpServerException("Missing required argument 'pizza_type'");
throw new McpException("Missing required argument 'pizza_type'");
}
if (quantity <= 0)
{
throw new McpServerException("Missing required argument 'quantity'");
throw new McpException("Missing required argument 'quantity'");
}
double unit_price = 0;
if (pizza_type.ToString() == "Pepperoni Pizza")

View file

@ -1,3 +1,4 @@
using ModelContextProtocol;
using ModelContextProtocol.Server;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
@ -15,15 +16,15 @@ public static class PlaceOrder
{
if (pizza_type is null)
{
throw new McpServerException("Missing required argument 'pizza_type'");
throw new McpException("Missing required argument 'pizza_type'");
}
if (quantity <= 0)
{
throw new McpServerException("Missing required argument 'quantity'");
throw new McpException("Missing required argument 'quantity'");
}
if (unit_price < 0)
{
throw new McpServerException("Missing required argument 'unit_price'");
throw new McpException("Missing required argument 'unit_price'");
}
return "The order number is P123-01: {order_number = \"P123-01\" }";