mcp-streamable-proxy 0.2.7

Streamable HTTP proxy implementation for MCP protocol using rmcp 3.1
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Backend client handler that bridges server notifications to upstream peers.
//!
//! Two isolation modes share one [`BackendNotificationBridge`] type so
//! [`crate::proxy_handler::BackendRunningService`] stays a single concrete type:
//!
//! - **Shared** ([`NotifyTarget::Shared`]): one backend, fan-out / progress·task
//!   routing via [`UpstreamPeerRegistry`] (stdio proxy).
//! - **Per-session** ([`NotifyTarget::Session`]): one backend connection owns one
//!   upstream [`Peer`](Peer); notifications are 1:1 (URL proxy).

use dashmap::DashMap;
use futures::future::join_all;
use rmcp::{
    ClientHandler, RoleClient, RoleServer,
    model::{
        ClientCapabilities, ClientInfo, Implementation, ProgressNotificationParam, ProgressToken,
        ResourceUpdatedNotificationParam, ServerNotification, TaskStatus, TaskStatusNotification,
        TaskStatusNotificationParams,
    },
    service::{NotificationContext, Peer, ServiceError},
};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, RwLock};
use std::time::Duration;
use tracing::{debug, warn};

/// Delay before unregistering a progress route after the owning request finishes.
///
/// Gives in-flight backend progress notifications a short window to arrive after
/// the JSON-RPC response has already been returned to the upstream client.
pub const PROGRESS_ROUTE_GRACE: Duration = Duration::from_millis(500);

/// Slot for the single upstream peer in per-session isolation mode.
#[derive(Clone, Default, Debug)]
pub struct UpstreamPeerSlot {
    peer: Arc<RwLock<Option<Peer<RoleServer>>>>,
}

impl UpstreamPeerSlot {
    /// Create an empty slot.
    pub fn new() -> Self {
        Self::default()
    }

    /// Bind the upstream session peer (typically from `on_initialized`).
    pub fn set(&self, peer: Peer<RoleServer>) {
        match self.peer.write() {
            Ok(mut guard) => *guard = Some(peer),
            Err(poisoned) => *poisoned.into_inner() = Some(peer),
        }
    }

    /// Clone the bound peer, if any.
    pub fn get(&self) -> Option<Peer<RoleServer>> {
        match self.peer.read() {
            Ok(guard) => guard.clone(),
            Err(poisoned) => poisoned.into_inner().clone(),
        }
    }

    /// Clear the slot (session closing).
    pub fn clear(&self) {
        match self.peer.write() {
            Ok(mut guard) => *guard = None,
            Err(poisoned) => *poisoned.into_inner() = None,
        }
    }
}

/// How backend notifications are delivered to upstream Streamable HTTP sessions.
#[derive(Clone, Debug)]
pub enum NotifyTarget {
    /// Shared backend: fan-out list-changed / logging; route progress/task.
    Shared(Arc<UpstreamPeerRegistry>),
    /// Per-session backend: deliver only to this session's upstream peer.
    Session(UpstreamPeerSlot),
}

/// Registry of upstream session peers that should receive backend notifications.
#[derive(Debug, Default)]
pub struct UpstreamPeerRegistry {
    next_id: AtomicU64,
    peers: DashMap<u64, Peer<RoleServer>>,
    /// progressToken → upstream peer (request-scoped; not broadcast)
    progress_routes: DashMap<ProgressToken, Peer<RoleServer>>,
    /// task_id → upstream peer (task-scoped; not broadcast)
    task_routes: DashMap<String, Peer<RoleServer>>,
}

