Merge pull request #1012 from iceljc/master

refine init mcp
This commit is contained in:
iceljc 2025-04-16 20:48:14 -05:00 committed by GitHub
commit 9c6f00f937
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 23 deletions

View file

@ -43,18 +43,22 @@ public static class BotSharpMcpExtensions
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 { }
}
}

View file

@ -28,30 +28,40 @@ public class McpToolAdapter : IFunctionCallback
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;