Skip to content

Commit 76d202c

Browse files
committed
feat(transport): add constructors for non_exhaustive error types
AuthRequiredError, InsufficientScopeError, and DynamicTransportError were marked #[non_exhaustive] in modelcontextprotocol#715/modelcontextprotocol#768 but don't have constructors usable by external crates. Add new() for the error types and from_parts() for DynamicTransportError (the existing new() requires a Transport type parameter, making it unusable for test fixtures). Fixes modelcontextprotocol#805
1 parent a64be23 commit 76d202c

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

crates/rmcp/src/transport.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,24 @@ impl DynamicTransportError {
252252
error: Box::new(e),
253253
}
254254
}
255+
256+
/// Create a `DynamicTransportError` from raw parts.
257+
///
258+
/// Unlike [`new`](Self::new), this does not require a concrete [`Transport`] type,
259+
/// making it usable in test fixtures and other contexts where a real transport
260+
/// implementation is not available.
261+
pub fn from_parts(
262+
transport_name: impl Into<Cow<'static, str>>,
263+
transport_type_id: std::any::TypeId,
264+
error: Box<dyn std::error::Error + Send + Sync>,
265+
) -> Self {
266+
Self {
267+
transport_name: transport_name.into(),
268+
transport_type_id,
269+
error,
270+
}
271+
}
272+
255273
pub fn downcast<T: Transport<R> + 'static, R: ServiceRole>(self) -> Result<T::Error, Self> {
256274
if !self.is::<T, R>() {
257275
Err(self)

crates/rmcp/src/transport/streamable_http_client.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ pub struct AuthRequiredError {
2929
pub www_authenticate_header: String,
3030
}
3131

32+
impl AuthRequiredError {
33+
/// Create a new `AuthRequiredError` instance.
34+
pub fn new(www_authenticate_header: String) -> Self {
35+
Self {
36+
www_authenticate_header,
37+
}
38+
}
39+
}
40+
3241
#[derive(Debug)]
3342
#[non_exhaustive]
3443
pub struct InsufficientScopeError {
@@ -37,6 +46,14 @@ pub struct InsufficientScopeError {
3746
}
3847

3948
impl InsufficientScopeError {
49+
/// Create a new `InsufficientScopeError` instance.
50+
pub fn new(www_authenticate_header: String, required_scope: Option<String>) -> Self {
51+
Self {
52+
www_authenticate_header,
53+
required_scope,
54+
}
55+
}
56+
4057
/// check if scope upgrade is possible (i.e., we know what scope is required)
4158
pub fn can_upgrade(&self) -> bool {
4259
self.required_scope.is_some()

0 commit comments

Comments
 (0)