feat : Agent add mcp property
This commit is contained in:
parent
ddecb4d19a
commit
9b662168ff
14
BotSharp.sln
14
BotSharp.sln
|
|
@ -127,10 +127,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.Rules", "src\
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.DeepSeekAI", "src\Plugins\BotSharp.Plugin.DeepSeekAI\BotSharp.Plugin.DeepSeekAI.csproj", "{AF329442-B48E-4B48-A18A-1C869D1BA6F5}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MCP", "MCP", "{B38A04AB-F4BD-4F10-9662-EC110881533B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.Mcp", "src\Plugins\BotSharp.Plugin.Mcp\BotSharp.Plugin.Mcp.csproj", "{9475E249-6964-457C-96C2-3F917111123F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -523,14 +519,6 @@ Global
|
|||
{AF329442-B48E-4B48-A18A-1C869D1BA6F5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{AF329442-B48E-4B48-A18A-1C869D1BA6F5}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{AF329442-B48E-4B48-A18A-1C869D1BA6F5}.Release|x64.Build.0 = Release|Any CPU
|
||||
{9475E249-6964-457C-96C2-3F917111123F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9475E249-6964-457C-96C2-3F917111123F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9475E249-6964-457C-96C2-3F917111123F}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{9475E249-6964-457C-96C2-3F917111123F}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{9475E249-6964-457C-96C2-3F917111123F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9475E249-6964-457C-96C2-3F917111123F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9475E249-6964-457C-96C2-3F917111123F}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{9475E249-6964-457C-96C2-3F917111123F}.Release|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -592,8 +580,6 @@ Global
|
|||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
||||
{AFD64412-4D6A-452E-82A2-79E5D8842E29} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
||||
{AF329442-B48E-4B48-A18A-1C869D1BA6F5} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||
{B38A04AB-F4BD-4F10-9662-EC110881533B} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
|
||||
{9475E249-6964-457C-96C2-3F917111123F} = {B38A04AB-F4BD-4F10-9662-EC110881533B}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
|
||||
|
|
|
|||
|
|
@ -57,5 +57,11 @@ public abstract class AgentHookBase : IAgentHook
|
|||
|
||||
public virtual void OnAgentUtilityLoaded(Agent agent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public virtual void OnAgentMCPLoaded(Agent agent)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ public enum AgentField
|
|||
Sample,
|
||||
LlmConfig,
|
||||
Utility,
|
||||
Mcp,
|
||||
KnowledgeBase,
|
||||
Rule,
|
||||
MaxMessageCount
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ public interface IAgentHook
|
|||
|
||||
void OnAgentUtilityLoaded(Agent agent);
|
||||
|
||||
void OnAgentMCPLoaded(Agent agent);
|
||||
|
||||
/// <summary>
|
||||
/// Triggered when agent is loaded completely.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -95,14 +95,18 @@ public class Agent
|
|||
public List<string> Labels { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Merge utilities from entry agent
|
||||
/// Merge mcps from entry agent
|
||||
/// </summary>
|
||||
public bool MergeUtility { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Agent utilities
|
||||
/// Agent mcps
|
||||
/// </summary>
|
||||
public List<AgentUtility> Utilities { get; set; } = new();
|
||||
/// <summary>
|
||||
/// Agent MCP
|
||||
/// </summary>
|
||||
public List<AgentMCP> Mcps { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Agent rules
|
||||
|
|
@ -157,6 +161,7 @@ public class Agent
|
|||
Responses = agent.Responses,
|
||||
Samples = agent.Samples,
|
||||
Utilities = agent.Utilities,
|
||||
Mcps = agent.Mcps,
|
||||
Knowledges = agent.Knowledges,
|
||||
IsPublic = agent.IsPublic,
|
||||
Disabled = agent.Disabled,
|
||||
|
|
@ -298,4 +303,10 @@ public class Agent
|
|||
LlmConfig = llmConfig;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Agent SetMcps(List<AgentMCP> mcps)
|
||||
{
|
||||
Mcps = mcps ?? [];
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
namespace BotSharp.Abstraction.Agents.Models;
|
||||
|
||||
public class AgentMCP
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public string ServerId { get; set; }
|
||||
|
||||
public bool Disabled { get; set; }
|
||||
|
||||
public IEnumerable<MCPFunction> Functions { get; set; } = [];
|
||||
public IEnumerable<MCPTemplate> Templates { get; set; } = [];
|
||||
|
||||
public AgentMCP()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public AgentMCP(
|
||||
string name,
|
||||
IEnumerable<MCPFunction>? functions = null,
|
||||
IEnumerable<MCPTemplate>? templates = null)
|
||||
{
|
||||
Name = name;
|
||||
Functions = functions ?? [];
|
||||
Templates = templates ?? [];
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class MCPFunction : MCPBase
|
||||
{
|
||||
public MCPFunction()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MCPFunction(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public class MCPTemplate : MCPBase
|
||||
{
|
||||
public MCPTemplate()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MCPTemplate(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public class MCPBase
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
|
@ -1,3 +1,6 @@
|
|||
using BotSharp.Core.Mcp;
|
||||
using BotSharp.Core.MCP;
|
||||
|
||||
namespace BotSharp.Core.Agents.Hooks;
|
||||
|
||||
public class BasicAgentHook : AgentHookBase
|
||||
|
|
@ -39,6 +42,36 @@ public class BasicAgentHook : AgentHookBase
|
|||
}
|
||||
}
|
||||
|
||||
public override void OnAgentMCPLoaded(Agent agent)
|
||||
{
|
||||
if (agent.Type == AgentType.Routing)
|
||||
return;
|
||||
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
if (!isConvMode) return;
|
||||
|
||||
agent.SecondaryFunctions ??= [];
|
||||
agent.SecondaryInstructions ??= [];
|
||||
agent.Mcps ??= [];
|
||||
|
||||
var (functions, templates) = GetMCPContent(agent);
|
||||
|
||||
foreach (var fn in functions)
|
||||
{
|
||||
if (!agent.SecondaryFunctions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
agent.SecondaryFunctions.Add(fn);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var prompt in templates)
|
||||
{
|
||||
agent.SecondaryInstructions.Add(prompt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(Agent agent)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
|
|
@ -82,4 +115,29 @@ public class BasicAgentHook : AgentHookBase
|
|||
|
||||
return (functionNames, templateNames);
|
||||
}
|
||||
|
||||
private (IEnumerable<FunctionDef>, IEnumerable<string>) GetMCPContent(Agent agent)
|
||||
{
|
||||
List<FunctionDef> functionDefs = new List<FunctionDef>();
|
||||
IEnumerable<string> templates = new List<string>();
|
||||
var mcpClientManager = _services.GetRequiredService<MCPClientManager>();
|
||||
var mcps = agent.Mcps;
|
||||
foreach (var item in mcps)
|
||||
{
|
||||
var ua = mcpClientManager.Factory.GetClientAsync(item.ServerId).Result;
|
||||
if (ua != null)
|
||||
{
|
||||
var tools = ua.ListToolsAsync().Result;
|
||||
var funcnames = item.Functions.Select(x => x.Name).ToList();
|
||||
foreach (var tool in tools.Tools.Where(x=> funcnames.Contains(x.Name,StringComparer.OrdinalIgnoreCase)))
|
||||
{
|
||||
var funDef = AIFunctionUtilities.MapToFunctionDef(tool);
|
||||
functionDefs.Add(funDef);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (functionDefs, templates);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,6 +71,11 @@ public partial class AgentService
|
|||
{
|
||||
hook.OnAgentUtilityLoaded(agent);
|
||||
}
|
||||
|
||||
if(agent.Mcps != null)
|
||||
{
|
||||
hook.OnAgentMCPLoaded(agent);
|
||||
}
|
||||
|
||||
hook.OnAgentLoaded(agent);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ public partial class AgentService
|
|||
record.Responses = agent.Responses ?? [];
|
||||
record.Samples = agent.Samples ?? [];
|
||||
record.Utilities = agent.Utilities ?? [];
|
||||
record.Mcps = agent.Mcps ?? [];
|
||||
record.KnowledgeBases = agent.KnowledgeBases ?? [];
|
||||
record.Rules = agent.Rules ?? [];
|
||||
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
|
||||
|
|
@ -106,6 +107,7 @@ public partial class AgentService
|
|||
.SetResponses(foundAgent.Responses)
|
||||
.SetSamples(foundAgent.Samples)
|
||||
.SetUtilities(foundAgent.Utilities)
|
||||
.SetMcps(foundAgent.Mcps)
|
||||
.SetKnowledgeBases(foundAgent.KnowledgeBases)
|
||||
.SetRules(foundAgent.Rules)
|
||||
.SetLlmConfig(foundAgent.LlmConfig);
|
||||
|
|
|
|||
|
|
@ -195,6 +195,7 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
|
||||
<PackageReference Include="Nanoid" Version="3.1.0" />
|
||||
<PackageReference Include="Rougamo.Fody" Version="4.0.4" />
|
||||
<PackageReference Include="mcpdotnet" Version="1.0.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
36
src/Infrastructure/BotSharp.Core/MCP/AIFunctionUtilities.cs
Normal file
36
src/Infrastructure/BotSharp.Core/MCP/AIFunctionUtilities.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using McpDotNet.Protocol.Types;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Core.MCP;
|
||||
|
||||
internal static class AIFunctionUtilities
|
||||
{
|
||||
public static FunctionDef MapToFunctionDef(Tool tool)
|
||||
{
|
||||
if (tool == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(tool));
|
||||
}
|
||||
|
||||
var properties = tool.InputSchema?.Properties;
|
||||
var required = tool.InputSchema?.Required ?? new List<string>();
|
||||
|
||||
FunctionDef funDef = new FunctionDef
|
||||
{
|
||||
Name = tool.Name,
|
||||
Description = tool.Description?? string.Empty,
|
||||
Type = "function",
|
||||
Parameters = new FunctionParametersDef
|
||||
{
|
||||
Type = "object",
|
||||
Properties = properties != null ? JsonSerializer.SerializeToDocument(properties) : JsonDocument.Parse("{}"),
|
||||
Required = required
|
||||
}
|
||||
};
|
||||
|
||||
return funDef;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,8 @@
|
|||
using BotSharp.Abstraction.Conversations.Models;
|
||||
using BotSharp.Abstraction.Functions;
|
||||
using McpDotNet.Client;
|
||||
using McpDotNet.Protocol.Types;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.Mcp.Functions;
|
||||
namespace BotSharp.Core.Mcp.Functions;
|
||||
|
||||
public class McpAIFunction : IFunctionCallback
|
||||
{
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
using BotSharp.Plugin.Mcp.Settings;
|
||||
using BotSharp.Core.Mcp.Settings;
|
||||
using McpDotNet.Client;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
|
||||
namespace BotSharp.Plugin.Mcp;
|
||||
namespace BotSharp.Core.Mcp;
|
||||
|
||||
public class MCPClientManager : IDisposable
|
||||
{
|
||||
|
|
@ -1,13 +1,10 @@
|
|||
using BotSharp.Abstraction.Functions;
|
||||
using BotSharp.Abstraction.Plugins;
|
||||
using BotSharp.Plugin.Mcp.Functions;
|
||||
using BotSharp.Plugin.Mcp.Settings;
|
||||
using BotSharp.Core.Mcp.Functions;
|
||||
using BotSharp.Core.Mcp.Settings;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace BotSharp.Plugin.Mcp;
|
||||
namespace BotSharp.Core.Mcp;
|
||||
|
||||
public class McpPlugin : IBotSharpPlugin
|
||||
{
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
using McpDotNet.Client;
|
||||
using McpDotNet.Configuration;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BotSharp.Plugin.Mcp.Settings;
|
||||
namespace BotSharp.Core.Mcp.Settings;
|
||||
|
||||
public class MCPSettings
|
||||
{
|
||||
|
|
@ -60,6 +60,9 @@ namespace BotSharp.Core.Repository
|
|||
case AgentField.Utility:
|
||||
UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities);
|
||||
break;
|
||||
case AgentField.Mcp:
|
||||
UpdateAgentMcp(agent.Id, agent.Mcps);
|
||||
break;
|
||||
case AgentField.KnowledgeBase:
|
||||
UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases);
|
||||
break;
|
||||
|
|
@ -191,6 +194,20 @@ namespace BotSharp.Core.Repository
|
|||
File.WriteAllText(agentFile, json);
|
||||
}
|
||||
|
||||
private void UpdateAgentMcp(string agentId, List<AgentMCP> mcps)
|
||||
{
|
||||
if (mcps == null) return;
|
||||
|
||||
var (agent, agentFile) = GetAgentFromFile(agentId);
|
||||
if (agent == null) return;
|
||||
|
||||
|
||||
agent.Mcps = mcps;
|
||||
agent.UpdatedDateTime = DateTime.UtcNow;
|
||||
var json = JsonSerializer.Serialize(agent, _options);
|
||||
File.WriteAllText(agentFile, json);
|
||||
}
|
||||
|
||||
private void UpdateAgentKnowledgeBases(string agentId, List<AgentKnowledgeBase> knowledgeBases)
|
||||
{
|
||||
if (knowledgeBases == null) return;
|
||||
|
|
@ -360,6 +377,7 @@ namespace BotSharp.Core.Repository
|
|||
agent.Profiles = inputAgent.Profiles;
|
||||
agent.Labels = inputAgent.Labels;
|
||||
agent.Utilities = inputAgent.Utilities;
|
||||
agent.Mcps = inputAgent.Mcps;
|
||||
agent.KnowledgeBases = inputAgent.KnowledgeBases;
|
||||
agent.RoutingRules = inputAgent.RoutingRules;
|
||||
agent.Rules = inputAgent.Rules;
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@ public class AgentCreationModel
|
|||
public int? MaxMessageCount { get; set; }
|
||||
|
||||
public List<AgentUtility> Utilities { get; set; } = new();
|
||||
public List<AgentMCP> Mcps { get; set; } = new();
|
||||
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new();
|
||||
public List<AgentKnowledgeBase> KnowledgeBases { get; set; } = new();
|
||||
public List<AgentRule> Rules { get; set; } = new();
|
||||
|
|
@ -73,6 +74,7 @@ public class AgentCreationModel
|
|||
Responses = Responses,
|
||||
Samples = Samples,
|
||||
Utilities = Utilities,
|
||||
Mcps = Mcps,
|
||||
IsPublic = IsPublic,
|
||||
Type = Type,
|
||||
Disabled = Disabled,
|
||||
|
|
|
|||
|
|
@ -39,6 +39,11 @@ public class AgentUpdateModel
|
|||
/// </summary>
|
||||
public List<AgentUtility>? Utilities { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Mcps
|
||||
/// </summary>
|
||||
public List<AgentMCP>? Mcps { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// knowledge bases
|
||||
/// </summary>
|
||||
|
|
@ -103,6 +108,7 @@ public class AgentUpdateModel
|
|||
Functions = Functions ?? [],
|
||||
Responses = Responses ?? [],
|
||||
Utilities = Utilities ?? [],
|
||||
Mcps = Mcps ?? [],
|
||||
KnowledgeBases = KnowledgeBases ?? [],
|
||||
Rules = Rules ?? [],
|
||||
LlmConfig = LlmConfig ?? new()
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ public class AgentViewModel
|
|||
[JsonPropertyName("merge_utility")]
|
||||
public bool MergeUtility { get; set; }
|
||||
public List<AgentUtility> Utilities { get; set; }
|
||||
public List<AgentMCP> Mcps { get; set; }
|
||||
|
||||
[JsonPropertyName("knowledge_bases")]
|
||||
public List<AgentKnowledgeBase> KnowledgeBases { get; set; }
|
||||
|
|
@ -86,6 +87,7 @@ public class AgentViewModel
|
|||
Responses = agent.Responses ?? [],
|
||||
Samples = agent.Samples ?? [],
|
||||
Utilities = agent.Utilities ?? [],
|
||||
Mcps = agent.Mcps ?? [],
|
||||
KnowledgeBases = agent.KnowledgeBases ?? [],
|
||||
IsPublic= agent.IsPublic,
|
||||
Disabled = agent.Disabled,
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>$(TargetFramework)</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
|
||||
<OutputPath>$(SolutionDir)packages</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="mcpdotnet" Version="1.0.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -18,6 +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<AgentMcpMongoElement> Mcps { get; set; }
|
||||
public List<AgentKnowledgeBaseMongoElement> KnowledgeBases { get; set; }
|
||||
public List<string> Profiles { get; set; }
|
||||
public List<string> Labels { get; set; }
|
||||
|
|
|
|||
|
|
@ -0,0 +1,73 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.MongoStorage.Models;
|
||||
|
||||
[BsonIgnoreExtraElements(Inherited = true)]
|
||||
public class AgentMcpMongoElement
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public string ServerId { get; set; }
|
||||
|
||||
public bool Disabled { get; set; }
|
||||
public List<McpFunctionMongoElement> Functions { get; set; } = [];
|
||||
public List<McpTemplateMongoElement> Templates { get; set; } = [];
|
||||
|
||||
public static AgentMcpMongoElement ToMongoElement(AgentMCP utility)
|
||||
{
|
||||
return new AgentMcpMongoElement
|
||||
{
|
||||
Name = utility.Name,
|
||||
Disabled = utility.Disabled,
|
||||
Functions = utility.Functions?.Select(x => new McpFunctionMongoElement(x.Name))?.ToList() ?? [],
|
||||
Templates = utility.Templates?.Select(x => new McpTemplateMongoElement(x.Name))?.ToList() ?? []
|
||||
};
|
||||
}
|
||||
|
||||
public static AgentMCP ToDomainElement(AgentMcpMongoElement utility)
|
||||
{
|
||||
return new AgentMCP
|
||||
{
|
||||
Name = utility.Name,
|
||||
Disabled = utility.Disabled,
|
||||
Functions = utility.Functions?.Select(x => new MCPFunction(x.Name))?.ToList() ?? [],
|
||||
Templates = utility.Templates?.Select(x => new MCPTemplate(x.Name))?.ToList() ?? []
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class McpFunctionMongoElement
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public McpFunctionMongoElement()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public McpFunctionMongoElement(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public class McpTemplateMongoElement
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public McpTemplateMongoElement()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public McpTemplateMongoElement(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -61,6 +61,9 @@ public partial class MongoRepository
|
|||
case AgentField.Utility:
|
||||
UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities);
|
||||
break;
|
||||
case AgentField.Mcp:
|
||||
UpdateAgentMcps(agent.Id, agent.Mcps);
|
||||
break;
|
||||
case AgentField.KnowledgeBase:
|
||||
UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases);
|
||||
break;
|
||||
|
|
@ -261,6 +264,19 @@ public partial class MongoRepository
|
|||
_dc.Agents.UpdateOne(filter, update);
|
||||
}
|
||||
|
||||
private void UpdateAgentMcps(string agentId, List<AgentMCP> mcps)
|
||||
{
|
||||
if (mcps == null) return;
|
||||
|
||||
var elements = mcps?.Select(x => AgentMcpMongoElement.ToMongoElement(x))?.ToList() ?? [];
|
||||
|
||||
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
|
||||
var update = Builders<AgentDocument>.Update
|
||||
.Set(x => x.Mcps, elements)
|
||||
.Set(x => x.UpdatedTime, DateTime.UtcNow);
|
||||
|
||||
_dc.Agents.UpdateOne(filter, update);
|
||||
}
|
||||
private void UpdateAgentKnowledgeBases(string agentId, List<AgentKnowledgeBase> knowledgeBases)
|
||||
{
|
||||
if (knowledgeBases == null) return;
|
||||
|
|
@ -330,6 +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.Mcps, agent.Mcps.Select(u => AgentMcpMongoElement.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))
|
||||
|
|
@ -510,6 +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() ?? [],
|
||||
Mcps = x.Mcps?.Select(u => AgentMcpMongoElement.ToMongoElement(u))?.ToList() ?? [],
|
||||
KnowledgeBases = x.KnowledgeBases?.Select(k => AgentKnowledgeBaseMongoElement.ToMongoElement(k))?.ToList() ?? [],
|
||||
Rules = x.Rules?.Select(e => AgentRuleMongoElement.ToMongoElement(e))?.ToList() ?? [],
|
||||
CreatedTime = x.CreatedDateTime,
|
||||
|
|
@ -604,6 +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() ?? [],
|
||||
Mcps = agentDoc.Mcps?.Select(u => AgentMcpMongoElement.ToDomainElement(u))?.ToList() ?? [],
|
||||
KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? [],
|
||||
Rules = agentDoc.Rules?.Select(e => AgentRuleMongoElement.ToDomainElement(e))?.ToList() ?? []
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue