Repository navigation
Streamable HTTP client: a 401 or 403 with a JSON-RPC error body and no WWW-Authenticate loses its HTTP status #1339
Description
When a Streamable HTTP server answers a POST with 401 Unauthorized (or 403 Forbidden) without a WWW-Authenticate header and with a JSON-RPC error body, the reqwest client returns Ok(StreamableHttpPostResponse::Json(..)). The caller then gets an ordinary JSON-RPC error, ClientInitializeError::JsonRpcError during initialize or ServiceError::McpError after it, and cannot tell that the server rejected the credentials.
Where
The non-success branch parses the body with parse_json_rpc_error before it looks at the status:
rust-sdk/crates/rmcp/src/transport/common/reqwest/streamable_http_client.rs
Lines 276 to 306 in c1a8eea
| // Non-success responses may carry valid JSON-RPC error payloads that | |
| // should be surfaced as McpError rather than lost in TransportSend. | |
| if !status.is_success() { | |
| let body = response | |
| .text() | |
| .await | |
| .unwrap_or_else(|_| "<failed to read response body>".to_owned()); | |
| // Must precede the JSON-RPC branch below, which would forward a | |
| // discover rejection with an id the lifecycle cannot correlate. | |
| if let Some(response) = | |
| legacy_discover_response(&message, session_was_attached, status, &body) | |
| { | |
| return Ok(response); | |
| } | |
| if content_type | |
| .as_deref() | |
| .is_some_and(|ct| ct.as_bytes().starts_with(JSON_MIME_TYPE.as_bytes())) | |
| { | |
| match parse_json_rpc_error(&body) { | |
| Some(message) => { | |
| return Ok(StreamableHttpPostResponse::Json(message, session_id)); | |
| } | |
| None => tracing::warn!( | |
| "HTTP {status}: could not parse JSON body as a JSON-RPC error" | |
| ), | |
| } | |
| } | |
| return Err(StreamableHttpError::UnexpectedServerResponse(Cow::Owned( | |
| format!("HTTP {status}: {body}"), | |
| ))); | |
| } |
AuthRequired and InsufficientScope come only from the checks above it, which require WWW-Authenticate. The same 401 with a plain-text body becomes UnexpectedServerResponse("HTTP 401 Unauthorized: …"), so the status survives on that path but not on the JSON one.
Reproduce
rmcp 3.5.1 (main has the same code). The server answers initialize with:
HTTP/1.1 401 Unauthorized
Content-Type: application/json
{"jsonrpc":"2.0","id":0,"error":{"code":-32001,"message":"Unauthorized"}}serve_client over StreamableHttpClientTransport::with_client(reqwest::Client, config) fails with ClientInitializeError::JsonRpcError (code -32001, message "Unauthorized"). Nothing in the error says HTTP 401.
Why it matters
A client that sends a static token through custom_headers wants to tell the user that the token was rejected. An OAuth client wants to start or refresh authorization. Both need the status. The MCP authorization spec asks servers that implement authorization to send WWW-Authenticate with a 401, but a server that only takes a static token is not bound by that and may answer with a JSON-RPC error body instead.
Possible fixes
- Handle 401 and 403 before the JSON-RPC branch, as
legacy_discover_responsealready does ("Keep authentication failures and server errors on their original paths"). For example, returnAuthRequiredorInsufficientScopefor every 401 or 403, with the challenge optional, or returnUnexpectedServerResponse("HTTP 401 …")as the plain-text path does. - Or keep the JSON-RPC error and carry the HTTP status with it.
Activity