Skip to content

Commit 7447603

Browse files
author
Jicheng Lu
committed
refine mcp
1 parent 5eca6b4 commit 7447603

File tree

18 files changed

+64
-94
lines changed

18 files changed

+64
-94
lines changed

BotSharp.sln

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.Rules", "src\
127127
EndProject
128128
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.DeepSeekAI", "src\Plugins\BotSharp.Plugin.DeepSeekAI\BotSharp.Plugin.DeepSeekAI.csproj", "{AF329442-B48E-4B48-A18A-1C869D1BA6F5}"
129129
EndProject
130-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.MCP", "src\Infrastructure\BotSharp.MCP\BotSharp.MCP.csproj", "{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C}"
131-
EndProject
132130
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.Realtime", "src\Infrastructure\BotSharp.Core.Realtime\BotSharp.Core.Realtime.csproj", "{781F1465-365C-0F22-1775-25025DAFA4C7}"
133131
EndProject
134132
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.PizzaBot.MCPServer", "tests\BotSharp.PizzaBot.MCPServer\BotSharp.PizzaBot.MCPServer.csproj", "{8D2AD45F-836A-516F-DE6A-71443CEBB18A}"
135133
EndProject
134+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.MCP", "src\Infrastructure\BotSharp.Core.MCP\BotSharp.Core.MCP.csproj", "{534D1DD5-9D62-115C-F230-47080D76CE52}"
135+
EndProject
136136
Global
137137
GlobalSection(SolutionConfigurationPlatforms) = preSolution
138138
Debug|Any CPU = Debug|Any CPU
@@ -525,14 +525,6 @@ Global
525525
{AF329442-B48E-4B48-A18A-1C869D1BA6F5}.Release|Any CPU.Build.0 = Release|Any CPU
526526
{AF329442-B48E-4B48-A18A-1C869D1BA6F5}.Release|x64.ActiveCfg = Release|Any CPU
527527
{AF329442-B48E-4B48-A18A-1C869D1BA6F5}.Release|x64.Build.0 = Release|Any CPU
528-
{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
529-
{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
530-
{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C}.Debug|x64.ActiveCfg = Debug|Any CPU
531-
{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C}.Debug|x64.Build.0 = Debug|Any CPU
532-
{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
533-
{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C}.Release|Any CPU.Build.0 = Release|Any CPU
534-
{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C}.Release|x64.ActiveCfg = Release|Any CPU
535-
{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C}.Release|x64.Build.0 = Release|Any CPU
536528
{781F1465-365C-0F22-1775-25025DAFA4C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
537529
{781F1465-365C-0F22-1775-25025DAFA4C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
538530
{781F1465-365C-0F22-1775-25025DAFA4C7}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -549,6 +541,14 @@ Global
549541
{8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|Any CPU.Build.0 = Release|Any CPU
550542
{8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|x64.ActiveCfg = Release|Any CPU
551543
{8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|x64.Build.0 = Release|Any CPU
544+
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
545+
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|Any CPU.Build.0 = Debug|Any CPU
546+
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|x64.ActiveCfg = Debug|Any CPU
547+
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|x64.Build.0 = Debug|Any CPU
548+
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|Any CPU.ActiveCfg = Release|Any CPU
549+
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|Any CPU.Build.0 = Release|Any CPU
550+
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|x64.ActiveCfg = Release|Any CPU
551+
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|x64.Build.0 = Release|Any CPU
552552
EndGlobalSection
553553
GlobalSection(SolutionProperties) = preSolution
554554
HideSolutionNode = FALSE
@@ -610,9 +610,9 @@ Global
610610
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
611611
{AFD64412-4D6A-452E-82A2-79E5D8842E29} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
612612
{AF329442-B48E-4B48-A18A-1C869D1BA6F5} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
613-
{684781D5-3DD4-6A0B-B53F-0A362CD6BB0C} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
614613
{781F1465-365C-0F22-1775-25025DAFA4C7} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
615614
{8D2AD45F-836A-516F-DE6A-71443CEBB18A} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}
615+
{534D1DD5-9D62-115C-F230-47080D76CE52} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
616616
EndGlobalSection
617617
GlobalSection(ExtensibilityGlobals) = postSolution
618618
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

src/Infrastructure/BotSharp.MCP/AIFunctionUtilities.cs renamed to src/Infrastructure/BotSharp.Core.MCP/AIFunctionUtilities.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using BotSharp.Abstraction.Functions.Models;
2-
using ModelContextProtocol.Client;
3-
using ModelContextProtocol.Protocol.Types;
4-
using System;
5-
using System.Collections.Generic;
61
using System.Text.Json;
72

3+
using ModelContextProtocol.Client;
4+
85
namespace BotSharp.Core.MCP;
96

107
internal static class AIFunctionUtilities
@@ -19,7 +16,7 @@ public static FunctionDef MapToFunctionDef(McpClientTool tool)
1916
var properties = tool.JsonSchema.GetProperty("properties");
2017
var required = tool.JsonSchema.GetProperty("required");
2118

22-
FunctionDef funDef = new FunctionDef
19+
var funDef = new FunctionDef
2320
{
2421
Name = tool.Name,
2522
Description = tool.Description ?? string.Empty,

src/Infrastructure/BotSharp.MCP/BotSharpMCPExtensions.cs renamed to src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
using BotSharp.Abstraction.Agents;
2-
using BotSharp.Abstraction.Functions;
3-
using BotSharp.Core.Mcp.Functions;
4-
using BotSharp.Core.Mcp.Settings;
5-
using BotSharp.Core.Mcp;
6-
using BotSharp.MCP.Hooks;
1+
using BotSharp.Core.MCP.Functions;
2+
using BotSharp.Core.MCP.Settings;
3+
using BotSharp.Core.MCP.Hooks;
74
using Microsoft.Extensions.Configuration;
8-
using Microsoft.Extensions.DependencyInjection;
9-
using Microsoft.Extensions.Hosting;
105
using ModelContextProtocol.Configuration;
11-
using System;
12-
using System.Collections.Generic;
13-
using System.Linq;
14-
using System.Text;
15-
using System.Threading.Tasks;
166
using ModelContextProtocol.Client;
177

18-
namespace BotSharp.MCP;
8+
namespace BotSharp.Core.MCP;
199

2010
public static class BotSharpMCPExtensions
2111
{
@@ -25,14 +15,12 @@ public static class BotSharpMCPExtensions
2515
/// <param name="services"></param>
2616
/// <param name="config"></param>
2717
/// <returns></returns>
28-
public static IServiceCollection AddBotSharpMCP(this IServiceCollection services,
29-
IConfiguration config)
18+
public static IServiceCollection AddBotSharpMCP(this IServiceCollection services, IConfiguration config)
3019
{
3120
var settings = config.GetSection("MCPSettings").Get<MCPSettings>();
3221
services.AddScoped<MCPSettings>(provider => { return settings; });
3322
if (settings != null)
3423
{
35-
3624
var clientManager = new MCPClientManager(settings);
3725
services.AddSingleton(clientManager);
3826

src/Infrastructure/BotSharp.MCP/Functions/McpToolAdapter.cs renamed to src/Infrastructure/BotSharp.Core.MCP/Functions/McpToolAdapter.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
using BotSharp.Abstraction.Agents;
2-
using BotSharp.Abstraction.Conversations.Models;
3-
using BotSharp.Abstraction.Functions;
4-
using BotSharp.Abstraction.Utilities;
5-
using Microsoft.Extensions.DependencyInjection;
6-
using ModelContextProtocol.Client;
7-
using ModelContextProtocol.Protocol.Types;
8-
using System;
9-
using System.Collections.Generic;
10-
using System.Linq;
111
using System.Text.Json;
12-
using System.Threading.Tasks;
2+
using ModelContextProtocol.Client;
133

14-
namespace BotSharp.Core.Mcp.Functions;
4+
namespace BotSharp.Core.MCP.Functions;
155

166
public class McpToolAdapter : IFunctionCallback
177
{
188
private readonly McpClientTool _tool;
199
private readonly MCPClientManager _clientManager;
20-
private readonly IServiceProvider _serviceProvider;
10+
private readonly IServiceProvider _services;
2111

22-
public McpToolAdapter(IServiceProvider provider, McpClientTool tool, MCPClientManager client)
12+
public McpToolAdapter(IServiceProvider services, McpClientTool tool, MCPClientManager client)
2313
{
24-
_serviceProvider = provider ?? throw new ArgumentNullException(nameof(provider));
14+
_services = services ?? throw new ArgumentNullException(nameof(services));
2515
_tool = tool ?? throw new ArgumentNullException(nameof(tool));
2616
_clientManager = client ?? throw new ArgumentNullException(nameof(client));
2717
}
@@ -33,21 +23,18 @@ public async Task<bool> Execute(RoleDialogModel message)
3323
// Convert arguments to dictionary format expected by mcpdotnet
3424
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
3525
var currentAgentId = message.CurrentAgentId;
36-
var agentService = _serviceProvider.GetRequiredService<IAgentService>();
26+
var agentService = _services.GetRequiredService<IAgentService>();
3727
var agent = await agentService.LoadAgent(currentAgentId);
3828
var serverId = agent.McpTools.Where(t => t.Functions.Any(f => f.Name == Name)).FirstOrDefault().ServerId;
3929

4030
var client = await _clientManager.GetMcpClientAsync(serverId);
31+
4132
// Call the tool through mcpdotnet
42-
var result = await client.CallToolAsync(
43-
_tool.Name,
44-
argDict.Count == 0 ? new() : argDict
45-
);
33+
var result = await client.CallToolAsync(_tool.Name, argDict.IsNullOrEmpty() ? new() : argDict);
4634

4735
// Extract the text content from the result
48-
var json = string.Join("\n", result.Content
49-
.Where(c => c.Type == "text")
50-
.Select(c => c.Text));
36+
var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text));
37+
5138
message.Content = json;
5239
message.Data = json.JsonContent();
5340
return true;

src/Infrastructure/BotSharp.MCP/Hooks/MCPToolAgentHook.cs renamed to src/Infrastructure/BotSharp.Core.MCP/Hooks/MCPToolAgentHook.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1-
using BotSharp.Abstraction.Agents;
2-
using BotSharp.Abstraction.Agents.Enums;
3-
using BotSharp.Abstraction.Agents.Models;
4-
using BotSharp.Abstraction.Agents.Settings;
5-
using BotSharp.Abstraction.Conversations;
6-
using BotSharp.Abstraction.Functions.Models;
7-
using BotSharp.Core.Mcp;
8-
using BotSharp.Core.MCP;
9-
using Microsoft.Extensions.DependencyInjection;
101
using ModelContextProtocol.Client;
11-
using System;
12-
using System.Collections.Generic;
13-
using System.Linq;
14-
using System.Threading.Tasks;
152

16-
namespace BotSharp.MCP.Hooks;
3+
namespace BotSharp.Core.MCP.Hooks;
174

185
public class MCPToolAgentHook : AgentHookBase
196
{
@@ -27,15 +14,17 @@ public MCPToolAgentHook(IServiceProvider services, AgentSettings settings)
2714
public override void OnAgentMCPToolLoaded(Agent agent)
2815
{
2916
if (agent.Type == AgentType.Routing)
17+
{
3018
return;
19+
}
20+
3121
var conv = _services.GetRequiredService<IConversationService>();
3222
var isConvMode = conv.IsConversationMode();
3323
if (!isConvMode) return;
3424

3525
agent.SecondaryFunctions ??= [];
3626

3727
var functions = GetMCPContent(agent).Result;
38-
3928
foreach (var fn in functions)
4029
{
4130
if (!agent.SecondaryFunctions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
@@ -47,7 +36,7 @@ public override void OnAgentMCPToolLoaded(Agent agent)
4736

4837
private async Task<IEnumerable<FunctionDef>> GetMCPContent(Agent agent)
4938
{
50-
List<FunctionDef> functionDefs = new List<FunctionDef>();
39+
var functionDefs = new List<FunctionDef>();
5140
var mcpClientManager = _services.GetRequiredService<MCPClientManager>();
5241
var mcps = agent.McpTools;
5342
foreach (var item in mcps)
@@ -57,7 +46,7 @@ private async Task<IEnumerable<FunctionDef>> GetMCPContent(Agent agent)
5746
{
5847
var tools = await mcpClient.ListToolsAsync();
5948
var toolnames = item.Functions.Select(x => x.Name).ToList();
60-
foreach (var tool in tools.ToList().Where(x => toolnames.Contains(x.Name, StringComparer.OrdinalIgnoreCase)))
49+
foreach (var tool in tools.Where(x => toolnames.Contains(x.Name, StringComparer.OrdinalIgnoreCase)))
6150
{
6251
var funDef = AIFunctionUtilities.MapToFunctionDef(tool);
6352
functionDefs.Add(funDef);

src/Infrastructure/BotSharp.MCP/MCPClientManager.cs renamed to src/Infrastructure/BotSharp.Core.MCP/MCPClientManager.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
using BotSharp.Core.Mcp.Settings;
2-
using Microsoft.Extensions.Logging;
1+
using BotSharp.Core.MCP.Settings;
32
using ModelContextProtocol.Client;
4-
using System;
5-
using System.Linq;
6-
using System.Threading.Tasks;
73

8-
namespace BotSharp.Core.Mcp;
4+
namespace BotSharp.Core.MCP;
95

106
public class MCPClientManager : IDisposable
117
{
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using ModelContextProtocol.Client;
22
using ModelContextProtocol.Configuration;
3-
using System.Collections.Generic;
43

5-
namespace BotSharp.Core.Mcp.Settings;
4+
namespace BotSharp.Core.MCP.Settings;
65

76
public class MCPSettings
87
{
98
public McpClientOptions McpClientOptions { get; set; }
10-
119
public List<McpServerConfig> McpServerConfigs { get; set; } = new();
1210

1311
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
global using System;
2+
global using System.Collections.Generic;
3+
global using System.Linq;
4+
global using System.Threading.Tasks;
5+
global using Microsoft.Extensions.DependencyInjection;
6+
7+
global using BotSharp.Abstraction.Agents;
8+
global using BotSharp.Abstraction.Agents.Enums;
9+
global using BotSharp.Abstraction.Agents.Models;
10+
global using BotSharp.Abstraction.Agents.Settings;
11+
global using BotSharp.Abstraction.Conversations;
12+
global using BotSharp.Abstraction.Conversations.Models;
13+
global using BotSharp.Abstraction.Functions;
14+
global using BotSharp.Abstraction.Functions.Models;
15+
global using BotSharp.Abstraction.Utilities;
16+

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task<Agent> LoadAgent(string id, bool loadUtility = true)
7272
hook.OnAgentUtilityLoaded(agent);
7373
}
7474

75-
if(agent.McpTools != null && agent.McpTools.Count >0)
75+
if(!agent.McpTools.IsNullOrEmpty())
7676
{
7777
hook.OnAgentMCPToolLoaded(agent);
7878
}

0 commit comments

Comments
 (0)