impl UpstreamPeerRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Register an upstream peer (typically from `ServerHandler::on_initialized`).
    pub fn register(&self, peer: Peer<RoleServer>) -> u64 {
        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
        self.peers.insert(id, peer);
        id
    }

    /// Explicitly remove a registered peer by id.
    pub fn unregister(&self, id: u64) {
        self.peers.remove(&id);
    }

    /// Number of registered peers (for diagnostics).
    pub fn len(&self) -> usize {
        self.peers.len()
    }

    /// Whether the registry has no peers.
    pub fn is_empty(&self) -> bool {
        self.peers.is_empty()
    }

    /// Bind a progress token to the upstream peer that owns the request.
    pub fn register_progress(&self, token: ProgressToken, peer: Peer<RoleServer>) {
        self.progress_routes.insert(token, peer);
    }

    /// Remove a progress-token route (request finished or cancelled).
    pub fn unregister_progress(&self, token: &ProgressToken) {
        self.progress_routes.remove(token);
    }

    /// Bind a task id to the upstream peer that created/enqueued the task.
    pub fn register_task(&self, task_id: impl Into<String>, peer: Peer<RoleServer>) {
        self.task_routes.insert(task_id.into(), peer);
    }

    /// Remove a task-id route.
    pub fn unregister_task(&self, task_id: &str) {
        self.task_routes.remove(task_id);
    }

    /// Drop peers whose transport is already closed (eager cleanup on session close).
    pub fn reap_closed_peers(&self) {
        self.peers.retain(|_, peer| !peer.is_transport_closed());
    }

    /// Deliver a progress notification to the owning upstream peer only.
    pub async fn deliver_progress(&self, params: ProgressNotificationParam) {
        let token = params.progress_token.clone();
        let Some(peer) = self
            .progress_routes
            .get(&token)
            .map(|entry| entry.value().clone())
        else {
            debug!(
                ?token,
                "No progress route for token; dropping backend progress notification"
            );
            return;
        };

        if peer.is_transport_closed() {
            self.progress_routes.remove(&token);
            return;
        }

        if let Err(err) = peer.notify_progress(params).await {
            warn!(?token, error = ?err, "Failed to deliver progress; removing route");
            self.progress_routes.remove(&token);
        }
    }

    /// Deliver a task-status notification to the owning upstream peer only.
    pub async fn deliver_task_status(&self, params: TaskStatusNotificationParams) {
        let task_id = params.task.task.task_id.clone();
        let terminal = matches!(
            params.task.status(),
            TaskStatus::Completed | TaskStatus::Failed | TaskStatus::Cancelled
        );

        let Some(peer) = self.task_routes.get(&task_id).map(|e| e.value().clone()) else {
            debug!(
                %task_id,
                "No task route for id; dropping backend task status notification"
            );
            return;
        };

        if peer.is_transport_closed() {
            self.task_routes.remove(&task_id);
            return;
        }

        let send_result = peer
            .send_notification(ServerNotification::TaskStatusNotification(
                TaskStatusNotification::new(params),
            ))
            .await;

        if let Err(err) = send_result {
            warn!(%task_id, error = ?err, "Failed to deliver task status; removing route");
            self.task_routes.remove(&task_id);
            return;
        }

        if terminal {
            self.task_routes.remove(&task_id);
        }
    }

    /// Fan-out a notification to all peers; remove peers that fail to send.
    pub async fn fan_out<F, Fut>(&self, mut send: F)
    where
        F: FnMut(Peer<RoleServer>) -> Fut,
        Fut: std::future::Future<Output = Result<(), ServiceError>>,
    {
        let snapshot: Vec<(u64, Peer<RoleServer>)> = self
            .peers
            .iter()
            .map(|entry| (*entry.key(), entry.value().clone()))
            .collect();

        let mut dead = Vec::new();
        let mut live = Vec::new();
        for (id, peer) in snapshot {
            if peer.is_transport_closed() {
                dead.push(id);
            } else {
                live.push((id, peer));
            }
        }

        let results = join_all(live.into_iter().map(|(id, peer)| {
            let fut = send(peer);
            async move { (id, fut.await) }
        }))
        .await;

        for (id, result) in results {
            if let Err(err) = result {
                warn!(
                    peer_id = id,
                    error = ?err,
                    "Failed to fan-out notification to upstream peer; removing"
                );
                dead.push(id);
            }
        }

        for id in dead {
            self.peers.remove(&id);
        }
    }
}

/// RAII guard that unregisters a progress route when dropped (after a short grace).
pub struct ProgressRouteGuard {
    registry: Arc<UpstreamPeerRegistry>,
    token: Option<ProgressToken>,
    grace: Duration,
}

