feat: Improve interactive CLI Ctrl+C handling #217
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request enhances the Ctrl+C (KeyboardInterrupt) handling within the interactive CLI to provide a more robust and user-friendly experience.
Changes Implemented:
Graceful Cancellation at Input Prompt (
src/mcp_agent/core/enhanced_prompt.py):agent_name >) would cause theget_enhanced_inputfunction to return "STOP", leading to the termination of the entire interactive session.KeyboardInterruptat the input prompt now results in:[yellow]Input cancelled. Type a command or 'STOP' to exit session.[/yellow]Cancellable Agent Tasks & Session Continuation (
src/mcp_agent/core/interactive_prompt.py):import asyncioforasyncio.CancelledError.prompt_loopnow wraps the call toawait send_func(...)(which executes agent tasks) in atry...exceptblock.KeyboardInterrupt(from Ctrl+C) andasyncio.CancelledErrorthat might occur during an ongoing agent operation (e.g., a long LLM call).[yellow]Request cancelled by user (Ctrl+C).[/yellow]or[yellow]Request task was cancelled.[/yellow]).rich.progressdisplay to prevent visual artifacts.continues, meaning the user is returned to a fresh input prompt, and the session remains active.Impact:
These changes address the issue where Ctrl+C would always terminate the interactive session, regardless of whether it was pressed at the prompt or during an agent's operation. The new behavior allows for more fine-grained control and a less disruptive user experience.