Handle a chat command. Args: command_text: The full command text (e.g., "/servers --detailed"). context: Optional context with tool_manager, etc. Returns: True if command was handled, False otherwise.
(
command_text: str, context: dict[str, Any] | None = None
)
| 67 | |
| 68 | @staticmethod |
| 69 | async def handle_command( |
| 70 | command_text: str, context: dict[str, Any] | None = None |
| 71 | ) -> bool: |
| 72 | """ |
| 73 | Handle a chat command. |
| 74 | |
| 75 | Args: |
| 76 | command_text: The full command text (e.g., "/servers --detailed"). |
| 77 | context: Optional context with tool_manager, etc. |
| 78 | |
| 79 | Returns: |
| 80 | True if command was handled, False otherwise. |
| 81 | """ |
| 82 | if not command_text.startswith("/"): |
| 83 | return False |
| 84 | |
| 85 | # Parse command and arguments using shlex for proper quote handling |
| 86 | import shlex |
| 87 | |
| 88 | # Remove leading slash and parse with proper quote handling |
| 89 | try: |
| 90 | parts = shlex.split(command_text[1:]) |
| 91 | except ValueError as e: |
| 92 | # Handle unmatched quotes |
| 93 | output.error(f"Invalid command format: {e}") |
| 94 | return False |
| 95 | |
| 96 | if not parts: |
| 97 | # Just "/" typed - show command menu |
| 98 | return await ChatCommandAdapter._show_command_menu(context) |
| 99 | |
| 100 | command_name = parts[0] |
| 101 | args = parts[1:] if len(parts) > 1 else [] |
| 102 | |
| 103 | # Get registry instance |
| 104 | registry = UnifiedCommandRegistry() |
| 105 | |
| 106 | # Look up command in registry (this handles subcommands internally) |
| 107 | # For command groups like "tools list", registry.get handles the full path |
| 108 | full_command_path = " ".join([command_name] + (args[:1] if args else [])) |
| 109 | command = registry.get(full_command_path, mode=CommandMode.CHAT) |
| 110 | |
| 111 | # If not found as subcommand, try just the base command |
| 112 | if not command: |
| 113 | command = registry.get(command_name, mode=CommandMode.CHAT) |
| 114 | |
| 115 | if not command: |
| 116 | output.error(f"Unknown command: /{command_name}") |
| 117 | return False |
| 118 | |
| 119 | # For command groups, check if we got a subcommand |
| 120 | from mcp_cli.commands.base import CommandGroup |
| 121 | |
| 122 | if isinstance(command, CommandGroup) and args: |
| 123 | # The first arg might be the subcommand |
| 124 | subcommand_name = args[0] |
| 125 | if subcommand_name in command.subcommands: |
| 126 | # It's a subcommand, adjust the args |
nothing calls this directly
no test coverage detected