From 530e01c5d865547686f726f8d2c28aa8a4a604c9 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 1 Apr 2025 10:24:48 -0500 Subject: [PATCH] refine mcp settings --- .../BotSharpMCPExtensions.cs | 4 ++-- .../Hooks/MCPToolAgentHook.cs | 8 ++++++++ .../BotSharp.Core.MCP/Settings/MCPSettings.cs | 1 + .../Instruct/FileInstructService.Image.cs | 19 ------------------- .../Services/Instruct/FileInstructService.cs | 19 +++++++++++++++++++ src/WebStarter/appsettings.json | 1 + 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs b/src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs index 44195f26..1a6ff235 100644 --- a/src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs +++ b/src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs @@ -19,8 +19,8 @@ public static class BotSharpMcpExtensions public static IServiceCollection AddBotSharpMCP(this IServiceCollection services, IConfiguration config) { var settings = config.GetSection("MCP").Get(); - services.AddScoped(provider => { return settings; }); - if (settings != null && !settings.McpServerConfigs.IsNullOrEmpty()) + services.AddScoped(provider => { return settings; }); + if (settings != null && settings.Enabled && !settings.McpServerConfigs.IsNullOrEmpty()) { var clientManager = new McpClientManager(settings); services.AddSingleton(clientManager); diff --git a/src/Infrastructure/BotSharp.Core.MCP/Hooks/MCPToolAgentHook.cs b/src/Infrastructure/BotSharp.Core.MCP/Hooks/MCPToolAgentHook.cs index 40a59e2b..c0ccaaab 100644 --- a/src/Infrastructure/BotSharp.Core.MCP/Hooks/MCPToolAgentHook.cs +++ b/src/Infrastructure/BotSharp.Core.MCP/Hooks/MCPToolAgentHook.cs @@ -1,5 +1,6 @@ using BotSharp.Core.MCP.Helpers; using BotSharp.Core.MCP.Managers; +using BotSharp.Core.MCP.Settings; using ModelContextProtocol.Client; namespace BotSharp.Core.MCP.Hooks; @@ -39,6 +40,13 @@ public class McpToolAgentHook : AgentHookBase private async Task> GetMcpContent(Agent agent) { var functionDefs = new List(); + + var settings = _services.GetRequiredService(); + if (settings?.Enabled != true) + { + return functionDefs; + } + var mcpClientManager = _services.GetRequiredService(); var mcps = agent.McpTools.Where(x => !x.Disabled); foreach (var item in mcps) diff --git a/src/Infrastructure/BotSharp.Core.MCP/Settings/MCPSettings.cs b/src/Infrastructure/BotSharp.Core.MCP/Settings/MCPSettings.cs index 09884f2b..364b0a91 100644 --- a/src/Infrastructure/BotSharp.Core.MCP/Settings/MCPSettings.cs +++ b/src/Infrastructure/BotSharp.Core.MCP/Settings/MCPSettings.cs @@ -5,6 +5,7 @@ namespace BotSharp.Core.MCP.Settings; public class McpSettings { + public bool Enabled { get; set; } = true; public McpClientOptions McpClientOptions { get; set; } public List McpServerConfigs { get; set; } = new(); diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs index 845cdc2d..defd1459 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs @@ -227,23 +227,4 @@ public partial class FileInstructService return message; } - - #region Private methods - private async Task DownloadFile(InstructFileModel file) - { - var bytes = new byte[0]; - if (!string.IsNullOrEmpty(file.FileUrl)) - { - var http = _services.GetRequiredService(); - using var client = http.CreateClient(); - bytes = await client.GetByteArrayAsync(file.FileUrl); - } - else if (!string.IsNullOrEmpty(file.FileData)) - { - (_, bytes) = FileUtility.GetFileInfoFromData(file.FileData); - } - - return bytes; - } - #endregion } diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs index b2da5f43..9ff3b46a 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs @@ -19,6 +19,7 @@ public partial class FileInstructService : IFileInstructService _services = services; } + #region Private methods private void DeleteIfExistDirectory(string? dir, bool createNew = false) { if (_fileStorage.ExistDirectory(dir)) @@ -31,6 +32,23 @@ public partial class FileInstructService : IFileInstructService } } + private async Task DownloadFile(InstructFileModel file) + { + var bytes = new byte[0]; + if (!string.IsNullOrEmpty(file.FileUrl)) + { + var http = _services.GetRequiredService(); + using var client = http.CreateClient(); + bytes = await client.GetByteArrayAsync(file.FileUrl); + } + else if (!string.IsNullOrEmpty(file.FileData)) + { + (_, bytes) = FileUtility.GetFileInfoFromData(file.FileData); + } + + return bytes; + } + private async Task GetAgentTemplate(string agentId, string? templateName) { if (string.IsNullOrWhiteSpace(agentId) || string.IsNullOrWhiteSpace(templateName)) @@ -48,4 +66,5 @@ public partial class FileInstructService : IFileInstructService var instruction = agentService.RenderedTemplate(agent, templateName); return instruction; } + #endregion } diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 4338ba59..c5d82692 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -173,6 +173,7 @@ }, "MCP": { + "Enabled": true, "McpClientOptions": { "ClientInfo": { "Name": "SimpleToolsBotsharp",