refine mcp settings

This commit is contained in:
Jicheng Lu 2025-04-01 10:24:48 -05:00
parent 832472feab
commit 530e01c5d8
6 changed files with 31 additions and 21 deletions

View file

@ -19,8 +19,8 @@ public static class BotSharpMcpExtensions
public static IServiceCollection AddBotSharpMCP(this IServiceCollection services, IConfiguration config)
{
var settings = config.GetSection("MCP").Get<McpSettings>();
services.AddScoped<McpSettings>(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);

View file

@ -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<IEnumerable<FunctionDef>> GetMcpContent(Agent agent)
{
var functionDefs = new List<FunctionDef>();
var settings = _services.GetRequiredService<McpSettings>();
if (settings?.Enabled != true)
{
return functionDefs;
}
var mcpClientManager = _services.GetRequiredService<McpClientManager>();
var mcps = agent.McpTools.Where(x => !x.Disabled);
foreach (var item in mcps)

View file

@ -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<McpServerConfig> McpServerConfigs { get; set; } = new();

View file

@ -227,23 +227,4 @@ public partial class FileInstructService
return message;
}
#region Private methods
private async Task<byte[]> DownloadFile(InstructFileModel file)
{
var bytes = new byte[0];
if (!string.IsNullOrEmpty(file.FileUrl))
{
var http = _services.GetRequiredService<IHttpClientFactory>();
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
}

View file

@ -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<byte[]> DownloadFile(InstructFileModel file)
{
var bytes = new byte[0];
if (!string.IsNullOrEmpty(file.FileUrl))
{
var http = _services.GetRequiredService<IHttpClientFactory>();
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<string?> 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
}

View file

@ -173,6 +173,7 @@
},
"MCP": {
"Enabled": true,
"McpClientOptions": {
"ClientInfo": {
"Name": "SimpleToolsBotsharp",