-
Notifications
You must be signed in to change notification settings - Fork 477
fix(rmcp): surface JSON-RPC error bodies on HTTP 4xx responses #748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kakarot-dev
wants to merge
2
commits into
modelcontextprotocol:main
Choose a base branch
from
kakarot-dev:fix/http-4xx-jsonrpc-error-body
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+28
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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)) | ||
|
|
@@ -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)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.