MCPcopy Create free account
hub / github.com/IBM/mcp-cli / execute_tool

Method execute_tool

src/mcp_cli/tools/dynamic_tools.py:90–150  ·  view source on GitHub ↗

Execute a tool via the tool manager. Handles MCP-specific result unwrapping. Args: tool_name: Name of the tool to execute arguments: Arguments to pass to the tool Returns: Execution result dict

(
        self,
        tool_name: str,
        arguments: dict[str, Any],
    )

Source from the content-addressed store, hash-verified

88 return await self.tool_manager.get_all_tools()
89
90 async def execute_tool(
91 self,
92 tool_name: str,
93 arguments: dict[str, Any],
94 ) -> dict[str, Any]:
95 """Execute a tool via the tool manager.
96
97 Handles MCP-specific result unwrapping.
98
99 Args:
100 tool_name: Name of the tool to execute
101 arguments: Arguments to pass to the tool
102
103 Returns:
104 Execution result dict
105 """
106 try:
107 result = await self.tool_manager.execute_tool(
108 tool_name=tool_name,
109 arguments=arguments,
110 namespace=None, # Let tool manager figure out the namespace
111 timeout=None, # Use default timeout
112 )
113
114 if result.success:
115 logger.info(
116 f"call_tool('{tool_name}') succeeded, "
117 f"result type: {type(result.result)}"
118 )
119 # Extract and unwrap the actual result value
120 actual_result = self._unwrap_result(result.result)
121
122 # Format the result for the LLM
123 try:
124 formatted_result = self.tool_manager.format_tool_response(
125 actual_result
126 )
127 logger.debug(f"Formatted result: {formatted_result}")
128 return {
129 "success": True,
130 "result": formatted_result,
131 }
132 except Exception as fmt_error:
133 logger.error(f"Error formatting result: {fmt_error}", exc_info=True)
134 return {
135 "success": True,
136 "result": str(actual_result),
137 }
138 else:
139 logger.warning(f"call_tool('{tool_name}') failed: {result.error}")
140 return {
141 "success": False,
142 "error": result.error or "Tool execution failed",
143 }
144
145 except Exception as e:
146 logger.error(f"Error executing tool '{tool_name}': {e}", exc_info=True)
147 return {

Callers

nothing calls this directly

Calls 2

_unwrap_resultMethod · 0.95
format_tool_responseMethod · 0.45

Tested by

no test coverage detected