2025-02-27 04:39:07 +00:00
|
|
|
using BotSharp.Abstraction.Agents;
|
|
|
|
|
using BotSharp.Abstraction.Conversations;
|
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-27 04:39:07 +00:00
|
|
|
using BotSharp.MCP.Hooks;
|
2025-02-23 11:45:05 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2025-02-25 14:57:22 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-02-23 11:45:05 +00:00
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
2025-03-21 05:14:19 +00:00
|
|
|
using ModelContextProtocol.Client;
|
|
|
|
|
using ModelContextProtocol.Configuration;
|
2025-03-18 10:43:31 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2025-02-23 11:45:05 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2025-03-18 10:43:31 +00:00
|
|
|
RegisterFunctionCall(services, server)
|
|
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
2025-02-23 11:45:05 +00:00
|
|
|
}
|
2025-02-27 04:39:07 +00:00
|
|
|
// Register hooks
|
|
|
|
|
services.AddScoped<IAgentHook, MCPToolAgentHook>();
|
2025-03-18 10:43:31 +00:00
|
|
|
services.AddScoped<IConversationHook, MCPResponseHook>();
|
2025-02-23 11:45:05 +00:00
|
|
|
}
|
2025-03-18 10:43:31 +00:00
|
|
|
|
2025-03-21 05:14:19 +00:00
|
|
|
private async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server)
|
2025-03-18 10:43:31 +00:00
|
|
|
{
|
|
|
|
|
var client = await clientManager.GetMcpClientAsync(server.Id);
|
|
|
|
|
var tools = await client.ListToolsAsync().ToListAsync();
|
|
|
|
|
|
|
|
|
|
foreach (var tool in tools)
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped(provider => { return tool; });
|
|
|
|
|
|
|
|
|
|
services.AddScoped<IFunctionCallback>(provider =>
|
|
|
|
|
{
|
2025-03-24 01:18:01 +00:00
|
|
|
var funcTool = new McpToolAdapter(provider, tool, clientManager);
|
2025-03-18 10:43:31 +00:00
|
|
|
return funcTool;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|