Merge pull request 'mcp: show error cause chain for HTTP failures' (#126) from mcp-http-error-chain into main
This commit was merged in pull request #126.
This commit is contained in:
commit
6f951ad7f8
2 files changed
+22
-10
No files matched your search
@@ -12,6 +12,17 @@ use std::time::Duration;
|
|||||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||||
use tokio::sync::{Mutex, oneshot};
|
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";
|
pub const PROTOCOL: &str = "2025-06-18";
|
||||||
|
|
||||||
type Pending = Arc<Mutex<HashMap<i64, oneshot::Sender<Result<Value, String>>>>>;
|
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() {
|
if let Some(s) = session.lock().await.as_ref() {
|
||||||
rq = rq.header("mcp-session-id", s);
|
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
|
if let Some(s) = r
|
||||||
.headers()
|
.headers()
|
||||||
.get("mcp-session-id")
|
.get("mcp-session-id")
|
||||||
@@ -229,7 +240,7 @@ impl Client {
|
|||||||
.and_then(|h| h.to_str().ok())
|
.and_then(|h| h.to_str().ok())
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.to_string();
|
.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() {
|
if !status.is_success() {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"HTTP {status}: {}",
|
"HTTP {status}: {}",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
//! ponytail: public clients only via DCR or a configured `clientId`; no client_credentials/device flow, no
|
//! 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.
|
//! 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 crate::config::Server;
|
||||||
use hx_ai::auth::{await_redirect, now_ms, parse_code, pkce, urlenc};
|
use hx_ai::auth::{await_redirect, now_ms, parse_code, pkce, urlenc};
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
@@ -131,9 +132,9 @@ async fn token_req(
|
|||||||
.form(form)
|
.form(form)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| chain(&e))?;
|
||||||
let st = r.status();
|
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() {
|
if !st.is_success() {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"token endpoint HTTP {st}: {}",
|
"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()
|
let h = reqwest::Client::builder()
|
||||||
.timeout(std::time::Duration::from_secs(30))
|
.timeout(std::time::Duration::from_secs(30))
|
||||||
.build()
|
.build()
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| chain(&e))?;
|
||||||
// 1. unauthenticated probe -> 401 + resource metadata pointer
|
// 1. unauthenticated probe -> 401 + resource metadata pointer
|
||||||
let probe = h
|
let probe = h
|
||||||
.post(url)
|
.post(url)
|
||||||
@@ -171,7 +172,7 @@ pub async fn login(s: &Server, announce: impl Fn(&str)) -> Result<(), String> {
|
|||||||
)
|
)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| chain(&e))?;
|
||||||
if probe.status().as_u16() != 401 {
|
if probe.status().as_u16() != 401 {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"server did not ask for auth (HTTP {})",
|
"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)
|
let origin = Url::parse(url)
|
||||||
.map_err(|e| e.to_string())?
|
.map_err(|e| chain(&e))?
|
||||||
.origin()
|
.origin()
|
||||||
.ascii_serialization();
|
.ascii_serialization();
|
||||||
let hdr = probe
|
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}"))?;
|
.map_err(|e| format!("bind callback port: {e}"))?;
|
||||||
let redirect = format!(
|
let redirect = format!(
|
||||||
"http://127.0.0.1:{}/callback",
|
"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 {
|
let (client_id, client_secret) = match &s.oauth.client_id {
|
||||||
Some(id) => (id.clone(), s.oauth.client_secret.clone()),
|
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"}))
|
"response_types": ["code"], "token_endpoint_auth_method": "none"}))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| chain(&e))?;
|
||||||
let st = r.status();
|
let st = r.status();
|
||||||
let b: Value = r
|
let b: Value = r
|
||||||
.json()
|
.json()
|
||||||
@@ -271,7 +272,7 @@ pub async fn login(s: &Server, announce: impl Fn(&str)) -> Result<(), String> {
|
|||||||
let (code, got) = parse_code(
|
let (code, got) = parse_code(
|
||||||
&await_redirect(Some(listener), "/callback")
|
&await_redirect(Some(listener), "/callback")
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?,
|
.map_err(|e| chain(&e))?,
|
||||||
);
|
);
|
||||||
if got.as_deref().is_some_and(|g| g != state) {
|
if got.as_deref().is_some_and(|g| g != state) {
|
||||||
return Err("OAuth state mismatch".into());
|
return Err("OAuth state mismatch".into());
|
||||||
|
|||||||
Reference in new issue
Block a user