impl ProgressRouteGuard {
    /// Register `token` → `peer` and return a guard that cleans up on drop.
    pub fn register(
        registry: Arc<UpstreamPeerRegistry>,
        token: ProgressToken,
        peer: Peer<RoleServer>,
    ) -> Self {
        registry.register_progress(token.clone(), peer);
        Self {
            registry,
            token: Some(token),
            grace: PROGRESS_ROUTE_GRACE,
        }
    }

    /// Register from request meta if a progress token is present.
    pub fn try_from_meta(
        registry: Arc<UpstreamPeerRegistry>,
        meta: &rmcp::model::RequestMetaObject,
        peer: Peer<RoleServer>,
    ) -> Option<Self> {
        let token = meta.get_progress_token()?;
        Some(Self::register(registry, token, peer))
    }
}

impl Drop for ProgressRouteGuard {
    fn drop(&mut self) {
        let Some(token) = self.token.take() else {
            return;
        };
        let registry = Arc::clone(&self.registry);
        let grace = self.grace;
        // Prefer delayed unregister so late progress can still be routed.
        // Fall back to immediate cleanup outside a Tokio runtime.
        match tokio::runtime::Handle::try_current() {
            Ok(handle) => {
                handle.spawn(async move {
                    tokio::time::sleep(grace).await;
                    registry.unregister_progress(&token);
                });
            }
            Err(_) => {
                registry.unregister_progress(&token);
            }
        }
    }
}

/// Client-side handler used when the proxy connects to a backend MCP server.
#[derive(Clone, Debug)]
pub struct BackendNotificationBridge {
    info: ClientInfo,
    target: NotifyTarget,
}

impl BackendNotificationBridge {
    /// Create a bridge with the given client info and notify target.
    pub fn new(info: ClientInfo, target: NotifyTarget) -> Self {
        Self { info, target }
    }

    /// Shared-backend bridge (fan-out / routed progress).
    pub fn with_default_info(upstream_peers: Arc<UpstreamPeerRegistry>) -> Self {
        Self::new(
            default_backend_client_info(),
            NotifyTarget::Shared(upstream_peers),
        )
    }

    /// Per-session bridge (1:1 notify to [`UpstreamPeerSlot`]).
    pub fn for_session(slot: UpstreamPeerSlot) -> Self {
        Self::new(default_backend_client_info(), NotifyTarget::Session(slot))
    }

    /// Shared upstream peer registry when target is [`NotifyTarget::Shared`].
    pub fn upstream_peers(&self) -> Option<&Arc<UpstreamPeerRegistry>> {
        match &self.target {
            NotifyTarget::Shared(reg) => Some(reg),
            NotifyTarget::Session(_) => None,
        }
    }

    /// Session peer slot when target is [`NotifyTarget::Session`].
    pub fn session_slot(&self) -> Option<&UpstreamPeerSlot> {
        match &self.target {
            NotifyTarget::Session(slot) => Some(slot),
            NotifyTarget::Shared(_) => None,
        }
    }
}

/// Default [`ClientInfo`] for proxy→backend handshake (enables task notifications).
pub fn default_backend_client_info() -> ClientInfo {
    let capabilities = ClientCapabilities::builder().enable_tasks().build();
    ClientInfo::new(
        capabilities,
        Implementation::new("mcp-streamable-proxy-client", env!("CARGO_PKG_VERSION")),
    )
}

async fn with_session_peer<F, Fut>(slot: &UpstreamPeerSlot, send: F)
where
    F: FnOnce(Peer<RoleServer>) -> Fut,
    Fut: std::future::Future<Output = Result<(), ServiceError>>,
{
    let Some(peer) = slot.get() else {
        debug!("Session notify slot empty; dropping backend notification");
        return;
    };
    if peer.is_transport_closed() {
        slot.clear();
        return;
    }
    if let Err(err) = send(peer).await {
        warn!(error = ?err, "Failed to deliver notification to session peer");
        slot.clear();
    }
}

