1use crate::combined::DynHandler;
7use crate::config::{CoreConfig, EnvReader};
8use crate::credential::{Credential, CredentialVerifier};
9use crate::policy::Guarded;
10use crate::server::BODY_LIMIT;
11use axum::extract::{Request, State};
12use axum::http::{Method, StatusCode};
13use axum::middleware::Next;
14use axum::response::{IntoResponse, Response};
15use base64::Engine as _;
16use base64::engine::general_purpose::STANDARD;
17use rmcp::ServerHandler;
18use rmcp::model::Icon;
19use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
20use rmcp::transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService};
21use std::collections::HashMap;
22use std::sync::{Arc, Mutex};
23use std::time::{Duration, Instant};
24use tokio_util::sync::CancellationToken;
25
26pub const QUIET_LOG_TARGETS: &[&str] = &[
30 "rmcp::transport::streamable_http_server",
31 "rmcp::transport::worker",
32];
33
34pub type ServiceEnv = EnvReader<Box<dyn Fn(&str) -> Option<String>>>;
36
37pub type ServiceFactory = fn(&mut ServiceEnv) -> Option<Arc<dyn Service>>;
40
41#[derive(Debug, Clone)]
43pub struct HttpSettings {
44 pub public_url: String,
46 pub public_host: String,
48 pub allowed_origins: Vec<String>,
50}
51
52impl HttpSettings {
53 pub fn from_core(core: &CoreConfig) -> Self {
55 Self {
56 public_url: core.public_url.clone(),
57 public_host: core.public_host.clone(),
58 allowed_origins: core.allowed_origins.clone(),
59 }
60 }
61}
62
63#[derive(Debug, Clone)]
65pub struct ServiceContext {
66 pub http: HttpSettings,
68 pub shutdown: CancellationToken,
70}
71
72pub trait Service: Send + Sync + 'static {
97 fn name(&self) -> &'static str;
101 fn handler(&self) -> DynHandler;
105 fn router(self: Arc<Self>, ctx: &ServiceContext) -> axum::Router {
110 mcp_service(ctx, move || Ok(self.handler()))
111 }
112 fn tool_names(&self) -> Vec<String> {
114 Vec::new()
115 }
116 fn log_caps(&self) -> &'static [&'static str] {
119 &[]
120 }
121 fn credential_verifier(&self) -> Option<Arc<dyn CredentialVerifier>> {
125 None
126 }
127}
128
129fn origin_with_port(public_url: &str) -> String {
132 let (scheme, rest) = public_url
133 .split_once("://")
134 .unwrap_or(("https", public_url));
135 let authority = rest.split('/').next().unwrap_or(rest);
136 let has_port = authority
137 .rsplit_once(':')
138 .is_some_and(|(host, port)| !host.ends_with(':') && port.parse::<u16>().is_ok());
139 if has_port {
140 return format!("{scheme}://{authority}");
141 }
142 let port = if scheme == "http" { 80 } else { 443 };
143 format!("{scheme}://{authority}:{port}")
144}
145
146pub fn mcp_service<S, F>(ctx: &ServiceContext, factory: F) -> axum::Router
160where
161 S: ServerHandler + Send + 'static,
162 F: Fn() -> Result<S, std::io::Error> + Send + Sync + 'static,
163{
164 let http = &ctx.http;
165 let mut hosts = vec![http.public_host.clone()];
166 for h in ["localhost", "127.0.0.1", "::1"] {
167 if !hosts.iter().any(|x| x == h) {
168 hosts.push(h.to_string());
169 }
170 }
171 let mut origins = vec![origin_with_port(&http.public_url)];
172 origins.extend(http.allowed_origins.iter().cloned());
173 let config = StreamableHttpServerConfig::default()
174 .with_allowed_hosts(hosts)
175 .with_allowed_origins(origins)
176 .enforce_origin_validation()
177 .with_max_request_body_bytes(BODY_LIMIT)
178 .with_cancellation_token(ctx.shutdown.clone());
179 let manager = Arc::new(LocalSessionManager::default());
180 let service =
181 StreamableHttpService::new(move || factory().map(Guarded::new), manager.clone(), config);
182 let owners = Arc::new(SessionOwners::new(manager, SessionLimits::DEFAULT));
183 axum::Router::new()
184 .fallback_service(service)
185 .layer(axum::middleware::from_fn_with_state(owners, bind_sessions))
186}
187
188const SESSION_HEADER: &str = "mcp-session-id";
189
190#[derive(Debug, Clone, Copy)]
192struct SessionLimits {
193 total: usize,
198 per_owner: usize,
200 idle: Duration,
203}
204
205impl SessionLimits {
206 const DEFAULT: SessionLimits = SessionLimits {
207 total: 10_000,
208 per_owner: 50,
209 idle: Duration::from_secs(360),
210 };
211}
212
213struct SessionOwners {
218 manager: Arc<LocalSessionManager>,
219 limits: SessionLimits,
220 table: Mutex<Table>,
221}
222
223type Who = Option<[u8; 32]>;
225
226#[derive(Default)]
227struct Table {
228 sessions: HashMap<String, (Who, Instant)>,
230 counts: HashMap<Who, usize>,
232 last_sweep: Option<Instant>,
234}
235
236impl Table {
237 fn insert(&mut self, id: String, who: Who, now: Instant) {
238 if self.sessions.insert(id, (who, now)).is_none() {
239 *self.counts.entry(who).or_default() += 1;
240 }
241 }
242
243 fn remove(&mut self, id: &str) -> bool {
244 let Some((who, _)) = self.sessions.remove(id) else {
245 return false;
246 };
247 if let Some(n) = self.counts.get_mut(&who) {
248 *n -= 1;
249 if *n == 0 {
250 self.counts.remove(&who);
251 }
252 }
253 true
254 }
255
256 fn retain(&mut self, mut keep: impl FnMut(&str, &(Who, Instant)) -> bool) {
257 self.sessions.retain(|k, v| keep(k, v));
258 self.counts.clear();
259 for (who, _) in self.sessions.values() {
260 *self.counts.entry(*who).or_default() += 1;
261 }
262 }
263
264 fn count(&self, who: Who) -> usize {
265 self.counts.get(&who).copied().unwrap_or(0)
266 }
267
268 fn oldest_of(&self, who: Who) -> Option<String> {
270 self.sessions
271 .iter()
272 .filter(|(_, (owner, _))| *owner == who)
273 .min_by_key(|(_, (_, used))| *used)
274 .map(|(k, _)| k.clone())
275 }
276
277 fn largest_other(&self, who: Who) -> Option<(Who, usize)> {
279 self.counts
280 .iter()
281 .filter(|(owner, _)| **owner != who)
282 .max_by_key(|(_, n)| **n)
283 .map(|(owner, n)| (*owner, *n))
284 }
285
286 fn fair_share_room(&self, who: Who) -> Option<Who> {
289 let mine = self.count(who);
290 self.largest_other(who)
291 .filter(|(_, n)| *n > mine + 1)
292 .map(|(owner, _)| owner)
293 }
294
295 fn room_for(&self, who: Who) -> Option<Who> {
299 self.fair_share_room(who)
300 .or_else(|| (who.is_some() && self.count(who) > 0).then_some(who))
301 }
302}
303
304#[derive(Debug, PartialEq, Eq)]
306enum Ownership {
307 Owner,
308 NotOwner,
309 Stale,
311}
312
313#[derive(Debug, PartialEq, Eq)]
315struct Admission {
316 admitted: bool,
317 dropped: Vec<String>,
319}
320
321impl SessionOwners {
322 fn new(manager: Arc<LocalSessionManager>, limits: SessionLimits) -> Self {
323 SessionOwners {
324 manager,
325 limits,
326 table: Mutex::new(Table::default()),
327 }
328 }
329
330 fn table(&self) -> std::sync::MutexGuard<'_, Table> {
331 self.table.lock().unwrap_or_else(|e| e.into_inner())
332 }
333
334 fn owned_by(&self, id: &str, who: Who) -> Ownership {
335 let now = Instant::now();
336 let mut table = self.table();
337 match table.sessions.get_mut(id) {
338 Some((owner, _)) if *owner != who => Ownership::NotOwner,
339 Some((_, used)) if now.duration_since(*used) < self.limits.idle => {
340 *used = now;
341 Ownership::Owner
342 }
343 Some(_) => Ownership::Stale,
344 None => Ownership::NotOwner,
345 }
346 }
347
348 fn touch(&self, id: &str, who: Who) {
350 if let Some((owner, used)) = self.table().sessions.get_mut(id)
351 && *owner == who
352 {
353 *used = Instant::now();
354 }
355 }
356
357 fn wants_live_sessions(&self) -> bool {
359 let table = self.table();
360 table.sessions.len() >= self.limits.total && self.sweep_due(&table)
361 }
362
363 fn sweep_due(&self, table: &Table) -> bool {
364 table.last_sweep.is_none_or(|t| t.elapsed() >= SWEEP_EVERY)
365 }
366
367 fn refuse_early(&self, who: Who) -> bool {
370 let table = self.table();
371 table.sessions.len() >= self.limits.total
372 && !self.sweep_due(&table)
373 && !(who.is_some() && table.count(who) >= self.limits.per_owner)
374 && table.room_for(who).is_none()
375 }
376
377 fn admit(
380 &self,
381 id: &str,
382 who: Who,
383 live: Option<&std::collections::HashSet<String>>,
384 ) -> Admission {
385 let now = Instant::now();
386 let limits = self.limits;
387 let mut table = self.table();
388 let mut dropped = Vec::new();
389 if who.is_some() {
391 while table.count(who) >= limits.per_owner {
392 let Some(oldest) = table.oldest_of(who) else {
393 break;
394 };
395 table.remove(&oldest);
396 dropped.push(oldest);
397 }
398 }
399 if table.sessions.len() >= limits.total && self.sweep_due(&table) {
400 table.retain(|_, (_, used)| now.duration_since(*used) < limits.idle);
401 if let Some(live) = live {
402 table.retain(|k, _| live.contains(k));
403 }
404 table.last_sweep = Some(now);
405 }
406 if table.sessions.len() >= limits.total {
407 let Some(owner) = table.room_for(who) else {
410 return Admission {
411 admitted: false,
412 dropped,
413 };
414 };
415 if let Some(oldest) = table.oldest_of(owner) {
416 table.remove(&oldest);
417 dropped.push(oldest);
418 }
419 }
420 table.insert(id.to_owned(), who, now);
421 Admission {
422 admitted: true,
423 dropped,
424 }
425 }
426
427 fn remove(&self, id: &str) {
428 self.table().remove(id);
429 }
430
431 async fn live_sessions(&self) -> std::collections::HashSet<String> {
432 self.manager
433 .sessions
434 .read()
435 .await
436 .keys()
437 .map(|k| k.to_string())
438 .collect()
439 }
440
441 async fn is_live(&self, id: &str) -> bool {
442 self.manager.sessions.read().await.contains_key(id)
443 }
444
445 async fn close(&self, id: &str) {
446 use rmcp::transport::streamable_http_server::session::SessionManager as _;
447 let _ = self.manager.close_session(&Arc::from(id)).await;
448 }
449}
450
451const SWEEP_EVERY: Duration = Duration::from_secs(1);
453
454fn too_many_sessions() -> Response {
455 let mut busy = (
456 StatusCode::SERVICE_UNAVAILABLE,
457 "This server has too many open sessions. Try again shortly.",
458 )
459 .into_response();
460 busy.headers_mut().insert(
461 axum::http::header::RETRY_AFTER,
462 axum::http::HeaderValue::from_static("30"),
463 );
464 busy
465}
466
467async fn bind_sessions(
470 State(owners): State<Arc<SessionOwners>>,
471 req: Request,
472 next: Next,
473) -> Response {
474 let who = req
475 .extensions()
476 .get::<Credential>()
477 .map(Credential::fingerprint);
478 let session = req
479 .headers()
480 .get(SESSION_HEADER)
481 .and_then(|v| v.to_str().ok())
482 .map(str::to_owned);
483 let delete = req.method() == Method::DELETE;
484 match &session {
485 Some(id) => match owners.owned_by(id, who) {
486 Ownership::Owner => {}
487 Ownership::NotOwner => {
488 return (StatusCode::NOT_FOUND, "Session not found").into_response();
489 }
490 Ownership::Stale => {
493 if owners.is_live(id).await {
494 owners.touch(id, who);
495 } else {
496 owners.remove(id);
497 return (StatusCode::NOT_FOUND, "Session not found").into_response();
498 }
499 }
500 },
501 None if owners.refuse_early(who) => return too_many_sessions(),
502 None => {}
503 }
504 let res = next.run(req).await;
505 match session {
506 None => {
507 let Some(id) = res
508 .headers()
509 .get(SESSION_HEADER)
510 .and_then(|v| v.to_str().ok())
511 .map(str::to_owned)
512 else {
513 return res;
514 };
515 let task = tokio::spawn({
518 let owners = owners.clone();
519 async move {
520 let live = if owners.wants_live_sessions() {
521 Some(owners.live_sessions().await)
522 } else {
523 None
524 };
525 let admission = owners.admit(&id, who, live.as_ref());
526 for old in &admission.dropped {
527 owners.close(old).await;
528 }
529 if !admission.admitted {
530 owners.close(&id).await;
531 }
532 admission.admitted
533 }
534 });
535 if task.await.unwrap_or(false) {
536 res
537 } else {
538 too_many_sessions()
539 }
540 }
541 Some(id) if delete && res.status().is_success() => {
542 owners.remove(&id);
543 res
544 }
545 Some(_) => res,
546 }
547}
548
549pub fn icons(png: &[u8], svg: &str) -> Vec<Icon> {
551 vec![
552 Icon::new(format!("data:image/png;base64,{}", STANDARD.encode(png)))
553 .with_mime_type("image/png")
554 .with_sizes(vec!["128x128".into()]),
555 Icon::new(format!(
556 "data:image/svg+xml;base64,{}",
557 STANDARD.encode(svg)
558 ))
559 .with_mime_type("image/svg+xml")
560 .with_sizes(vec!["any".into()]),
561 ]
562}
563
564#[cfg(test)]
565mod tests {
566 use super::*;
567
568 #[test]
569 fn icons_are_png_then_svg_data_uris() {
570 let icons = icons(b"png-bytes", "<svg/>");
571 assert_eq!(icons.len(), 2);
572 assert_eq!(
573 icons[0].src,
574 format!("data:image/png;base64,{}", STANDARD.encode(b"png-bytes"))
575 );
576 assert_eq!(icons[0].mime_type.as_deref(), Some("image/png"));
577 assert_eq!(icons[0].sizes, Some(vec!["128x128".to_string()]));
578 assert_eq!(
579 icons[1].src,
580 format!("data:image/svg+xml;base64,{}", STANDARD.encode("<svg/>"))
581 );
582 assert_eq!(icons[1].mime_type.as_deref(), Some("image/svg+xml"));
583 assert_eq!(icons[1].sizes, Some(vec!["any".to_string()]));
584 }
585
586 fn owners(total: usize, per_owner: usize) -> SessionOwners {
587 SessionOwners::new(
588 Arc::new(LocalSessionManager::default()),
589 SessionLimits {
590 total,
591 per_owner,
592 idle: Duration::from_secs(360),
593 },
594 )
595 }
596
597 fn ok(dropped: &[&str]) -> Admission {
598 Admission {
599 admitted: true,
600 dropped: dropped.iter().map(|s| s.to_string()).collect(),
601 }
602 }
603
604 fn live(ids: &[&str]) -> std::collections::HashSet<String> {
605 ids.iter().map(|s| s.to_string()).collect()
606 }
607
608 #[test]
609 fn one_owner_cannot_hold_more_than_its_share() {
610 let o = owners(100, 3);
611 let (alice, bob) = (Some([1; 32]), Some([2; 32]));
612 assert_eq!(o.admit("a1", alice, None), ok(&[]));
613 for i in 0..3 {
614 assert_eq!(o.admit(&format!("b{i}"), bob, None), ok(&[]));
615 }
616 assert_eq!(
617 o.admit("b3", bob, None),
618 ok(&["b0"]),
619 "Bob's own oldest session makes room"
620 );
621 assert_eq!(
622 o.owned_by("a1", alice),
623 Ownership::Owner,
624 "Alice is untouched"
625 );
626 assert_eq!(o.owned_by("b0", bob), Ownership::NotOwner);
627 assert_eq!(o.owned_by("b3", bob), Ownership::Owner);
628 assert_eq!(o.owned_by("b3", alice), Ownership::NotOwner);
629 }
630
631 #[test]
632 fn a_full_table_frees_ended_sessions_first() {
633 let o = owners(3, 10);
634 for (i, who) in [[1; 32], [2; 32], [3; 32]].into_iter().enumerate() {
635 assert_eq!(o.admit(&format!("s{i}"), Some(who), None), ok(&[]));
636 }
637 assert!(o.wants_live_sessions());
638 assert_eq!(
639 o.admit("new", Some([4; 32]), Some(&live(&["s0", "s2"]))),
640 ok(&[]),
641 "the session rmcp has ended makes room"
642 );
643 assert_eq!(o.owned_by("s1", Some([2; 32])), Ownership::NotOwner);
644 assert_eq!(o.owned_by("new", Some([4; 32])), Ownership::Owner);
645 }
646
647 #[test]
648 fn a_full_table_takes_room_from_whoever_holds_most() {
649 let o = owners(4, 3);
650 let (a, b, c) = (Some([1; 32]), Some([2; 32]), Some([3; 32]));
651 for id in ["a1", "a2", "a3"] {
652 assert_eq!(o.admit(id, a, None), ok(&[]));
653 }
654 assert_eq!(o.admit("b1", b, None), ok(&[]));
655 let all = live(&["a1", "a2", "a3", "b1"]);
656 assert_eq!(
657 o.admit("c1", c, Some(&all)),
658 ok(&["a1"]),
659 "the largest holder gives up its oldest"
660 );
661 assert_eq!(
662 o.owned_by("b1", b),
663 Ownership::Owner,
664 "Bob's only session is kept"
665 );
666 assert_eq!(o.admit("d1", Some([4; 32]), None), ok(&["a2"]));
668 assert_eq!(
669 o.admit("e1", Some([5; 32]), None),
670 Admission {
671 admitted: false,
672 dropped: vec![]
673 },
674 "everyone holds one: nobody is pushed out for a newcomer"
675 );
676 assert!(
677 o.refuse_early(Some([6; 32])),
678 "and the next is refused cheaply"
679 );
680 let o = owners(2, 1);
682 o.admit("a1", a, None);
683 o.admit("b1", b, None);
684 assert!(!o.admit("c1", c, Some(&live(&["a1", "b1"]))).admitted);
685 assert!(o.refuse_early(c));
686 assert!(!o.refuse_early(a));
687 assert_eq!(o.admit("a2", a, None), ok(&["a1"]));
688 }
689
690 #[test]
691 fn the_largest_holder_below_its_cap_replaces_its_own_sessions() {
692 let o = owners(4, 10);
693 let (a, b) = (Some([1; 32]), Some([2; 32]));
694 for id in ["a1", "a2", "a3"] {
695 assert_eq!(o.admit(id, a, None), ok(&[]));
696 }
697 assert_eq!(o.admit("b1", b, None), ok(&[]));
698 let all = live(&["a1", "a2", "a3", "b1"]);
699 assert!(!o.refuse_early(a), "a holds the most, so it is not refused");
700 assert_eq!(
701 o.admit("a4", a, Some(&all)),
702 ok(&["a1"]),
703 "a full table makes room from a's own oldest"
704 );
705 assert_eq!(o.admit("b2", b, None), ok(&["a2"]), "b still takes from a");
706 }
707
708 #[test]
709 fn a_key_with_one_fewer_than_the_largest_replaces_its_own() {
710 let o = owners(3, 10);
711 let (a, b) = (Some([1; 32]), Some([2; 32]));
712 o.admit("a1", a, None);
713 o.admit("a2", a, None);
714 o.admit("b1", b, None);
715 let all = live(&["a1", "a2", "b1"]);
716 assert!(!o.refuse_early(b));
717 assert_eq!(o.admit("b2", b, Some(&all)), ok(&["b1"]));
718 assert_eq!(o.admit("c1", Some([3; 32]), None), ok(&["a1"]));
720 assert!(
721 o.refuse_early(Some([4; 32])),
722 "everyone holds one: a newcomer with none is refused"
723 );
724 }
725
726 #[test]
727 fn a_quiet_session_is_stale_not_gone() {
728 let o = SessionOwners::new(
729 Arc::new(LocalSessionManager::default()),
730 SessionLimits {
731 total: 10,
732 per_owner: 10,
733 idle: Duration::ZERO,
734 },
735 );
736 let who = Some([1; 32]);
737 o.admit("s", who, None);
738 assert_eq!(o.owned_by("s", who), Ownership::Stale);
739 assert_eq!(o.owned_by("s", Some([2; 32])), Ownership::NotOwner);
740 }
741
742 #[test]
743 fn origin_gets_explicit_default_port() {
744 assert_eq!(
745 origin_with_port("https://mail.example.com"),
746 "https://mail.example.com:443"
747 );
748 assert_eq!(
749 origin_with_port("http://localhost:8443"),
750 "http://localhost:8443"
751 );
752 assert_eq!(origin_with_port("http://localhost"), "http://localhost:80");
753 assert_eq!(origin_with_port("https://[::1]"), "https://[::1]:443");
754 assert_eq!(origin_with_port("https://[::1]:9000"), "https://[::1]:9000");
755 }
756}