pub struct StreamableHttpService<S, M = LocalSessionManager> { /* private fields */ }Expand description
Streamable HTTP transport service for actix-web integration.
Provides bidirectional MCP communication over HTTP with session management. This service can be integrated into existing actix-web applications. Uses a builder pattern for configuration.
§Type Parameters
S- The MCP service type that handles protocol messagesM- The session manager type (defaults toLocalSessionManager)
§Architecture
The service manages endpoints with multiple HTTP methods:
- GET: For streaming event connections
- POST: For sending messages and creating sessions
- DELETE: For closing sessions
Each client is identified by a session ID that must be provided in request headers.
§Example
use rmcp_actix_web::transport::StreamableHttpService;
use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
use actix_web::{App, HttpServer, web};
use std::{sync::Arc, time::Duration};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// Create service OUTSIDE HttpServer::new() to share across workers
let service = StreamableHttpService::builder()
.service_factory(Arc::new(|| Ok(MyService::new())))
.session_manager(Arc::new(LocalSessionManager::default()))
.stateful_mode(true)
.sse_keep_alive(Duration::from_secs(30))
.build();
HttpServer::new(move || {
App::new()
// Clone service for each worker (shares the same LocalSessionManager)
.service(web::scope("/mcp").service(service.clone().scope()))
})
.bind("127.0.0.1:8080")?
.run()
.await
}Implementations§
Source§impl<S, M> StreamableHttpService<S, M>
impl<S, M> StreamableHttpService<S, M>
Sourcepub fn builder() -> StreamableHttpServiceBuilder<S, M>
pub fn builder() -> StreamableHttpServiceBuilder<S, M>
Create an instance of StreamableHttpService using the builder syntax
Source§impl<S, M> StreamableHttpService<S, M>
impl<S, M> StreamableHttpService<S, M>
Sourcepub fn scope(
self,
) -> Scope<impl ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse, Error = Error, InitError = ()>>
pub fn scope( self, ) -> Scope<impl ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse, Error = Error, InitError = ()>>
Creates a new scope configured with this service for framework-level composition.
This method provides framework-level composition aligned with RMCP patterns,
similar to how SseService::scope() works. This allows mounting the
streamable HTTP service at custom paths using actix-web’s routing.
The method consumes self, so you can call it directly on the service instance.
If you need to use the service multiple times, wrap it in an Arc and clone it.
This method is equivalent to scope_with_path("").
§Returns
Returns an actix-web Scope configured with the streamable HTTP routes
§Example
use rmcp_actix_web::transport::StreamableHttpService;
use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
use actix_web::{App, HttpServer, web};
use std::sync::Arc;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// Create service OUTSIDE HttpServer::new() to share across workers
let service = StreamableHttpService::builder()
.service_factory(Arc::new(|| Ok(MyService::new())))
.session_manager(Arc::new(LocalSessionManager::default()))
.build();
HttpServer::new(move || {
App::new()
// Clone service for each worker (shares the same LocalSessionManager)
.service(web::scope("/api/v1/mcp").service(service.clone().scope()))
})
.bind("127.0.0.1:8080")?
.run();
Ok(())
}Sourcepub fn scope_with_path(
self,
path: &str,
) -> Scope<impl ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse, Error = Error, InitError = ()>>
pub fn scope_with_path( self, path: &str, ) -> Scope<impl ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse, Error = Error, InitError = ()>>
Creates a new scope configured with this service for framework-level composition.
This method provides framework-level composition aligned with RMCP patterns,
similar to how SseService::scope() works. This allows mounting the
streamable HTTP service at custom paths using actix-web’s routing.
The method consumes self, so you can call it directly on the service instance.
If you need to use the service multiple times, wrap it in an Arc and clone it.
This method is similar to scope except that it allows specifying a custom path.
§Returns
Returns an actix-web Scope configured with the streamable HTTP routes
§Example
use rmcp_actix_web::transport::{StreamableHttpService, AuthorizationHeader};
use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
use actix_web::{App, HttpServer, web};
use std::sync::Arc;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// Create service OUTSIDE HttpServer::new() to share across workers
let service = StreamableHttpService::builder()
.service_factory(Arc::new(|| Ok(MyService::new())))
.session_manager(Arc::new(LocalSessionManager::default()))
.build();
HttpServer::new(move || {
App::new()
// Clone service for each worker (shares the same LocalSessionManager)
.service(service.clone().scope_with_path("/api/v1/mcp"))
})
.bind("127.0.0.1:8080")?
.run();
Ok(())
}