SwiftHome

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

MoreLess about this notice

libtmux for Swift

Swift-based tmux client

tmux client library for Swift. Control tmux, connect to and create servers, navigate sessions, windows, and panes, send keys, watch for changes via control mode, capture pane contents

Product

Control tmux from Swift code 0.x Released; the API may change. Read more Core Library home →

import Foundation
import LibTmux
@main
struct Quickstart {
static func main() async throws {
let server = try Server(
socketName: "libtmux-home-\(UUID().uuidString)",
configurationFile: "/dev/null"
)
try await work(server)
}
static func work(_ server: Server) async throws {
let session = try await server.newSession(
named: "demo",
shell: "/bin/sh"
)
let layout = try await server.snapshot()
guard let editor = layout.windows(of: session).first,
let left = layout.panes(of: editor).first(where: \.isActive)
else {
throw QuickstartError.missing
}
try await server.rename(editor, to: "editor")
let right = try await server.split(
left,
direction: .right,
shell: "/bin/sh"
)
let logs = try await server.newWindow(
in: session,
named: "logs",
shell: "/bin/sh"
)
try await server.run("printf 'hello from %s\\n' tmux", in: right)
let ready = try RegexPattern("^hello from tmux$")
let result = try await server.waitForOutput(
in: right,
matching: [ready]
)
guard result.outcome == .matched else {
throw QuickstartError.outputTimedOut
}
try await server.select(right)
try await server.select(logs.link)
let snapshot = try await server.snapshot()
for session in snapshot.sessions {
print("session \(session.name)")
let windows = snapshot.windows(of: session)
let links = snapshot.windowLinks(of: session)
for (window, link) in zip(windows, links) {
let panes = snapshot.panes(of: window)
let ids = panes.map { $0.id.rawValue }.joined(separator: " ")
print(" window \(link.index):\(window.name): \(ids)")
}
}
let home = right.windowID
guard let window = snapshot.windows.first(where: { $0.id == home }),
let owner = snapshot.sessions(of: window).first
else {
throw QuickstartError.missing
}
print("\(right.id.rawValue) is in window \(window.name)"
+ " of session \(owner.name)")
}
enum QuickstartError: Error {
case missing
case outputTimedOut
}
}
import Foundation
import LibTmux
@main
struct Quickstart {
static func main() async throws {
let server = try Server(
socketName: "libtmux-home-\(UUID().uuidString)",
configurationFile: "/dev/null"
)
do {
try await work(server)
} catch {
print("tmux quickstart failed: \(error)")
throw error
}
}
static func work(_ server: Server) async throws {
let session = try await server.newSession(
named: "demo",
shell: "/bin/sh"
)
let layout = try await server.snapshot()
guard let editor = layout.windows(of: session).first,
let left = layout.panes(of: editor).first(where: \.isActive)
else {
throw QuickstartError.missing
}
try await server.rename(editor, to: "editor")
let right = try await server.split(
left,
direction: .right,
shell: "/bin/sh"
)
let logs = try await server.newWindow(
in: session,
named: "logs",
shell: "/bin/sh"
)
try await server.run("printf 'hello from %s\\n' tmux", in: right)
let ready = try RegexPattern("^hello from tmux$")
let result = try await server.waitForOutput(
in: right,
matching: [ready]
)
guard result.outcome == .matched else {
throw QuickstartError.outputTimedOut
}
try await server.select(right)
try await server.select(logs.link)
let snapshot = try await server.snapshot()
for session in snapshot.sessions {
print("session \(session.name)")
let windows = snapshot.windows(of: session)
let links = snapshot.windowLinks(of: session)
for (window, link) in zip(windows, links) {
let panes = snapshot.panes(of: window)
let ids = panes.map { $0.id.rawValue }.joined(separator: " ")
print(" window \(link.index):\(window.name): \(ids)")
}
}
let home = right.windowID
guard let window = snapshot.windows.first(where: { $0.id == home }),
let owner = snapshot.sessions(of: window).first
else {
throw QuickstartError.missing
}
print("\(right.id.rawValue) is in window \(window.name)"
+ " of session \(owner.name)")
}
enum QuickstartError: Error {
case missing
case outputTimedOut
}
}
import Foundation
import LibTmux
@main
struct Quickstart {
static func main() async throws {
let server = try Server(
socketName: "libtmux-home-\(UUID().uuidString)",
configurationFile: "/dev/null"
)
do {
try await work(server)
} catch {
if try await server.isRunning() {
try await server.killServer()
}
throw error
}
try await server.killServer()
}
static func work(_ server: Server) async throws {
let session = try await server.newSession(
named: "demo",
shell: "/bin/sh"
)
let layout = try await server.snapshot()
guard let editor = layout.windows(of: session).first,
let left = layout.panes(of: editor).first(where: \.isActive)
else {
throw QuickstartError.missing
}
try await server.rename(editor, to: "editor")
let right = try await server.split(
left,
direction: .right,
shell: "/bin/sh"
)
let logs = try await server.newWindow(
in: session,
named: "logs",
shell: "/bin/sh"
)
try await server.run("printf 'hello from %s\\n' tmux", in: right)
let ready = try RegexPattern("^hello from tmux$")
let result = try await server.waitForOutput(
in: right,
matching: [ready]
)
guard result.outcome == .matched else {
throw QuickstartError.outputTimedOut
}
try await server.select(right)
try await server.select(logs.link)
let snapshot = try await server.snapshot()
for session in snapshot.sessions {
print("session \(session.name)")
let windows = snapshot.windows(of: session)
let links = snapshot.windowLinks(of: session)
for (window, link) in zip(windows, links) {
let panes = snapshot.panes(of: window)
let ids = panes.map { $0.id.rawValue }.joined(separator: " ")
print(" window \(link.index):\(window.name): \(ids)")
}
}
let home = right.windowID
guard let window = snapshot.windows.first(where: { $0.id == home }),
let owner = snapshot.sessions(of: window).first
else {
throw QuickstartError.missing
}
print("\(right.id.rawValue) is in window \(window.name)"
+ " of session \(owner.name)")
}
enum QuickstartError: Error {
case missing
case outputTimedOut
}
}
import Foundation
import LibTmux
@main
struct Quickstart {
static func main() async throws {
let server = try Server(
socketName: "libtmux-home-\(UUID().uuidString)",
configurationFile: "/dev/null"
)
do {
try await work(server)
} catch {
print("tmux quickstart failed: \(error)")
if try await server.isRunning() {
try await server.killServer()
}
throw error
}
try await server.killServer()
}
static func work(_ server: Server) async throws {
let session = try await server.newSession(
named: "demo",
shell: "/bin/sh"
)
let layout = try await server.snapshot()
guard let editor = layout.windows(of: session).first,
let left = layout.panes(of: editor).first(where: \.isActive)
else {
throw QuickstartError.missing
}
try await server.rename(editor, to: "editor")
let right = try await server.split(
left,
direction: .right,
shell: "/bin/sh"
)
let logs = try await server.newWindow(
in: session,
named: "logs",
shell: "/bin/sh"
)
try await server.run("printf 'hello from %s\\n' tmux", in: right)
let ready = try RegexPattern("^hello from tmux$")
let result = try await server.waitForOutput(
in: right,
matching: [ready]
)
guard result.outcome == .matched else {
throw QuickstartError.outputTimedOut
}
try await server.select(right)
try await server.select(logs.link)
let snapshot = try await server.snapshot()
for session in snapshot.sessions {
print("session \(session.name)")
let windows = snapshot.windows(of: session)
let links = snapshot.windowLinks(of: session)
for (window, link) in zip(windows, links) {
let panes = snapshot.panes(of: window)
let ids = panes.map { $0.id.rawValue }.joined(separator: " ")
print(" window \(link.index):\(window.name): \(ids)")
}
}
let home = right.windowID
guard let window = snapshot.windows.first(where: { $0.id == home }),
let owner = snapshot.sessions(of: window).first
else {
throw QuickstartError.missing
}
print("\(right.id.rawValue) is in window \(window.name)"
+ " of session \(owner.name)")
}
enum QuickstartError: Error {
case missing
case outputTimedOut
}
}
Example details
No tmux server yet

