From ae173934f6b09a8e347015d2b156790535a74bab Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Wed, 16 Apr 2025 20:33:31 -0500 Subject: [PATCH] refine init mcp --- .../BotSharpMCPExtensions.cs | 22 ++++++----- .../Functions/McpToolAdapter.cs | 38 ++++++++++++------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs b/src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs index 38522fa9f..8760935db 100644 --- a/src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs +++ b/src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs @@ -43,18 +43,22 @@ public static IServiceCollection AddBotSharpMCP(this IServiceCollection services private static async Task RegisterFunctionCall(IServiceCollection services, McpServerConfigModel server, McpClientManager clientManager) { - var client = await clientManager.GetMcpClientAsync(server.Id); - var tools = await client.ListToolsAsync(); - - foreach (var tool in tools) + try { - services.AddScoped(provider => tool); + var client = await clientManager.GetMcpClientAsync(server.Id); + var tools = await client.ListToolsAsync(); - services.AddScoped(provider => + foreach (var tool in tools) { - var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager); - return funcTool; - }); + services.AddScoped(provider => tool); + + services.AddScoped(provider => + { + var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager); + return funcTool; + }); + } } + catch { } } } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core.MCP/Functions/McpToolAdapter.cs b/src/Infrastructure/BotSharp.Core.MCP/Functions/McpToolAdapter.cs index ad0272199..0e4335979 100644 --- a/src/Infrastructure/BotSharp.Core.MCP/Functions/McpToolAdapter.cs +++ b/src/Infrastructure/BotSharp.Core.MCP/Functions/McpToolAdapter.cs @@ -28,30 +28,40 @@ public McpToolAdapter( public async Task Execute(RoleDialogModel message) { - // Convert arguments to dictionary format expected by mcpdotnet - Dictionary argDict = JsonToDictionary(message.FunctionArgs); - var currentAgentId = message.CurrentAgentId; - var agentService = _services.GetRequiredService(); - var agent = await agentService.LoadAgent(currentAgentId); - var serverId = agent.McpTools.Where(t => t.Functions.Any(f => f.Name == Name)).FirstOrDefault().ServerId; + try + { + // Convert arguments to dictionary format expected by mcpdotnet + Dictionary argDict = JsonToDictionary(message.FunctionArgs); + var currentAgentId = message.CurrentAgentId; + var agentService = _services.GetRequiredService(); + var agent = await agentService.LoadAgent(currentAgentId); + var serverId = agent.McpTools.Where(t => t.Functions.Any(f => f.Name == Name)).FirstOrDefault().ServerId; - var client = await _clientManager.GetMcpClientAsync(serverId); + var client = await _clientManager.GetMcpClientAsync(serverId); - // Call the tool through mcpdotnet - var result = await client.CallToolAsync(_tool.Name, argDict.IsNullOrEmpty() ? new() : argDict); + // Call the tool through mcpdotnet + var result = await client.CallToolAsync(_tool.Name, !argDict.IsNullOrEmpty() ? argDict : []); - // Extract the text content from the result - var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text)); + // Extract the text content from the result + var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text)); - message.Content = json; - message.Data = json.JsonContent(); - return true; + message.Content = json; + message.Data = json.JsonContent(); + return true; + } + catch (Exception ex) + { + message.Content = $"Error when calling tool {Name} of MCP server {Provider}. {ex.Message}"; + return false; + } } private static Dictionary JsonToDictionary(string? json) { if (string.IsNullOrEmpty(json)) + { return []; + } using JsonDocument doc = JsonDocument.Parse(json); JsonElement root = doc.RootElement;