Skip to main content

soma_mcp/
transport.rs

1//! MCP transport configuration and allowed-host/origin helpers.
2//!
3//! Separated from `rmcp_server.rs` to keep the `ServerHandler` impl focused on
4//! protocol concerns. Everything in this file is about wiring the HTTP transport
5//! layer: how connections are accepted and which origins are allowed. The
6//! deterministic host/origin computation and the generic Streamable HTTP
7//! transport wiring live in `soma-mcp-server`; this file only adapts Soma's
8//! `McpConfig` into the primitives that the role crate expects.
9
10#[cfg(test)]
11#[path = "transport_tests.rs"]
12mod tests;
13
14use rmcp::transport::streamable_http_server::{
15    session::local::LocalSessionManager, StreamableHttpServerConfig, StreamableHttpService,
16};
17use soma_mcp_server::http::{self, AllowedHostsInput, AllowedOriginsInput};
18
19use soma_config::McpConfig;
20
21use super::rmcp_server::{rmcp_server as make_server, SomaRmcpServer};
22
23// ── Transport builders ────────────────────────────────────────────────────────
24
25pub fn streamable_http_config(config: &McpConfig) -> StreamableHttpServerConfig {
26    http::streamable_http_config(allowed_hosts(config), allowed_origins(config))
27}
28
29pub fn streamable_http_service(
30    state: crate::McpState,
31    config: StreamableHttpServerConfig,
32) -> StreamableHttpService<SomaRmcpServer, LocalSessionManager> {
33    http::streamable_http_service(move || Ok(make_server(state.clone())), config)
34}
35
36// ── Allowed hosts / origins ───────────────────────────────────────────────────
37
38pub fn allowed_hosts(config: &McpConfig) -> Vec<String> {
39    http::allowed_hosts(AllowedHostsInput {
40        bind_host: &config.host,
41        port: config.port,
42        extra_hosts: &config.allowed_hosts,
43        public_url: config.auth.public_url.as_deref(),
44        public_url_label: "SOMA_MCP_PUBLIC_URL",
45    })
46}
47
48pub fn allowed_origins(config: &McpConfig) -> Vec<String> {
49    http::allowed_origins(AllowedOriginsInput {
50        port: config.port,
51        extra_origins: &config.allowed_origins,
52        public_url: config.auth.public_url.as_deref(),
53        extra_origins_label: "SOMA_MCP_ALLOWED_ORIGINS",
54        public_url_label: "SOMA_MCP_PUBLIC_URL",
55    })
56}