tmux before the first call: no server is running.

No tmux server yet

tmux after Server.init: no server is running; the call sent no command.

%0 sh
[demo]

tmux after Server.newSession: added session demo.

%0 sh
[demo]

tmux after Server.snapshot: unchanged.

%0 sh
[demo]

tmux after Snapshot.windows(of:): unchanged, the call sent no command.

%0 sh
[demo]

tmux after Snapshot.panes(of:): unchanged, the call sent no command.

%0 sh
[demo]

tmux after Server.rename: renamed window 0:sh to 0:editor.

%0 sh
%1 sh
[demo]

tmux after Server.split: added pane %1 to window 0:editor.

%0 sh
%1 sh
[demo]

tmux after Server.newWindow: added window 1:logs.

%0 sh
%1 sh
[demo]

tmux after Server.run: sent keys to pane %1.

%0 sh
%1 sh
[demo]

tmux after RegexPattern.init: unchanged, the call sent no command.

%0 sh
%1 sh
$ printf 'hello from %s\n' tmux
hello from tmux
$
[demo]

tmux after Server.waitForOutput: read pane %1.

%0 sh
%1 sh
$ printf 'hello from %s\n' tmux
hello from tmux
$
[demo]

tmux after Server.select(Pane): selected pane %1 in window 0:editor.

