Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,42 @@ impl StreamableHttpClient for reqwest::Client {
if status == reqwest::StatusCode::NOT_FOUND && session_was_attached {
return Err(StreamableHttpError::SessionExpired);
}
let content_type = response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.map(|ct| String::from_utf8_lossy(ct.as_bytes()).to_string());
let session_id = response
.headers()
.get(HEADER_SESSION_ID)
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
// For non-success responses, attempt to parse JSON-RPC error bodies
// before falling back to a transport error. HTTP 4xx responses with
// Content-Type: application/json may carry valid JSON-RPC error
// payloads that should be surfaced as McpError, not TransportSend.
Comment on lines +202 to +205
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says "HTTP 4xx responses" but the guard is !status.is_success(), which also covers 1xx, 3xx, and 5xx. This could be clearer and simpler.

Suggested change
// For non-success responses, attempt to parse JSON-RPC error bodies
// before falling back to a transport error. HTTP 4xx responses with
// Content-Type: application/json may carry valid JSON-RPC error
// payloads that should be surfaced as McpError, not TransportSend.
// 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());
if content_type
.as_deref()
.is_some_and(|ct| ct.as_bytes().starts_with(JSON_MIME_TYPE.as_bytes()))
{
match serde_json::from_str::<ServerJsonRpcMessage>(&body) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ServerJsonRpcMessage can parse any valid JSON-RPC message including Request, Response, Notification, not just Error. The deserialization should verify the parsed message is actually a JsonRpcMessage::Error.

Ok(message) => {
return Ok(StreamableHttpPostResponse::Json(message, session_id));
}
Err(e) => tracing::warn!(
"HTTP {status}: could not parse JSON response as ServerJsonRpcMessage: {e}"
),
}
}
return Err(StreamableHttpError::UnexpectedServerResponse(Cow::Owned(
format!("HTTP {status}: {body}"),
)));
}
let content_type = response.headers().get(reqwest::header::CONTENT_TYPE);
let session_id = response.headers().get(HEADER_SESSION_ID);
let session_id = session_id
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
match content_type {
match content_type.as_deref() {
Some(ct) if ct.as_bytes().starts_with(EVENT_STREAM_MIME_TYPE.as_bytes()) => {
let event_stream = SseStream::from_byte_stream(response.bytes_stream()).boxed();
Ok(StreamableHttpPostResponse::Sse(event_stream, session_id))
Expand All @@ -226,9 +247,7 @@ impl StreamableHttpClient for reqwest::Client {
_ => {
// unexpected content type
tracing::error!("unexpected content type: {:?}", content_type);
Err(StreamableHttpError::UnexpectedContentType(
content_type.map(|ct| String::from_utf8_lossy(ct.as_bytes()).to_string()),
))
Err(StreamableHttpError::UnexpectedContentType(content_type))
}
}
}
Expand Down
Loading