|
| 1 | +(architecture)= |
| 2 | + |
| 3 | +# Architecture |
| 4 | + |
| 5 | +For contributors who need to understand the codebase internals. |
| 6 | + |
| 7 | +## Source layout |
| 8 | + |
| 9 | +``` |
| 10 | +src/libtmux_mcp/ |
| 11 | + __init__.py # Entry point: main() |
| 12 | + __main__.py # python -m libtmux_mcp support |
| 13 | + server.py # FastMCP instance and configuration |
| 14 | + _utils.py # Server caching, resolvers, serializers, error handling |
| 15 | + models.py # Pydantic output models |
| 16 | + middleware.py # Safety tier middleware |
| 17 | + tools/ |
| 18 | + server_tools.py # list_sessions, create_session, kill_server, get_server_info |
| 19 | + session_tools.py # list_windows, create_window, rename_session, kill_session |
| 20 | + window_tools.py # list_panes, split_window, rename_window, kill_window, select_layout, resize_window |
| 21 | + pane_tools.py # send_keys, capture_pane, resize_pane, kill_pane, set_pane_title, get_pane_info, clear_pane, search_panes, wait_for_text |
| 22 | + option_tools.py # show_option, set_option |
| 23 | + env_tools.py # show_environment, set_environment |
| 24 | + resources/ |
| 25 | + hierarchy.py # tmux:// URI resources |
| 26 | +``` |
| 27 | + |
| 28 | +## Request flow |
| 29 | + |
| 30 | +``` |
| 31 | +MCP Client (Claude, Cursor, etc.) |
| 32 | + → stdio transport |
| 33 | + → FastMCP server (server.py) |
| 34 | + → SafetyMiddleware (middleware.py) |
| 35 | + → Tool function (tools/*.py) |
| 36 | + → libtmux Server/Session/Window/Pane |
| 37 | + → tmux binary (via subprocess) |
| 38 | +``` |
| 39 | + |
| 40 | +## Key design decisions |
| 41 | + |
| 42 | +### Tool registration |
| 43 | + |
| 44 | +Each tool module defines a `register(mcp)` function that registers tools with metadata: |
| 45 | +- `title` — human-readable name |
| 46 | +- `annotations` — MCP tool annotations (readOnlyHint, destructiveHint, idempotentHint) |
| 47 | +- `tags` — safety tier tags for middleware filtering |
| 48 | + |
| 49 | +### Server caching |
| 50 | + |
| 51 | +`_utils.py` maintains a thread-safe cache keyed by `(socket_name, socket_path, tmux_bin)`. Dead servers are evicted on access via `is_alive()` checks. |
| 52 | + |
| 53 | +### Object resolution |
| 54 | + |
| 55 | +Tools use resolver functions (`_resolve_session`, `_resolve_window`, `_resolve_pane`) that accept multiple targeting parameters and resolve to the correct libtmux object. Resolution follows a priority chain: direct ID → name lookup → error. |
| 56 | + |
| 57 | +### Safety middleware |
| 58 | + |
| 59 | +`SafetyMiddleware` implements FastMCP's middleware interface. It operates as a secondary gate behind FastMCP's native tag visibility system, providing clear error messages when a tool above the configured tier is invoked. |
| 60 | + |
| 61 | +### Error handling |
| 62 | + |
| 63 | +The `@handle_tool_errors` decorator wraps all tool functions, catching libtmux exceptions and converting them to `ToolError` with descriptive messages. |
| 64 | + |
| 65 | +## References |
| 66 | + |
| 67 | +- [libtmux](https://libtmux.git-pull.com/) — Core tmux Python library |
| 68 | +- [FastMCP](https://github.com/jlowin/fastmcp) — MCP server framework |
| 69 | +- [MCP Specification](https://modelcontextprotocol.io/) — Model Context Protocol |
| 70 | +- [tmux man page](http://man.openbsd.org/OpenBSD-current/man1/tmux.1) |
0 commit comments