Repository navigation
fix(transport): match explicit HTTP media types and Accept quality values #1382
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
passionworkeer
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
passionworkeer:fix/http-media-type-matching
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.
+204
−15
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
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 | ||
|---|---|---|---|---|
|
|
@@ -45,11 +45,21 @@ async fn spawn_server() -> (reqwest::Client, String, CancellationToken) { | |||
| async fn spawn_server_with<S>( | ||||
| service_factory: impl Fn() -> Result<S, std::io::Error> + Send + Sync + 'static, | ||||
| ) -> (reqwest::Client, String, CancellationToken) | ||||
| where | ||||
| S: ServerHandler + Send + 'static, | ||||
| { | ||||
| spawn_server_with_mode(service_factory, false).await | ||||
| } | ||||
|
|
||||
| async fn spawn_server_with_mode<S>( | ||||
| service_factory: impl Fn() -> Result<S, std::io::Error> + Send + Sync + 'static, | ||||
| legacy_session_mode: bool, | ||||
| ) -> (reqwest::Client, String, CancellationToken) | ||||
| where | ||||
| S: ServerHandler + Send + 'static, | ||||
| { | ||||
| let config = StreamableHttpServerConfig::default() | ||||
| .with_legacy_session_mode(false) | ||||
| .with_legacy_session_mode(legacy_session_mode) | ||||
| .with_json_response(true) | ||||
| .with_sse_keep_alive(None) | ||||
| .with_cancellation_token(CancellationToken::new()); | ||||
|
|
@@ -495,3 +505,134 @@ async fn accepts_initialize_with_matching_mcp_method() -> anyhow::Result<()> { | |||
| ct.cancel(); | ||||
| Ok(()) | ||||
| } | ||||
|
|
||||
| #[tokio::test] | ||||
| async fn media_type_headers_follow_http_semantics() { | ||||
| let (client, url, ct) = spawn_server().await; | ||||
| let cases: &[(&str, &[&str], &str, u16)] = &[ | ||||
| ( | ||||
| "ordinary", | ||||
| &["application/json, text/event-stream"], | ||||
| "application/json", | ||||
| 202, | ||||
| ), | ||||
| ( | ||||
| "content type case", | ||||
| &["application/json, text/event-stream"], | ||||
| "Application/JSON", | ||||
| 202, | ||||
| ), | ||||
| ( | ||||
| "accept case", | ||||
| &["APPLICATION/JSON, TEXT/EVENT-STREAM"], | ||||
| "application/json", | ||||
| 202, | ||||
| ), | ||||
| ( | ||||
| "split accept", | ||||
| &["application/json", "text/event-stream"], | ||||
| "application/json", | ||||
| 202, | ||||
| ), | ||||
| ( | ||||
| "content type suffix", | ||||
| &["application/json, text/event-stream"], | ||||
| "application/jsonx", | ||||
| 415, | ||||
| ), | ||||
| ( | ||||
| "accept suffix", | ||||
| &["application/json-seq, text/event-stream"], | ||||
| "application/json", | ||||
| 406, | ||||
| ), | ||||
| ( | ||||
| "zero quality", | ||||
| &["application/json;q=0, text/event-stream"], | ||||
| "application/json", | ||||
| 406, | ||||
| ), | ||||
| ( | ||||
| "zero quality decimal", | ||||
| &["application/json;Q=0.000, text/event-stream"], | ||||
| "application/json", | ||||
| 406, | ||||
| ), | ||||
| ( | ||||
| "positive quality", | ||||
| &["application/json;q=0.001, text/event-stream"], | ||||
| "application/json", | ||||
| 202, | ||||
| ), | ||||
| ( | ||||
| "parameters", | ||||
| &["application/json;profile=\"a,b;c\", text/event-stream"], | ||||
| "application/json; charset=utf-8", | ||||
| 202, | ||||
| ), | ||||
| ( | ||||
| "parameter is not media", | ||||
| &["text/plain;profile=\"application/json, text/event-stream\""], | ||||
| "application/json", | ||||
| 406, | ||||
| ), | ||||
| ( | ||||
| "escaped quoted parameter", | ||||
| &[r#"application/json;profile="a\",b;c", text/event-stream"#], | ||||
| "application/json", | ||||
| 202, | ||||
| ), | ||||
| ( | ||||
| "quoted quality is not a weight", | ||||
| &[r#"application/json;profile="a;q=0", text/event-stream"#], | ||||
| "application/json", | ||||
| 202, | ||||
| ), | ||||
| ( | ||||
| "escaped parameter is not media", | ||||
| &[r#"text/plain;profile="a\",application/json, text/event-stream""#], | ||||
| "application/json", | ||||
| 406, | ||||
| ), | ||||
| ("wildcard alone", &["*/*"], "application/json", 406), | ||||
| ]; | ||||
| let mut failures = Vec::new(); | ||||
| for (name, accept, content_type, expected) in cases { | ||||
| let mut request = client | ||||
| .post(&url) | ||||
| .header("Content-Type", *content_type) | ||||
| .body(r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#); | ||||
| for value in *accept { | ||||
| request = request.header("Accept", *value); | ||||
| } | ||||
| let actual = request.send().await.unwrap().status().as_u16(); | ||||
| eprintln!("POST {name}: expected {expected}, got {actual}"); | ||||
|
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. The collected
Suggested change
|
||||
| if actual != *expected { | ||||
| failures.push(format!("{name}: expected {expected}, got {actual}")); | ||||
| } | ||||
| } | ||||
| ct.cancel(); | ||||
| // Legacy mode dispatches GET through media validation before requiring a session ID. | ||||
| let (client, url, ct) = spawn_server_with_mode(|| Ok(HeaderValidationServer), true).await; | ||||
| for (accept, expected) in [ | ||||
| ("text/event-stream", 400), | ||||
| ("TEXT/EVENT-STREAM", 400), | ||||
| ("text/event-streamx", 406), | ||||
| ("text/event-stream;q=0.0", 406), | ||||
| ] { | ||||
| let actual = client | ||||
| .get(&url) | ||||
| .header("Accept", accept) | ||||
| .send() | ||||
| .await | ||||
| .unwrap() | ||||
| .status() | ||||
| .as_u16(); | ||||
| eprintln!("GET {accept}: expected {expected}, got {actual}"); | ||||
|
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.
Suggested change
|
||||
| if actual != expected { | ||||
| failures.push(format!("GET {accept}: expected {expected}, got {actual}")); | ||||
| } | ||||
| } | ||||
| ct.cancel(); | ||||
| assert!(failures.is_empty(), "{}", failures.join("\n")); | ||||
| } | ||||
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 table covers the zero and positive boundary, but not the upper bound or malformed weights. So the
"1"case and rejection paths inquality_is_acceptableare still untested. Could we add tests for those too, based on whatever behavior we settle on for malformed weights?