Crate rmcp_actix_web

Crate rmcp_actix_web 

Source
Expand description

§rmcp-actix-web

actix-web transport implementations for RMCP (Rust Model Context Protocol).

This crate provides HTTP-based transport layers for the Model Context Protocol (MCP), offering a complete alternative to the default Axum-based transports in the main RMCP crate. If you’re already using actix-web in your application or prefer its API, this crate allows you to integrate MCP services seamlessly without introducing additional web frameworks.

§Features

  • [SSE (Server-Sent Events) Transport][SseServer]: Real-time, unidirectional communication from server to client
  • Streamable HTTP Transport: Bidirectional communication with session management
  • Full MCP Compatibility: Implements the complete MCP specification
  • Drop-in Replacement: Same service implementations work with either Axum or actix-web transports
  • Production Ready: Built on battle-tested actix-web framework

§Quick Start

§SSE Service Example

use rmcp_actix_web::SseService;
use actix_web::{App, HttpServer};
use std::time::Duration;

#[actix_web::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let sse_service = SseService::builder()
        .service_factory(std::sync::Arc::new(|| Ok(MyMcpService::new())))
        .sse_path("/sse".to_string())
        .post_path("/message".to_string())
        .sse_keep_alive(Duration::from_secs(30))
        .build();

    HttpServer::new(move || {
        App::new()
            .service(sse_service.clone().scope())
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await?;
     
    Ok(())
}

§Streamable HTTP Server Example

use rmcp_actix_web::StreamableHttpService;
use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
use rmcp::{ServerHandler, model::ServerInfo};
use actix_web::{App, HttpServer};
use std::{sync::Arc, time::Duration};

#[actix_web::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    HttpServer::new(|| {
        let http_service = StreamableHttpService::builder()
            .service_factory(Arc::new(|| Ok(MyMcpService::new())))
            .session_manager(Arc::new(LocalSessionManager::default()))
            .stateful_mode(true)
            .sse_keep_alive(Duration::from_secs(30))
            .build();

        App::new()
            .service(http_service.scope())
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await?;
     
    Ok(())
}

§Transport Selection

Choose between the two transport types based on your needs:

  • SSE Transport: Best for server-to-client streaming, simpler protocol, works through proxies
  • Streamable HTTP: Full bidirectional communication, session management, more complex protocol

§Examples

See the examples/ directory for complete working examples:

  • counter_sse.rs - SSE server with a simple counter service
  • counter_streamable_http.rs - Streamable HTTP server example
  • composition_sse_example.rs - SSE server with framework-level composition
  • composition_streamable_http_example.rs - StreamableHttp with custom mounting
  • multi_service_example.rs - Multiple MCP services with different transports

§Framework-Level Composition

Both transports support framework-level composition aligned with RMCP patterns, allowing you to mount MCP services at custom paths within existing actix-web applications.

§SSE Service Composition

use rmcp_actix_web::SseService;
use actix_web::{App, web};
use std::time::Duration;

let sse_service = SseService::builder()
    .service_factory(std::sync::Arc::new(|| Ok(MyService::new())))
    .sse_path("/events".to_string())
    .post_path("/messages".to_string())
    .sse_keep_alive(Duration::from_secs(30))
    .build();

// Mount at custom path using scope()
let app = App::new()
    .service(web::scope("/api/v1/calculator").service(sse_service.scope()));

§StreamableHttp Service Composition

use rmcp_actix_web::StreamableHttpService;
use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
use actix_web::{App, web};
use std::{sync::Arc, time::Duration};

HttpServer::new(|| {
    let http_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();

    // Mount at custom path using scope()
    App::new()
        .service(web::scope("/api/v1/calculator").service(http_service.scope()))
}).bind("127.0.0.1:8080")?.run().await

§Multi-Service Composition

use rmcp_actix_web::{SseService, StreamableHttpService};
use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
use actix_web::{App, web};
use std::{sync::Arc, time::Duration};

HttpServer::new(|| {
    // Both services use identical builder pattern
    let sse_service = SseService::builder()
        .service_factory(Arc::new(|| Ok(MyService::new())))
        .sse_path("/events".to_string())
        .post_path("/messages".to_string())
        .build();

    let http_service = StreamableHttpService::builder()
        .service_factory(Arc::new(|| Ok(MyService::new())))
        .session_manager(Arc::new(LocalSessionManager::default()))
        .stateful_mode(true)
        .build();

    // Both services mount identically via scope()
    App::new()
        .service(web::scope("/api/sse").service(sse_service.scope()))
        .service(web::scope("/api/http").service(http_service.scope()))
}).bind("127.0.0.1:8080")?.run().await

See the examples/ directory for complete working examples of composition patterns.

§Performance Considerations

  • SSE transport has lower overhead for unidirectional communication
  • Streamable HTTP maintains persistent sessions which may use more memory
  • Both transports use efficient async I/O through actix-web’s actor system
  • Framework-level composition adds minimal overhead

§Feature Flags

This crate supports selective compilation of transport types:

  • transport-sse-server (default): Enables SSE transport
  • transport-streamable-http-server (default): Enables StreamableHttp transport

To use only specific transports, disable default features:

[dependencies]
rmcp-actix-web = { version = "0.1", default-features = false, features = ["transport-sse-server"] }

Re-exports§

pub use transport::sse_server::SseService;
pub use transport::streamable_http_server::StreamableHttpService;

Modules§

transport
Transport implementations for the Model Context Protocol using actix-web.

Structs§

StreamableHttpServerConfig
Configuration for the streamable HTTP server transport.

Type Aliases§

SessionId
Unique identifier for client sessions in server-side HTTP transports.