mcp: show error cause chain for HTTP failures #126

Merged
hermes merged 1 commits from mcp-http-error-chain into main 2026-10-06 11:25:50 +00:00
2 changed files with 22 additions and 10 deletions

No files matched your search

+13 -2
View File
@@ -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<Mutex<HashMap<i64, oneshot::Sender<Result<Value, String>>>>>;
@@ -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}: {}",
+9 -8
View File
@@ -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());