Skip to content

feat: Improve interactive CLI Ctrl+C handling #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

monotykamary
Copy link
Contributor

This pull request enhances the Ctrl+C (KeyboardInterrupt) handling within the interactive CLI to provide a more robust and user-friendly experience.

Changes Implemented:

  1. Graceful Cancellation at Input Prompt (src/mcp_agent/core/enhanced_prompt.py):

    • Previously, pressing Ctrl+C while at the interactive input prompt (agent_name >) would cause the get_enhanced_input function to return "STOP", leading to the termination of the entire interactive session.
    • This has been modified so that a KeyboardInterrupt at the input prompt now results in:
      • A message: [yellow]Input cancelled. Type a command or 'STOP' to exit session.[/yellow]
      • The input field being cleared.
      • The interactive session continuing, allowing the user to enter a new command.
  2. Cancellable Agent Tasks & Session Continuation (src/mcp_agent/core/interactive_prompt.py):

    • Added import asyncio for asyncio.CancelledError.
    • The prompt_loop now wraps the call to await send_func(...) (which executes agent tasks) in a try...except block.
    • This block catches KeyboardInterrupt (from Ctrl+C) and asyncio.CancelledError that might occur during an ongoing agent operation (e.g., a long LLM call).
    • Upon catching such an interruption:
      • A message is displayed (e.g., [yellow]Request cancelled by user (Ctrl+C).[/yellow] or [yellow]Request task was cancelled.[/yellow]).
      • An attempt is made to stop and clear any active rich.progress display to prevent visual artifacts.
      • The interactive loop continues, meaning the user is returned to a fresh input prompt, and the session remains active.

Impact:

  • Users can now safely press Ctrl+C at the input prompt without unintentionally exiting their interactive session.
  • Long-running agent tasks can be interrupted using Ctrl+C, and the user can immediately issue new commands within the same session, significantly improving workflow efficiency and responsiveness.

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.

@monotykamary monotykamary force-pushed the feat/interactive-ctrl-c-cancel branch from 856b752 to 484338e Compare June 6, 2025 04:25
- Modify enhanced_prompt.py to allow session continuation when Ctrl+C
  is pressed at the input prompt.
- Modify interactive_prompt.py to catch KeyboardInterrupt and
  asyncio.CancelledError during agent tasks, allowing the session to
  continue after task cancellation.
@monotykamary monotykamary force-pushed the feat/interactive-ctrl-c-cancel branch from 484338e to ffd57ad Compare June 6, 2025 04:29
@evalstate
Copy link
Owner

Hi @monotykamary -- would you mind taking a look at the check failures on these PRs please? TY!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants