use std::path::{Path, PathBuf};
use rho_protocol::{
ClientEnvironment, ErrorCode, HooksListParams, HooksListResponse, McpListParams,
McpListResponse, RpcError,
};
use super::{
sessions::{session_environment, Workspace},
settings::ConfigStore,
};
use crate::{app::configured_mcp::ConfiguredMcp, workspace::ProjectTrusts};
pub(super) async fn mcp(
params: McpListParams,
settings: &ConfigStore,
) -> Result<McpListResponse, RpcError> {
let (workspace, trust) = parse(params.workspace.as_deref(), ¶ms.environment)?;
let config = settings
.config_to_open()
.await
.map_err(|error| RpcError::new(ErrorCode::InternalError, error.to_string()))?;
let cwd = workspace.or_else(crate::paths::home_dir);
let servers = ConfiguredMcp::discover(&config.mcp, cwd.as_deref(), trust).listing();
let config_path = crate::paths::without_verbatim_prefix(settings.agent_path().to_path_buf());
Ok(McpListResponse::new(config_path, servers))
}
pub(super) fn hooks(params: HooksListParams) -> Result<HooksListResponse, RpcError> {
let (workspace, trust) = parse(params.workspace.as_deref(), ¶ms.environment)?;
Ok(HooksListResponse::new(crate::hooks::configured_for_cwd(
workspace.as_deref(),
trust,
)))
}
pub(super) fn parse(
workspace: Option<&Path>,
environment: &ClientEnvironment,
) -> Result<(Option<PathBuf>, ProjectTrusts), RpcError> {
let workspace = workspace
.map(|path| {
Workspace::parse("workspace", path).map(|workspace| workspace.path().to_path_buf())
})
.transpose()?;
let ambient = session_environment(environment)?;
Ok((workspace, ambient.project_trusts()))
}