Skip to content
Open
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
54 changes: 54 additions & 0 deletions crates/rmcp/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,10 @@ pub struct ErrorData {
impl ErrorData {
#[cfg(any(feature = "client", feature = "server"))]
const TRANSPORT_CLOSED_MARKER: &str = "io.modelcontextprotocol/transportClosed";
#[cfg(any(feature = "client", feature = "server"))]
const OVERSIZED_MESSAGE_MARKER: &str = "io.modelcontextprotocol/oversizedMessage";
#[cfg(any(feature = "client", feature = "server"))]
const OVERSIZED_MESSAGE_LIMIT_KEY: &str = "maxLineLength";

pub fn new(
code: ErrorCode,
Expand Down Expand Up @@ -762,6 +766,56 @@ impl ErrorData {
hasher.finish()
})
}

#[cfg(any(feature = "client", feature = "server"))]
pub(crate) fn oversized_message(max_line_length: usize) -> Self {
let mut data = JsonObject::new();
data.insert(
Self::OVERSIZED_MESSAGE_MARKER.to_owned(),
Value::from(Self::oversized_message_token()),
);
data.insert(
Self::OVERSIZED_MESSAGE_LIMIT_KEY.to_owned(),
Value::from(max_line_length as u64),
);
Self::internal_error(
format!(
"Incoming response exceeded the maximum line length ({max_line_length} bytes); \
raise it with AsyncRwTransport::with_max_line_length"
),
Some(Value::Object(data)),
)
}

#[cfg(any(feature = "client", feature = "server"))]
pub(crate) fn oversized_message_limit(&self) -> Option<usize> {
let data = self.data.as_ref()?;
let token = data
.get(Self::OVERSIZED_MESSAGE_MARKER)
.and_then(Value::as_u64)?;
if token != Self::oversized_message_token() {
return None;
}
data.get(Self::OVERSIZED_MESSAGE_LIMIT_KEY)
.and_then(Value::as_u64)
.and_then(|limit| usize::try_from(limit).ok())
}

#[cfg(any(feature = "client", feature = "server"))]
fn oversized_message_token() -> u64 {
use std::{
collections::hash_map::RandomState,
hash::{BuildHasher, Hasher},
sync::OnceLock,
};

static TOKEN: OnceLock<u64> = OnceLock::new();
*TOKEN.get_or_init(|| {
let mut hasher = RandomState::new().build_hasher();
hasher.write(b"rmcp oversized-message marker");
hasher.finish()
})
}
}

/// Represents any JSON-RPC message that can be sent or received.
Expand Down
8 changes: 8 additions & 0 deletions crates/rmcp/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ pub enum ServiceError {
/// A `list_all_*` walk kept returning a cursor beyond the configured page cap.
#[error("listing did not finish within {max_pages} pages")]
ListPaginationExceeded { max_pages: usize },
/// An incoming response exceeded the transport's configured line length limit.
#[error(
"Incoming response exceeded the maximum line length ({max_line_length} bytes); \
raise it with AsyncRwTransport::with_max_line_length"
)]
ResponseTooLarge { max_line_length: usize },
}

impl ServiceError {
Expand Down Expand Up @@ -1850,6 +1856,8 @@ where
{
let service_error = if error.is_transport_closed() {
ServiceError::TransportClosed
} else if let Some(max_line_length) = error.oversized_message_limit() {
ServiceError::ResponseTooLarge { max_line_length }
} else {
ServiceError::McpError(error)
};
Expand Down
23 changes: 20 additions & 3 deletions crates/rmcp/src/service/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ pub enum ClientInitializeError {
discover: Box<ClientInitializeError>,
fallback: Box<ClientInitializeError>,
},

#[error(
"Incoming response exceeded the maximum line length ({max_line_length} bytes); \
raise it with AsyncRwTransport::with_max_line_length"
)]
ResponseTooLarge { max_line_length: usize },
}

impl ClientInitializeError {
Expand Down Expand Up @@ -249,12 +255,22 @@ where
}
return Err(match &error.id {
Some(id) if expected_id.matches_response_id(id) => {
ClientInitializeError::JsonRpcError(error.error)
if let Some(max_line_length) = error.error.oversized_message_limit() {
ClientInitializeError::ResponseTooLarge { max_line_length }
} else {
ClientInitializeError::JsonRpcError(error.error)
}
}
// Spec: error id is optional; a server that cannot read
// the request id omits it. The error is still a response
// to our request, so it remains available to the caller.
None => ClientInitializeError::JsonRpcError(error.error),
None => {
if let Some(max_line_length) = error.error.oversized_message_limit() {
ClientInitializeError::ResponseTooLarge { max_line_length }
} else {
ClientInitializeError::JsonRpcError(error.error)
}
}
Some(id) => ClientInitializeError::UncorrelatedErrorResponse {
expected: expected_id.clone(),
received: id.clone(),
Expand Down Expand Up @@ -1225,7 +1241,8 @@ fn is_stale_fallback_eligible(error: &ServiceError) -> bool {
| ServiceError::Cancelled { .. }
| ServiceError::SubscriptionLagged { .. }
| ServiceError::InputRequiredRoundsExceeded { .. }
| ServiceError::ListPaginationExceeded { .. } => false,
| ServiceError::ListPaginationExceeded { .. }
| ServiceError::ResponseTooLarge { .. } => false,
}
}

Expand Down
Loading