Skip to content

Commit 5bf54d6

Browse files
authored
refactor: expose Store Traits and add ToMcpServerHandler for Improved Framework Flexibility (#107)
* refactor: expose session store and event store traits and implementations * chore: update cargo feature
1 parent d10488b commit 5bf54d6

File tree

7 files changed

+30
-14
lines changed

7 files changed

+30
-14
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use crate::{
55
utils::{
66
DEFAULT_MESSAGES_ENDPOINT, DEFAULT_SSE_ENDPOINT, DEFAULT_STREAMABLE_HTTP_ENDPOINT,
77
},
8-
InMemorySessionStore, McpAppState,
8+
McpAppState,
99
},
1010
mcp_server::hyper_runtime::HyperRuntime,
1111
mcp_traits::{mcp_handler::McpServerHandler, IdGenerator},
12+
session_store::InMemorySessionStore,
1213
};
1314
#[cfg(feature = "ssl")]
1415
use axum_server::tls_rustls::RustlsConfig;

crates/rust-mcp-sdk/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub(crate) mod mcp_http;
77
mod mcp_macros;
88
mod mcp_runtimes;
99
mod mcp_traits;
10+
#[cfg(any(feature = "server", feature = "hyper-server"))]
11+
pub mod session_store;
1012
mod utils;
1113

1214
#[cfg(feature = "client")]
@@ -65,7 +67,7 @@ pub mod mcp_server {
6567
//! handle each message based on its type and parameters.
6668
//!
6769
//! Refer to [examples/hello-world-mcp-server-stdio-core](https://github.com/rust-mcp-stack/rust-mcp-sdk/tree/main/examples/hello-world-mcp-server-stdio-core) for an example.
68-
pub use super::mcp_handlers::mcp_server_handler::ServerHandler;
70+
pub use super::mcp_handlers::mcp_server_handler::{ServerHandler, ToMcpServerHandler};
6971
pub use super::mcp_handlers::mcp_server_handler_core::ServerHandlerCore;
7072

7173
pub use super::mcp_runtimes::server_runtime::mcp_server_runtime as server_runtime;
@@ -79,6 +81,9 @@ pub mod mcp_server {
7981
#[cfg(feature = "hyper-server")]
8082
pub use super::hyper_servers::*;
8183
pub use super::utils::enforce_compatible_protocol_version;
84+
85+
#[cfg(feature = "hyper-server")]
86+
pub use super::mcp_http::{McpAppState, McpHttpHandler};
8287
}
8388

8489
#[cfg(feature = "client")]

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::schema::{schema_utils::CallToolError, *};
1+
use crate::{
2+
mcp_server::server_runtime::ServerRuntimeInternalHandler,
3+
mcp_traits::mcp_handler::McpServerHandler,
4+
schema::{schema_utils::CallToolError, *},
5+
};
26
use async_trait::async_trait;
37
use serde_json::Value;
48
use std::sync::Arc;
@@ -326,3 +330,14 @@ pub trait ServerHandler: Send + Sync + 'static {
326330
Ok(())
327331
}
328332
}
333+
334+
// Custom trait for conversion
335+
pub trait ToMcpServerHandler {
336+
fn to_mcp_server_handler(self) -> Arc<dyn McpServerHandler + 'static>;
337+
}
338+
339+
impl<T: ServerHandler + 'static> ToMcpServerHandler for T {
340+
fn to_mcp_server_handler(self) -> Arc<dyn McpServerHandler + 'static> {
341+
Arc::new(ServerRuntimeInternalHandler::new(Box::new(self)))
342+
}
343+
}

crates/rust-mcp-sdk/src/mcp_http.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
mod app_state;
22
mod mcp_http_handler;
33
pub(crate) mod mcp_http_utils;
4-
mod session_store;
54

6-
pub(crate) use app_state::*;
5+
pub use app_state::*;
76
pub use mcp_http_handler::*;
8-
pub use session_store::*;
97

108
pub(crate) mod utils {
119
pub use super::mcp_http_utils::*;

crates/rust-mcp-sdk/src/mcp_http/app_state.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::{sync::Arc, time::Duration};
2-
3-
use super::session_store::SessionStore;
41
use crate::mcp_traits::mcp_handler::McpServerHandler;
2+
use crate::session_store::SessionStore;
53
use crate::{id_generator::FastIdGenerator, mcp_traits::IdGenerator, schema::InitializeResult};
6-
74
use rust_mcp_transport::event_store::EventStore;
8-
95
use rust_mcp_transport::{SessionId, TransportOptions};
6+
use std::{sync::Arc, time::Duration};
107

118
/// Application state struct for the Hyper ser
129
///

crates/rust-mcp-sdk/src/mcp_http/session_store.rs renamed to crates/rust-mcp-sdk/src/session_store.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
mod in_memory;
1+
mod in_memory_session_store;
22
use std::sync::Arc;
33

44
use async_trait::async_trait;
5-
pub use in_memory::*;
5+
pub use in_memory_session_store::*;
66
use rust_mcp_transport::SessionId;
77
use tokio::sync::Mutex;
88

99
use crate::mcp_server::ServerRuntime;
1010

1111
// Type alias for the server-side duplex stream used in sessions
12-
pub type TxServer = Arc<ServerRuntime>;
12+
type TxServer = Arc<ServerRuntime>;
1313

1414
/// Trait defining the interface for session storage operations
1515
///

0 commit comments

Comments
 (0)