%2 sh
[demo]

tmux after Server.select(WindowLink): selected window 1:logs.

%2 sh
[demo]

tmux after Server.snapshot: unchanged.

%2 sh
[demo]

tmux after Snapshot.windows(of:): unchanged, the call sent no command.

%2 sh
[demo]

tmux after Snapshot.windowLinks(of:): unchanged, the call sent no command.

%2 sh
[demo]

tmux after Snapshot.panes(of:): unchanged, the call sent no command.

%2 sh
[demo]

tmux after Snapshot.panes(of:): unchanged, the call sent no command.

%2 sh
[demo]

tmux after Snapshot.windows: unchanged, the call sent no command.

%2 sh
[demo]

tmux after Snapshot.sessions(of:): unchanged, the call sent no command.

No sessions

tmux after Server.killServer: the server has exited.

  1. Before the first call

    No tmux server is running yet.

  2. Server.init returned Server

    {
    "socketName": "libtmux-home-00000000-0000-0000-0000-000000000000"
    }

    The call sent no tmux command.

  3. Server.newSession returned Session

    {
    "id": "$0",
    "name": "demo"
    }
    2 tmux commands, 2 that change tmux
    1. run-shell -C libtmux-version-6691bf5607ee461f9bcef6f3090768be-#{version} error connecting to /tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000 (No such file or directory)
    2. new-session -d -P -F #{session_id}␞#{session_name}␞#{session_windows}␞#{session_attached}␞#{session_created}␞#{socket_path}␞#{pid}␞#{start_time} -s demo /bin/sh $0␞demo␞1␞0␞1793889970␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970

    Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

  4. Server.snapshot returned Snapshot

    {
    "sessions": 1,
    "windows": 1,
    "panes": 1
    }
    7 tmux commands, 3 that change tmux
    1. run-shell -C libtmux-version-421257856dca4a6a859a9b665cb52dba-#{version} unknown command: libtmux-version-421257856dca4a6a859a9b665cb52dba-3.8a
    2. run-shell -C libtmux-version-a3bd5014baf1406aa91ff5d081083dcb-#{pid},#{start_time},#{q:socket_path} unknown command: libtmux-version-a3bd5014baf1406aa91ff5d081083dcb-3367525,1793889970,/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000
    3. list-sessions -F #{session_id}␞#{session_name}␞#{session_windows}␞#{session_attached}␞#{session_created}␞#{socket_path}␞#{pid}␞#{start_time} $0␞demo␞1␞0␞1793889970␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970
    4. list-windows -a -F #{window_id}␞#{window_name}␞#{window_index}␞#{window_panes}␞#{window_active}␞#{window_width}␞#{window_height}␞#{session_id}␞#{socket_path}␞#{pid}␞#{start_time} @0␞sh␞0␞1␞1␞80␞24␞$0␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970
    5. list-panes -a -F #{pane_id}␞#{pane_index}␞#{pane_width}␞#{pane_height}␞#{pane_active}␞#{pane_dead}␞#{pane_input_off}␞#{pane_in_mode}␞#{pane_synchronized}␞#{pane_current_command}␞#{pane_current_path}␞#{pane_at_top}␞#{pane_at_bottom}␞#{pane_at_left}␞#{pane_at_right}␞#{window_id}␞#{socket_path}␞#{pid}␞#{start_time} %0␞0␞80␞24␞1␞0␞0␞0␞0␞sh␞/tmp/marked␞1␞1␞1␞1␞@0␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970
    6. list-clients -F #{client_name}␞#{client_tty}␞#{client_pid}␞#{client_width}␞#{client_height}␞#{client_control_mode}␞#{session_id}␞#{pane_id}␞#{window_zoomed_flag}␞#{client_flags}␞#{socket_path}␞#{pid}␞#{start_time} no output
    7. run-shell -C libtmux-version-4b34e8f5afeb440ba72abd2466d048c0-#{pid},#{start_time},#{q:socket_path} unknown command: libtmux-version-4b34e8f5afeb440ba72abd2466d048c0-3367525,1793889970,/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000
  5. Snapshot.windows(of:) returned Window

    {
    "id": "@0",
    "name": "sh"
    }

    The call sent no tmux command.

  6. Snapshot.panes(of:) returned Pane

    {
    "id": "%0"
    }

    The call sent no tmux command.

  7. Server.rename returned Void

    null
    1 tmux command, 1 that changes tmux
    1. rename-window -t @0 -- editor no output
  8. Server.split returned Pane

    {
    "id": "%1"
    }
    2 tmux commands, 1 that changes tmux
    1. split-window -d -P -F #{pane_id} -t %0 -h /bin/sh %1
    2. list-panes -a -f #{==:#{pane_id},%1} -F #{pane_id}␞#{pane_index}␞#{pane_width}␞#{pane_height}␞#{pane_active}␞#{pane_dead}␞#{pane_input_off}␞#{pane_in_mode}␞#{pane_synchronized}␞#{pane_current_command}␞#{pane_current_path}␞#{pane_at_top}␞#{pane_at_bottom}␞#{pane_at_left}␞#{pane_at_right}␞#{window_id}␞#{socket_path}␞#{pid}␞#{start_time} %1␞1␞39␞24␞0␞0␞0␞0␞0␞sh␞/tmp/marked␞1␞1␞0␞1␞@0␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970
  9. Server.newWindow returned WindowAppearance

    {
    "id": "@1",
    "index": 1,
    "name": "logs"
    }
    1 tmux command, 1 that changes tmux
    1. new-window -d -P -F #{window_id}␞#{window_name}␞#{window_index}␞#{window_panes}␞#{window_active}␞#{window_width}␞#{window_height}␞#{session_id}␞#{socket_path}␞#{pid}␞#{start_time} -t $0 -n logs /bin/sh @1␞logs␞1␞1␞0␞80␞24␞$0␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970
  10. Server.run returned Void

    null
    1 tmux command, 1 that changes tmux
    1. send-keys -t %1 -- 'printf '\''hello from %s\n'\'' tmux' Enter no output
  11. RegexPattern.init returned RegexPattern

    {
    "source": "^hello from tmux$"
    }

    The call sent no tmux command.

  12. Server.waitForOutput returned OutputWait

    {
    "outcome": "matched",
    "matched": "^hello from tmux$",
    "tail": [
    "$ printf 'hello from %s\\n' tmux",
    "hello from tmux",
    "$"
    ]
    }
    2 tmux commands
    1. display-message -p -t %1 #{pane_id}␞#{history_size}␞#{cursor_y}␞#{pane_pid}␞#{pane_height}␞#{history_bytes}␞#{history_limit}␞#{pane_width}␞#{alternate_on} %1␞0␞2␞3367770␞24␞1295␞2000␞39␞0
    2. capture-pane -p -t %1 -S 0 -E 2 $ printf 'hello from %s\n' tmux hello from tmux $
  13. Server.select(Pane) returned Void

    null
    1 tmux command, 1 that changes tmux
    1. select-pane -t %1 no output
  14. Server.select(WindowLink) returned Void

    null
    1 tmux command, 1 that changes tmux
    1. select-window -t $0:1 no output
  15. Server.snapshot returned Snapshot

    {
    "sessions": 1,
    "windows": 2,
    "panes": 3
    }
    6 tmux commands, 2 that change tmux
    1. run-shell -C libtmux-version-9ef137956f844270bad84806a2c2dc7e-#{pid},#{start_time},#{q:socket_path} unknown command: libtmux-version-9ef137956f844270bad84806a2c2dc7e-3367525,1793889970,/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000
    2. list-sessions -F #{session_id}␞#{session_name}␞#{session_windows}␞#{session_attached}␞#{session_created}␞#{socket_path}␞#{pid}␞#{start_time} $0␞demo␞2␞0␞1793889970␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970
    3. list-windows -a -F #{window_id}␞#{window_name}␞#{window_index}␞#{window_panes}␞#{window_active}␞#{window_width}␞#{window_height}␞#{session_id}␞#{socket_path}␞#{pid}␞#{start_time} @0␞editor␞0␞2␞0␞80␞24␞$0␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970 @1␞logs␞1␞1␞1␞80␞24␞$0␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970
    4. list-panes -a -F #{pane_id}␞#{pane_index}␞#{pane_width}␞#{pane_height}␞#{pane_active}␞#{pane_dead}␞#{pane_input_off}␞#{pane_in_mode}␞#{pane_synchronized}␞#{pane_current_command}␞#{pane_current_path}␞#{pane_at_top}␞#{pane_at_bottom}␞#{pane_at_left}␞#{pane_at_right}␞#{window_id}␞#{socket_path}␞#{pid}␞#{start_time} %0␞0␞40␞24␞0␞0␞0␞0␞0␞sh␞/tmp/marked␞1␞1␞1␞0␞@0␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970 %1␞1␞39␞24␞1␞0␞0␞0␞0␞sh␞/tmp/marked␞1␞1␞0␞1␞@0␞/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000␞3367525␞1793889970 %2␞0␞80␞24␞1␞0␞0␞0␞0␞sh␞/tmp/marked␞1␞…
    5. list-clients -F #{client_name}␞#{client_tty}␞#{client_pid}␞#{client_width}␞#{client_height}␞#{client_control_mode}␞#{session_id}␞#{pane_id}␞#{window_zoomed_flag}␞#{client_flags}␞#{socket_path}␞#{pid}␞#{start_time} no output
    6. run-shell -C libtmux-version-ead3bee558414981b0b13c584c0854a7-#{pid},#{start_time},#{q:socket_path} unknown command: libtmux-version-ead3bee558414981b0b13c584c0854a7-3367525,1793889970,/tmp/tmux-1000/libtmux-home-00000000-0000-0000-0000-000000000000
  16. Snapshot.windows(of:) returned [Window]

    [
    "@0",
    "@1"
    ]

    The call sent no tmux command.

  17. Snapshot.windowLinks(of:) returned [WindowLink]

    [
    "$0:0",
    "$0:1"
    ]

    The call sent no tmux command.

  18. Snapshot.panes(of:) returned [Pane]

    [
    "%0",
    "%1"
    ]

    The call sent no tmux command.

  19. Snapshot.panes(of:) returned [Pane]

    [
    "%2"
    ]

    The call sent no tmux command.

  20. Snapshot.windows returned Window

    {
    "id": "@0",
    "name": "editor"
    }

    The call sent no tmux command.

  21. Snapshot.sessions(of:) returned Session

    {
    "id": "$0",
    "name": "demo"
    }

    The call sent no tmux command.

  22. Server.killServer returned Void

    null
    1 tmux command, 1 that changes tmux
    1. kill-server no output
1/20
Run this example

Swift 6.2+, tmux on Linux/macOS

Save the program above as Sources/Main.swift in a new directory, along with the files below.

Cleanup is omitted; the private tmux server and its temporary files may remain. Select Cleanup to remove them when the program finishes. The program stops its private tmux server and removes any temporary files it created when it finishes. Error handling adds diagnostics; every version checks required results and deadlines.

Package.swift
// swift-tools-version: 6.2
import PackageDescription
let package = Package(
name: "ApiExample",
platforms: [.macOS(.v13)],
dependencies: [
.package(
url: "https://github.com/libtmux/libtmux-swift.git",
from: "0.1.1"
)
],
targets: [
.executableTarget(
name: "ApiExample",
dependencies: [
.product(name: "LibTmux", package: "libtmux-swift"),
],
path: "Sources"
)
]
)

Setup and run

# Save the files above at their displayed relative paths.
swift build --jobs 2 --product ApiExample
unset TMUX TMUX_PANE
export ENV=/dev/null BASH_ENV=/dev/null SHELL=/bin/sh
.build/debug/ApiExample

Expected output

session demo
window 0:editor: %0 %1
window 1:logs: %2
%1 is in window editor of session demo

Install libtmux-swift

Package.swiftFrom Git
Package.swift
Package.swift
libtmux-swift 0.1.1 0.x Swift Package Index
.package(
    url: "https://github.com/libtmux/libtmux-swift.git",
    from: "0.1.1"
)
.package(url: "https://github.com/libtmux/libtmux-swift", exact: "0.1.1")

