Skip to content
Merged
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
4 changes: 4 additions & 0 deletions pkgs/mcpx/docs/exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ loopback TCP can be a container with its own filesystem; neither transport
tells you whether the caller can see the daemon's disk. So the *caller* says
so, with `capabilities: ["artifacts"]` and optionally `sharedFs`.

An `ssh://` endpoint is the clearest case of `sharedFs: false`: the bytes
cross a process boundary on another machine, so a `{path}` is never
hardlinked there and an artifact is uploaded instead.

### The store

Content-addressed by sha256 under `<state>/artifacts/blobs/ab/cdef...`, with a
Expand Down
3 changes: 2 additions & 1 deletion pkgs/mcpx/docs/parity.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ carry instead is a description that says so and MCP annotations
| `tool_invoke` | `POST /v1/tools/{tool}` | `mcpx tool invoke` *generated* | `mcpx_tool_invoke` | `ops.toolInvoke()` | |
| `types` | `GET /v1/types` | `mcpx types` | `mcpx_types` | `ops.types()`, `types()` | |

## CLI commands (66)
## CLI commands (67)

Every command the binary dispatches, with the operations it reaches. A command
that reaches none says why: those are decisions, not gaps.
Expand Down Expand Up @@ -183,6 +183,7 @@ that reaches none says why: those are decisions, not gaps.
| `skills` | – | Manages embedded agent skills. |
| `stats` | `stats_query` | |
| `status` | `status` | |
| `stdio-proxy` | – | It is a byte pipe: it dials the daemon's unix socket on this host and copies stdin/stdout to it, which no /v1 operation can express. |
| `stop` | `shutdown` | |
| `supervise` | – | Runs in its own process as the re-exec target of the supervised transport; it serves no API. |
| `task` / `tasks` | `tasks_list`, `task_get`, `task_cancel`, `task_result` | Generated from the operation table. |
Expand Down
16 changes: 16 additions & 0 deletions pkgs/mcpx/docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,22 @@ the rest of this page:
2026-07-28 request. Every `/mcp` client used to share the daemon's pid and
cwd, and so one stateful instance and one set of artifacts.

#### MCP over ssh

An MCP host that can only spawn a process gets a remote daemon the same way
the CLI does: with ssh as the transport and no streamable-HTTP endpoint to
configure.

```json
{"mcpServers":{"mcpx":{"command":"ssh","args":["buildbox","mcpx","serve","--transport","stdio"]}}}
```

`mcpx serve` on the remote host is a stdio shim over the daemon **there**, so
the tool calls, elicitations and artifact bytes all belong to that daemon. No
port is opened and the only credential is the ssh one. For a client that sets
a daemon endpoint instead, `ssh://buildbox` is the shorter form and the client
starts ssh itself — see [`remote.md`](./remote.md).

---

### 2.5 Pass-through: upstreams under their own names
Expand Down
132 changes: 132 additions & 0 deletions pkgs/mcpx/docs/remote.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Reaching a daemon on another machine

The daemon speaks HTTP on a unix socket, and every client surface — the
CLI, scripts, the TUI, MCP — goes through the same client. Which means a
daemon on another machine is a transport question, not a feature: point
`daemon.endpoint` at it and everything works there.

Three routes exist, in order of how much setup they need.

## 1. `ssh://` — nothing to set up

```sh
export MCPX_DAEMON_ENDPOINT=ssh://buildbox
mcpx ls
mcpx tui
```

The client runs your own `ssh` binary (`ssh -T -o BatchMode=yes buildbox
mcpx stdio-proxy`) and speaks the daemon's HTTP API through it. Your ssh
agent, `ssh_config`, `known_hosts`, jump hosts and `Match` rules are the
access control, because it is literally your ssh doing the connecting.
Nothing listens on the remote host beyond the daemon's own unix socket, and
no mcpx credential exists to leak.

Details worth knowing:

