rho-coding-agent 3.3.0

A fast Rust agent harness with a small footprint and opinionated defaults
//! `rho mcp remote`: the other machines' `rho mcp serve --http` that this
//! machine's `rho mcp serve` drives as a hub (`app::mcp_server::remotes`).
//! The secret, the token a remote issued this machine, is read from stdin
//! only, so `ssh` can pipe it straight from the remote's `rho mcp token
//! rotate` without it reaching a terminal, argv, or shell history. `add`
//! checks the remote answers before it saves it.

use serde::Serialize;
use serde_json::{Map, Value};

use super::{serve::open_credential_store, webhook::read_secret};
use crate::{
    app::{
        mcp_server::{
            remotes::{self, check_name, check_url, Remote, RemoteSet},
            token_store,
        },
        session_control::Action,
    },
    caller_context::CallerContext,
    cli::{Cli, McpRemoteCommand},
    credential_store::UntrackedCredentialStore,
};

pub(in crate::app) async fn remote(command: &McpRemoteCommand, cli: &Cli) -> anyhow::Result<()> {
    open_credential_store(cli)?;
    match command {
        McpRemoteCommand::Add { name, url } => {
            // Before the secret is read, so a typo fails before anything else.
            check_name(name)?;
            let url = check_url(url).await?.url;
            let secret = read_secret(&format!(
                "pipe the secret of the token {name} issued this machine on stdin, as `ssh {name} rho mcp token rotate hub --scope write --workspace DIR | rho mcp remote add {name} --url {url}`"
            ))?;
            let remote = Remote::new(name, url, &secret)?;
            probe(&remote).await?;
            let url = remote.url.clone();
            token_store::update(|remotes: &mut RemoteSet| {
                remotes.upsert(remote);
                Ok(())
            })?;
            println!(
                "saved remote {name:?} at {url}; it answered. Every token of this machine's `rho mcp serve` reaches it, as far as the token {name} issued allows"
            );
        }
        McpRemoteCommand::List { json } => {
            let remotes = RemoteSet::load(&UntrackedCredentialStore)?;
            let listing: Vec<Entry<'_>> = remotes
                .remotes
                .iter()
                .map(|remote| Entry {
                    name: &remote.name,
                    url: &remote.url,
                })
                .collect();
            if *json {
                println!("{}", serde_json::to_string_pretty(&listing)?);
            } else if listing.is_empty() {
                println!("no MCP server remotes; add one with `rho mcp remote add`");
            } else {
                for entry in &listing {
                    println!("{}  {}", entry.name, entry.url);
                }
            }
        }
        McpRemoteCommand::Remove { name } => {
            token_store::update(|remotes: &mut RemoteSet| {
                if !remotes.remove(name) {
                    anyhow::bail!("no MCP server remote named {name:?}");
                }
                Ok(())
            })?;
            println!("removed remote {name:?}");
        }
    }
    Ok(())
}

/// Lists one session on `remote`, which proves it answers and takes the
/// secret; nothing is saved unless it does.
async fn probe(remote: &Remote) -> anyhow::Result<()> {
    let arguments = Map::from_iter([("limit".to_owned(), Value::from(1))]);
    remotes::call(
        remote,
        Action::List,
        arguments,
        &CallerContext::ROOT,
        remotes::Asked::ByCall,
    )
    .await
    .map(drop)
    .map_err(|error| anyhow::anyhow!("{error}; remote {:?} was not saved", remote.name))
}

/// One remote as `list` shows it.
#[derive(Serialize)]
struct Entry<'a> {
    name: &'a str,
    url: &'a str,
}