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

Method _execute_tool_direct

src/mcp_cli/commands/cmd/cmd.py:147–214  ·  view source on GitHub ↗

Execute a tool directly without LLM interaction.

(
        self,
        tool_manager: Any | None,
        tool_name: str,
        tool_args_json: str | None,
        output_file: str | None,
        raw: bool,
    )

Source from the content-addressed store, hash-verified

145 # ── tool direct execution ────────────────────────────────────────
146
147 async def _execute_tool_direct(
148 self,
149 tool_manager: Any | None,
150 tool_name: str,
151 tool_args_json: str | None,
152 output_file: str | None,
153 raw: bool,
154 ) -> CommandResult:
155 """Execute a tool directly without LLM interaction."""
156 if not tool_manager:
157 return CommandResult(
158 success=False,
159 error="Tool manager not available. Are servers connected?",
160 )
161
162 # Parse tool arguments
163 tool_args: dict[str, Any] = {}
164 if tool_args_json:
165 try:
166 tool_args = json.loads(tool_args_json)
167 except json.JSONDecodeError as e:
168 return CommandResult(
169 success=False,
170 error=f"Invalid JSON in tool arguments: {e}",
171 )
172
173 try:
174 if not raw:
175 output.info(f"Executing tool: {tool_name}")
176
177 tool_call_result = await tool_manager.execute_tool(tool_name, tool_args)
178
179 if not tool_call_result.success or tool_call_result.error:
180 return CommandResult(
181 success=False,
182 error=f"Tool execution failed: {tool_call_result.error}",
183 )
184
185 result_data = tool_call_result.result
186
187 # Unwrap middleware ToolExecutionResult if present
188 result_data = unwrap_tool_result(result_data)
189
190 # Convert to JSON-serializable form
191 result_data = to_serializable(result_data)
192
193 # Format result
194 result_str = (
195 json.dumps(result_data, indent=None if raw else 2)
196 if not isinstance(result_data, str)
197 else result_data
198 )
199
200 # Write output
201 if output_file and output_file != "-":
202 Path(output_file).write_text(result_str)
203 if not raw:
204 output.success(f"Output written to: {output_file}")

Callers 1

executeMethod · 0.95

Calls 5

CommandResultClass · 0.90
unwrap_tool_resultFunction · 0.90
to_serializableFunction · 0.90
successMethod · 0.80
execute_toolMethod · 0.45

Tested by

no test coverage detected