From 083403a54cd717d99c9a8499b0e37649f30a3573 Mon Sep 17 00:00:00 2001 From: hermes Date: Tue, 6 Oct 2026 11:02:11 +0000 Subject: [PATCH] mcp: show full error cause chain for HTTP/OAuth request failures --- crates/hx-mcp/src/client.rs | 15 +++++++++++++-- crates/hx-mcp/src/oauth.rs | 17 +++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/hx-mcp/src/client.rs b/crates/hx-mcp/src/client.rs index 15f31cd..2026039 100644 --- a/crates/hx-mcp/src/client.rs +++ b/crates/hx-mcp/src/client.rs @@ -12,6 +12,17 @@ use std::time::Duration; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::sync::{Mutex, oneshot}; +/// `Display` of an error plus its `source()` chain (reqwest hides the cause behind "error sending request"). +pub fn chain(e: &dyn std::error::Error) -> String { + let mut s = e.to_string(); + let mut c = e.source(); + while let Some(x) = c { + s.push_str(&format!(": {x}")); + c = x.source(); + } + s +} + pub const PROTOCOL: &str = "2025-06-18"; type Pending = Arc>>>>; @@ -214,7 +225,7 @@ impl Client { if let Some(s) = session.lock().await.as_ref() { rq = rq.header("mcp-session-id", s); } - let r = rq.send().await.map_err(|e| e.to_string())?; + let r = rq.send().await.map_err(|e| chain(&e))?; if let Some(s) = r .headers() .get("mcp-session-id") @@ -229,7 +240,7 @@ impl Client { .and_then(|h| h.to_str().ok()) .unwrap_or_default() .to_string(); - let body = r.text().await.map_err(|e| e.to_string())?; + let body = r.text().await.map_err(|e| chain(&e))?; if !status.is_success() { return Err(format!( "HTTP {status}: {}", diff --git a/crates/hx-mcp/src/oauth.rs b/crates/hx-mcp/src/oauth.rs index 55aac14..f6bbc82 100644 --- a/crates/hx-mcp/src/oauth.rs +++ b/crates/hx-mcp/src/oauth.rs @@ -5,6 +5,7 @@ //! ponytail: public clients only via DCR or a configured `clientId`; no client_credentials/device flow, no //! token revocation on logout (just forget). Browser is never opened automatically: `hx mcp login` prints the URL. +use crate::client::chain; use crate::config::Server; use hx_ai::auth::{await_redirect, now_ms, parse_code, pkce, urlenc}; use reqwest::Url; @@ -131,9 +132,9 @@ async fn token_req( .form(form) .send() .await - .map_err(|e| e.to_string())?; + .map_err(|e| chain(&e))?; let st = r.status(); - let body = r.text().await.map_err(|e| e.to_string())?; + let body = r.text().await.map_err(|e| chain(&e))?; if !st.is_success() { return Err(format!( "token endpoint HTTP {st}: {}", @@ -159,7 +160,7 @@ pub async fn login(s: &Server, announce: impl Fn(&str)) -> Result<(), String> { let h = reqwest::Client::builder() .timeout(std::time::Duration::from_secs(30)) .build() - .map_err(|e| e.to_string())?; + .map_err(|e| chain(&e))?; // 1. unauthenticated probe -> 401 + resource metadata pointer let probe = h .post(url) @@ -171,7 +172,7 @@ pub async fn login(s: &Server, announce: impl Fn(&str)) -> Result<(), String> { ) .send() .await - .map_err(|e| e.to_string())?; + .map_err(|e| chain(&e))?; if probe.status().as_u16() != 401 { return Err(format!( "server did not ask for auth (HTTP {})", @@ -179,7 +180,7 @@ pub async fn login(s: &Server, announce: impl Fn(&str)) -> Result<(), String> { )); } let origin = Url::parse(url) - .map_err(|e| e.to_string())? + .map_err(|e| chain(&e))? .origin() .ascii_serialization(); let hdr = probe @@ -216,7 +217,7 @@ pub async fn login(s: &Server, announce: impl Fn(&str)) -> Result<(), String> { .map_err(|e| format!("bind callback port: {e}"))?; let redirect = format!( "http://127.0.0.1:{}/callback", - listener.local_addr().map_err(|e| e.to_string())?.port() + listener.local_addr().map_err(|e| chain(&e))?.port() ); let (client_id, client_secret) = match &s.oauth.client_id { Some(id) => (id.clone(), s.oauth.client_secret.clone()), @@ -232,7 +233,7 @@ pub async fn login(s: &Server, announce: impl Fn(&str)) -> Result<(), String> { "response_types": ["code"], "token_endpoint_auth_method": "none"})) .send() .await - .map_err(|e| e.to_string())?; + .map_err(|e| chain(&e))?; let st = r.status(); let b: Value = r .json() @@ -271,7 +272,7 @@ pub async fn login(s: &Server, announce: impl Fn(&str)) -> Result<(), String> { let (code, got) = parse_code( &await_redirect(Some(listener), "/callback") .await - .map_err(|e| e.to_string())?, + .map_err(|e| chain(&e))?, ); if got.as_deref().is_some_and(|g| g != state) { return Err("OAuth state mismatch".into());