@@ -16,24 +16,34 @@ Textual widgets responsible for UI rendering:
1616- ** UserInput** : Handles user text input and submission
1717- ** ThinkingIndicator** : Shows when agent is processing
1818
19- ### Utils Layer
19+ ### System Layer
2020
21- #### Agent Loop (` agent_loop.py ` )
21+ #### Agent Loop (` system/ agent_loop.py` )
2222Manages the conversation loop with Claude SDK:
2323- Maintains async queue for user queries
2424- Handles streaming responses
2525- Parses SDK messages into structured AgentMessage objects
2626- Emits AgentMessageType events (STREAM_EVENT, ASSISTANT, RESULT)
2727- Manages session persistence via session_id
28+ - Supports dynamic MCP server inference and loading
29+
30+ #### MCP Server Inference (` system/mcp_inference.py ` )
31+ Intelligently determines which MCP servers are needed for each query:
32+ - Uses a persistent Haiku client for fast inference (~ 1-3s after initial boot)
33+ - Analyzes user queries to infer required servers
34+ - Maintains a cached set of inferred servers across conversation
35+ - Returns only newly needed servers to minimize reconnections
36+ - Can be disabled via ` mcp_server_inference: false ` config option
2837
29- #### Message Bus (` message_bus.py ` )
38+ #### Message Bus (` system/ message_bus.py` )
3039Routes agent messages to appropriate UI components:
3140- Handles streaming text updates
3241- Mounts tool use messages
3342- Controls thinking indicator state
3443- Manages scroll-to-bottom behavior
44+ - Displays system messages (e.g., MCP server connection notifications)
3545
36- #### Actions (` actions.py ` )
46+ #### Actions (` system/ actions.py` )
3747Centralizes all user-initiated actions and controls:
3848- ** quit()** : Exits the application
3949- ** query(user_input)** : Sends user query to agent loop queue
@@ -46,15 +56,20 @@ Actions are triggered via:
4656- Keybindings in app.py (ESC → action_interrupt, Ctrl+N → action_new)
4757- Text commands in user_input.py ("exit", "clear")
4858
49- #### Config (` config.py ` )
59+ ### Utils Layer
60+
61+ #### Config (` utils/config.py ` )
5062Loads and validates YAML configuration:
5163- Filters disabled MCP servers
5264- Loads prompts from files
5365- Expands environment variables
5466- Combines system prompt with MCP server prompts
67+ - Provides ` get_sdk_config() ` to filter app-specific config before passing to SDK
5568
5669## Data Flow
5770
71+ ### Standard Query Flow (with MCP Inference enabled)
72+
5873```
5974User Input
6075 ↓
@@ -64,7 +79,16 @@ MessagePosted event → ChatHistory (immediate UI update)
6479 ↓
6580Actions.query(user_input) → AgentLoop.query_queue.put()
6681 ↓
67- Claude SDK (streaming response)
82+ AgentLoop: MCP Server Inference (if enabled)
83+ ↓
84+ infer_mcp_servers(user_message) → Haiku query
85+ ↓
86+ If new servers needed:
87+ - Post SYSTEM message ("Connecting to [servers]...")
88+ - Disconnect client
89+ - Reconnect with new servers (preserving session_id)
90+ ↓
91+ Claude SDK (streaming response with connected MCP tools)
6892 ↓
6993AgentLoop._handle_message
7094 ↓
@@ -73,9 +97,26 @@ AgentMessage (typed message) → MessageBus.handle_agent_message
7397Match on AgentMessageType:
7498 - STREAM_EVENT → Update streaming message widget
7599 - ASSISTANT → Mount tool use widgets
100+ - SYSTEM → Display system notification
76101 - RESULT → Reset thinking indicator
77102```
78103
104+ ### Query Flow (with MCP Inference disabled)
105+
106+ ```
107+ User Input
108+ ↓
109+ UserInput.on_input_submitted
110+ ↓
111+ MessagePosted event → ChatHistory (immediate UI update)
112+ ↓
113+ Actions.query(user_input) → AgentLoop.query_queue.put()
114+ ↓
115+ Claude SDK (all servers pre-connected at startup)
116+ ↓
117+ [Same as above from _handle_message onwards]
118+ ```
119+
79120### Control Commands Flow
80121```
81122User Action (ESC, Ctrl+N, "clear", "exit")
@@ -138,14 +179,38 @@ class Message:
138179Configuration is loaded from ` agent-chat-cli.config.yaml ` :
139180- ** system_prompt** : Base system prompt (supports file paths)
140181- ** model** : Claude model to use
141- - ** include_partial_messages** : Enable streaming
182+ - ** include_partial_messages** : Enable streaming responses (default: true)
183+ - ** mcp_server_inference** : Enable dynamic MCP server inference (default: true)
184+ - When ` true ` : App boots instantly without MCP servers, connects only when needed
185+ - When ` false ` : All enabled MCP servers load at startup (traditional behavior)
142186- ** mcp_servers** : MCP server configurations (filtered by enabled flag)
143187- ** agents** : Named agent configurations
144188- ** disallowed_tools** : Tool filtering
145189- ** permission_mode** : Permission handling mode
146190
147191MCP server prompts are automatically appended to the system prompt.
148192
193+ ### MCP Server Inference
194+
195+ When ` mcp_server_inference: true ` (default):
196+
197+ 1 . ** Fast Boot** : App starts without connecting to any MCP servers
198+ 2 . ** Smart Detection** : Before each query, Haiku analyzes which servers are needed
199+ 3 . ** Dynamic Loading** : Only connects to newly required servers
200+ 4 . ** Session Preservation** : Maintains conversation history when reconnecting with new servers
201+ 5 . ** Performance** : ~ 1-3s inference latency after initial boot (first query ~ 8-12s)
202+
203+ Example config:
204+ ``` yaml
205+ mcp_server_inference : true # or false to disable
206+
207+ mcp_servers :
208+ github :
209+ description : " Search code, PRs, issues"
210+ enabled : true
211+ # ... rest of config
212+ ```
213+
149214## User Commands
150215
151216### Text Commands
0 commit comments