fix startup and use camel case

This commit is contained in:
Jicheng Lu 2025-03-31 20:59:19 -05:00
parent acf1ed9cbc
commit adfb8f4fc4
23 changed files with 112 additions and 95 deletions

View file

@ -60,7 +60,7 @@ public abstract class AgentHookBase : IAgentHook
}
public virtual void OnAgentMCPToolLoaded(Agent agent)
public virtual void OnAgentMcpToolLoaded(Agent agent)
{
}

View file

@ -27,7 +27,7 @@ public interface IAgentHook
void OnAgentUtilityLoaded(Agent agent);
void OnAgentMCPToolLoaded(Agent agent);
void OnAgentMcpToolLoaded(Agent agent);
/// <summary>
/// Triggered when agent is loaded completely.

View file

@ -107,7 +107,7 @@ public class Agent
/// <summary>
/// Agent MCP Tools
/// </summary>
public List<MCPTool> McpTools { get; set; } = new();
public List<McpTool> McpTools { get; set; } = new();
/// <summary>
/// Agent rules
@ -305,7 +305,7 @@ public class Agent
return this;
}
public Agent SetMcps(List<MCPTool> mcps)
public Agent SetMcpTools(List<McpTool>? mcps)
{
McpTools = mcps ?? [];
return this;

View file

@ -1,22 +1,22 @@
namespace BotSharp.Abstraction.Agents.Models;
public class MCPTool
public class McpTool
{
public string Name { get; set; }
public string ServerId { get; set; }
public bool Disabled { get; set; }
public IEnumerable<MCPFunction> Functions { get; set; } = [];
public IEnumerable<McpFunction> Functions { get; set; } = [];
public MCPTool()
public McpTool()
{
}
public MCPTool(
public McpTool(
string name,
string serverId,
bool disabled = false,
IEnumerable<MCPFunction>? functions = null)
IEnumerable<McpFunction>? functions = null)
{
Name = name;
ServerId = serverId;
@ -31,11 +31,11 @@ public class MCPTool
}
public class MCPFunction
public class McpFunction
{
public string Name { get; set; }
public MCPFunction(string name)
public McpFunction(string name)
{
Name = name;
}

View file

@ -3,4 +3,6 @@ namespace BotSharp.Abstraction.Plugins;
public class PluginSettings
{
public string[] Assemblies { get; set; } = new string[0];
public string[] ExcludedFunctions { get; set; } = new string[0];
}

View file

@ -4,10 +4,11 @@ using BotSharp.Core.MCP.Hooks;
using Microsoft.Extensions.Configuration;
using ModelContextProtocol.Configuration;
using ModelContextProtocol.Client;
using BotSharp.Core.MCP.Managers;
namespace BotSharp.Core.MCP;
public static class BotSharpMCPExtensions
public static class BotSharpMcpExtensions
{
/// <summary>
/// Add mcp
@ -17,11 +18,11 @@ public static class BotSharpMCPExtensions
/// <returns></returns>
public static IServiceCollection AddBotSharpMCP(this IServiceCollection services, IConfiguration config)
{
var settings = config.GetSection("MCPSettings").Get<MCPSettings>();
services.AddScoped<MCPSettings>(provider => { return settings; });
if (settings != null)
var settings = config.GetSection("MCPSettings").Get<McpSettings>();
services.AddScoped<McpSettings>(provider => { return settings; });
if (settings != null && !settings.McpServerConfigs.IsNullOrEmpty())
{
var clientManager = new MCPClientManager(settings);
var clientManager = new McpClientManager(settings);
services.AddSingleton(clientManager);
foreach (var server in settings.McpServerConfigs)
@ -31,13 +32,14 @@ public static class BotSharpMCPExtensions
.GetAwaiter()
.GetResult();
}
// Register hooks
services.AddScoped<IAgentHook, MCPToolAgentHook>();
services.AddScoped<IAgentHook, McpToolAgentHook>();
}
return services;
}
private static async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server, MCPClientManager clientManager)
private static async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server, McpClientManager clientManager)
{
var client = await clientManager.GetMcpClientAsync(server.Id);
var tools = await client.ListToolsAsync();

View file

@ -1,4 +1,5 @@
using System.Text.Json;
using BotSharp.Core.MCP.Managers;
using ModelContextProtocol.Client;
namespace BotSharp.Core.MCP.Functions;
@ -6,10 +7,10 @@ namespace BotSharp.Core.MCP.Functions;
public class McpToolAdapter : IFunctionCallback
{
private readonly McpClientTool _tool;
private readonly MCPClientManager _clientManager;
private readonly McpClientManager _clientManager;
private readonly IServiceProvider _services;
public McpToolAdapter(IServiceProvider services, McpClientTool tool, MCPClientManager client)
public McpToolAdapter(IServiceProvider services, McpClientTool tool, McpClientManager client)
{
_services = services ?? throw new ArgumentNullException(nameof(services));
_tool = tool ?? throw new ArgumentNullException(nameof(tool));

View file

@ -1,10 +1,9 @@
using System.Text.Json;
using ModelContextProtocol.Client;
namespace BotSharp.Core.MCP;
namespace BotSharp.Core.MCP.Helpers;
internal static class AIFunctionUtilities
internal static class AiFunctionHelper
{
public static FunctionDef MapToFunctionDef(McpClientTool tool)
{

View file

@ -1,17 +1,19 @@
using BotSharp.Core.MCP.Helpers;
using BotSharp.Core.MCP.Managers;
using ModelContextProtocol.Client;
namespace BotSharp.Core.MCP.Hooks;
public class MCPToolAgentHook : AgentHookBase
public class McpToolAgentHook : AgentHookBase
{
public override string SelfId => string.Empty;
public MCPToolAgentHook(IServiceProvider services, AgentSettings settings)
public McpToolAgentHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
}
public override void OnAgentMCPToolLoaded(Agent agent)
public override void OnAgentMcpToolLoaded(Agent agent)
{
if (agent.Type == AgentType.Routing)
{
@ -24,7 +26,7 @@ public class MCPToolAgentHook : AgentHookBase
agent.SecondaryFunctions ??= [];
var functions = GetMCPContent(agent).Result;
var functions = GetMcpContent(agent).Result;
foreach (var fn in functions)
{
if (!agent.SecondaryFunctions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
@ -34,11 +36,11 @@ public class MCPToolAgentHook : AgentHookBase
}
}
private async Task<IEnumerable<FunctionDef>> GetMCPContent(Agent agent)
private async Task<IEnumerable<FunctionDef>> GetMcpContent(Agent agent)
{
var functionDefs = new List<FunctionDef>();
var mcpClientManager = _services.GetRequiredService<MCPClientManager>();
var mcps = agent.McpTools;
var mcpClientManager = _services.GetRequiredService<McpClientManager>();
var mcps = agent.McpTools.Where(x => !x.Disabled);
foreach (var item in mcps)
{
var mcpClient = await mcpClientManager.GetMcpClientAsync(item.ServerId);
@ -48,7 +50,7 @@ public class MCPToolAgentHook : AgentHookBase
var toolnames = item.Functions.Select(x => x.Name).ToList();
foreach (var tool in tools.Where(x => toolnames.Contains(x.Name, StringComparer.OrdinalIgnoreCase)))
{
var funDef = AIFunctionUtilities.MapToFunctionDef(tool);
var funDef = AiFunctionHelper.MapToFunctionDef(tool);
functionDefs.Add(funDef);
}
}

View file

@ -1,26 +0,0 @@
using BotSharp.Core.MCP.Settings;
using ModelContextProtocol.Client;
namespace BotSharp.Core.MCP;
public class MCPClientManager : IDisposable
{
private readonly MCPSettings mcpSettings;
public MCPClientManager(MCPSettings settings)
{
mcpSettings = settings;
}
public async Task<IMcpClient> GetMcpClientAsync(string serverId)
{
return await McpClientFactory.CreateAsync(mcpSettings.McpServerConfigs
.Where(x=> x.Name == serverId).First(), mcpSettings.McpClientOptions);
}
public void Dispose()
{
}
}

View file

@ -0,0 +1,27 @@
using BotSharp.Core.MCP.Settings;
using ModelContextProtocol.Client;
namespace BotSharp.Core.MCP.Managers;
public class McpClientManager : IDisposable
{
private readonly McpSettings mcpSettings;
public McpClientManager(McpSettings settings)
{
mcpSettings = settings;
}
public async Task<IMcpClient> GetMcpClientAsync(string serverId)
{
return await McpClientFactory.CreateAsync(
mcpSettings.McpServerConfigs.Where(x=> x.Name == serverId).First(),
mcpSettings.McpClientOptions);
}
public void Dispose()
{
}
}

View file

@ -3,7 +3,7 @@ using ModelContextProtocol.Configuration;
namespace BotSharp.Core.MCP.Settings;
public class MCPSettings
public class McpSettings
{
public McpClientOptions McpClientOptions { get; set; }
public List<McpServerConfig> McpServerConfigs { get; set; } = new();

View file

@ -74,7 +74,7 @@ public partial class AgentService
if(!agent.McpTools.IsNullOrEmpty())
{
hook.OnAgentMCPToolLoaded(agent);
hook.OnAgentMcpToolLoaded(agent);
}
hook.OnAgentLoaded(agent);

View file

@ -107,7 +107,7 @@ public partial class AgentService
.SetResponses(foundAgent.Responses)
.SetSamples(foundAgent.Samples)
.SetUtilities(foundAgent.Utilities)
.SetMcps(foundAgent.McpTools)
.SetMcpTools(foundAgent.McpTools)
.SetKnowledgeBases(foundAgent.KnowledgeBases)
.SetRules(foundAgent.Rules)
.SetLlmConfig(foundAgent.LlmConfig);

View file

@ -135,6 +135,8 @@ public static class BotSharpCoreExtensions
{
var pluginSettings = new PluginSettings();
config.Bind("PluginLoader", pluginSettings);
var excludedFunctions = pluginSettings.ExcludedFunctions ?? [];
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
@ -161,8 +163,9 @@ public static class BotSharpCoreExtensions
// Register function callback
var functions = assembly.GetTypes()
.Where(x => x.IsClass)
.Where(x => x.GetInterface(nameof(IFunctionCallback)) != null)
.Where(x => x.IsClass
&& x.GetInterface(nameof(IFunctionCallback)) != null
&& !excludedFunctions.Contains(x.Name))
.ToArray();
foreach (var function in functions)

View file

@ -194,7 +194,7 @@ namespace BotSharp.Core.Repository
File.WriteAllText(agentFile, json);
}
private void UpdateAgentMcpTools(string agentId, List<MCPTool> mcptools)
private void UpdateAgentMcpTools(string agentId, List<McpTool> mcptools)
{
if (mcptools == null) return;

View file

@ -55,7 +55,7 @@ public class AgentCreationModel
public int? MaxMessageCount { get; set; }
public List<AgentUtility> Utilities { get; set; } = new();
public List<MCPTool> McpTools { get; set; } = new();
public List<McpTool> McpTools { get; set; } = new();
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new();
public List<AgentKnowledgeBase> KnowledgeBases { get; set; } = new();
public List<AgentRule> Rules { get; set; } = new();

View file

@ -42,7 +42,7 @@ public class AgentUpdateModel
/// <summary>
/// McpTools
/// </summary>
public List<MCPTool>? McpTools { get; set; }
public List<McpTool>? McpTools { get; set; }
/// <summary>
/// knowledge bases

View file

@ -24,7 +24,9 @@ public class AgentViewModel
[JsonPropertyName("merge_utility")]
public bool MergeUtility { get; set; }
public List<AgentUtility> Utilities { get; set; }
public List<MCPTool> McpTools { get; set; }
[JsonPropertyName("mcp_tools")]
public List<McpTool> McpTools { get; set; }
[JsonPropertyName("knowledge_bases")]
public List<AgentKnowledgeBase> KnowledgeBases { get; set; }

View file

@ -18,7 +18,7 @@ public class AgentDocument : MongoBase
public List<AgentResponseMongoElement> Responses { get; set; }
public List<string> Samples { get; set; }
public List<AgentUtilityMongoElement> Utilities { get; set; }
public List<AgentMCPToolMongoElement> McpTools { get; set; }
public List<AgentMcpToolMongoElement> McpTools { get; set; }
public List<AgentKnowledgeBaseMongoElement> KnowledgeBases { get; set; }
public List<string> Profiles { get; set; }
public List<string> Labels { get; set; }

View file

@ -3,16 +3,16 @@ using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Plugin.MongoStorage.Models;
[BsonIgnoreExtraElements(Inherited = true)]
public class AgentMCPToolMongoElement
public class AgentMcpToolMongoElement
{
public string Name { get; set; }
public string ServerId { get; set; }
public bool Disabled { get; set; }
public List<McpFunctionMongoElement> Functions { get; set; } = [];
public static AgentMCPToolMongoElement ToMongoElement(MCPTool tool)
public static AgentMcpToolMongoElement ToMongoElement(McpTool tool)
{
return new AgentMCPToolMongoElement
return new AgentMcpToolMongoElement
{
Name = tool.Name,
ServerId = tool.ServerId,
@ -21,14 +21,14 @@ public class AgentMCPToolMongoElement
};
}
public static MCPTool ToDomainElement(AgentMCPToolMongoElement tool)
public static McpTool ToDomainElement(AgentMcpToolMongoElement tool)
{
return new MCPTool
return new McpTool
{
Name = tool.Name,
ServerId = tool.ServerId,
Disabled = tool.Disabled,
Functions = tool.Functions?.Select(x => new MCPFunction(x.Name))?.ToList() ?? [],
Functions = tool.Functions?.Select(x => new McpFunction(x.Name))?.ToList() ?? [],
};
}
}

View file

@ -264,11 +264,11 @@ public partial class MongoRepository
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentMcpTools(string agentId, List<MCPTool> mcps)
private void UpdateAgentMcpTools(string agentId, List<McpTool> mcps)
{
if (mcps == null) return;
var elements = mcps?.Select(x => AgentMCPToolMongoElement.ToMongoElement(x))?.ToList() ?? [];
var elements = mcps?.Select(x => AgentMcpToolMongoElement.ToMongoElement(x))?.ToList() ?? [];
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
var update = Builders<AgentDocument>.Update
@ -346,7 +346,7 @@ public partial class MongoRepository
.Set(x => x.Responses, agent.Responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList())
.Set(x => x.Samples, agent.Samples)
.Set(x => x.Utilities, agent.Utilities.Select(u => AgentUtilityMongoElement.ToMongoElement(u)).ToList())
.Set(x => x.McpTools, agent.McpTools.Select(u => AgentMCPToolMongoElement.ToMongoElement(u)).ToList())
.Set(x => x.McpTools, agent.McpTools.Select(u => AgentMcpToolMongoElement.ToMongoElement(u)).ToList())
.Set(x => x.KnowledgeBases, agent.KnowledgeBases.Select(u => AgentKnowledgeBaseMongoElement.ToMongoElement(u)).ToList())
.Set(x => x.Rules, agent.Rules.Select(e => AgentRuleMongoElement.ToMongoElement(e)).ToList())
.Set(x => x.LlmConfig, AgentLlmConfigMongoElement.ToMongoElement(agent.LlmConfig))
@ -527,7 +527,7 @@ public partial class MongoRepository
Responses = x.Responses?.Select(r => AgentResponseMongoElement.ToMongoElement(r))?.ToList() ?? [],
RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [],
Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [],
McpTools = x.McpTools?.Select(u => AgentMCPToolMongoElement.ToMongoElement(u))?.ToList() ?? [],
McpTools = x.McpTools?.Select(u => AgentMcpToolMongoElement.ToMongoElement(u))?.ToList() ?? [],
KnowledgeBases = x.KnowledgeBases?.Select(k => AgentKnowledgeBaseMongoElement.ToMongoElement(k))?.ToList() ?? [],
Rules = x.Rules?.Select(e => AgentRuleMongoElement.ToMongoElement(e))?.ToList() ?? [],
CreatedTime = x.CreatedDateTime,
@ -622,7 +622,7 @@ public partial class MongoRepository
Responses = agentDoc.Responses?.Select(r => AgentResponseMongoElement.ToDomainElement(r))?.ToList() ?? [],
RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [],
Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [],
McpTools = agentDoc.McpTools?.Select(u => AgentMCPToolMongoElement.ToDomainElement(u))?.ToList() ?? [],
McpTools = agentDoc.McpTools?.Select(u => AgentMcpToolMongoElement.ToDomainElement(u))?.ToList() ?? [],
KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? [],
Rules = agentDoc.Rules?.Select(e => AgentRuleMongoElement.ToDomainElement(e))?.ToList() ?? []
};

View file

@ -171,23 +171,25 @@
"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,
@ -440,6 +442,9 @@
"BotSharp.Plugin.EmailHandler",
"BotSharp.Plugin.AudioHandler",
"BotSharp.Plugin.TencentCos"
],
"ExcludedFunctions": [
"McpToolAdapter"
]
}
}