Architecture
MoMCP-TUI follows a modular architecture with clear separation of concerns.
High-Level Architecture
MoMCP-TUI is organized into four layers. The TUI Layer handles all user interaction through Textual, including the chat interface, modal dialogs, and command palette. The Core Layer contains the LLM client for communicating with model APIs, the MCP manager for orchestrating tool servers, and the config manager. The MCP Layer manages connections to local (stdio) and remote (HTTP) tool servers. The Utility Layer provides cross-cutting concerns like token compression, cancellation, clipboard access, and internationalization.
Request Flow
When you send a message, the TUI appends it to the conversation history and passes the full context along with available tool definitions to the LLM client. The LLM streams back its thinking process and response in real-time. If the model decides a tool is needed, it emits a tool call event — the TUI routes this to the MCP manager, which executes the tool on the appropriate server and feeds the result back to the LLM. This loop can repeat up to 12 times for complex multi-step tasks.
Threading Model
MoMCP-TUI uses a multi-thread architecture:
The MCP manager runs its own asyncio event loop in a daemon thread, using run_coroutine_threadsafe to bridge sync/async boundaries.
Key Design Decisions
- Sync-first TUI: The Textual app runs synchronously; LLM calls happen in background threads
- Async MCP: MCP servers use asyncio for concurrent tool calls
- Token-based compression: Conversations are compressed when they exceed
max_context_tokens - Whitelist security: File access is restricted to explicitly allowed paths