list-sessions
Retrieve all active tmux sessions to view, manage, and interact with terminal environments through the Tmux MCP Server.
Instructions
List all active tmux sessions
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:27-50 (registration)Registers the MCP tool 'list-sessions' with server.tool(). The inline async handler fetches sessions using tmux.listSessions() and returns JSON-formatted text content or error.server.tool( "list-sessions", "List all active tmux sessions", {}, async () => { try { const sessions = await tmux.listSessions(); return { content: [{ type: "text", text: JSON.stringify(sessions, null, 2) }] }; } catch (error) { return { content: [{ type: "text", text: `Error listing tmux sessions: ${error}` }], isError: true }; } } );
- src/tmux.ts:82-97 (handler)Core handler logic for listing tmux sessions: executes tmux 'list-sessions' with custom format, parses output into TmuxSession array.export async function listSessions(): Promise<TmuxSession[]> { const format = "#{session_id}:#{session_name}:#{?session_attached,1,0}:#{session_windows}"; const output = await executeTmux(`list-sessions -F '${format}'`); if (!output) return []; return output.split('\n').map(line => { const [id, name, attached, windows] = line.split(':'); return { id, name, attached: attached === '1', windows: parseInt(windows, 10) }; }); }
- src/tmux.ts:8-13 (schema)Type definition (schema) for TmuxSession objects returned by listSessions().export interface TmuxSession { id: string; name: string; attached: boolean; windows: number; }
- src/tmux.ts:58-65 (helper)Utility helper to execute arbitrary tmux commands, used by listSessions().export async function executeTmux(tmuxCommand: string): Promise<string> { try { const { stdout } = await exec(`tmux ${tmuxCommand}`); return stdout.trim(); } catch (error: any) { throw new Error(`Failed to execute tmux command: ${error.message}`); } }