@@ -28,30 +28,40 @@ public McpToolAdapter(
28
28
29
29
public async Task < bool > Execute ( RoleDialogModel message )
30
30
{
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 ;
37
39
38
- var client = await _clientManager . GetMcpClientAsync ( serverId ) ;
40
+ var client = await _clientManager . GetMcpClientAsync ( serverId ) ;
39
41
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 : [ ] ) ;
42
44
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 ) ) ;
45
47
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
+ }
49
57
}
50
58
51
59
private static Dictionary < string , object > JsonToDictionary ( string ? json )
52
60
{
53
61
if ( string . IsNullOrEmpty ( json ) )
62
+ {
54
63
return [ ] ;
64
+ }
55
65
56
66
using JsonDocument doc = JsonDocument . Parse ( json ) ;
57
67
JsonElement root = doc . RootElement ;
0 commit comments