2025-02-23 11:45:05 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
2025-02-25 14:57:22 +00:00
|
|
|
using BotSharp.Abstraction.Plugins;
|
2025-02-23 14:55:35 +00:00
|
|
|
using BotSharp.Core.Mcp.Functions;
|
|
|
|
|
using BotSharp.Core.Mcp.Settings;
|
2025-02-23 11:45:05 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2025-02-25 14:57:22 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-02-23 11:45:05 +00:00
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
|
2025-02-23 14:55:35 +00:00
|
|
|
namespace BotSharp.Core.Mcp;
|
2025-02-23 11:45:05 +00:00
|
|
|
|
|
|
|
|
public class McpPlugin : IBotSharpPlugin
|
|
|
|
|
{
|
|
|
|
|
public string Id => "5d779611-0012-46cb-a754-4ca4770e88ac";
|
|
|
|
|
public string Name => "MCP Plugin";
|
|
|
|
|
public string Description => "Integrated MCP tools";
|
|
|
|
|
|
|
|
|
|
private MCPClientManager clientManager;
|
|
|
|
|
|
|
|
|
|
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
var settings = config.GetSection("MCPSettings").Get<MCPSettings>();
|
2025-02-26 04:14:37 +00:00
|
|
|
services.AddScoped<MCPSettings>(provider => { return settings; });
|
|
|
|
|
|
|
|
|
|
clientManager = new MCPClientManager(settings, NullLoggerFactory.Instance);
|
|
|
|
|
services.AddSingleton(clientManager);
|
2025-02-23 11:45:05 +00:00
|
|
|
|
|
|
|
|
foreach (var server in settings.McpServerConfigs)
|
|
|
|
|
{
|
|
|
|
|
var client = clientManager.Factory.GetClientAsync(server.Id).Result;
|
|
|
|
|
var tools = client.ListToolsAsync().Result;
|
|
|
|
|
|
|
|
|
|
foreach (var tool in tools.Tools)
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped<IFunctionCallback>(provider =>
|
|
|
|
|
{
|
2025-02-25 14:57:22 +00:00
|
|
|
var func = new McpToolFunction(tool, client);
|
2025-02-23 11:45:05 +00:00
|
|
|
return func;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|