# Rust MCP examples

Source: https://libtmux.org/en/rs/latest/mcp/examples/

> List sessions through the Rust MCP server and inspect implementation examples.

Connect the server using the [setup guide](../guides/), then call
[`list_sessions`](../tools/list_sessions/) from your MCP client.

## List sessions

This is the `params` object for an MCP `tools/call` request. Send it
through the connected client:

```json
{
  "name": "list_sessions",
  "arguments": {}
}
```

Use the returned session IDs when choosing a window or pane. The
[tool reference](../tools/list_sessions/) describes this port's result
and optional arguments.

## Internals

The following examples are for applications that embed or extend the server.
Installing and connecting an MCP client does not require this code.

The crate includes a complete stdio server with its tool selection chosen in Rust
code. Use it when the offered surface is part of the embedding
application's policy.

### Embed a read-only surface

```rust file="crates/tmux-mcp/examples/readonly.rs"
//! Serve tmux over MCP, offering only the inspect toolset.
//!
//! The shipped binary reads `LIBTMUX_TOOLSETS` at startup. Building the server
//! yourself is how you decide it in code instead, which is
//! what you want when the surface is a property of the program rather than of
//! how someone launched it.
//!
//! ```console
//! $ cargo run --example readonly
//! ```
//!
//! Then talk MCP to it on stdin and stdout.

use libtmux::Server;
use rmcp::ServiceExt as _;
use rmcp::transport::stdio;
use tmux_mcp::{Selection, TmuxTools};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let tools = TmuxTools::builder(Server::new()?)
        .selection(Selection::parse(Some("inspect"), None, None)?)
        .build();

    // stdout carries the protocol, so this goes to stderr.
    eprintln!("serving {} inspect tools", tools.offered().len());

    tools.serve(stdio()).await?.waiting().await?;
    Ok(())
}
```

The example waits until the transport ends. It selects libtmux's ambient
server, so choose the tmux environment before launching it. Application
code can instead construct a `Server` for its own endpoint.

### Run the source example

From the Rust repository with its toolchain and tmux installed:

```console
$ cargo run \
    -p tmux-mcp \
    --example readonly
```

The process speaks MCP on stdin and stdout. It writes the selected tool
count to stderr.

To inspect the configured tools and output schemas without
starting a tmux server:

```console
$ cargo run \
    -p tmux-mcp \
    --example surface
```

The [readonly source](https://github.com/libtmux/libtmux-rs/blob/f0e37052c232636b61d095817046e6bfc8f2ca40/crates/tmux-mcp/examples/readonly.rs)
and [surface source](https://github.com/libtmux/libtmux-rs/blob/f0e37052c232636b61d095817046e6bfc8f2ca40/crates/tmux-mcp/examples/surface.rs)
are shipped crate examples. The crate's test suite also drives an isolated
real tmux server. Rendering this source is not an execution of the example.

See [Topics](../topics/) for command deadlines and tool selection.
