Skip to content

Commit

Permalink
simplify error msgs im mcp-client
Browse files Browse the repository at this point in the history
  • Loading branch information
salman1993 committed Jan 14, 2025
1 parent 2160189 commit 8dd2a93
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 41 deletions.
15 changes: 10 additions & 5 deletions crates/mcp-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use thiserror::Error;
use tokio::sync::Mutex;
use tower::{Service, ServiceExt}; // for Service::ready()

pub type BoxError = Box<dyn std::error::Error + Sync + Send>;

/// Error type for MCP client operations.
#[derive(Debug, Error)]
pub enum Error {
Expand All @@ -31,13 +33,16 @@ pub enum Error {
#[error("Timeout or service not ready")]
NotReady,

#[error("Box error: {0}")]
BoxError(Box<dyn std::error::Error + Send + Sync>),
#[error("Request timed out")]
Timeout(#[from] tower::timeout::error::Elapsed),

#[error("MCP Server error: {0}")]
McpServerError(BoxError),
}

impl From<Box<dyn std::error::Error + Send + Sync>> for Error {
fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
Error::BoxError(err)
impl From<BoxError> for Error {
fn from(err: BoxError) -> Self {
Error::McpServerError(err)
}
}

Expand Down
7 changes: 0 additions & 7 deletions crates/mcp-client/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,3 @@ where
.service(McpService::new(transport))
}
}

// Implement From<tower::timeout::error::Elapsed> for our Error type
impl From<tower::timeout::error::Elapsed> for Error {
fn from(_: tower::timeout::error::Elapsed) -> Self {
Error::Timeout
}
}
35 changes: 9 additions & 26 deletions crates/mcp-client/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashMap;
use thiserror::Error;
use tokio::sync::{mpsc, oneshot, RwLock};

pub type BoxError = Box<dyn std::error::Error + Sync + Send>;
/// A generic error type for transport operations.
#[derive(Debug, Error)]
pub enum Error {
Expand All @@ -13,41 +14,23 @@ pub enum Error {
#[error("Transport was not connected or is already closed")]
NotConnected,

#[error("Invalid URL provided")]
InvalidUrl,

#[error("Connection timeout")]
Timeout,

#[error("Failed to send message")]
SendFailed,

#[error("Channel closed")]
ChannelClosed,

#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),

#[error("HTTP error: {status} - {message}")]
HttpError { status: u16, message: String },
#[error("Unsupported message type. JsonRpcMessage can only be Request or Notification.")]
UnsupportedMessage,

#[error("Stdio process error: {0}")]
StdioProcessError(String),

#[error("SSE connection error: {0}")]
SseConnection(String),

#[error("Connection closed by server")]
ConnectionClosed,

#[error("Unexpected transport error: {0}")]
Other(String),

#[error("Box error: {0}")]
BoxError(Box<dyn std::error::Error + Send + Sync>),
}

impl From<Box<dyn std::error::Error + Send + Sync>> for Error {
fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
Error::BoxError(err)
}
#[error("HTTP error: {status} - {message}")]
HttpError { status: u16, message: String },
}

/// A message that can be sent through the transport
Expand Down Expand Up @@ -100,7 +83,7 @@ pub async fn send_message(
sender.send(msg).await.map_err(|_| Error::ChannelClosed)?;
Ok(JsonRpcMessage::Nil)
}
_ => Err(Error::Other("Unsupported message type".to_string())),
_ => Err(Error::UnsupportedMessage),
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/mcp-client/src/transport/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,17 @@ impl StdioTransport {
// 0 sets the process group ID equal to the process ID
.process_group(0) // don't inherit signal handling from parent process
.spawn()
.map_err(|e| Error::Other(e.to_string()))?;
.map_err(|e| Error::StdioProcessError(e.to_string()))?;

let stdin = process
.stdin
.take()
.ok_or_else(|| Error::Other("Failed to get stdin".into()))?;
.ok_or_else(|| Error::StdioProcessError("Failed to get stdin".into()))?;

let stdout = process
.stdout
.take()
.ok_or_else(|| Error::Other("Failed to get stdout".into()))?;
.ok_or_else(|| Error::StdioProcessError("Failed to get stdout".into()))?;

Ok((process, stdin, stdout))
}
Expand Down

0 comments on commit 8dd2a93

Please sign in to comment.