- The remote socket path is resolved **by the remote process**, from mcpx's
own paths and configuration file there. The client makes no assumption
about the remote filesystem layout.
- `?bin=/path/to/mcpx` names a remote mcpx that is not on `PATH`:
`ssh://buildbox?bin=/opt/mcpx/bin/mcpx`.
- `?sock=/run/user/1000/mcpx/daemon.sock` picks a specific remote daemon,
for a host running more than one.
- `user@` and `:port` work as in any ssh target:
`ssh://drew@buildbox:2222`.
- `BatchMode=yes` is set, so ssh never stops to ask for a password or a
passphrase: an HTTP request has no terminal to answer on. Load the key in
your agent, or use a key with no passphrase.
- A remote endpoint is **never** auto-started. If nothing answers, the error
names the host and the exact remote command that was tried; start the
daemon there with `ssh buildbox mcpx daemon` (or `mcpx daemon
--detached`).
- Server-sent events (`/v1/events`, `mcpx log --follow`) stream over the
same tunnel; there is no separate event path.

`mcpx stdio-proxy` is the remote half and is a normal command: it dials the
local daemon socket and copies stdin/stdout to it. Running it by hand is the
quickest way to check a host:

```sh
ssh buildbox mcpx stdio-proxy --sock /run/user/1000/mcpx/daemon.sock
```

## 2. `ssh -L` — forward the socket itself

For tools that cannot run `ssh` themselves, or when you want one tunnel for
many shells, forward the unix socket as a local socket:

```sh
ssh -N -L /tmp/buildbox-mcpx.sock:/home/you/.local/state/mcpx/daemon.sock buildbox &
export MCPX_DAEMON_ENDPOINT=unix:///tmp/buildbox-mcpx.sock
mcpx status
```

The forward is a plain byte pipe, so it is the same thing the `ssh://`
transport does, just started by you instead of by the client. The remote
path must be the socket the daemon there is actually using — `mcpx status
--json` on the remote host prints it (`socket`).

`ssh -L` also works in the other direction of convenience: `-L
127.0.0.1:8065:127.0.0.1:8065` forwards the daemon's loopback TCP port, for
a remote daemon started with `daemon.address` widened or a container that
serves TCP. Prefer the socket: it is the same filesystem permission check
as being there.

## 3. `ssh -R` — a daemon behind NAT

The two routes above dial out from the client, which fails when the daemon's
host cannot be reached at all. Reverse the direction instead: the daemon's
host connects out to a host that is reachable, and publishes the socket
there.

On the daemon's host (behind NAT), forward its socket to a socket on the
public host:

```sh
ssh -N -R /tmp/team-mcpx.sock:/home/you/.local/state/mcpx/daemon.sock public-host
```

Then, anywhere that can reach `public-host`:

```sh
ssh -N -L /tmp/mcpx.sock:/tmp/team-mcpx.sock public-host &
MCPX_DAEMON_ENDPOINT=unix:///tmp/mcpx.sock mcpx ls
```

Or, with no local forward at all, use `ssh://` from the middle host, since
the socket is already there:

```sh
ssh public-host MCPX_DAEMON_ENDPOINT=unix:///tmp/team-mcpx.sock mcpx ls
```

`ssh -R` needs `AllowStreamLocalForwarding` (the default) on the server, and
`StreamLocalBindUnlink=yes` in the forwarding client's `ssh_config` to
replace a stale socket after a dropped connection.

## 4. `mcpx serve` over ssh — MCP to a remote daemon

