libtmux-mcp 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 892583808fe4a27248b270071193932a4f7b36fb5eb592358c4ceb2ac24f296c
4
- data.tar.gz: b45a1a838d2831cc34a1fab2c9a4d36780cae5594050dd9a3063dbb2f5e79067
3
+ metadata.gz: a78a8cc4828931a8366e7ce6cd7f5076d44cb6f11eb40374c08d9a1307770ac2
4
+ data.tar.gz: 47996f86b62b3272da818ea267bd36969cae3aa54361329d7e8bedf2850c0d80
5
5
  SHA512:
6
- metadata.gz: 3d0d730cb95acf640cbaf2ddf1702c917558662a9427fc0c5b9d17bca81321998563efa7eed740140e7813d779f0cfc777b3ec6977528c59562fae957e6d17f1
7
- data.tar.gz: 05eb4ecc928723328ee886877821e89ea5f05654dfe31c1f0652825dd582c4978e4442e7876fd37d265681aa3a92b1facd14210b62860d9d1088895feeb87f6d
6
+ metadata.gz: 248803757f7a48856d2f628a826fad754795942b54768b0eb76089c4ab4c901d3690ba12f3d05032eab2c01a4e9fc289957fc894d926b00bf58e8ca4c4f97f55
7
+ data.tar.gz: 3f2967cbae674b8e8fe567eb7aeb9de0341199e837d979e1b0e53468cb119a1239775b8b9311d6e63e1f0b7363ff34449b5d3d98a5130e8160a35bbd2ff37e06
data/CHANGELOG.md ADDED
@@ -0,0 +1,85 @@
1
+ # Changelog
2
+
3
+ Notable changes to `libtmux-mcp`.
4
+
5
+ Releases are numbered `0.1.N`, and each release increments `N`. The API is not
6
+ settled: pin an exact version to avoid a break.
7
+
8
+ The gems are released together at one version, so every release has a section
9
+ here. Changes that span gems, such as documentation, CI and release tooling, are
10
+ in the [main changelog](../../CHANGELOG.md).
11
+
12
+ ## 0.1.4 (unreleased)
13
+
14
+ ## 0.1.3 (2026-12-05)
15
+
16
+ - MCP snapshot errors identify the invalid input field so clients can correct
17
+ and resubmit the request. (#183)
18
+ - `tools/list` states each tool's toolset, process reach and tmux effects under
19
+ `_meta["com.git-pull.libtmux-mcp/capability"]`, so a client, and libtmux.org,
20
+ can show how far a tool reaches without inferring it from its name.
21
+ `tmux_capabilities`, `tmux_snapshot`, `tmux_capture` and `tmux_wait` are
22
+ `inspect`, `tmux_create`, `tmux_send` and `tmux_run` are `execute`, and
23
+ `tmux_close` is `teardown`. The annotations are unchanged. (#133)
24
+ - `libtmux-mcp --start-session NAME` starts a tmux server on the chosen socket
25
+ when none answers there, with an empty configuration and a session named
26
+ `NAME`, and stops it at exit. `--keep` leaves it running. A server that
27
+ answers is borrowed as before, and without the option the server still never
28
+ starts one, so the quick start no longer needs a `tmux new-session` command.
29
+ (#113)
30
+
31
+ ## 0.1.2 (2026-11-04)
32
+
33
+ No entries for `libtmux-mcp`.
34
+
35
+ ## 0.1.1 (2026-10-31)
36
+
37
+ - **Behavior change:** `libtmux-mcp` no longer needs `--socket` or
38
+ `--socket-name`, and still refuses both together. Without a flag it serves
39
+ the same choice as `Server.new`, so start it with `--socket-name` or
40
+ `LIBTMUX_SOCKET` when an agent needs a server of its own. (#71)
41
+
42
+ ## 0.1.0 (2026-10-25)
43
+
44
+ No entries for `libtmux-mcp`.
45
+
46
+ ## 0.1.0.alpha.3 (2026-10-24)
47
+
48
+ ### libtmux-mcp
49
+
50
+ - `tmux_wait` with `screen_contains` polls the pane screen on tmux older than
51
+ 3.7 instead of attaching a control client, and uses control events from
52
+ 3.7. (#37)
53
+ - `tmux_wait` with `screen_contains` joins wrapped rows, so text that the pane
54
+ width split across rows now matches. (#37)
55
+ - `tmux_wait` fails with the new `pane_died` code, `delivery: observed` and
56
+ the last rows read in `error.capture`, when the pane or its tmux server ends
57
+ before the text appears. That case used to return `stale_target`, or
58
+ `observation_lost` after a killed server. A control stream that ends while
59
+ the pane lives, such as after a detach, returns `observation_lost` instead
60
+ of raising `NoMethodError`. (#37)
61
+
62
+ ## 0.1.0.alpha.2 (2026-10-18)
63
+
64
+ ### libtmux-mcp
65
+
66
+ - `libtmux-mcp` reports an unsupported tmux daemon as refused to start,
67
+ before any session exists. If the daemon changes while the server runs, a
68
+ tool call returns an `unsupported` result with `delivery: not_sent` and a
69
+ fixed line that names the floor and the remedy. (#36)
70
+ - Declare object roots in tool output schemas so clients using the
71
+ 2025-11-25 protocol accept the `tools/list` response with MCP SDK 1.5.1.
72
+
73
+ ## 0.1.0.alpha.1 (2026-09-20)
74
+
75
+ ### libtmux-mcp
76
+
77
+ - Serve an explicit tmux endpoint over MCP stdio with `libtmux-mcp`, or embed
78
+ `Application` and `StdioTransport` in Ruby. Capability discovery and snapshot
79
+ queries are enabled by default. [Server setup](../../gems/libtmux-mcp/README.md#quick-start).
80
+ - Opt into screen captures, event-driven waits, creation, input and close
81
+ tools. Snapshot cursors page one retained capture; resources expose metadata
82
+ and pane screens. [Protocol example](../../examples/mcp_protocol.rb).
83
+ - Run scripts through `tmux_run` after explicitly enabling the tool and
84
+ enrolling a zsh shell. Results distinguish stdout, stderr and observed
85
+ completion. [Shell enrollment](../../gems/libtmux-mcp/README.md#run-a-script-in-a-pane).
data/README.md CHANGED
@@ -11,17 +11,19 @@
11
11
 
12
12
  # libtmux-mcp
13
13
 
14
- Expose an existing tmux server over MCP stdio. Read snapshots, capture pane
15
- output and wait for events, or enable creation, input and shell commands one
16
- tool at a time.
14
+ Expose a tmux server over MCP stdio. Read snapshots, capture pane output and
15
+ wait for events, or enable creation, input and shell commands one tool at a
16
+ time.
17
17
 
18
18
  [API reference](https://libtmux.org/en/ruby/latest/mcp/reference/) ·
19
19
  [Tools](https://libtmux.org/en/ruby/latest/mcp/tools/) ·
20
20
  [Guides](https://libtmux.org/en/ruby/latest/mcp/guides/) ·
21
21
  [RubyGems](https://rubygems.org/gems/libtmux-mcp)
22
22
 
23
- [![libtmux.org](https://img.shields.io/badge/docs-libtmux.org-0b7285)](https://libtmux.org/en/ruby/latest/mcp/)
23
+ [![compatibility](https://github.com/libtmux/libtmux-ruby/actions/workflows/compatibility.yml/badge.svg)](https://github.com/libtmux/libtmux-ruby/actions/workflows/compatibility.yml)
24
24
  [![libtmux-mcp](https://img.shields.io/gem/v/libtmux-mcp?label=libtmux-mcp)](https://rubygems.org/gems/libtmux-mcp)
25
+ [![libtmux.org](https://img.shields.io/badge/docs-libtmux.org-0b7285)](https://libtmux.org/en/ruby/latest/mcp/)
26
+ [![license](https://img.shields.io/badge/license-MIT-blue)](https://github.com/libtmux/libtmux-ruby/blob/master/LICENSE)
25
27
 
26
28
  </div>
27
29
 
@@ -29,9 +31,7 @@ Part of [libtmux for Ruby](https://github.com/libtmux/libtmux-ruby#readme).
29
31
  Built on [libtmux](https://github.com/libtmux/libtmux-ruby/tree/master/gems/libtmux)
30
32
  and [libtmux-async](https://github.com/libtmux/libtmux-ruby/tree/master/gems/libtmux-async).
31
33
 
32
- An MCP client starts `libtmux-mcp` and talks to it over stdin and stdout. The
33
- official MCP SDK handles the protocol, and bounded Async tasks handle
34
- transport.
34
+ An MCP client starts `libtmux-mcp` and talks to it over stdin and stdout.
35
35
 
36
36
  **Contents** — [Quick start](#quick-start) ·
37
37
  [Connect a client](#connect-a-client) ·
@@ -50,21 +50,24 @@ and tmux 3.3a or newer:
50
50
  $ gem install libtmux-mcp
51
51
  ```
52
52
 
53
- The server borrows a tmux server that is already running. Start one on a
54
- socket of its own:
55
-
56
- ```console
57
- $ tmux -L agent new-session -d -s work
58
- ```
59
-
60
- Add the server to Claude Code. Until you enable more tools, it serves
53
+ Add the server to Claude Code. `--start-session work` starts a tmux server on
54
+ the socket `agent` when none answers there, with a session named `work`, and
55
+ stops it when the server exits. Until you enable more tools, the server serves
61
56
  [`tmux_capabilities`][tool-tmux_capabilities] and
62
57
  [`tmux_snapshot`][tool-tmux_snapshot]:
63
58
 
64
59
  ```console
65
- $ claude mcp add tmux -- libtmux-mcp --socket-name agent --endpoint dev
60
+ $ claude mcp add tmux -- libtmux-mcp \
61
+ --socket-name agent \
62
+ --start-session work \
63
+ --endpoint dev
66
64
  ```
67
65
 
66
+ [`tmux_capabilities`][tool-tmux_capabilities] reports the connected daemon's
67
+ version. Each row in `optional_features` identifies the feature, its release
68
+ requirement and whether it is `available`. The `workarounds` rows identify
69
+ release-specific behavior. Check `enabled_tools` before calling a tool.
70
+
68
71
  Run by hand, `libtmux-mcp` waits for input until EOF. `libtmux-mcp --help`
69
72
  lists every option.
70
73
 
@@ -72,15 +75,17 @@ lists every option.
72
75
 
73
76
  ### `claude mcp add`: Claude Code
74
77
 
75
- The command in the quick start registers the server with Claude Code. Start the
76
- tmux server first, as above.
78
+ The command in the quick start registers the server with Claude Code.
77
79
 
78
80
  ### `codex mcp add`: Codex CLI
79
81
 
80
82
  Codex CLI takes the same command after `--`:
81
83
 
82
84
  ```console
83
- $ codex mcp add tmux -- libtmux-mcp --socket-name agent --endpoint dev
85
+ $ codex mcp add tmux -- libtmux-mcp \
86
+ --socket-name agent \
87
+ --start-session work \
88
+ --endpoint dev
84
89
  ```
85
90
 
86
91
  ### `mcpServers`: clients that read JSON
@@ -94,7 +99,7 @@ A client that reads a JSON configuration, such as Claude Desktop's
94
99
  "mcpServers": {
95
100
  "tmux": {
96
101
  "command": "libtmux-mcp",
97
- "args": ["--socket-name", "agent", "--endpoint", "dev"]
102
+ "args": ["--socket-name", "agent", "--start-session", "work", "--endpoint", "dev"]
98
103
  }
99
104
  }
100
105
  }
@@ -118,26 +123,42 @@ $ libtmux-mcp \
118
123
  ```
119
124
 
120
125
  Use `--socket-name NAME` instead of `--socket PATH` to select a named socket,
121
- as the quick start does:
122
-
123
- ```console
124
- $ libtmux-mcp \
125
- --socket-name agent
126
- ```
126
+ as the quick start does.
127
127
 
128
128
  - With neither flag, the server borrows the socket in `LIBTMUX_SOCKET_PATH`,
129
129
  else the name in `LIBTMUX_SOCKET`, else the server `TMUX` names when it runs
130
130
  inside tmux, else tmux's default socket. When both variables are set, the
131
131
  path wins, and an empty variable counts as unset. A flag ignores both
132
132
  variables, and the two flags are refused together.
133
- - The server never starts a tmux server. When none answers at the socket it
134
- chose, it exits with status 1.
133
+ - Without `--start-session`, the server never starts a tmux server. When none
134
+ answers at the socket it chose, it exits with status 1.
135
135
  - Started from a tmux pane with no flag or variable, the server drives that
136
136
  pane's server. Pass `--socket-name` to give an agent a server of its own.
137
- - EOF retires the clients the server owns and leaves the daemon running.
137
+ - EOF retires the clients the server owns and leaves a borrowed daemon
138
+ running.
138
139
  - `--endpoint` sets the public alias in discovery and resource URIs, and
139
140
  defaults to `local`.
140
141
 
142
+ ### `--start-session`: start the server too
143
+
144
+ `--start-session NAME` starts a tmux server on the socket the other flags
145
+ choose when none answers there. The server has an empty configuration and one
146
+ session named `NAME`, which runs your default shell:
147
+
148
+ ```console
149
+ $ libtmux-mcp \
150
+ --socket-name agent \
151
+ --start-session work
152
+ ```
153
+
154
+ - A server that already answers is borrowed as it is. The flag creates no
155
+ session in it and stops nothing.
156
+ - EOF stops the server the flag started. Add `--keep` to leave it running,
157
+ for example to attach to it from a terminal.
158
+ - A fresh server's first pane is `%0`.
159
+ - `NAME` starts with a letter or digit, then takes letters, digits, `_` and
160
+ `-`, up to 128 characters.
161
+
141
162
  ## The tools
142
163
 
143
164
  Each tool name links to its page on libtmux.org, with the arguments, limits and
@@ -162,7 +183,9 @@ cleanup through actual pipes, including the installed executable.
162
183
 
163
184
  ### [`tmux_snapshot`][tool-tmux_snapshot]: the layout, in pages
164
185
 
165
- - Pages come from one immutable capture, and a cursor continues that capture.
186
+ - Provide `entity` for a new capture, with optional `criteria` and `limit`.
187
+ To continue that capture, pass `cursor` alone.
188
+ - An `invalid_input` error names the failed field or request rule.
166
189
  - An expired, evicted or foreign cursor returns an error. The server never
167
190
  substitutes a fresh listing.
168
191
 
@@ -190,9 +213,8 @@ $ libtmux-mcp \
190
213
  ### [`tmux_wait`][tool-tmux_wait]: wait for text or exit
191
214
 
192
215
  - It waits for screen text, or for the pane process to exit. Screen text
193
- comes from control events on tmux 3.7 and newer, and from polling the pane
194
- every 50 ms on older daemons, which take no control client. Process exit
195
- uses process descriptors.
216
+ comes from control events on tmux 3.7 and newer, and from polling on older
217
+ daemons, which take no control client.
196
218
  - Wrapped rows are joined, so text that the pane width split across rows
197
219
  still matches.
198
220
  - If the pane, or the tmux server holding it, ends before the text appears,
@@ -202,8 +224,8 @@ $ libtmux-mcp \
202
224
  `observation_lost`.
203
225
  - A cancelled or expired wait closes its observers and never signals the pane
204
226
  program.
205
- - Process tracking needs a native identity backend: Linux peer pidfds with
206
- matching PID namespaces, or Darwin kqueue. Without one, the call is refused.
227
+ - Process tracking needs native process identity. Without it, the call is
228
+ refused.
207
229
 
208
230
  ### [Resource templates][mcp-resources]: the same data as URIs
209
231
 
@@ -226,6 +248,7 @@ Enable the three tools together:
226
248
  ```console
227
249
  $ libtmux-mcp \
228
250
  --socket-name agent \
251
+ --start-session work \
229
252
  --enable-tool tmux_create \
230
253
  --enable-tool tmux_send \
231
254
  --enable-tool tmux_close
@@ -253,21 +276,14 @@ $ libtmux-mcp \
253
276
  [`tmux_run`][tool-tmux_run] runs a POSIX script in a pane's interactive zsh
254
277
  5.9. It needs both the tool and an enrolled pane.
255
278
 
256
- List the pane IDs of the server:
257
-
258
- ```console
259
- $ tmux -L agent list-panes -a -F '#{pane_id}'
260
- ```
261
-
262
- ```text
263
- %0
264
- ```
265
-
266
- Enable the tool and write a setup file for one exact pane:
279
+ Enable the tool and write a setup file for one exact pane. The first pane of
280
+ the server that `--start-session` starts is `%0`; on a server you started
281
+ another way, read the pane IDs with [`tmux_snapshot`][tool-tmux_snapshot]:
267
282
 
268
283
  ```console
269
284
  $ libtmux-mcp \
270
285
  --socket-name agent \
286
+ --start-session work \
271
287
  --enable-tool tmux_run \
272
288
  --enroll-pane %0=enroll.zsh
273
289
  ```
@@ -346,26 +362,24 @@ pass the invitation to [`accept_shell`][mcp-application-accept_shell].
346
362
 
347
363
  ## Support and status
348
364
 
349
- Releases are numbered `0.1.N`, and each release increments `N`. The API is not
350
- settled: any release may change or remove exported identifiers, and the
351
- changelog marks each break. Pin an exact version to avoid one.
352
-
353
- - **Ruby 3.3 or newer.** The gem depends on `libtmux` and `libtmux-async` at
354
- the same version, the `mcp` SDK 1.5, `digest` and `optparse`.
355
- - **tmux 3.3a or newer.** The server refuses an older daemon when it starts,
356
- before it serves any tool, and prints the reason and the remedy on stderr.
365
+ - **Unstable API.** Releases are numbered `0.1.N`, and any release may change
366
+ or remove exported identifiers. Pin an exact version. See the
367
+ [changelog](CHANGELOG.md), and the [main
368
+ changelog](https://github.com/libtmux/libtmux-ruby/blob/master/CHANGELOG.md)
369
+ for changes that span gems.
370
+ - **Ruby 3.3 or newer** and **tmux 3.3a or newer.** The server refuses an older
371
+ daemon when it starts, before it serves any tool, and prints the reason and
372
+ the remedy on stderr.
357
373
  - **A daemon that changes while it runs.** A tool call returns an
358
374
  `unsupported` result with `delivery: not_sent`. The result names the floor
359
375
  and says to upgrade tmux and restart the server.
360
376
  - **The `screen_contains` wait.** It uses control events on tmux 3.7 and
361
- newer and polls the pane screen on older daemons, which take no control
362
- client.
363
- - **Linux, including WSL, and macOS.** The core
364
- [compatibility workflow][compat] runs Ruby 3.3, 3.4 and 4.0 with tmux 3.3a
365
- through 3.8 on Ubuntu 24.04. A push and a pull request run the oldest and
366
- newest of those releases, 3.3a and 3.8, and a release runs all of them. macOS
367
- is supported on a best-effort basis, and CI coverage for it is highly
368
- limited.
377
+ newer and polls the pane screen on older daemons.
378
+ - **Dependencies.** The gem needs `libtmux` and `libtmux-async` at the same
379
+ version as itself, and the `mcp` SDK.
380
+ - **Linux, including WSL, and macOS.** CI runs on Linux; macOS is best effort.
381
+ See the [compatibility workflow][compat] and the
382
+ [CI matrix](../../.github/CONTRIBUTING.md#running-ci-against-any-tmux).
369
383
 
370
384
  ## Documentation
371
385
 
@@ -83,7 +83,7 @@ module LibTmux
83
83
  application = self
84
84
  @tools = @enabled.map do |name|
85
85
  CatalogTool.define(name: name, description: Catalog.description(name),
86
- input_schema: Catalog.input(name), output_schema: Catalog.output(name),
86
+ input_schema: Catalog.input(name), output_schema: Catalog.output(name), meta: Catalog.meta(name),
87
87
  annotations: {read_only_hint: false, destructive_hint: Catalog::MUTATIONS.include?(name), idempotent_hint: false, open_world_hint: false}) do |server_context: nil, **arguments|
88
88
  application.call(name, arguments, cancellation: server_context&.cancellation)
89
89
  end
@@ -124,12 +124,19 @@ module LibTmux
124
124
 
125
125
  begin
126
126
  tool = @by_name.fetch(name)
127
- tool.input_schema_value
127
+ input_schema = tool.input_schema_value
128
128
  tool.output_schema_value
129
- wire = JSON.generate(arguments)
130
- raise CapacityError.new("MCP input exceeds its byte limit", phase: :admission) if wire.bytesize > 1 << 20
131
- arguments = JSON.parse(wire, max_nesting: 68, allow_nan: false, allow_duplicate_key: false)
132
- tool.input_schema_value.validate_arguments(arguments)
129
+ begin
130
+ wire = JSON.generate(arguments)
131
+ raise CapacityError.new("MCP input exceeds its byte limit", phase: :admission) if wire.bytesize > 1 << 20
132
+ arguments = JSON.parse(wire, max_nesting: 68, allow_nan: false, allow_duplicate_key: false)
133
+ input_schema.validate_arguments(arguments)
134
+ rescue ::MCP::Tool::InputSchema::ValidationError
135
+ message = tool.invalid_arguments_message(arguments).byteslice(0, 256).scrub("")
136
+ return failure_response("invalid_input", message)
137
+ rescue JSON::JSONError
138
+ return failure_response("invalid_input", "Arguments must be valid JSON within the nesting limit.")
139
+ end
133
140
  token = Internal::Cancellation.new
134
141
  @calls[token] = ::Async::Task.current
135
142
  callback = cancellation&.on_cancel { token.cancel }
@@ -153,17 +160,19 @@ module LibTmux
153
160
  ensure
154
161
  finish_request(token, cancellation, callback, primary: $!)
155
162
  end
156
- rescue ::MCP::Tool::InputSchema::ValidationError, JSON::JSONError, ArgumentError
157
- failure_response("invalid_input", "Input does not match the operation schema.")
158
163
  rescue CursorError, Observation::StaleCursor
159
164
  failure_response("stale_cursor", "The cursor is unknown, expired, or outside its captured result.")
160
- rescue LibTmux::Error, SystemCallError, IOError => error
165
+ rescue LibTmux::Error, SystemCallError, IOError, ArgumentError, JSON::JSONError, ::MCP::Tool::OutputSchema::ValidationError => error
161
166
  code = {InvalidFilterError => "invalid_filter", FieldDecodeError => "decode_error",
162
167
  IncompleteSnapshotError => "incomplete_snapshot", Cancelled => "cancelled",
163
168
  DeadlineExceeded => "deadline", CapacityError => "capacity", TargetNotFoundError => "stale_target",
164
169
  CommandError => "command_failed", UnsupportedFeatureError => "unsupported", ClosedError => "closed",
165
170
  Observation::LostObservation => "observation_lost", Observation::PaneDied => "pane_died"}.fetch(error.class, "transport_error")
166
- delivery = error.respond_to?(:delivery) ? (mutation ? mutation.delivery(error) : error.delivery) : :possibly_sent
171
+ delivery = if error.respond_to?(:delivery)
172
+ mutation ? mutation.delivery(error) : error.delivery
173
+ else
174
+ token ? :possibly_sent : :not_sent
175
+ end
167
176
  delivery = :observed if result
168
177
  effects = if name == "tmux_run"
169
178
  receipt = result && result["authorization"] || (error.run_receipt if error.respond_to?(:run_receipt))
@@ -407,9 +416,13 @@ module LibTmux
407
416
  end
408
417
 
409
418
  def capabilities(cancel)
410
- snapshot = acquire(cancel)
411
- observation = Observation.capabilities(snapshot.server_info.fetch(:version))
412
- {"endpoint" => @endpoint, "server_identity" => identity(snapshot), "enabled_tools" => @enabled,
419
+ report = @server.capabilities(timeout: @limits.fetch("request_timeout_seconds"), cancel: cancel)
420
+ observation = Observation.capabilities(report.version)
421
+ data = report.to_h
422
+ optional = data.delete("features").map do |feature|
423
+ feature.merge("available" => feature.fetch("supported")).except("supported")
424
+ end
425
+ data.merge("optional_features" => optional, "endpoint" => @endpoint, "enabled_tools" => @enabled,
413
426
  "criteria_schema" => FilterExpr.json_schema, "limits" => @limits,
414
427
  "owns_daemon" => false, "resource_subscriptions" => false,
415
428
  "observation" => observation,
@@ -417,7 +430,7 @@ module LibTmux
417
430
  "shell_profile" => "zsh-5.9-zle", "enrollment" => "explicit_source",
418
431
  "authorization" => "exact_generation_at_queue_grant", "stdin" => "closed",
419
432
  "persistent_shell_changes" => false, "descendant_termination" => "unobserved",
420
- "requirements" => observation.fetch("requirements") + ["explicit idle/empty ZLE enrollment", "installed Ruby/core helper dependencies"]}}
433
+ "requirements" => observation.fetch("requirements") + ["explicit idle/empty ZLE enrollment", "installed Ruby/core helper dependencies"]})
421
434
  end
422
435
 
423
436
  def snapshot(arguments, cancel)
@@ -3,6 +3,22 @@
3
3
  module LibTmux
4
4
  module MCP
5
5
  module Catalog
6
+ CAPABILITY_KEY = "com.git-pull.libtmux-mcp/capability"
7
+ # Toolset, process reach and tmux effects of each tool's own operation, in
8
+ # the capability model's vocabulary. tmux_create starts the caller's argv
9
+ # in a pane of its own with no shell, so it is pane-program. tmux_run
10
+ # runs a script in an existing pane's shell, so it is pane-command.
11
+ CAPABILITIES = {
12
+ "tmux_capabilities" => ["inspect", "none", %w[observe]],
13
+ "tmux_snapshot" => ["inspect", "none", %w[observe]],
14
+ "tmux_capture" => ["inspect", "none", %w[observe]],
15
+ "tmux_wait" => ["inspect", "none", %w[observe]],
16
+ "tmux_create" => ["execute", "pane-program", %w[observe change]],
17
+ "tmux_send" => ["execute", "pane-input", %w[observe change]],
18
+ "tmux_run" => ["execute", "pane-command", %w[observe change]],
19
+ "tmux_close" => ["teardown", "none", %w[observe delete]]
20
+ }.freeze
21
+
6
22
  READ_ONLY = %w[tmux_capabilities tmux_snapshot].freeze
7
23
  MUTATIONS = %w[tmux_send tmux_create tmux_close tmux_run].freeze
8
24
  OBSERVATIONS = %w[tmux_capture tmux_wait].freeze
@@ -10,6 +26,11 @@ module LibTmux
10
26
  MUTATION_BYTES = 1 << 16
11
27
  MIN_MUTATION_RESPONSE_BYTES = 4096
12
28
 
29
+ def self.meta(name)
30
+ toolset, process_reach, tmux_effects = CAPABILITIES.fetch(name)
31
+ {CAPABILITY_KEY => {"toolset" => toolset, "processReach" => process_reach, "tmuxEffects" => tmux_effects.dup}}
32
+ end
33
+
13
34
  def self.object(properties, required = properties.keys)
14
35
  {"type" => "object", "properties" => properties, "required" => required, "additionalProperties" => false}
15
36
  end
@@ -44,6 +65,24 @@ module LibTmux
44
65
  "start_time" => {"type" => "integer"}, "tmux_version" => text})
45
66
  end
46
67
 
68
+ def self.feature_properties
69
+ {"id" => text, "name" => text, "summary" => text,
70
+ "handling" => {"enum" => %w[refuse fallback repair]},
71
+ "apis" => {"type" => "array", "items" => text},
72
+ "commands" => {"type" => "array", "items" => object({"command" => text,
73
+ "flags" => {"type" => "array", "items" => text}})}}
74
+ end
75
+
76
+ def self.feature_decision
77
+ object(feature_properties.merge("since" => text, "error" => {"type" => %w[string null]},
78
+ "except" => {"type" => "array", "items" => text}, "available" => {"type" => "boolean"}))
79
+ end
80
+
81
+ def self.workaround_decision
82
+ object(feature_properties.merge("releases" => {"type" => "array", "items" => text},
83
+ "active" => {"type" => "boolean"}))
84
+ end
85
+
47
86
  def self.capture_limits
48
87
  {"max_lines" => {"type" => "integer", "minimum" => 1, "maximum" => 1000},
49
88
  "max_bytes" => {"type" => "integer", "minimum" => 1, "maximum" => 262144},
@@ -159,6 +198,8 @@ module LibTmux
159
198
  wait_output
160
199
  elsif name == "tmux_capabilities"
161
200
  object({"endpoint" => text, "server_identity" => identity,
201
+ "optional_features" => {"type" => "array", "items" => feature_decision},
202
+ "workarounds" => {"type" => "array", "items" => workaround_decision},
162
203
  "enabled_tools" => {"type" => "array", "items" => text},
163
204
  "criteria_schema" => {"type" => "object"},
164
205
  "limits" => {"type" => "object", "additionalProperties" => {"type" => "number"}},
@@ -248,9 +289,9 @@ module LibTmux
248
289
  elsif name == "tmux_close"
249
290
  "Destroy one exact session, window or pane. Opt-in destructive operation; closing a session or window also removes its contained topology. Never selects by name or closes the daemon directly. Returns the final tmux client outcome."
250
291
  elsif name == "tmux_capabilities"
251
- "Discover the fixed endpoint, server binding, enabled tools, criteria schema and limits. Acquires metadata; starts no daemon."
292
+ "Discover the bound daemon's optional features, required releases and active workarounds, plus enabled tools, criteria schema and limits. Feature IDs match capabilities.json. Starts no daemon; feature support does not grant tool permission."
252
293
  else
253
- "Capture ordered metadata and evaluate Ruby criteria locally. Pages retain one immutable capture; expired cursors require a new query. Limit defaults to 50 records, maximum 200."
294
+ "Capture ordered metadata. Provide entity with optional criteria and limit for a new query, or cursor alone to continue a capture. Limit defaults to 50 records, maximum 200. Expired cursors require a new query."
254
295
  end
255
296
  end
256
297
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json_schemer"
4
+
3
5
  module LibTmux
4
6
  module MCP
5
7
  # Handshake metadata does not need schemas. Validate each through the SDK
@@ -21,6 +23,43 @@ module LibTmux
21
23
  @output_schema_value ||= ::MCP::Tool::OutputSchema.new(@output_definition)
22
24
  end
23
25
 
26
+ def invalid_arguments_message(arguments)
27
+ fallback = "Arguments do not match the operation schema."
28
+ return fallback unless name_value == "tmux_snapshot"
29
+ return "Arguments must be a JSON object." unless arguments.is_a?(Hash)
30
+
31
+ fields = @input_definition.fetch("properties").keys
32
+ unless (arguments.keys - fields).empty?
33
+ return "Unexpected property. Use #{fields.join(", ")}."
34
+ end
35
+ return "Provide entity or cursor." unless arguments.key?("entity") || arguments.key?("cursor")
36
+ return "Provide cursor alone to continue a capture." if arguments.key?("cursor") && arguments.size > 1
37
+
38
+ @input_diagnostics ||= JSONSchemer.schema(@input_definition, format: false)
39
+ issue = @input_diagnostics.validate(arguments).first
40
+ return fallback unless issue
41
+
42
+ pointer = issue.fetch("data_pointer")
43
+ field = pointer.split("/", 3)[1]
44
+ return fallback unless fields.include?(field)
45
+
46
+ declared_path = issue.fetch("schema_pointer").scan(%r{/properties(/[^/]+)}).flatten.join
47
+ return "/#{field} must match the advertised schema." unless pointer == declared_path
48
+
49
+ schema = issue.fetch("schema")
50
+ case issue.fetch("type")
51
+ when "enum" then "#{pointer} must be one of #{schema.fetch("enum").join(", ")}."
52
+ when "minimum" then "#{pointer} must be at least #{schema.fetch("minimum")}."
53
+ when "maximum" then "#{pointer} must be at most #{schema.fetch("maximum")}."
54
+ else
55
+ if issue["x-error"] || issue["i18n"]
56
+ "#{pointer} must match the advertised schema."
57
+ else
58
+ issue.fetch("error")
59
+ end
60
+ end
61
+ end
62
+
24
63
  def to_h
25
64
  input_schema_value
26
65
  output_schema_value
@@ -6,6 +6,13 @@ require "optparse"
6
6
  module LibTmux
7
7
  module MCP
8
8
  class CLI
9
+ # The tmux server --start-session asked for did not come up.
10
+ class StartError < StandardError; end
11
+ private_constant :StartError
12
+
13
+ SESSION_NAME = /\A[A-Za-z0-9][A-Za-z0-9_-]{0,127}\z/
14
+ private_constant :SESSION_NAME
15
+
9
16
  def self.run(arguments, input: $stdin, out: $stdout, err: $stderr, environment: ENV)
10
17
  new(arguments, input, out, err, environment).run
11
18
  end
@@ -34,6 +41,8 @@ module LibTmux
34
41
  0
35
42
  rescue Interrupt, LibTmux::Cancelled => error
36
43
  report("MCP interrupted; dispatched effects may remain.", 130, failure: error)
44
+ rescue StartError => error
45
+ report("MCP #{error.message}.", 1, failure: error)
37
46
  rescue StandardError => error
38
47
  if error.is_a?(LibTmux::UnsupportedFeatureError) && error.phase == :admission && error.delivery == :not_sent
39
48
  report("MCP refused to start: #{error.message}.", 1, failure: error)
@@ -72,6 +81,80 @@ module LibTmux
72
81
  end
73
82
 
74
83
  def serve(endpoint, task)
84
+ started = start_session(endpoint)
85
+ failure = nil
86
+ begin
87
+ serve_endpoint(endpoint, task)
88
+ rescue Exception => error
89
+ failure = error
90
+ ensure
91
+ if started && !@options[:keep]
92
+ begin
93
+ stop_started_server(endpoint, started)
94
+ rescue Exception => error
95
+ if failure
96
+ ProcessIdentity.attach_cleanup(failure, ["MCP could not stop the tmux server it started (#{error.class})"])
97
+ else
98
+ failure = error
99
+ end
100
+ end
101
+ end
102
+ end
103
+ raise failure if failure
104
+ end
105
+
106
+ # With --start-session and no server answering at the endpoint, starts
107
+ # one with an empty configuration that holds the session, and answers its
108
+ # pid. A server that answers is borrowed as it is, so the option never
109
+ # changes a server it did not start.
110
+ def start_session(endpoint)
111
+ name = @options[:start_session]
112
+ return nil if name.nil? || answering?(endpoint)
113
+
114
+ directory = File.dirname(endpoint.socket_path)
115
+ # tmux makes this directory itself for a socket named with -L, not for -S.
116
+ Dir.mkdir(directory, 0o700) if File.basename(directory) == "tmux-#{Process.uid}" && !File.exist?(directory)
117
+ argv = [endpoint.executable, "-S", endpoint.socket_path, "-f", File::NULL, "new-session", "-d", "-s", name]
118
+ result = Internal::ProcessExecutor.new.run(argv, timeout: @options.fetch(:timeout), discard_stdout: true)
119
+ reason = result.stderr.dup.force_encoding(Encoding::UTF_8).scrub.lines.first.to_s.gsub(/[[:cntrl:]]/, " ").strip.byteslice(0, 200).to_s.scrub
120
+ failure = StartError.new("could not start session #{name}: #{reason.empty? ? 'tmux started no server' : reason}")
121
+ raise failure unless result.success?
122
+
123
+ begin
124
+ Server.open(endpoint: endpoint) do |server|
125
+ begin
126
+ server_pid(server)
127
+ rescue Exception
128
+ # Started moments ago: a failure here must not leave it running.
129
+ server.kill(timeout: @options.fetch(:timeout))
130
+ raise
131
+ end
132
+ end
133
+ rescue TargetNotFoundError
134
+ # tmux 3.7c and older exit with status 0 when they cannot create the socket.
135
+ raise failure
136
+ end
137
+ end
138
+
139
+ def answering?(endpoint)
140
+ Server.open(endpoint: endpoint) { true }
141
+ rescue TargetNotFoundError
142
+ false
143
+ end
144
+
145
+ def server_pid(server)
146
+ server.display('#{pid}', timeout: @options.fetch(:timeout)).text.strip
147
+ end
148
+
149
+ # Stops the server this process started, and only that one: when the
150
+ # socket now answers with another pid, the one it started is already gone.
151
+ def stop_started_server(endpoint, pid)
152
+ Server.open(endpoint: endpoint) { |server| server.kill(timeout: @options.fetch(:timeout)) if server_pid(server) == pid }
153
+ rescue TargetNotFoundError
154
+ nil
155
+ end
156
+
157
+ def serve_endpoint(endpoint, task)
75
158
  Server.open(endpoint: endpoint) do |server|
76
159
  LibTmux::Async.open(server: server, parent: task) do |scope|
77
160
  app = Application.new(server: scope.server, endpoint_name: @options.fetch(:endpoint_name),
@@ -209,6 +292,10 @@ module LibTmux
209
292
  "server TMUX names, then tmux's default socket") { |value| @options[:socket_path] = value }
210
293
  options.on("--socket-name NAME", "Borrow the tmux server with this socket name",
211
294
  "Not with --socket") { |value| @options[:socket_name] = value }
295
+ options.on("--start-session NAME", "When no tmux server answers at the socket, start one with an",
296
+ "empty configuration that holds session NAME, and stop it at",
297
+ "exit. A server that answers is borrowed as it is") { |value| @options[:start_session] = value }
298
+ options.on("--keep", "With --start-session, leave the server it started running at exit") { @options[:keep] = true }
212
299
  options.on("--endpoint NAME", "Public endpoint alias (default: local)") { |value| @options[:endpoint_name] = value }
213
300
  options.on("--tmux EXECUTABLE", "Resolve and pin this tmux executable at startup") { |value| @options[:executable] = value }
214
301
  options.on("--enable-tool NAME", "Enable one additional tool; repeat as needed") { |value| @options.fetch(:tools) << value }
@@ -233,6 +320,8 @@ module LibTmux
233
320
  def validate_arguments
234
321
  raise ArgumentError unless @arguments.empty?
235
322
  raise ArgumentError if @options[:socket_path] && @options[:socket_name]
323
+ raise ArgumentError if @options[:start_session] && !SESSION_NAME.match?(@options[:start_session])
324
+ raise ArgumentError if @options[:keep] && !@options[:start_session]
236
325
  raise ArgumentError unless @options.fetch(:tools).all? { |name| Catalog::NAMES.include?(name) }
237
326
  raise ArgumentError unless /\A[A-Za-z0-9][A-Za-z0-9_.-]{0,127}\z/.match?(@options.fetch(:endpoint_name))
238
327
  raise ArgumentError unless @options.fetch(:timeout).finite? && @options.fetch(:timeout).positive?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module LibTmux
4
4
  module MCP
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libtmux-mcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - libtmux contributors
@@ -15,56 +15,88 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.1.1
18
+ version: 0.1.3
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.1.1
25
+ version: 0.1.3
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: libtmux-async
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - '='
31
31
  - !ruby/object:Gem::Version
32
- version: 0.1.1
32
+ version: 0.1.3
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: 0.1.1
39
+ version: 0.1.3
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: mcp
42
42
  requirement: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - "~>"
44
+ - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: 1.5.1
47
+ - - "<"
48
+ - !ruby/object:Gem::Version
49
+ version: '2'
47
50
  type: :runtime
48
51
  prerelease: false
49
52
  version_requirements: !ruby/object:Gem::Requirement
50
53
  requirements:
51
- - - "~>"
54
+ - - ">="
52
55
  - !ruby/object:Gem::Version
53
56
  version: 1.5.1
57
+ - - "<"
58
+ - !ruby/object:Gem::Version
59
+ version: '2'
54
60
  - !ruby/object:Gem::Dependency
55
61
  name: digest
56
62
  requirement: !ruby/object:Gem::Requirement
57
63
  requirements:
58
- - - "~>"
64
+ - - ">="
59
65
  - !ruby/object:Gem::Version
60
66
  version: 3.2.1
67
+ - - "<"
68
+ - !ruby/object:Gem::Version
69
+ version: '4'
61
70
  type: :runtime
62
71
  prerelease: false
63
72
  version_requirements: !ruby/object:Gem::Requirement
64
73
  requirements:
65
- - - "~>"
74
+ - - ">="
66
75
  - !ruby/object:Gem::Version
67
76
  version: 3.2.1
77
+ - - "<"
78
+ - !ruby/object:Gem::Version
79
+ version: '4'
80
+ - !ruby/object:Gem::Dependency
81
+ name: json_schemer
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '2.4'
87
+ - - "<"
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '2.4'
97
+ - - "<"
98
+ - !ruby/object:Gem::Version
99
+ version: '3'
68
100
  - !ruby/object:Gem::Dependency
69
101
  name: optparse
70
102
  requirement: !ruby/object:Gem::Requirement
@@ -92,6 +124,7 @@ executables:
92
124
  extensions: []
93
125
  extra_rdoc_files: []
94
126
  files:
127
+ - CHANGELOG.md
95
128
  - LICENSE
96
129
  - README.md
97
130
  - assets/logo.png
@@ -118,7 +151,8 @@ licenses:
118
151
  - MIT
119
152
  metadata:
120
153
  source_code_uri: https://github.com/libtmux/libtmux-ruby
121
- documentation_uri: https://libtmux.org/en/ruby/v0.1.1/mcp/
154
+ changelog_uri: https://github.com/libtmux/libtmux-ruby/blob/master/gems/libtmux-mcp/CHANGELOG.md
155
+ documentation_uri: https://libtmux.org/en/ruby/v0.1.3/mcp/
122
156
  rdoc_options: []
123
157
  require_paths:
124
158
  - lib