@@ -1342,41 +1342,85 @@ def cmd_mcp_tools(self, args: list):
13421342 console .print ()
13431343
13441344 try :
1345- tools_by_server = asyncio .run (self .mcp_manager .list_tools (server_name ))
1345+ self ._run_mcp_async (self ._mcp_tools_async (server_name ))
1346+ except Exception as e :
1347+ from ..mcp .exceptions import MCPError , format_mcp_error
13461348
1347- if not tools_by_server :
1348- show_warning_message ("No tools available" )
1349- console .print ()
1350- return
1349+ # Use formatted error for MCP errors
1350+ if isinstance (e , MCPError ):
1351+ error_msg = format_mcp_error (e , verbose = True )
1352+ show_error_message (error_msg )
1353+ else :
1354+ # For other exceptions, show the error with details
1355+ error_parts = [f"Failed to list tools: { str (e ) or type (e ).__name__ } " ]
1356+ if hasattr (e , "__cause__" ) and e .__cause__ :
1357+ error_parts .append (f"\n Cause: { e .__cause__ } " )
1358+ show_error_message ("\n " .join (error_parts ))
1359+ # Print traceback for debugging
1360+ import traceback
1361+ logger .debug ("Error listing MCP tools" , exc_info = True )
1362+
1363+ async def _mcp_tools_async (self , server_name : str | None ):
1364+ """Async helper for MCP tools listing."""
1365+ from ..mcp .exceptions import MCPConnectionError , MCPOperationError
1366+
1367+ # Auto-connect if server is specified but not connected
1368+ if server_name :
1369+ session = await self .mcp_manager .get_session (server_name )
1370+ if not session :
1371+ show_info_message (f"Connecting to '{ server_name } '..." )
1372+ try :
1373+ await self .mcp_manager .connect (server_name )
1374+ show_success_message (f"Connected to '{ server_name } '" )
1375+ except MCPConnectionError as e :
1376+ # Re-raise connection errors so they can be formatted properly
1377+ raise
1378+ except Exception as e :
1379+ # Wrap other connection errors
1380+ from ..mcp .exceptions import MCPConnectionError as MCPConnErr
1381+ raise MCPConnErr (
1382+ f"Failed to connect to '{ server_name } ': { e } " ,
1383+ server_name = server_name ,
1384+ details = {"error" : str (e ), "error_type" : type (e ).__name__ },
1385+ )
13511386
1352- for srv_name , tools in tools_by_server .items ():
1353- table = Table (title = f"Tools from '{ srv_name } '" )
1354- table .add_column ("Name" , style = "cyan" )
1355- table .add_column ("Description" , style = "white" )
1387+ try :
1388+ tools_by_server = await self .mcp_manager .list_tools (server_name )
1389+ except MCPOperationError :
1390+ # Re-raise operation errors so they can be formatted properly
1391+ raise
13561392
1357- for tool in tools :
1358- table .add_row (tool .name , tool .description or "" )
1393+ if not tools_by_server :
1394+ show_warning_message ("No tools available" )
1395+ console .print ()
1396+ return
13591397
1360- console .print (table )
1361- console .print ()
1398+ for srv_name , tools in tools_by_server .items ():
1399+ table = Table (title = f"Tools from '{ srv_name } '" )
1400+ table .add_column ("Name" , style = "cyan" )
1401+ table .add_column ("Description" , style = "white" )
13621402
1363- # Show detailed schema for each tool
1364- console .print ("[bold]Tool Schemas:[/bold]" )
1365- for tool in tools :
1366- console .print (f"\n [cyan]{ tool .name } [/cyan]" )
1367- if tool .description :
1368- console .print (f" Description: { tool .description } " )
1369- if hasattr (tool , "inputSchema" ) and tool .inputSchema :
1370- import json
1371-
1372- schema_str = json .dumps (tool .inputSchema , indent = 2 )
1373- console .print (" Schema:" )
1374- console .print (f" [dim]{ schema_str } [/dim]" )
1375- console .print ()
1403+ for tool in tools :
1404+ table .add_row (tool .name , tool .description or "" )
13761405
1406+ console .print (table )
13771407 console .print ()
1378- except Exception as e :
1379- show_error_message (f"Failed to list tools: { e } " )
1408+
1409+ # Show detailed schema for each tool
1410+ console .print ("[bold]Tool Schemas:[/bold]" )
1411+ for tool in tools :
1412+ console .print (f"\n [cyan]{ tool .name } [/cyan]" )
1413+ if tool .description :
1414+ console .print (f" Description: { tool .description } " )
1415+ if hasattr (tool , "inputSchema" ) and tool .inputSchema :
1416+ import json
1417+
1418+ schema_str = json .dumps (tool .inputSchema , indent = 2 )
1419+ console .print (" Schema:" )
1420+ console .print (f" [dim]{ schema_str } [/dim]" )
1421+ console .print ()
1422+
1423+ console .print ()
13801424
13811425 def cmd_mcp_call (self , args : list ):
13821426 """
0 commit comments