Skip to content

feat: allow failable service creation in streamable HTTP tower service #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions crates/rmcp/src/transport/streamable_http_server/tower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Default for StreamableHttpServerConfig {
pub struct StreamableHttpService<S, M = super::session::local::LocalSessionManager> {
pub config: StreamableHttpServerConfig,
session_manager: Arc<M>,
service_factory: Arc<dyn Fn() -> S + Send + Sync>,
service_factory: Arc<dyn Fn() -> Result<S, std::io::Error> + Send + Sync>,
}

impl<S, M> Clone for StreamableHttpService<S, M> {
Expand Down Expand Up @@ -92,7 +92,7 @@ where
M: SessionManager,
{
pub fn new(
service_factory: impl Fn() -> S + Send + Sync + 'static,
service_factory: impl Fn() -> Result<S, std::io::Error> + Send + Sync + 'static,
session_manager: Arc<M>,
config: StreamableHttpServerConfig,
) -> Self {
Expand All @@ -102,7 +102,7 @@ where
service_factory: Arc::new(service_factory),
}
}
fn get_service(&self) -> S {
fn get_service(&self) -> Result<S, std::io::Error> {
(self.service_factory)()
}
pub async fn handle<B>(&self, request: Request<B>) -> Response<UnsyncBoxBody<Bytes, Infallible>>
Expand Down Expand Up @@ -318,7 +318,9 @@ where
.create_session()
.await
.map_err(internal_error_response("create session"))?;
let service = self.get_service();
let service = self
.get_service()
.map_err(internal_error_response("get service"))?;
// spawn a task to serve the session
tokio::spawn({
let session_manager = self.session_manager.clone();
Expand Down Expand Up @@ -372,7 +374,9 @@ where
Ok(response)
}
} else {
let service = self.get_service();
let service = self
.get_service()
.map_err(internal_error_response("get service"))?;
match message {
ClientJsonRpcMessage::Request(request) => {
let (transport, receiver) =
Expand Down
2 changes: 1 addition & 1 deletion crates/rmcp/tests/test_with_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async fn test_with_js_streamable_http_client() -> anyhow::Result<()> {

let service: StreamableHttpService<Calculator, LocalSessionManager> =
StreamableHttpService::new(
Calculator::default,
|| Ok(Calculator),
Default::default(),
StreamableHttpServerConfig {
stateful_mode: true,
Expand Down
2 changes: 1 addition & 1 deletion examples/servers/src/counter_hyper_streamable_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rmcp::transport::streamable_http_server::{
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let service = TowerToHyperService::new(StreamableHttpService::new(
Counter::new,
|| Ok(Counter::new()),
LocalSessionManager::default().into(),
Default::default(),
));
Expand Down
2 changes: 1 addition & 1 deletion examples/servers/src/counter_streamhttp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> anyhow::Result<()> {
.init();

let service = StreamableHttpService::new(
Counter::new,
|| Ok(Counter::new()),
LocalSessionManager::default().into(),
Default::default(),
);
Expand Down
Loading