Skip to content

Commit 91be09f

Browse files
committed
refactor: updated the handler traits and fixed error handling for mcp
error messages
1 parent 287b713 commit 91be09f

File tree

15 files changed

+50
-20
lines changed

15 files changed

+50
-20
lines changed

crates/rust-mcp-sdk/src/mcp_handlers/mcp_client_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub trait ClientHandler: Send + Sync + 'static {
148148
//********************//
149149
async fn handle_error(
150150
&self,
151-
error: RpcError,
151+
error: &RpcError,
152152
runtime: &dyn McpClient,
153153
) -> std::result::Result<(), RpcError> {
154154
Ok(())

crates/rust-mcp-sdk/src/mcp_handlers/mcp_client_handler_core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub trait ClientHandlerCore: Send + Sync + 'static {
3838
/// - `error` – The error data received from the MCP server.
3939
async fn handle_error(
4040
&self,
41-
error: RpcError,
41+
error: &RpcError,
4242
runtime: &dyn McpClient,
4343
) -> std::result::Result<(), RpcError>;
4444

crates/rust-mcp-sdk/src/mcp_handlers/mcp_server_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ pub trait ServerHandler: Send + Sync + 'static {
319319
/// Customize this function in your specific handler to implement behavior tailored to your MCP server's capabilities and requirements.
320320
async fn handle_error(
321321
&self,
322-
error: RpcError,
322+
error: &RpcError,
323323
runtime: &dyn McpServer,
324324
) -> std::result::Result<(), RpcError> {
325325
Ok(())

crates/rust-mcp-sdk/src/mcp_handlers/mcp_server_handler_core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub trait ServerHandlerCore: Send + Sync + 'static {
4545
/// - `error` – The error data received from the MCP client.
4646
async fn handle_error(
4747
&self,
48-
error: RpcError,
48+
error: &RpcError,
4949
runtime: &dyn McpServer,
5050
) -> std::result::Result<(), RpcError>;
5151
async fn on_server_started(&self, runtime: &dyn McpServer) {

crates/rust-mcp-sdk/src/mcp_runtimes/client_runtime.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,19 @@ impl ClientRuntime {
130130
None
131131
}
132132
ServerMessage::Error(jsonrpc_error) => {
133-
self.handler.handle_error(jsonrpc_error.error, self).await?;
133+
self.handler
134+
.handle_error(&jsonrpc_error.error, self)
135+
.await?;
136+
if let Some(tx_response) = transport.pending_request_tx(&jsonrpc_error.id).await {
137+
tx_response
138+
.send(ServerMessage::Error(jsonrpc_error))
139+
.map_err(|e| RpcError::internal_error().with_message(e.to_string()))?;
140+
} else {
141+
tracing::warn!(
142+
"Received an error response with no corresponding request: {:?}",
143+
&jsonrpc_error.id
144+
);
145+
}
134146
None
135147
}
136148
ServerMessage::Response(response) => {
@@ -140,7 +152,7 @@ impl ClientRuntime {
140152
.map_err(|e| RpcError::internal_error().with_message(e.to_string()))?;
141153
} else {
142154
tracing::warn!(
143-
"Received response or error without a matching request: {:?}",
155+
"Received a response with no corresponding request: {:?}",
144156
&response.id
145157
);
146158
}

crates/rust-mcp-sdk/src/mcp_runtimes/client_runtime/mcp_client_runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl McpClientHandler for ClientInternalHandler<Box<dyn ClientHandler>> {
113113
/// Handles errors received from the server by passing the request to self.handler
114114
async fn handle_error(
115115
&self,
116-
jsonrpc_error: RpcError,
116+
jsonrpc_error: &RpcError,
117117
runtime: &dyn McpClient,
118118
) -> SdkResult<()> {
119119
self.handler.handle_error(jsonrpc_error, runtime).await?;

crates/rust-mcp-sdk/src/mcp_runtimes/client_runtime/mcp_client_runtime_core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl McpClientHandler for ClientCoreInternalHandler<Box<dyn ClientHandlerCore>>
8383

8484
async fn handle_error(
8585
&self,
86-
jsonrpc_error: RpcError,
86+
jsonrpc_error: &RpcError,
8787
runtime: &dyn McpClient,
8888
) -> SdkResult<()> {
8989
self.handler.handle_error(jsonrpc_error, runtime).await?;

crates/rust-mcp-sdk/src/mcp_runtimes/server_runtime.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,19 @@ impl ServerRuntime {
261261
None
262262
}
263263
ClientMessage::Error(jsonrpc_error) => {
264-
self.handler.handle_error(jsonrpc_error.error, self).await?;
264+
self.handler
265+
.handle_error(&jsonrpc_error.error, self)
266+
.await?;
267+
if let Some(tx_response) = transport.pending_request_tx(&jsonrpc_error.id).await {
268+
tx_response
269+
.send(ClientMessage::Error(jsonrpc_error))
270+
.map_err(|e| RpcError::internal_error().with_message(e.to_string()))?;
271+
} else {
272+
tracing::warn!(
273+
"Received an error response with no corresponding request {:?}",
274+
&jsonrpc_error.id
275+
);
276+
}
265277
None
266278
}
267279
// The response is the result of a request, it is processed at the transport level.
@@ -272,7 +284,7 @@ impl ServerRuntime {
272284
.map_err(|e| RpcError::internal_error().with_message(e.to_string()))?;
273285
} else {
274286
tracing::warn!(
275-
"Received response or error without a matching request: {:?}",
287+
"Received a response with no corresponding request: {:?}",
276288
&response.id
277289
);
278290
}

crates/rust-mcp-sdk/src/mcp_runtimes/server_runtime/mcp_server_runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl McpServerHandler for ServerRuntimeInternalHandler<Box<dyn ServerHandler>> {
177177

178178
async fn handle_error(
179179
&self,
180-
jsonrpc_error: RpcError,
180+
jsonrpc_error: &RpcError,
181181
runtime: &dyn McpServer,
182182
) -> SdkResult<()> {
183183
self.handler.handle_error(jsonrpc_error, runtime).await?;

crates/rust-mcp-sdk/src/mcp_runtimes/server_runtime/mcp_server_runtime_core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl McpServerHandler for RuntimeCoreInternalHandler<Box<dyn ServerHandlerCore>>
8787
}
8888
async fn handle_error(
8989
&self,
90-
jsonrpc_error: RpcError,
90+
jsonrpc_error: &RpcError,
9191
runtime: &dyn McpServer,
9292
) -> SdkResult<()> {
9393
self.handler.handle_error(jsonrpc_error, runtime).await?;

0 commit comments

Comments
 (0)