Skip to content

Commit f2d19b9

Browse files
committed
chore: update dispatcher to use Duration timeout param
1 parent af48a2b commit f2d19b9

4 files changed

Lines changed: 16 additions & 11 deletions

File tree

Makefile.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ args = [
2929
"rust-mcp-transport",
3030
]
3131

32+
33+
[tasks.check]
34+
dependencies = ["fmt", "clippy", "test", "doc-test"]
35+
3236
[tasks.clippy-fix]
3337
command = "cargo"
3438
args = ["clippy", "--fix", "--allow-dirty"]

crates/rust-mcp-transport/src/mcp_stream.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{
99
collections::HashMap,
1010
pin::Pin,
1111
sync::{atomic::AtomicI64, Arc},
12+
time::Duration,
1213
};
1314
use tokio::{
1415
io::{AsyncBufReadExt, BufReader},
@@ -34,7 +35,7 @@ impl MCPStream {
3435
writable: Mutex<Pin<Box<dyn tokio::io::AsyncWrite + Send + Sync>>>,
3536
error_io: IoStream,
3637
pending_requests: Arc<Mutex<HashMap<RequestId, tokio::sync::oneshot::Sender<R>>>>,
37-
timeout_msec: u64,
38+
request_timeout: Duration,
3839
shutdown_rx: Receiver<bool>,
3940
) -> (
4041
Pin<Box<dyn Stream<Item = R> + Send>>,
@@ -62,7 +63,7 @@ impl MCPStream {
6263
pending_requests,
6364
writable,
6465
Arc::new(AtomicI64::new(0)),
65-
timeout_msec,
66+
request_timeout,
6667
);
6768

6869
(stream, sender, error_io)

crates/rust-mcp-transport/src/message_dispatcher.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct MessageDispatcher<R> {
2828
pending_requests: Arc<Mutex<HashMap<RequestId, oneshot::Sender<R>>>>,
2929
writable_std: Mutex<Pin<Box<dyn tokio::io::AsyncWrite + Send + Sync>>>,
3030
message_id_counter: Arc<AtomicI64>,
31-
timeout_msec: u64,
31+
request_timeout: Duration,
3232
}
3333

3434
impl<R> MessageDispatcher<R> {
@@ -38,21 +38,21 @@ impl<R> MessageDispatcher<R> {
3838
/// * `pending_requests` - A thread-safe map for storing pending request IDs and their response channels.
3939
/// * `writable_std` - A mutex-protected, pinned writer (e.g., stdout) for sending serialized messages.
4040
/// * `message_id_counter` - An atomic counter for generating unique request IDs.
41-
/// * `timeout_msec` - The timeout duration in milliseconds for awaiting responses.
41+
/// * `request_timeout` - The timeout duration in milliseconds for awaiting responses.
4242
///
4343
/// # Returns
4444
/// A new `MessageDispatcher` instance configured for MCP message handling.
4545
pub fn new(
4646
pending_requests: Arc<Mutex<HashMap<RequestId, oneshot::Sender<R>>>>,
4747
writable_std: Mutex<Pin<Box<dyn tokio::io::AsyncWrite + Send + Sync>>>,
4848
message_id_counter: Arc<AtomicI64>,
49-
timeout_msec: u64,
49+
request_timeout: Duration,
5050
) -> Self {
5151
Self {
5252
pending_requests,
5353
writable_std,
5454
message_id_counter,
55-
timeout_msec,
55+
request_timeout,
5656
}
5757
}
5858

@@ -148,7 +148,7 @@ impl McpDispatch<ServerMessage, MessageFromClient> for MessageDispatcher<ServerM
148148

149149
if let Some(rx) = rx_response {
150150
// Wait for the response with timeout
151-
match await_timeout(rx, Duration::from_millis(self.timeout_msec)).await {
151+
match await_timeout(rx, self.request_timeout).await {
152152
Ok(response) => Ok(Some(response)),
153153
Err(error) => match error {
154154
TransportError::OneshotRecvError(_) => {
@@ -220,7 +220,7 @@ impl McpDispatch<ClientMessage, MessageFromServer> for MessageDispatcher<ClientM
220220
writable_std.flush().await?;
221221

222222
if let Some(rx) = rx_response {
223-
match await_timeout(rx, Duration::from_millis(self.timeout_msec)).await {
223+
match await_timeout(rx, self.request_timeout).await {
224224
Ok(response) => Ok(Some(response)),
225225
Err(error) => Err(error),
226226
}

crates/rust-mcp-transport/src/transport.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::pin::Pin;
1+
use std::{pin::Pin, time::Duration};
22

33
use async_trait::async_trait;
44
use rust_mcp_schema::{schema_utils::McpMessage, RequestId};
@@ -29,12 +29,12 @@ pub struct TransportOptions {
2929
///
3030
/// This value defines the maximum amount of time to wait for a response before
3131
/// considering the request as timed out.
32-
pub timeout: u64,
32+
pub timeout: Duration,
3333
}
3434
impl Default for TransportOptions {
3535
fn default() -> Self {
3636
Self {
37-
timeout: DEFAULT_TIMEOUT_MSEC,
37+
timeout: Duration::from_millis(DEFAULT_TIMEOUT_MSEC),
3838
}
3939
}
4040
}

0 commit comments

Comments
 (0)