Skip to content
Closed
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions crates/rmcp/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl std::fmt::Display for ProtocolVersion {
}

impl ProtocolVersion {
pub const V_2026_07_28: Self = Self(Cow::Borrowed("2026-07-28"));
pub const V_2025_11_25: Self = Self(Cow::Borrowed("2025-11-25"));
pub const V_2025_06_18: Self = Self(Cow::Borrowed("2025-06-18"));
pub const V_2025_03_26: Self = Self(Cow::Borrowed("2025-03-26"));
Expand All @@ -164,6 +165,7 @@ impl ProtocolVersion {
Self::V_2025_03_26,
Self::V_2025_06_18,
Self::V_2025_11_25,
Self::V_2026_07_28,
];

/// Returns the string representation of this protocol version.
Expand Down Expand Up @@ -193,6 +195,7 @@ impl<'de> Deserialize<'de> for ProtocolVersion {
"2025-03-26" => return Ok(ProtocolVersion::V_2025_03_26),
"2025-06-18" => return Ok(ProtocolVersion::V_2025_06_18),
"2025-11-25" => return Ok(ProtocolVersion::V_2025_11_25),
"2026-07-28" => return Ok(ProtocolVersion::V_2026_07_28),
_ => {}
}
Ok(ProtocolVersion(Cow::Owned(s)))
Expand Down Expand Up @@ -544,6 +547,25 @@ impl ErrorData {
pub fn resource_not_found(message: impl Into<Cow<'static, str>>, data: Option<Value>) -> Self {
Self::new(ErrorCode::RESOURCE_NOT_FOUND, message, data)
}

/// Create a resource-not-found error using the code required by the negotiated protocol version.
///
/// SEP-2164 standardizes resource-not-found as JSON-RPC `INVALID_PARAMS` (`-32602`)
/// starting with protocol version `2026-07-28`. Older protocol versions continue to use
/// the legacy MCP-specific `RESOURCE_NOT_FOUND` code (`-32002`).
pub fn resource_not_found_for(
protocol_version: &ProtocolVersion,
message: impl Into<Cow<'static, str>>,
data: Option<Value>,
) -> Self {
let code = if protocol_version.as_str() >= ProtocolVersion::V_2026_07_28.as_str() {
ErrorCode::INVALID_PARAMS
} else {
ErrorCode::RESOURCE_NOT_FOUND
};
Self::new(code, message, data)
}

pub fn parse_error(message: impl Into<Cow<'static, str>>, data: Option<Value>) -> Self {
Self::new(ErrorCode::PARSE_ERROR, message, data)
}
Expand Down Expand Up @@ -4021,6 +4043,26 @@ mod tests {
assert_eq!(json_url, expected_url_json);
}

#[test]
fn resource_not_found_for_uses_legacy_code_for_older_protocol_versions() {
let error = ErrorData::resource_not_found_for(
&ProtocolVersion::V_2025_11_25,
"resource not found",
None,
);
assert_eq!(error.code, ErrorCode::RESOURCE_NOT_FOUND);
}

#[test]
fn resource_not_found_for_uses_invalid_params_for_sep_2164_protocol_versions() {
let error = ErrorData::resource_not_found_for(
&ProtocolVersion::V_2026_07_28,
"resource not found",
None,
);
assert_eq!(error.code, ErrorCode::INVALID_PARAMS);
}

#[test]
fn notification_without_params_should_deserialize_as_bare_jsonrpc_message() {
let payload = b"{\"method\":\"notifications/initialized\",\"jsonrpc\":\"2.0\"}";
Expand Down
4 changes: 3 additions & 1 deletion crates/rmcp/tests/test_custom_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,16 +866,18 @@ async fn test_server_rejects_unsupported_protocol_version() {
fn test_protocol_version_utilities() {
use rmcp::model::ProtocolVersion;

assert_eq!(ProtocolVersion::V_2026_07_28.as_str(), "2026-07-28");
assert_eq!(ProtocolVersion::V_2025_11_25.as_str(), "2025-11-25");
assert_eq!(ProtocolVersion::V_2025_06_18.as_str(), "2025-06-18");
assert_eq!(ProtocolVersion::V_2025_03_26.as_str(), "2025-03-26");
assert_eq!(ProtocolVersion::V_2024_11_05.as_str(), "2024-11-05");

assert_eq!(ProtocolVersion::KNOWN_VERSIONS.len(), 4);
assert_eq!(ProtocolVersion::KNOWN_VERSIONS.len(), 5);
assert!(ProtocolVersion::KNOWN_VERSIONS.contains(&ProtocolVersion::V_2024_11_05));
assert!(ProtocolVersion::KNOWN_VERSIONS.contains(&ProtocolVersion::V_2025_03_26));
assert!(ProtocolVersion::KNOWN_VERSIONS.contains(&ProtocolVersion::V_2025_06_18));
assert!(ProtocolVersion::KNOWN_VERSIONS.contains(&ProtocolVersion::V_2025_11_25));
assert!(ProtocolVersion::KNOWN_VERSIONS.contains(&ProtocolVersion::V_2026_07_28));
}

/// Integration test: Verify server validates only the Host header for DNS rebinding protection
Expand Down