impl ClientHandler for BackendNotificationBridge {
    fn get_info(&self) -> ClientInfo {
        self.info.clone()
    }

    async fn on_resource_updated(
        &self,
        params: ResourceUpdatedNotificationParam,
        _context: NotificationContext<RoleClient>,
    ) {
        match &self.target {
            NotifyTarget::Shared(reg) => {
                debug!(uri = %params.uri, "Fan-out resources/updated to upstream peers");
                reg.fan_out(|peer| {
                    let params = params.clone();
                    async move { peer.notify_resource_updated(params).await }
                })
                .await;
            }
            NotifyTarget::Session(slot) => {
                with_session_peer(slot, |peer| async move {
                    peer.notify_resource_updated(params).await
                })
                .await;
            }
        }
    }

    async fn on_resource_list_changed(&self, _context: NotificationContext<RoleClient>) {
        match &self.target {
            NotifyTarget::Shared(reg) => {
                reg.fan_out(|peer| async move { peer.notify_resource_list_changed().await })
                    .await;
            }
            NotifyTarget::Session(slot) => {
                with_session_peer(slot, |peer| async move {
                    peer.notify_resource_list_changed().await
                })
                .await;
            }
        }
    }

    async fn on_tool_list_changed(&self, _context: NotificationContext<RoleClient>) {
        match &self.target {
            NotifyTarget::Shared(reg) => {
                reg.fan_out(|peer| async move { peer.notify_tool_list_changed().await })
                    .await;
            }
            NotifyTarget::Session(slot) => {
                with_session_peer(
                    slot,
                    |peer| async move { peer.notify_tool_list_changed().await },
                )
                .await;
            }
        }
    }

    async fn on_prompt_list_changed(&self, _context: NotificationContext<RoleClient>) {
        match &self.target {
            NotifyTarget::Shared(reg) => {
                reg.fan_out(|peer| async move { peer.notify_prompt_list_changed().await })
                    .await;
            }
            NotifyTarget::Session(slot) => {
                with_session_peer(slot, |peer| async move {
                    peer.notify_prompt_list_changed().await
                })
                .await;
            }
        }
    }

    #[allow(deprecated)]
    async fn on_logging_message(
        &self,
        params: rmcp::model::LoggingMessageNotificationParam,
        _context: NotificationContext<RoleClient>,
    ) {
        match &self.target {
            NotifyTarget::Shared(reg) => {
                reg.fan_out(|peer| {
                    let params = params.clone();
                    async move { peer.notify_logging_message(params).await }
                })
                .await;
            }
            NotifyTarget::Session(slot) => {
                with_session_peer(slot, |peer| async move {
                    peer.notify_logging_message(params).await
                })
                .await;
            }
        }
    }

    async fn on_task_status(
        &self,
        params: TaskStatusNotificationParams,
        _context: NotificationContext<RoleClient>,
    ) {
        match &self.target {
            NotifyTarget::Shared(reg) => {
                reg.deliver_task_status(params).await;
            }
            NotifyTarget::Session(slot) => {
                with_session_peer(slot, |peer| async move {
                    peer.send_notification(ServerNotification::TaskStatusNotification(
                        TaskStatusNotification::new(params),
                    ))
                    .await
                })
                .await;
            }
        }
    }

    async fn on_progress(
        &self,
        params: ProgressNotificationParam,
        _context: NotificationContext<RoleClient>,
    ) {
        match &self.target {
            NotifyTarget::Shared(reg) => {
                reg.deliver_progress(params).await;
            }
            NotifyTarget::Session(slot) => {
                with_session_peer(
                    slot,
                    |peer| async move { peer.notify_progress(params).await },
                )
                .await;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn progress_route_register_unregister() {
        let registry = UpstreamPeerRegistry::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
        registry.unregister(999);
        registry.unregister_progress(&ProgressToken(rmcp::model::NumberOrString::Number(1)));
        registry.unregister_task("nope");
    }

    #[test]
    fn upstream_peer_slot_set_clear() {
        let slot = UpstreamPeerSlot::new();
        assert!(slot.get().is_none());
        slot.clear();
        assert!(slot.get().is_none());
    }
}