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

Method handle_command

src/mcp_cli/adapters/interactive.py:37–125  ·  view source on GitHub ↗

Handle an interactive command. Args: command_line: The full command line (e.g., "servers --detailed"). Returns: True if command was handled, False otherwise.

(command_line: str)

Source from the content-addressed store, hash-verified

35
36 @staticmethod
37 async def handle_command(command_line: str) -> bool:
38 """
39 Handle an interactive command.
40
41 Args:
42 command_line: The full command line (e.g., "servers --detailed").
43
44 Returns:
45 True if command was handled, False otherwise.
46 """
47 if not command_line.strip():
48 return False
49
50 # Parse command line using shell-style parsing
51 try:
52 parts = shlex.split(command_line)
53 except ValueError as e:
54 output.error(f"Invalid command syntax: {e}")
55 return False
56
57 if not parts:
58 return False
59
60 command_name = parts[0]
61
62 # Handle slash commands - strip the leading slash if present
63 if command_name.startswith("/"):
64 command_name = command_name[1:]
65
66 args = parts[1:] if len(parts) > 1 else []
67
68 logger.debug(f"Parsed command: {command_name}, args: {args}")
69
70 # Look up command in registry
71 command = registry.get(command_name, mode=CommandMode.INTERACTIVE)
72 if not command:
73 # Not a registered command, might be a shell command
74 return False
75
76 # Parse arguments into kwargs
77 kwargs = InteractiveCommandAdapter._parse_arguments(command, args)
78
79 # Add context if the command needs it
80 if command.requires_context:
81 context = get_context()
82 if context:
83 kwargs["tool_manager"] = context.tool_manager
84 kwargs["model_manager"] = context.model_manager
85
86 # Validate parameters
87 error = command.validate_parameters(**kwargs)
88 if error:
89 output.error(error)
90 return True # Command was handled, just had an error
91
92 try:
93 # Execute command
94 result = await command.execute(**kwargs)

Callers

nothing calls this directly

Calls 6

get_contextFunction · 0.90
validate_parametersMethod · 0.80
getMethod · 0.45
_parse_argumentsMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected