Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,37 @@ _Notes on upcoming releases will be added here_
`libtmux-mcp` requires Python 3.11 or newer. Python 3.10 reached end of life
in October 2026.

### Dependencies

**Program tools require the core development source**

The dependency is pinned to the source of
[libtmux core PR #833](https://github.com/tmux-python/libtmux/pull/833).
Release remains held until that API is published and the pin is replaced
with its release. AnyIO is an explicit runtime dependency for cooperative
worker execution.

### What's new

**Run a program and recover its cleanup**

{tooliconl}`run-program` starts literal argv in an owned terminal, waits for its
exit and returns status or signal plus a bounded screen. The borrowed session
survives. It requires a connected tmux 3.8+ daemon; use
{tooliconl}`get-server-capabilities` to inspect the library's feature decisions.
Cancellation joins the worker before the request settles.

{tooliconl}`list-program-runs` pages through running and unresolved owners.
{tooliconl}`get-program-run` reads one owner's bounded error and prior output;
{tooliconl}`cleanup-program` retries cleanup using that retained owner. Records
remain in the MCP process until cleanup succeeds. Terminal ownership does not
confine filesystem, network or privileges, and removing a pane does not prove
that every descendant exited. See {doc}`tools/program/index`.

Cleanup retries check for an absent owned pane even when termination observation
fails. If cleanup also fails, the record keeps both errors. Native command
representations and audit summaries hide submitted argv and environment values.

**Kill tools ask the user first**

When the client supports MCP elicitation, {tooliconl}`kill-session`,
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ commands, read output, orchestrate panes.
| **Session** | `list_windows`, `get_session_info`, `create_window`, `rename_session`, `select_window`, `kill_session` |
| **Window** | `list_panes`, `get_window_info`, `split_window`, `rename_window`, `select_layout`, `resize_window`, `move_window`, `kill_window` |
| **Pane** | `run_command`, `send_keys`, `send_keys_batch`, `paste_text`, `capture_pane`, `capture_since`, `snapshot_pane`, `search_panes`, `find_pane_by_position`, `get_pane_info`, `wait_for_text`, `wait_for_channel`, `signal_channel`, `display_message`, `select_pane`, `swap_pane`, `resize_pane`, `set_pane_title`, `clear_pane`, `pipe_pane`, `enter_copy_mode`, `exit_copy_mode`, `respawn_pane`, `kill_pane` |
| **Owned program** | `run_program`, `list_program_runs`, `get_program_run`, `cleanup_program`, `get_server_capabilities` |
| **Options** | `show_option`, `set_option` |
| **Environment** | `show_environment`, `set_environment` |
| **Buffers** | `load_buffer`, `paste_buffer`, `show_buffer`, `delete_buffer` |
Expand All @@ -45,6 +46,11 @@ commands, read output, orchestrate panes.

**Requirements:** Python 3.11+, tmux >= 3.2a on `$PATH`.

Owned program tools require a connected tmux 3.8+ daemon. This development
branch pins [libtmux core PR #833](https://github.com/tmux-python/libtmux/pull/833)
as a source dependency. From this checkout, run `uv sync --all-extras --dev`
and `uv run libtmux-mcp`. Publication is held until the core API is released.

The distribution is `libtmux-mcp`, the import is `libtmux_mcp`, and the
installed executable is `libtmux-mcp`.

Expand Down Expand Up @@ -111,6 +117,13 @@ to tmux, but a better place to put the control loop.

The server-side moves are:

**Owned programs.** `run_program` launches literal argv in a fresh PTY, returns
exit evidence and a bounded rendered tail, and cleans up its pane. The borrowed
session survives. `get_server_capabilities` reports live feature decisions;
`list_program_runs`, `get_program_run` and `cleanup_program` expose unresolved cleanup after
failure or cancellation. See the [owned-program guide](docs/tools/program/index.md)
for the output, descendant and process-restart limits.

**Running.** [`run_command`](https://libtmux-mcp.git-pull.com/tools/pane/run-command/)
sends an authored shell command, waits for deterministic completion,
and returns exit status plus tail-preserved output as one typed value.
Expand Down
10 changes: 10 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def _patched_tool_collector_tool(self: ToolCollector, **kwargs: t.Any) -> t.Any:
"libtmux_mcp.tools.session_tools",
"libtmux_mcp.tools.window_tools",
"libtmux_mcp.tools.pane_tools",
"libtmux_mcp.tools.program_tools",
"libtmux_mcp.tools.option_tools",
"libtmux_mcp.tools.env_tools",
"libtmux_mcp.tools.buffer_tools",
Expand All @@ -149,6 +150,7 @@ def _patched_tool_collector_tool(self: ToolCollector, **kwargs: t.Any) -> t.Any:
"session_tools": "session/index",
"window_tools": "window/index",
"pane_tools": "pane/index",
"program_tools": "program/index",
"option_tools": "server/index",
"env_tools": "server/index",
"buffer_tools": "buffer/index",
Expand All @@ -171,6 +173,14 @@ def _patched_tool_collector_tool(self: ToolCollector, **kwargs: t.Any) -> t.Any:
"EnvironmentSetResult",
"WaitForTextResult",
"RunCommandResult",
"ProgramRunResult",
"ProgramObservation",
"ProgramRunInfo",
"ProgramRunSummary",
"ProgramRunPage",
"ProgramCleanupResult",
"ServerCapabilityInfo",
"LibraryFeatureInfo",
"SendKeysOperation",
"SendKeysOperationResult",
"SendKeysBatchResult",
Expand Down
10 changes: 9 additions & 1 deletion docs/reference/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@
| >= 3.2a | Supported |
| < 3.2a | Not supported (libtmux requirement) |

Owned program tools require tmux 3.8+. Use `get_server_capabilities` to inspect
the connected daemon before choosing them.

## Dependencies

This draft pins the unreleased core implementation in
[libtmux PR #833](https://github.com/tmux-python/libtmux/pull/833). Install from
this checkout; publication remains held until that core API is released.

| Package | Required version |
|---------|-----------------|
| [libtmux](https://libtmux.git-pull.com/) | >= 0.62.0, < 1.0 |
| [libtmux](https://libtmux.git-pull.com/) | @ git+https://github.com/tmux-python/libtmux.git@4cdc8da33dbb93bc5b4c45292e2d44501b08c6a6 |
| [AnyIO](https://anyio.readthedocs.io/) | >= 4.16.0, < 5.0 |
| [FastMCP](https://github.com/jlowin/fastmcp) | >= 4.0.2, < 5.0.0 |
| [regex](https://github.com/mrabarnett/mrab-regex) | >= 2024.11.6 |

Expand Down
37 changes: 36 additions & 1 deletion docs/tools/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
Targeted tmux tools accept an optional `socket_name` parameter for
multi-server support. It defaults to the {envvar}`LIBTMUX_SOCKET` env
var. {toolref}`list-servers` discovers sockets itself, and batch tools
leave socket selection inside each nested tool's arguments. See
leave socket selection inside each nested tool's arguments. Program recovery
records keep their original socket; listing and cleanup use their local IDs. See
{ref}`configuration`.

## Which tool do I want?
Expand All @@ -25,6 +26,9 @@ leave socket selection inside each nested tool's arguments. See
- Already know the `pane_id` → use it directly

**Running a command?**
- {tool}`run-program` — literal argv in a fresh owned PTY, exact exit evidence and cleanup on tmux 3.8+
- {tool}`get-server-capabilities` — connected daemon requirements and feature decisions
- {tool}`list-program-runs` / {tool}`get-program-run` / {tool}`cleanup-program` — page through, inspect and retry unresolved cleanup
- {tool}`run-command` — one call to run a shell command, wait for completion, capture output, and return exit status
- {tool}`send-keys` / {tool}`send-keys-batch` — raw interactive input for TUIs, control keys, and persistent shell state
- {tool}`wait-for-channel` — low-level custom completion when `run-command` does not fit the shell composition
Expand Down Expand Up @@ -155,6 +159,24 @@ Search text across panes.
Wait for text to appear in a pane.
:::

:::{grid-item-card} get_server_capabilities
:link: get-server-capabilities
:link-type: ref
Read the connected daemon's library feature decisions.
:::

:::{grid-item-card} get_program_run
:link: get-program-run
:link-type: ref
Read one owner's bounded error and prior-output evidence.
:::

:::{grid-item-card} list_program_runs
:link: list-program-runs
:link-type: ref
Inspect active programs and unresolved cleanup records.
:::

:::{grid-item-card} get_server_info
:link: get-server-info
:link-type: ref
Expand Down Expand Up @@ -357,6 +379,18 @@ Send raw keystrokes to a pane.
Send several ordered raw-input operations.
:::

:::{grid-item-card} run_program
:link: run-program
:link-type: ref
Run literal argv in a fresh PTY and remove its owned pane.
:::

:::{grid-item-card} cleanup_program
:link: cleanup-program
:link-type: ref
Retry cleanup using the original retained program owner.
:::

:::{grid-item-card} run_command
:link: run-command
:link-type: ref
Expand Down Expand Up @@ -455,6 +489,7 @@ batch/index
session/index
window/index
pane/index
program/index
buffer/index
hook/index
```
17 changes: 17 additions & 0 deletions docs/tools/program/cleanup-program.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Clean up program

```{fastmcp-tool} program_tools.cleanup_program
```

Pass the `run_id` from {tool}`list-program-runs` or the `program_recovery_id`
from error metadata. Cleanup uses the retained owner; it does not reconstruct
ownership from a pane ID. Failed cleanup keeps the record for retry.
If termination fails, cleanup still checks whether the owned pane is absent.
A verified absence retires the record; otherwise both failures remain available
in its cleanup details.

The tool belongs to `execute` so an operator who permits owned execution can
also recover its resources. It can terminate that owner's remaining program.

```{fastmcp-tool-input} program_tools.cleanup_program
```
12 changes: 12 additions & 0 deletions docs/tools/program/get-program-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Get program run

```{fastmcp-tool} program_tools.get_program_run
```

Read one owner's bounded error summaries and any exit or capture obtained before
a failure. The record contains the original route and public cleanup disposition.
The capture's truncation flag and unknown history completeness remain explicit.
Use {tool}`cleanup-program` when its status is `cleanup_required`.

```{fastmcp-tool-input} program_tools.get_program_run
```
78 changes: 78 additions & 0 deletions docs/tools/program/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Owned programs

Run an executable with literal arguments in a fresh PTY, read its exit and
rendered output, then remove its pane. The borrowed session and its existing
panes survive. Use {tool}`run-program` for a bounded command on tmux 3.8+.
Use {tool}`run-command` when the command needs an existing interactive shell.

This development branch depends on the source revision of
[libtmux core PR #833](https://github.com/tmux-python/libtmux/pull/833) pinned in
`pyproject.toml` and `uv.lock`. It uses no experimental engine package. Release
is held until that core API is published and the dependency uses its release.

Discover a stable session ID with {tool}`list-sessions`, then check
{tool}`get-server-capabilities`. Direct Python callers use the same async tool:

```python
>>> import asyncio
>>> import sys
>>> from libtmux_mcp.tools.program_tools import run_program
>>> from libtmux_mcp.tools.server_tools import get_server_capabilities
>>> report = asyncio.run(get_server_capabilities(socket_name=server.socket_name))
>>> if any(item.name == "program-owned" and item.status == "supported" for item in report.features):
... result = asyncio.run(run_program(
... session_id=session.session_id,
... socket_name=server.socket_name,
... argv=[sys.executable, "-c", "print('finished'); raise SystemExit(7)"],
... ))
... assert result.exit_status == 7
... assert result.screen_text == "finished"
... assert result.pane_removed
```

## Cancellation and recovery

Request cancellation signals the native worker, which attempts cleanup before
the server handler settles. An MCP client can acknowledge cancellation before
remote cleanup finishes. {tool}`list-program-runs` reports running workers and
retained cleanup summaries in pages of four (at most sixteen).
Use {tool}`get-program-run` to inspect one owner's bounded error and prior-output
details. Records survive request cancellation, including
repeated cancellation during cleanup.

Failed calls include `program_cleanup_errors` and any `program_observation` in
MCP error metadata. If cleanup remains pending, `program_recovery_id` identifies
the original owner. Call {tool}`cleanup-program` with that ID after correcting
the cause. Retry is serialized and rechecks the original ownership. Missing
receipts or replaced lifetimes refuse cleanup; a pane ID alone cannot establish
ownership. Do not infer that a missing receipt means creation failed.

At most 32 running, queued or unresolved owners share capacity. New work is
refused before effects when capacity is full. Records are never evicted to make
room. They exist in this MCP process and do not survive a process restart.
An unverified receipt can require operator intervention; this API cannot adopt
or force-delete an arbitrary pane to settle the record.

## Output and lifetime limits

The result is a tail of rendered terminal history and screen. Stdout and stderr
share the PTY. `screen_truncated` reports clipping by the MCP byte bound;
`history_complete` remains `null` because tmux may have discarded earlier
history. A core capture exceeding its own bound fails rather than returning
unmarked loss. This initial tool does not expose raw streaming or a persistent
interactive handle.

`timeout` is clamped to `LIBTMUX_MCP_WAIT_MAX_SECONDS`; successful results report
`effective_timeout`. Cleanup has separate finite budgets and can continue after
the operation deadline. Pane removal closes the PTY but does not prove that
every descendant exited. Terminal ownership does not restrict filesystem,
network or privilege access, and it provides no controller-crash lease.

```{toctree}
:maxdepth: 1

run-program
list-program-runs
get-program-run
cleanup-program
```
18 changes: 18 additions & 0 deletions docs/tools/program/list-program-runs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# List program runs

```{fastmcp-tool} program_tools.list_program_runs
```

Read compact local summaries without querying tmux. Follow `next_cursor` until
it is `null`; each page defaults to four items and accepts at most sixteen.
Deleting a previous page's record does not invalidate its cursor. Pages reflect
current state, so newly created records may appear later and completed records
can disappear. Start again without a cursor to refresh the whole view.

Use {tool}`get-program-run` for one owner's cleanup errors and prior exit or
capture evidence. A record marked `running` or `cleanup_in_progress` is still
settling. Retry {tool}`cleanup-program` only for `cleanup_required` records.
Successful runs leave no record. Pagination never evicts an unresolved owner.

```{fastmcp-tool-input} program_tools.list_program_runs
```
27 changes: 27 additions & 0 deletions docs/tools/program/run-program.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Run program

```{fastmcp-tool} program_tools.run_program
```

Pass an executable and at least one argument. No command shell expands the
arguments; explicitly invoke a shell when the application needs one.

```json
{
"tool": "run_program",
"arguments": {
"session_id": "$0",
"argv": ["python3", "-c", "print('finished'); raise SystemExit(7)"],
"timeout": 10,
"output_bytes": 8192
}
}
```

A nonzero application exit is a successful tool result with that exit status.
An operation or cleanup failure is a tool error; its metadata preserves
available exit and capture evidence and identifies any retained cleanup owner.
See {doc}`index` for cancellation, output bounds and recovery.

```{fastmcp-tool-input} program_tools.run_program
```
15 changes: 15 additions & 0 deletions docs/tools/server/get-server-capabilities.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Get server capabilities

```{fastmcp-tool} server_tools.get_server_capabilities
```

The report includes the observed daemon PID, start time, version and each
library feature's minimum release, status and reason. It reads the selected
daemon afresh and does not start one. Unknown version strings remain unknown.
Operations recheck their own requirements; a saved report cannot authorize a
later operation after the daemon restarts.

MCP toolsets still determine the tools advertised by `tools/list`.

```{fastmcp-tool-input} server_tools.get_server_capabilities
```
5 changes: 5 additions & 0 deletions docs/tools/server/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Discover tmux daemons on the system.
Query server process identity and version.
:::

:::{grid-item-card} {tooliconl}`get-server-capabilities`
Read connected daemon feature requirements and availability.
:::

:::{grid-item-card} {tooliconl}`create-session`
Create a new tmux session.
:::
Expand Down Expand Up @@ -50,6 +54,7 @@ Set an environment variable on the server.
list-sessions
list-servers
get-server-info
get-server-capabilities
create-session
kill-server
show-option
Expand Down
Loading
Loading