A range from this release up to the next major version. Any 0.1.N release may change the API, so pin `exact:` instead to avoid a break.

Pins the tagged source; the registry release is the default.

MCP

Give an assistant tmux tools concept Working example; may change without notice. Read more MCP home →

tmux through MCP

Replay Hover a call to preview the tmux state after it. Use its pin button to hold it. With the replay focused, the arrow keys, Home and End step through the calls.

tmux before the first call.

tmux window of 1 pane
%0 sh
[checks] 0:sh*

tmux after create_session.

tmux window of 1 pane
%0 sh
[checks] 0:sh*

tmux after list_panes: unchanged, the call only reads.

tmux window of 2 panes
%0 sh
%1 sh
[checks] 0:sh*

tmux after split_window.

tmux window of 2 panes
%0 sh
%1 sh
[checks] 0:sh*

tmux after get_tmux_variables: unchanged, the call only reads.

tmux window of 2 panes
%0 sh
$ pytest -q
..                               [100%]
2 passed in 0.00s
%1 sh
[checks] 0:sh*

tmux after run_shell_command.

tmux window of 2 panes
%0 sh
$ pytest -q
..                               [100%]
2 passed in 0.00s
%1 sh
$ cat checks.log
2026-01-01 checks started
2026-01-01 checks finished
[checks] 0:sh*

tmux after run_shell_command.

No sessions

tmux after kill_session.

You

Create a tmux session called checks, split its window into two panes, run pytest -q in one and show checks.log in the other, show me the results, then remove the session.

AgentCreate the session.

create_session Execute {"name":"checks"}
Result
{
"isAttached": false,
"id": "$0",
"windowCount": 1,
"ref": "tmux1_AWXw31wwjyXoEaqBo3la-V4mtPL0vRI4iGVS1yE78h4Y",
"createdAt": 1795061804,
"name": "checks"
}

AgentList the panes in the new session.

list_panes Inspect {"session":"checks"}
Result
{
"panes": [
{
"currentPath": "/home/user/project",
"windowID": "@0",
"isActive": true,
"isAtTop": true,
"isAtBottom": true,
"width": 80,
"isAtRight": true,
"isAtLeft": true,
"ref": "tmux1_BGXw31wwjyXoEaqBo3la-V4QetAYfM5JN9hpsAyrKwx3",
"height": 24,
"currentCommand": "sh",
"isDead": false,
"…": "4 more"
}
]
}

AgentSplit the pane the list returned (%0) in two.

split_window Execute {"paneId":"%0","direction":"right"}
Result
{
"id": "%1",
"height": 24,
"isDead": false,
"ref": "tmux1_BGXw31wwjyXoEaqBo3la-V6jHcgKRph3CM6YZTGyd4fB",
"isAtLeft": false,
"isActive": false,
"windowID": "@0",
"isSynchronized": false,
"width": 39,
"isAtRight": true,
"isAtBottom": true,
"modeCount": 0,
"…": "4 more"
}

AgentRead the window's layout through the pane the list returned (%0).

get_tmux_variables Inspect {"names":["window_layout"],"paneId":"%0"}
Result
{
"values": {
"window_layout": "8205,80x24,0,0{40x24,0,0,0,39x24,41,0,1}"
}
}

AgentRun the tests in the pane the list returned (%0).

run_shell_command Execute {"paneId":"%0","command":"pytest -q"}
Result
{
"pane": "%0",
"droppedLines": 0,
"seconds": 0.449415896,
"exitStatus": 0,
"effectiveTimeout": 30,
"paneRef": "tmux1_BGXw31wwjyXoEaqBo3la-V4QetAYfM5JN9hpsAyrKwx3",
"linesMissed": false,
"output": [
".. [100%]",
"2 passed in 0.00s"
],
"timedOut": false
}

AgentShow the log in the new pane the split returned (%1).

run_shell_command Execute {"paneId":"%1","command":"cat checks.log"}
Result
{
"pane": "%1",
"effectiveTimeout": 30,
"seconds": 0.085019186,
"timedOut": false,
"droppedLines": 0,
"exitStatus": 0,
"paneRef": "tmux1_BGXw31wwjyXoEaqBo3la-V6jHcgKRph3CM6YZTGyd4fB",
"linesMissed": false,
"output": [
"2026-01-01 checks started",
"2026-01-01 checks finished"
]
}

AgentRemove the session once the result is read.

kill_session Teardown {"session":"checks"}
Result
{
"deleted": true,
"sessionId": "$0"
}
Agent

pytest printed 2 passed and checks.log ends with checks finished. I removed the checks session.

1/7

Install libtmux-mcp

Choose your client and configuration scope, then run the command or paste the configuration.

Terminal
Terminal
Terminal
claude_desktop_config.json
Terminal
.codex/config.toml
Terminal
Terminal
.cursor/mcp.json
~/.cursor/mcp.json
Terminal
Terminal
~/.gemini/config/mcp_config.json
Terminal
libtmux-mcp concept Swift Package Index
First
$ swift build --product libtmux-mcp
$ claude mcp add tmux -- /absolute/path/to/.build/debug/libtmux-mcp
First
$ swift build --product libtmux-mcp
$ claude mcp add tmux --scope user -- /absolute/path/to/.build/debug/libtmux-mcp
First
$ swift build --product libtmux-mcp
$ claude mcp add tmux \
    --scope project \
    -- /absolute/path/to/.build/debug/libtmux-mcp
First
$ swift build --product libtmux-mcp
{
    "mcpServers": {
        "tmux": {
            "command": "/absolute/path/to/.build/debug/libtmux-mcp"
        }
    }
}
First
$ swift build --product libtmux-mcp
$ codex mcp add tmux -- /absolute/path/to/.build/debug/libtmux-mcp
First
$ swift build --product libtmux-mcp
[mcp_servers.tmux]
command = "/absolute/path/to/.build/debug/libtmux-mcp"
First
$ swift build --product libtmux-mcp
$ gemini mcp add --scope user tmux /absolute/path/to/.build/debug/libtmux-mcp
First
$ swift build --product libtmux-mcp
$ gemini mcp add --scope project tmux /absolute/path/to/.build/debug/libtmux-mcp
First
$ swift build --product libtmux-mcp
{
    "mcpServers": {
        "tmux": {
            "command": "/absolute/path/to/.build/debug/libtmux-mcp"
        }
    }
}
First
$ swift build --product libtmux-mcp
{
    "mcpServers": {
        "tmux": {
            "command": "/absolute/path/to/.build/debug/libtmux-mcp"
        }
    }
}
First
$ swift build --product libtmux-mcp
$ grok mcp add --scope user tmux -- /absolute/path/to/.build/debug/libtmux-mcp
First
$ swift build --product libtmux-mcp
$ grok mcp add --scope project tmux -- /absolute/path/to/.build/debug/libtmux-mcp
First
$ swift build --product libtmux-mcp
{
    "mcpServers": {
        "tmux": {
            "command": "/absolute/path/to/.build/debug/libtmux-mcp"
        }
    }
}
First
$ swift build --product libtmux-mcp
$ opencode mcp add tmux -- /absolute/path/to/.build/debug/libtmux-mcp

