From 1ddbc33b160a09992bf91247e56c879dc2def343 Mon Sep 17 00:00:00 2001 From: geffzhang Date: Mon, 31 Mar 2025 23:23:03 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Arefactor=20mcp=20with=20extensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BotSharp.MCP/BotSharpMCPExtensions.cs | 68 +++++++++++++++++++ src/Infrastructure/BotSharp.MCP/McpPlugin.cs | 61 ----------------- .../BotSharp.MCP/Settings/MCPSettings.cs | 2 +- src/WebStarter/Program.cs | 2 + src/WebStarter/appsettings.json | 35 +++++----- 5 files changed, 88 insertions(+), 80 deletions(-) create mode 100644 src/Infrastructure/BotSharp.MCP/BotSharpMCPExtensions.cs delete mode 100644 src/Infrastructure/BotSharp.MCP/McpPlugin.cs diff --git a/src/Infrastructure/BotSharp.MCP/BotSharpMCPExtensions.cs b/src/Infrastructure/BotSharp.MCP/BotSharpMCPExtensions.cs new file mode 100644 index 00000000..a6fe4e33 --- /dev/null +++ b/src/Infrastructure/BotSharp.MCP/BotSharpMCPExtensions.cs @@ -0,0 +1,68 @@ +using BotSharp.Abstraction.Agents; +using BotSharp.Abstraction.Functions; +using BotSharp.Core.Mcp.Functions; +using BotSharp.Core.Mcp.Settings; +using BotSharp.Core.Mcp; +using BotSharp.MCP.Hooks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using ModelContextProtocol.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ModelContextProtocol.Client; + +namespace BotSharp.MCP; + +public static class BotSharpMCPExtensions +{ + /// + /// Add mcp + /// + /// + /// + /// + public static IServiceCollection AddBotSharpMCP(this IServiceCollection services, + IConfiguration config) + { + var settings = config.GetSection("MCPSettings").Get(); + services.AddScoped(provider => { return settings; }); + if (settings != null) + { + + var clientManager = new MCPClientManager(settings); + services.AddSingleton(clientManager); + + foreach (var server in settings.McpServerConfigs) + { + RegisterFunctionCall(services, server, clientManager) + .ConfigureAwait(false) + .GetAwaiter() + .GetResult(); + } + // Register hooks + services.AddScoped(); + } + return services; + } + + private static async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server, MCPClientManager clientManager) + { + var client = await clientManager.GetMcpClientAsync(server.Id); + var tools = await client.ListToolsAsync(); + + foreach (var tool in tools) + { + services.AddScoped(provider => { return tool; }); + + services.AddScoped(provider => + { + var funcTool = new McpToolAdapter(provider, tool, clientManager); + return funcTool; + }); + } + } +} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.MCP/McpPlugin.cs b/src/Infrastructure/BotSharp.MCP/McpPlugin.cs deleted file mode 100644 index f84e7531..00000000 --- a/src/Infrastructure/BotSharp.MCP/McpPlugin.cs +++ /dev/null @@ -1,61 +0,0 @@ -using BotSharp.Abstraction.Agents; -using BotSharp.Abstraction.Conversations; -using BotSharp.Abstraction.Functions; -using BotSharp.Abstraction.Plugins; -using BotSharp.Core.Mcp.Functions; -using BotSharp.Core.Mcp.Settings; -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; - -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(); - services.AddScoped(provider => { return settings; }); - - clientManager = new MCPClientManager(settings); - services.AddSingleton(clientManager); - - foreach (var server in settings.McpServerConfigs) - { - RegisterFunctionCall(services, server) - .ConfigureAwait(false) - .GetAwaiter() - .GetResult(); - } - // Register hooks - services.AddScoped(); - } - - private async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server) - { - var client = await clientManager.GetMcpClientAsync(server.Id); - var tools = await client.ListToolsAsync(); - - foreach (var tool in tools) - { - services.AddScoped(provider => { return tool; }); - - services.AddScoped(provider => - { - var funcTool = new McpToolAdapter(provider, tool, clientManager); - return funcTool; - }); - } - } -} diff --git a/src/Infrastructure/BotSharp.MCP/Settings/MCPSettings.cs b/src/Infrastructure/BotSharp.MCP/Settings/MCPSettings.cs index 1279a08d..3395bf26 100644 --- a/src/Infrastructure/BotSharp.MCP/Settings/MCPSettings.cs +++ b/src/Infrastructure/BotSharp.MCP/Settings/MCPSettings.cs @@ -8,6 +8,6 @@ public class MCPSettings { public McpClientOptions McpClientOptions { get; set; } - public List McpServerConfigs { get; set; } + public List McpServerConfigs { get; set; } = new(); } diff --git a/src/WebStarter/Program.cs b/src/WebStarter/Program.cs index d4015267..aab6dc0e 100644 --- a/src/WebStarter/Program.cs +++ b/src/WebStarter/Program.cs @@ -5,6 +5,7 @@ using BotSharp.Plugin.ChatHub; using Serilog; using BotSharp.Abstraction.Messaging.JsonConverters; using StackExchange.Redis; +using BotSharp.MCP; var builder = WebApplication.CreateBuilder(args); @@ -23,6 +24,7 @@ string[] allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get options.JsonSerializerOptions.Converters.Add(new RichContentJsonConverter()); options.JsonSerializerOptions.Converters.Add(new TemplateMessageJsonConverter()); }).AddBotSharpOpenAPI(builder.Configuration, allowedOrigins, builder.Environment, true) + .AddBotSharpMCP(builder.Configuration) .AddBotSharpLogger(builder.Configuration); // Add service defaults & Aspire components. diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 37952b50..a82da220 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -171,23 +171,23 @@ "Model": "gpt-4o-mini" } }, - "MCPSettings": { - "McpClientOptions": { - "ClientInfo": { - "Name": "SimpleToolsBotsharp", - "Version": "1.0.0" - } - }, - "McpServerConfigs": [ - { - "Id": "PizzaServer", - "Name": "PizzaServer", - "TransportType": "sse", - "TransportOptions": [], - "Location": "http://localhost:58905/sse" - } - ] - }, + //"MCPSettings": { + // "McpClientOptions": { + // "ClientInfo": { + // "Name": "SimpleToolsBotsharp", + // "Version": "1.0.0" + // } + // }, + // "McpServerConfigs": [ + // { + // "Id": "PizzaServer", + // "Name": "PizzaServer", + // "TransportType": "sse", + // "TransportOptions": [], + // "Location": "http://localhost:58905/sse" + // } + // ] + //}, "Conversation": { "DataDir": "conversations", "ShowVerboseLog": false, @@ -413,7 +413,6 @@ "BotSharp.Core.Crontab", "BotSharp.Core.Realtime", "BotSharp.Logger", - "BotSharp.MCP", "BotSharp.Plugin.MongoStorage", "BotSharp.Plugin.Dashboard", "BotSharp.Plugin.OpenAI",