Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IFunctionCallback>(provider =>
foreach (var tool in tools)
{
var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager);
return funcTool;
});
services.AddScoped(provider => tool);

services.AddScoped<IFunctionCallback>(provider =>
{
var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager);
return funcTool;
});
}
}
catch { }
}
}
38 changes: 24 additions & 14 deletions src/Infrastructure/BotSharp.Core.MCP/Functions/McpToolAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,40 @@ public McpToolAdapter(

public async Task<bool> Execute(RoleDialogModel message)
{
// Convert arguments to dictionary format expected by mcpdotnet
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
var currentAgentId = message.CurrentAgentId;
var agentService = _services.GetRequiredService<IAgentService>();
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<string, object> argDict = JsonToDictionary(message.FunctionArgs);
var currentAgentId = message.CurrentAgentId;
var agentService = _services.GetRequiredService<IAgentService>();
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<string, object> JsonToDictionary(string? json)
{
if (string.IsNullOrEmpty(json))
{
return [];
}

using JsonDocument doc = JsonDocument.Parse(json);
JsonElement root = doc.RootElement;
Expand Down
Loading