rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! `rho mcp secret`: stores and deletes the secrets MCP servers read through
//! `env_from_secret` and `headers_from_secret` (#2103), as a client of the
//! rho daemon, which keeps them in its own credential store
//! (`rho/mcp/secret/*`), starting it when none runs.

use rho_protocol::{
    methods::{McpSecretDelete, McpSecretSet},
    ClientError, ErrorCode, McpSecretDeleteParams, McpSecretSetParams,
};

use crate::{
    app::{daemon_cli, secret_input},
    cli::McpSecretCommand,
    tools::mcp::{validate_identity, validate_secret_name},
};

pub(in crate::app) async fn secret(command: &McpSecretCommand) -> anyhow::Result<()> {
    match command {
        McpSecretCommand::Set { id, name } => {
            // Refused before asking for a value that could not be stored.
            validate_identity(id)?;
            validate_secret_name(name)?;
            let value = secret_input::read_or_stdin(&format!("{name} for {id}")).await?;
            let connection = daemon_cli::connect().await?;
            connection
                .client
                .request::<McpSecretSet>(&McpSecretSetParams::new(id, name, value))
                .await
                .map_err(store_refused)?;
            eprintln!("stored {name} for {id}; its servers use it when they next connect");
        }
        McpSecretCommand::Delete { id, name } => {
            let connection = daemon_cli::connect().await?;
            let deleted = connection
                .client
                .request::<McpSecretDelete>(&McpSecretDeleteParams::new(id, name))
                .await
                .map_err(store_refused)?
                .deleted;
            if deleted {
                eprintln!("deleted {name} for {id}");
            } else {
                eprintln!("no {name} was stored for {id}");
            }
        }
    }
    Ok(())
}

/// The daemon's refusal; one that cannot reach its credential store, as a
/// daemon started from an SSH login cannot reach the OS keyring, says how to
/// choose the file store. The daemon reads that choice from its own config,
/// unless the environment it was started with sets `RHO_CREDENTIAL_STORE`.
fn store_refused(error: ClientError) -> anyhow::Error {
    if error.error_code() == Some(ErrorCode::CredentialStoreUnavailable) {
        anyhow::anyhow!(
            "{error}\nto keep secrets in a private file instead, run `rho credential-store set file`, then try again; if the daemon's own environment sets RHO_CREDENTIAL_STORE, as a service unit can, change it there and restart the daemon"
        )
    } else {
        error.into()
    }
}