Writes ~/.claude.json (this project).

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Writes ~/.claude.json (all projects).

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Writes .mcp.json (in repo, version-controlled).

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Writes ~/.codex/config.toml.

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Codex's CLI doesn't support project scope yet — paste this into .codex/config.toml at the repo root.

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Writes ~/.gemini/settings.json.

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Writes .gemini/settings.json (in repo).

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Writes ~/.grok/config.toml.

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Writes ./.grok/config.toml (in repo).

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Writes ~/.config/opencode/opencode.jsonc.

Nothing publishes this server as a binary yet. The server takes no flags — which tmux it talks to is environment, so the client config is where you say so.

Embed the server in a program
Examples/Sources/ExampleCode/MCPEmbedding.swiftExcerpt · lines 4–11
public func useEmbeddedTools(on server: Server) async throws(ToolError) -> Int {
let tools = TmuxTools(server: server)
for definition in tools.visibleDefinitions {
print(definition.name, definition.summary)
}
let result = try await tools.call(ToolCall(name: "list_panes"))
return result.structured["panes"]?.arrayValue?.count ?? 0
}

Excerpt of Examples/Sources/ExampleCode/MCPEmbedding.swift. This program is tested on every change.

Workspace Manager

Load a workspace file concept Working example; may change without notice. Read more Workspace Manager home →

Start directory

start_directory chooses where a session, a window or a pane starts.

session_name: missingdir
start_directory: /nonexistent/definitely/not/here
windows:
- panes:
- ~
{
"session_name": "missingdir",
"start_directory": "/nonexistent/definitely/not/here",
"windows": [
{
"panes": [
null
]
}
]
}

Converted from the YAML; the tests load the YAML.

0:window
Window 0: 1 pane, even-vertical
no layout set

Install tmux-workspace

The workspace package is separate from the core library.

SwiftPM
Terminal
tmux-workspace concept Swift Package Index
$ swift build \
    --jobs 2 \
    --traits YAMLWorkspaces \
    --force-resolved-versions \
    --product tmux-workspace
Then
$ tmux-workspace load ./workspace.yaml

Run from a libtmux-swift checkout with Swift 6.2 or newer on Linux, or Xcode Swift 6.3 or newer on macOS. The command is .build/debug/tmux-workspace.

Save the file as workspace.yaml and load it with:

tmux-workspace load workspace.yaml

Workspace file: Tests/TmuxWorkspaceCLITests/WorkspaceCLITests.swift. Tested by startDirectorySemantics (Tests/TmuxWorkspaceCLITests/WorkspaceCLITests.swift; a test runs the command).

Build the workspace from a program
Examples/Sources/ExampleCode/Workspaces.swiftExcerpt · lines 7–23
public func describeAWorkspaceInSwift() -> Workspace {
let workspace = Workspace(
sessionName: "work",
windows: [
WindowPlan(
windowName: "editor",
layout: "even-horizontal",
panes: [PanePlan(), PanePlan()]
),
WindowPlan(
windowName: "logs",
panes: [PanePlan(shellCommands: ["tail -f /tmp/build.log"])]
),
]
)
return workspace
}

Excerpt of Examples/Sources/ExampleCode/Workspaces.swift. This program is tested on every change.

Packages and integrations

API references

Esc
Search in

Type to search.

Reference previewOpen reference