Skip to content

Commit f2f0afb

Browse files
authored
fix: session ID access in handlers and add helper for listing active (#90)
sessions
1 parent f05c234 commit f2f0afb

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

crates/rust-mcp-sdk/src/hyper_servers/hyper_runtime.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ impl HyperRuntime {
7070
result.map_err(|err| err.into())
7171
}
7272

73+
/// Returns a list of active session IDs from the session store.
74+
pub async fn sessions(&self) -> Vec<String> {
75+
self.state.session_store.keys().await
76+
}
77+
78+
/// Retrieves the runtime associated with the given session ID from the session store.
7379
pub async fn runtime_by_session(
7480
&self,
7581
session_id: &SessionId,

crates/rust-mcp-sdk/src/hyper_servers/routes/hyper_utils.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ pub async fn start_new_session(
172172
session_id.to_owned(),
173173
));
174174

175-
tracing::info!(
176-
"a new client joined : {}",
177-
runtime.session_id().await.unwrap_or_default().to_owned()
178-
);
175+
tracing::info!("a new client joined : {}", &session_id);
179176

180177
let response = create_sse_stream(
181178
runtime.clone(),

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ impl McpServer for ServerRuntime {
197197
}
198198
Ok(())
199199
}
200+
201+
#[cfg(feature = "hyper-server")]
202+
fn session_id(&self) -> Option<SessionId> {
203+
self.session_id.to_owned()
204+
}
200205
}
201206

202207
impl ServerRuntime {
@@ -435,11 +440,6 @@ impl ServerRuntime {
435440
}
436441
}
437442

438-
#[cfg(feature = "hyper-server")]
439-
pub(crate) async fn session_id(&self) -> Option<SessionId> {
440-
self.session_id.to_owned()
441-
}
442-
443443
#[cfg(feature = "hyper-server")]
444444
pub(crate) fn new_instance(
445445
server_details: Arc<InitializeResult>,

crates/rust-mcp-sdk/src/mcp_traits/mcp_server.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::time::Duration;
2-
31
use crate::schema::{
42
schema_utils::{
53
ClientMessage, McpMessage, MessageFromServer, NotificationFromServer, RequestFromServer,
@@ -16,6 +14,8 @@ use crate::schema::{
1614
SetLevelRequest, ToolListChangedNotification, ToolListChangedNotificationParams,
1715
};
1816
use async_trait::async_trait;
17+
use rust_mcp_transport::SessionId;
18+
use std::time::Duration;
1919

2020
use crate::{error::SdkResult, utils::format_assertion_message};
2121

@@ -405,4 +405,7 @@ pub trait McpServer: Sync + Send {
405405
}
406406
Ok(())
407407
}
408+
409+
#[cfg(feature = "hyper-server")]
410+
fn session_id(&self) -> Option<SessionId>;
408411
}

0 commit comments

Comments
 (0)