use anyhow::Result;
use pidge_client::ClientError;
use crate::cli::McpCommands;
use crate::commands::{mcp_connect, mcp_logout, mcp_status};
pub async fn run(command: McpCommands, json: bool) -> Result<()> {
match command {
McpCommands::Serve => crate::local::serve_stdio().await,
McpCommands::Connect { url, store, yes } => {
mcp_connect::run(url, store.into(), yes, json).await
}
McpCommands::Status { url, store } => {
mcp_status::run(url, store.map(Into::into), json).await
}
McpCommands::Logout { url, store } => {
mcp_logout::run(url, store.map(Into::into), json).await
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum McpUsageError {
#[error("no hosted server connected; run `pidge mcp connect <url>`")]
NoServerConnected,
#[error(
"connected to multiple hosted servers ({servers}); pass the server url explicitly, e.g. `{example}`"
)]
AmbiguousServer { servers: String, example: String },
#[error("not connected to {server}; run `pidge mcp connect {server}` to sign in")]
NotConnected { server: String },
}
pub fn remap_session_expired(url: &str, err: anyhow::Error) -> anyhow::Error {
let expired = err.chain().any(|cause| {
matches!(
cause.downcast_ref::<ClientError>(),
Some(ClientError::SessionExpired { .. })
)
});
if expired {
ClientError::McpSessionExpired {
server: url.to_string(),
}
.into()
} else {
err
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn remap_rewrites_session_expired_hint_to_mcp_connect() {
let err = anyhow::Error::from(ClientError::SessionExpired {
email: "https://mcp.example.com/mcp".into(),
});
let remapped = remap_session_expired("https://mcp.example.com", err);
assert!(
remapped
.to_string()
.contains("pidge mcp connect https://mcp.example.com"),
"{remapped}"
);
}
#[test]
fn remap_produces_a_client_error_so_it_still_classifies_as_auth_expired() {
let err = anyhow::Error::from(ClientError::SessionExpired {
email: "https://mcp.example.com/mcp".into(),
});
let remapped = remap_session_expired("https://mcp.example.com", err);
assert!(matches!(
remapped.downcast_ref::<ClientError>(),
Some(ClientError::McpSessionExpired { .. })
));
}
#[test]
fn remap_leaves_other_errors_untouched() {
let err = anyhow::anyhow!("some other failure");
let remapped = remap_session_expired("https://mcp.example.com", err);
assert_eq!(remapped.to_string(), "some other failure");
}
}