11using BotSharp . Abstraction . Routing . Executor ;
22using BotSharp . Core . MCP . Managers ;
3- using ModelContextProtocol . Client ;
3+ using ModelContextProtocol . Protocol ;
44
55namespace BotSharp . Core . Routing . Executor ;
66
7- public class McpToolExecutor : IFunctionExecutor
7+ public class McpToolExecutor : IFunctionExecutor
88{
99 private readonly IServiceProvider _services ;
1010 private readonly string _mcpServerId ;
1111 private readonly string _functionName ;
1212
1313 public McpToolExecutor ( IServiceProvider services , string mcpServerId , string functionName )
14- {
14+ {
1515 _services = services ;
1616 _mcpServerId = mcpServerId ;
1717 _functionName = functionName ;
@@ -22,16 +22,22 @@ public async Task<bool> ExecuteAsync(RoleDialogModel message)
2222 try
2323 {
2424 // Convert arguments to dictionary format expected by mcpdotnet
25- Dictionary < string , object > argDict = JsonToDictionary ( message . FunctionArgs ) ;
25+ Dictionary < string , object ? > argDict = JsonToDictionary ( message . FunctionArgs ) ;
2626
2727 var clientManager = _services . GetRequiredService < McpClientManager > ( ) ;
2828 var client = await clientManager . GetMcpClientAsync ( _mcpServerId ) ;
2929
30+ if ( client == null )
31+ {
32+ message . Content = $ "MCP client for server { _mcpServerId } not found.";
33+ return false ;
34+ }
35+
3036 // Call the tool through mcpdotnet
3137 var result = await client . CallToolAsync ( _functionName , ! argDict . IsNullOrEmpty ( ) ? argDict : [ ] ) ;
3238
3339 // Extract the text content from the result
34- var json = string . Join ( "\n " , result . Content . Where ( c => c . Type == "text" ) . Select ( c => c . Text ) ) ;
40+ var json = string . Join ( "\n " , result . Content . Where ( c => c is TextContentBlock ) . Select ( c => ( ( TextContentBlock ) c ) . Text ) ) ;
3541
3642 message . Content = json ;
3743 message . Data = json . JsonContent ( ) ;
@@ -50,7 +56,7 @@ public async Task<string> GetIndicatorAsync(RoleDialogModel message)
5056 }
5157
5258
53- private static Dictionary < string , object > JsonToDictionary ( string ? json )
59+ private static Dictionary < string , object ? > JsonToDictionary ( string ? json )
5460 {
5561 if ( string . IsNullOrEmpty ( json ) )
5662 {
@@ -62,9 +68,9 @@ private static Dictionary<string, object> JsonToDictionary(string? json)
6268 return JsonElementToDictionary ( root ) ;
6369 }
6470
65- private static Dictionary < string , object > JsonElementToDictionary ( JsonElement element )
71+ private static Dictionary < string , object ? > JsonElementToDictionary ( JsonElement element )
6672 {
67- Dictionary < string , object > dictionary = [ ] ;
73+ Dictionary < string , object ? > dictionary = [ ] ;
6874
6975 if ( element . ValueKind == JsonValueKind . Object )
7076 {
0 commit comments