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)
| 162 | |
| 163 | |
| 164 | async 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: |