Skip to content

Commit 9c6f00f

Browse files
authored
Merge pull request #1012 from iceljc/master
refine init mcp
2 parents 532c8da + ae17393 commit 9c6f00f

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,22 @@ public static IServiceCollection AddBotSharpMCP(this IServiceCollection services
4343

4444
private static async Task RegisterFunctionCall(IServiceCollection services, McpServerConfigModel server, McpClientManager clientManager)
4545
{
46-
var client = await clientManager.GetMcpClientAsync(server.Id);
47-
var tools = await client.ListToolsAsync();
48-
49-
foreach (var tool in tools)
46+
try
5047
{
51-
services.AddScoped(provider => tool);
48+
var client = await clientManager.GetMcpClientAsync(server.Id);
49+
var tools = await client.ListToolsAsync();
5250

53-
services.AddScoped<IFunctionCallback>(provider =>
51+
foreach (var tool in tools)
5452
{
55-
var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager);
56-
return funcTool;
57-
});
53+
services.AddScoped(provider => tool);
54+
55+
services.AddScoped<IFunctionCallback>(provider =>
56+
{
57+
var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager);
58+
return funcTool;
59+
});
60+
}
5861
}
62+
catch { }
5963
}
6064
}

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,40 @@ public McpToolAdapter(
2828

2929
public async Task<bool> Execute(RoleDialogModel message)
3030
{
31-
// Convert arguments to dictionary format expected by mcpdotnet
32-
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
33-
var currentAgentId = message.CurrentAgentId;
34-
var agentService = _services.GetRequiredService<IAgentService>();
35-
var agent = await agentService.LoadAgent(currentAgentId);
36-
var serverId = agent.McpTools.Where(t => t.Functions.Any(f => f.Name == Name)).FirstOrDefault().ServerId;
31+
try
32+
{
33+
// Convert arguments to dictionary format expected by mcpdotnet
34+
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
35+
var currentAgentId = message.CurrentAgentId;
36+
var agentService = _services.GetRequiredService<IAgentService>();
37+
var agent = await agentService.LoadAgent(currentAgentId);
38+
var serverId = agent.McpTools.Where(t => t.Functions.Any(f => f.Name == Name)).FirstOrDefault().ServerId;
3739

38-
var client = await _clientManager.GetMcpClientAsync(serverId);
40+
var client = await _clientManager.GetMcpClientAsync(serverId);
3941

40-
// Call the tool through mcpdotnet
41-
var result = await client.CallToolAsync(_tool.Name, argDict.IsNullOrEmpty() ? new() : argDict);
42+
// Call the tool through mcpdotnet
43+
var result = await client.CallToolAsync(_tool.Name, !argDict.IsNullOrEmpty() ? argDict : []);
4244

43-
// Extract the text content from the result
44-
var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text));
45+
// Extract the text content from the result
46+
var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text));
4547

46-
message.Content = json;
47-
message.Data = json.JsonContent();
48-
return true;
48+
message.Content = json;
49+
message.Data = json.JsonContent();
50+
return true;
51+
}
52+
catch (Exception ex)
53+
{
54+
message.Content = $"Error when calling tool {Name} of MCP server {Provider}. {ex.Message}";
55+
return false;
56+
}
4957
}
5058

5159
private static Dictionary<string, object> JsonToDictionary(string? json)
5260
{
5361
if (string.IsNullOrEmpty(json))
62+
{
5463
return [];
64+
}
5565

5666
using JsonDocument doc = JsonDocument.Parse(json);
5767
JsonElement root = doc.RootElement;

0 commit comments

Comments
 (0)