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

Function run_agent_loop

src/mcp_cli/agents/loop.py:17–113  ·  view source on GitHub ↗

Run a headless chat loop for a spawned agent. Similar to ``_run_enhanced_chat_loop`` in ``chat_handler.py`` but: * No terminal I/O — reads prompts from *input_queue*. * Uses ``HeadlessUIManager`` (no rich/curses). * Reports completion via *done_event*. * Returns a summary of th

(
    context: Any,
    ui_manager: Any,
    input_queue: asyncio.Queue,
    done_event: asyncio.Event,
    max_turns: int = 50,
)

Source from the content-addressed store, hash-verified

15
16
17async def run_agent_loop(
18 context: Any,
19 ui_manager: Any,
20 input_queue: asyncio.Queue,
21 done_event: asyncio.Event,
22 max_turns: int = 50,
23) -> str:
24 """Run a headless chat loop for a spawned agent.
25
26 Similar to ``_run_enhanced_chat_loop`` in ``chat_handler.py`` but:
27
28 * No terminal I/O — reads prompts from *input_queue*.
29 * Uses ``HeadlessUIManager`` (no rich/curses).
30 * Reports completion via *done_event*.
31 * Returns a summary of the final assistant response.
32
33 Parameters
34 ----------
35 context:
36 ChatContext for this agent.
37 ui_manager:
38 HeadlessUIManager instance.
39 input_queue:
40 ``asyncio.Queue`` that receives user/supervisor prompts.
41 done_event:
42 Set when the loop finishes.
43 max_turns:
44 Maximum conversation turns per prompt.
45
46 Returns
47 -------
48 str
49 Summary of the agent's last response, or error description.
50 """
51 from mcp_cli.chat.conversation import ConversationProcessor
52
53 convo = ConversationProcessor(context, ui_manager)
54 last_response = ""
55
56 # Wire dashboard bridge if present
57 if bridge := getattr(context, "dashboard_bridge", None):
58 bridge.set_input_queue(input_queue)
59
60 try:
61 while not context.exit_requested:
62 try:
63 # Wait for a prompt (with timeout so we can check exit)
64 try:
65 user_msg = await asyncio.wait_for(input_queue.get(), timeout=1.0)
66 except asyncio.TimeoutError:
67 continue
68
69 if user_msg is None:
70 continue
71
72 # Exit signals
73 msg_lower = str(user_msg).lower().strip()
74 if msg_lower in ("exit", "quit", "__stop__"):

Calls 7

process_conversationMethod · 0.95
set_input_queueMethod · 0.80
on_messageMethod · 0.80
setMethod · 0.80
getMethod · 0.45
add_user_messageMethod · 0.45