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

Function cli_execute

src/mcp_cli/adapters/cli.py:164–229  ·  view source on GitHub ↗

Convenience function to execute a unified command from CLI. This is a simplified interface for main.py to use when migrating from legacy action functions to unified commands. Args: command_name: Name of the command to execute **kwargs: Command parameters Retur

(command_name: str, **kwargs: Any)

Source from the content-addressed store, hash-verified

162
163
164async def cli_execute(command_name: str, **kwargs: Any) -> Any:
165 """
166 Convenience function to execute a unified command from CLI.
167
168 This is a simplified interface for main.py to use when migrating from
169 legacy action functions to unified commands.
170
171 Args:
172 command_name: Name of the command to execute
173 **kwargs: Command parameters
174
175 Returns:
176 Command result data (or True on success, False on failure)
177
178 Example:
179 await cli_execute("tools", raw=True, details=False)
180 """
181 from mcp_cli.commands.registry import UnifiedCommandRegistry
182
183 # Get registry instance
184 cmd_registry = UnifiedCommandRegistry()
185
186 # Look up command in registry
187 command = cmd_registry.get(command_name, mode=CommandMode.CLI)
188
189 if not command:
190 output.error(f"Unknown command: {command_name}")
191 return False
192
193 try:
194 # Add context if available (don't fail if not initialized)
195 if command.requires_context:
196 try:
197 context = get_context()
198 if context:
199 kwargs.setdefault("tool_manager", context.tool_manager)
200 kwargs.setdefault("model_manager", context.model_manager)
201 except RuntimeError:
202 # Context not initialized - command will run without it
203 pass
204
205 # Execute command
206 result = await command.execute(**kwargs)
207
208 # Handle result
209 if result.success:
210 if result.output:
211 # Output is already formatted by the command (str or Rich object)
212 output.print(result.output)
213
214 # Return data for programmatic use
215 return result.data if result.data else True
216
217 # Handle failure case
218 else:
219 if result.error:
220 output.error(result.error)
221 else:

Callers 15

main_callbackFunction · 0.90
_run_provider_commandFunction · 0.90
_tools_wrapperFunction · 0.90
_servers_wrapperFunction · 0.90
_resources_wrapperFunction · 0.90
_prompts_wrapperFunction · 0.90
_theme_wrapperFunction · 0.90
_token_wrapperFunction · 0.90
_tokens_wrapperFunction · 0.90
_cmd_wrapperFunction · 0.90
_ping_wrapperFunction · 0.90

Calls 4

getMethod · 0.95
get_contextFunction · 0.90
executeMethod · 0.45