An MCP host that cannot set an endpoint can spawn ssh instead of mcpx: see
the MCP-over-ssh form in [`protocol.md`](./protocol.md#24-transport-and-identity).
The short version:

```json
{"mcpServers":{"mcpx":{"command":"ssh","args":["buildbox","mcpx","serve","--transport","stdio"]}}}
```

That runs `mcpx serve` on the remote host, so the daemon there is the one
answering — again with no port and no credential beyond ssh.

## What a remote endpoint is not

- It is not a second authentication layer. **The daemon API is
unauthenticated**; who may reach it is who may reach the host and the
socket. `ssh://` inherits exactly the ssh access you already grant.
- It is not a shared filesystem. Scripts and artifacts sent to a remote
daemon travel as bytes; a `{path}` artifact is only hardlinked when the
caller proved it shares the daemon's disk, which an ssh tunnel never
proves. See [`exec.md`](./exec.md#the-store).
- It is never started from here. Local autostart does not apply, by design:
answering from a local daemon when the remote one is unreachable would be
worse than failing.
3 changes: 3 additions & 0 deletions pkgs/mcpx/docs/story.md
Original file line number Diff line number Diff line change
Expand Up @@ -2905,6 +2905,9 @@ else in this document is detail underneath those four verbs.
has a shape, with answers recorded inline.
- [`docs/dependencies.md`](./dependencies.md) — every library and external
program, and why each is needed.
- [`docs/remote.md`](./remote.md) — reaching a daemon on another machine:
`ssh://` endpoints, `ssh -L` socket forwarding, `ssh -R` for a daemon
behind NAT, and MCP over ssh.
- [`docs/opencode-plugin.md`](./opencode-plugin.md) — what the plugin does and
why, opencode v1 against v2, and which parts are opencode's rather than
general enough to port to another harness.
Expand Down
68 changes: 67 additions & 1 deletion pkgs/mcpx/internal/cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ type Client struct {
// endpoint is empty for the local socket, or a base URL for a daemon
// somewhere else.
endpoint string
// ssh, when set, is the ssh:// endpoint this client dials; sshURL is
// the endpoint as the user wrote it, which is what errors report (the
// http URL the transport uses is an internal placeholder).
ssh *daemon.SSHEndpoint
sshURL string
// lazySocket, for an in-process (inline) client, opens the daemon's
// socket on first use and returns its path. Inline mode creates no
// socket for its own calls; only a script — a child process, which
Expand Down Expand Up @@ -160,6 +165,36 @@ func NewClientAt(paths daemon.Paths, configPath, endpoint string) *Client {
}
return c
}
// An ssh:// endpoint (issue #1880) tunnels the same HTTP API through
// the operator's own ssh, so a remote daemon needs no listening port
// and no new credential. The system ssh binary is the transport: its
// agent, ssh_config, known_hosts and bastion rules already describe
// who may reach that host.
if ep, isSSH, perr := daemon.ParseSSHEndpoint(c.endpoint); isSSH {
c.ssh = &ep
c.sshURL = ep.String()
dialer := daemon.NewSSHDialer(ep)
var dial func(context.Context, string, string) (net.Conn, error)
if perr != nil {
dial = func(context.Context, string, string) (net.Conn, error) { return nil, perr }
} else {
dial = dialer.DialContext
}
c.endpoint = "http://" + dialer.Address()
c.hc = &http.Client{
Timeout: timeout,
Transport: &http.Transport{
DialContext: dial,
// One ssh process per connection, reused while it lives:
// starting ssh costs a handshake, so it is worth keeping
// an idle one exactly as a VPN endpoint is.
MaxIdleConns: set.Int("http.remoteIdleConns"),
MaxIdleConnsPerHost: set.Int("http.remoteIdleConns"),
IdleConnTimeout: idle,
},
}
return c
}
// A tsnet:// endpoint names a daemon by tailnet identity (#1967). The
// client joins the tailnet as an ephemeral node to dial it; there is
// no address to fall back to, and the dialer says so when it cannot
Expand Down Expand Up @@ -372,6 +407,22 @@ func (c *Client) Ping(ctx context.Context) bool {
return err == nil
}

// reachable is Ping with the ssh:// fallback EnsureDaemon has always had:
// an ssh handshake can legitimately take longer than
// autostart.pingTimeout, so a timed-out Ping is retried once as a plain
// health request bounded only by the caller's ctx before the daemon is
// declared unreachable. Non-ssh endpoints get Ping exactly.
func (c *Client) reachable(ctx context.Context) bool {
if c.Ping(ctx) {
return true
}
if c.ssh == nil {
return false
}
_, err := c.do(ctx, http.MethodGet, "/v1/health", nil)
return err == nil
}

// EnsureDaemon starts a background daemon if one is not already running and
// waits for it to become reachable. This is what makes every command work with
// no setup step.
Expand All @@ -387,12 +438,27 @@ func (c *Client) EnsureDaemon(ctx context.Context) error {
// Starting a local daemon because a remote one is unreachable would
// silently answer from the wrong machine, which is worse than
// failing.
if c.Ping(ctx) {
if c.reachable(ctx) {
return nil
}
if err := ctx.Err(); err != nil {
return err
}
if c.ssh != nil {
// Say what was tried. Over ssh the interesting failure is
// usually on the far side -- no daemon on that socket, or no
// mcpx on that PATH -- and "unreachable" alone sends the
// reader looking at the wrong machine.
// A successful health request here — after a Ping that
// timed out on a slow handshake — means the daemon IS
// reachable; accept it instead of reporting ErrNoDaemon.
if _, derr := c.do(ctx, http.MethodGet, "/v1/health", nil); derr == nil {
return nil
Comment on lines +455 to +456

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Apply the SSH health-check fallback to the CLI connection ladder

When an SSH handshake takes longer than autostart.pingTimeout, ordinary commands such as mcpx ls still reject the reachable daemon. Although EnsureDaemon now accepts a successful second health check, the actual command path is App.ensure → App.Connect, whose explicit-endpoint branch only calls c.Ping(ctx) and immediately returns an error on failure (internal/cli/connect.go). That path never reaches this fallback or its SSH diagnostics. This is fresh integration evidence beyond the previously fixed EnsureDaemon branch: route SSH connection checks in Connect through the same bounded fallback so normal CLI commands benefit from the fix.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0a9ee54: the fallback is now a shared Client.reachable() — Ping first, then (ssh:// endpoints only) one plain /v1/health request bounded by the caller's ctx — and Connect's named-endpoint rung calls it, as does EnsureDaemon's remote branch, so the two paths cannot drift apart again. Regression test TestSSHConnectFallsBackPastSlowHandshake: the fake ssh sleeps 3s on its first invocation (past the 2s autostart.pingTimeout); Connect still chooses the named endpoint. Full internal/cli suite passes on the laptop.

} else {
return fmt.Errorf("no mcpx daemon at %s (remote command: %s): %v; it is not started from here",
c.sshURL, strings.Join(c.ssh.RemoteCommand(), " "), derr)
}
}
return fmt.Errorf("no mcpx daemon at %s; it is not started from here", c.endpoint)
}
if c.Ping(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/mcpx/internal/cli/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (a *App) Connect(ctx context.Context, opt ConnectOptions) (*Client, *Ladder
if opt.Endpoint != "" {
start := time.Now()
c := NewClientAt(paths, a.ConfigPath, opt.Endpoint)
if c.Ping(ctx) {
if c.reachable(ctx) {
record("url", opt.Endpoint, start, nil)
l.Chosen = "url"
return c, l, nil
Expand Down
1 change: 1 addition & 0 deletions pkgs/mcpx/internal/cli/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (a *App) handWritten() map[string]Handler {
"secrets": a.CmdSecrets,
"dashboard": a.CmdDashboard,
"proxy": a.CmdProxy,
"stdio-proxy": a.CmdStdioProxy,
"embeddings": a.CmdEmbeddings,
"feedback": a.CmdFeedback,
"interactions": a.CmdInteractions,
Expand Down
18 changes: 18 additions & 0 deletions pkgs/mcpx/internal/cli/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,24 @@ func handCommands() []Command {
"mcpx serve --tools",
},
},
{
Name: "stdio-proxy", Group: "daemon",
Local: "It is a byte pipe: it dials the daemon's unix socket on this host and copies stdin/stdout to it, which no /v1 operation can express.",
Usage: "[--sock <path>]",
Summary: "serve the daemon's API on stdin/stdout, for an ssh tunnel",
Detail: "The remote half of an ssh:// daemon endpoint: `ssh host mcpx " +
"stdio-proxy` turns that ssh session's pipes into an HTTP connection " +
"to the daemon on the host. The socket is resolved here, by this " +
"process, from mcpx's own paths and configuration file, so the client " +
"needs no knowledge of the remote filesystem; --sock names a socket " +
"explicitly, for a host running more than one daemon. Public rather " +
"than hidden because the ssh command line a client runs names it. " +
"See docs/protocol.md for the MCP-over-ssh form.",
Examples: []string{
"ssh buildbox mcpx stdio-proxy",
"ssh buildbox mcpx stdio-proxy --sock /run/user/1000/mcpx/daemon.sock",
},
},
{
Name: "openapi", Group: "discovery",
Usage: "[-o file]",
Expand Down
Loading
Loading