|
| 1 | +import asyncio |
| 2 | +import sys |
| 3 | +from functools import partial |
| 4 | + |
| 5 | +import typer |
| 6 | + |
| 7 | + |
| 8 | +def _patch_anyio_open_process(): |
| 9 | + """ |
| 10 | + Patch anyio.open_process to allow detached processes on Windows and Unix-like systems. |
| 11 | +
|
| 12 | + This is necessary to prevent the MCP client from being interrupted by Ctrl+C when running in the CLI. |
| 13 | + """ |
| 14 | + import subprocess |
| 15 | + |
| 16 | + import anyio |
| 17 | + |
| 18 | + if getattr(anyio, "_tiny_agents_patched", False): |
| 19 | + return |
| 20 | + anyio._tiny_agents_patched = True |
| 21 | + |
| 22 | + original_open_process = anyio.open_process |
| 23 | + |
| 24 | + if sys.platform == "win32": |
| 25 | + # On Windows, we need to set the creation flags to create a new process group |
| 26 | + |
| 27 | + async def open_process_in_new_group(*args, **kwargs): |
| 28 | + """ |
| 29 | + Wrapper for open_process to handle Windows-specific process creation flags. |
| 30 | + """ |
| 31 | + # Ensure we pass the creation flags for Windows |
| 32 | + kwargs.setdefault("creationflags", subprocess.CREATE_NEW_PROCESS_GROUP) |
| 33 | + return await original_open_process(*args, **kwargs) |
| 34 | + |
| 35 | + anyio.open_process = open_process_in_new_group |
| 36 | + else: |
| 37 | + # For Unix-like systems, we can use setsid to create a new session |
| 38 | + async def open_process_in_new_group(*args, **kwargs): |
| 39 | + """ |
| 40 | + Wrapper for open_process to handle Unix-like systems with start_new_session=True. |
| 41 | + """ |
| 42 | + kwargs.setdefault("start_new_session", True) |
| 43 | + return await original_open_process(*args, **kwargs) |
| 44 | + |
| 45 | + anyio.open_process = open_process_in_new_group |
| 46 | + |
| 47 | + |
| 48 | +async def _async_prompt(exit_event: asyncio.Event, prompt: str = "» ") -> str: |
| 49 | + """ |
| 50 | + Asynchronous prompt function that reads input from stdin without blocking. |
| 51 | +
|
| 52 | + This function is designed to work in an asynchronous context, allowing the event loop to gracefully stop it (e.g. on Ctrl+C). |
| 53 | +
|
| 54 | + Alternatively, we could use https://github.com/vxgmichel/aioconsole but that would be an additional dependency. |
| 55 | + """ |
| 56 | + loop = asyncio.get_event_loop() |
| 57 | + |
| 58 | + if sys.platform == "win32": |
| 59 | + # Windows: Use run_in_executor to avoid blocking the event loop |
| 60 | + # Degraded solution: this is not ideal as user will have to CTRL+C once more to stop the prompt (and it'll not be graceful) |
| 61 | + return await loop.run_in_executor(None, partial(typer.prompt, prompt, prompt_suffix=" ")) |
| 62 | + else: |
| 63 | + # UNIX-like: Use loop.add_reader for non-blocking stdin read |
| 64 | + future = loop.create_future() |
| 65 | + |
| 66 | + def on_input(): |
| 67 | + line = sys.stdin.readline() |
| 68 | + loop.remove_reader(sys.stdin) |
| 69 | + future.set_result(line) |
| 70 | + |
| 71 | + print(prompt, end=" ", flush=True) |
| 72 | + loop.add_reader(sys.stdin, on_input) # not supported on Windows |
| 73 | + |
| 74 | + # Wait for user input or exit event |
| 75 | + # Wait until either the user hits enter or exit_event is set |
| 76 | + await asyncio.wait( |
| 77 | + [future, exit_event.wait()], |
| 78 | + return_when=asyncio.FIRST_COMPLETED, |
| 79 | + ) |
| 80 | + |
| 81 | + # Check which one has been triggered |
| 82 | + if exit_event.is_set(): |
| 83 | + future.cancel() |
| 84 | + return "" |
| 85 | + |
| 86 | + line = await future |
| 87 | + return line.strip() |
0 commit comments