BotSharp/src/Infrastructure/BotSharp.MCP/McpPlugin.cs

63 lines
2.1 KiB
C#
Raw Normal View History

2025-02-27 04:39:07 +00:00
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Functions;
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;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using ModelContextProtocol.Client;
using ModelContextProtocol.Configuration;
using System.Linq;
using System.Threading.Tasks;
2025-02-23 14:55:35 +00:00
namespace BotSharp.Core.Mcp;
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);
foreach (var server in settings.McpServerConfigs)
{
RegisterFunctionCall(services, server)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
}
2025-02-27 04:39:07 +00:00
// Register hooks
services.AddScoped<IAgentHook, MCPToolAgentHook>();
services.AddScoped<IConversationHook, MCPResponseHook>();
}
private async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server)
{
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 =>
{
var funcTool = new McpToolAdapter(provider, tool, clientManager);
return funcTool;
});
}
}
}