hyper-mcp-remote 0.1.0

A stdio to streamable-http MCP proxy with OAuth support
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! Bidirectional MCP proxy between a local stdio server (this process's
//! stdin/stdout) and a remote Streamable-HTTP MCP server.
//!
//! The implementation closely mirrors `hyper-mcp-proxy`, but with the roles
//! inverted:
//!
//! ```text
//! stdio MCP client  ──stdin/stdout──▶  ProxyHandler (ServerHandler)
//!//!//!                                  Peer<RoleClient> ──HTTP──▶  Remote MCP server
//!//!//!                                  RemoteClientHandler (ClientHandler)
//! ```
//!
//! `ProxyHandler` implements [`rmcp::ServerHandler`] for the local stdio
//! connection and forwards every typed MCP call to the remote via a
//! [`Peer<RoleClient>`]. `RemoteClientHandler` implements
//! [`rmcp::ClientHandler`] for the remote connection and forwards reverse
//! traffic (sampling, elicitation, list_roots, log notifications, …) back to
//! the stdio client.
//!
//! Capability reflection: the result of [`ProxyHandler::initialize`] is the
//! upstream remote's own [`InitializeResult`] (with only `server_info`
//! patched to identify the proxy), so the local client sees exactly the
//! capabilities the remote advertises.

use std::sync::Mutex;

use anyhow::Result;
use rmcp::model::{
    CallToolRequestParams, CallToolResult, CancelledNotificationParam, ClientInfo,
    CompleteRequestParams, CompleteResult, CreateElicitationRequestParams, CreateElicitationResult,
    CreateMessageRequestParams, CreateMessageResult, ElicitationResponseNotificationParam,
    ErrorCode, ErrorData, GetPromptRequestParams, GetPromptResult, Implementation,
    InitializeRequestParams, InitializeResult, ListPromptsResult, ListResourceTemplatesResult,
    ListResourcesResult, ListRootsResult, ListToolsResult, LoggingMessageNotificationParam,
    PaginatedRequestParams, ProgressNotificationParam, ReadResourceRequestParams,
    ReadResourceResult, ResourceUpdatedNotificationParam, ServerCapabilities,
    SetLevelRequestParams, SubscribeRequestParams, UnsubscribeRequestParams,
};
use rmcp::service::{
    NotificationContext, Peer, RequestContext, RoleClient, RoleServer, RunningService, ServiceExt,
};
use rmcp::{ClientHandler, ServerHandler};
use tokio::sync::OnceCell;

use crate::transport::RemoteTransport;

// ---------------------------------------------------------------------------
// RemoteClientHandler — relays remote→client traffic back to the stdio client
// ---------------------------------------------------------------------------

/// A [`ClientHandler`] that runs against the remote MCP server connection
/// and forwards every server→client request and notification back to the
/// local stdio client via `upstream`.
pub struct RemoteClientHandler {
    upstream: Peer<RoleServer>,
    /// `ClientInfo` advertised to the remote during the outbound handshake.
    ///
    /// Built from the local stdio client's own `initialize` request so that
    /// the remote sees the *actual* client's protocol version and
    /// capabilities (sampling / elicitation / roots), not the rmcp default.
    /// `client_info.name` is suffixed with `(via hyper-mcp-remote)` so the
    /// remote can still tell traffic is being proxied.
    proxied_info: ClientInfo,
}

impl ClientHandler for RemoteClientHandler {
    fn get_info(&self) -> ClientInfo {
        self.proxied_info.clone()
    }
    // -- Requests originated by the remote server -------------------------

    #[tracing::instrument(skip_all, fields(dir = "remote←local"))]
    async fn create_message(
        &self,
        params: CreateMessageRequestParams,
        _ctx: RequestContext<RoleClient>,
    ) -> Result<CreateMessageResult, ErrorData> {
        self.upstream
            .create_message(params)
            .await
            .map_err(upstream_error)
    }

    #[tracing::instrument(skip_all, fields(dir = "remote←local"))]
    async fn list_roots(
        &self,
        _ctx: RequestContext<RoleClient>,
    ) -> Result<ListRootsResult, ErrorData> {
        self.upstream.list_roots().await.map_err(upstream_error)
    }

    #[tracing::instrument(skip_all, fields(dir = "remote←local"))]
    async fn create_elicitation(
        &self,
        request: CreateElicitationRequestParams,
        _ctx: RequestContext<RoleClient>,
    ) -> Result<CreateElicitationResult, ErrorData> {
        self.upstream
            .create_elicitation(request)
            .await
            .map_err(upstream_error)
    }

    // -- Notifications originated by the remote server --------------------

    #[tracing::instrument(skip_all, fields(dir = "remote→local"))]
    async fn on_cancelled(
        &self,
        params: CancelledNotificationParam,
        _ctx: NotificationContext<RoleClient>,
    ) {
        if let Err(e) = self.upstream.notify_cancelled(params).await {
            tracing::warn!(error = %e, "failed to forward cancelled to stdio client");
        }
    }

    /// Demoted to `debug` because progress notifications can fire many
    /// times per second during long-running tool calls.
    #[tracing::instrument(level = "debug", skip_all, fields(dir = "remote→local"))]
    async fn on_progress(
        &self,
        params: ProgressNotificationParam,
        _ctx: NotificationContext<RoleClient>,
    ) {
        if let Err(e) = self.upstream.notify_progress(params).await {
            tracing::warn!(error = %e, "failed to forward progress to stdio client");
        }
    }

    #[tracing::instrument(level = "debug", skip_all, fields(dir = "remote→local"))]
    async fn on_logging_message(
        &self,
        params: LoggingMessageNotificationParam,
        _ctx: NotificationContext<RoleClient>,
    ) {
        if let Err(e) = self.upstream.notify_logging_message(params).await {
            tracing::warn!(error = %e, "failed to forward logging message to stdio client");
        }
    }

    #[tracing::instrument(skip_all, fields(dir = "remote→local", uri = %params.uri))]
    async fn on_resource_updated(
        &self,
        params: ResourceUpdatedNotificationParam,
        _ctx: NotificationContext<RoleClient>,
    ) {
        if let Err(e) = self.upstream.notify_resource_updated(params).await {
            tracing::warn!(error = %e, "failed to forward resource_updated to stdio client");
        }
    }

    #[tracing::instrument(skip_all, fields(dir = "remote→local"))]
    async fn on_resource_list_changed(&self, _ctx: NotificationContext<RoleClient>) {
        if let Err(e) = self.upstream.notify_resource_list_changed().await {
            tracing::warn!(error = %e, "failed to forward resource_list_changed to stdio client");
        }
    }

    #[tracing::instrument(skip_all, fields(dir = "remote→local"))]
    async fn on_tool_list_changed(&self, _ctx: NotificationContext<RoleClient>) {
        if let Err(e) = self.upstream.notify_tool_list_changed().await {
            tracing::warn!(error = %e, "failed to forward tool_list_changed to stdio client");
        }
    }

    #[tracing::instrument(skip_all, fields(dir = "remote→local"))]
    async fn on_prompt_list_changed(&self, _ctx: NotificationContext<RoleClient>) {
        if let Err(e) = self.upstream.notify_prompt_list_changed().await {
            tracing::warn!(error = %e, "failed to forward prompt_list_changed to stdio client");
        }
    }

    #[tracing::instrument(skip_all, fields(dir = "remote→local"))]
    async fn on_url_elicitation_notification_complete(
        &self,
        params: ElicitationResponseNotificationParam,
        _ctx: NotificationContext<RoleClient>,
    ) {
        if let Err(e) = self.upstream.notify_url_elicitation_completed(params).await {
            tracing::warn!(error = %e, "failed to forward elicitation completion to stdio client");
        }
    }
}

// ---------------------------------------------------------------------------
// ProxyHandler — implements ServerHandler for the local stdio connection
// ---------------------------------------------------------------------------

/// State established once the remote connection is up.
struct Remote {
    /// Peer used to call methods on the remote MCP server.
    peer: Peer<RoleClient>,
    /// Initialization result advertised by the remote server.
    init_result: InitializeResult,
    /// Background task keeping the remote client service alive.
    _service_handle: tokio::task::JoinHandle<()>,
}

/// `ServerHandler` for the local stdio connection. Lazily connects to the
/// remote MCP server on the first `initialize` request and forwards every
/// subsequent message through.
pub struct ProxyHandler {
    /// `std::sync::Mutex` (not `tokio::sync::Mutex`) so that locking does not
    /// produce a non-`Send` guard across an `await` point: we only ever do a
    /// brief, synchronous `take()` while the connection is being established.
    transport: Mutex<Option<RemoteTransport>>,
    remote: OnceCell<Remote>,
}

impl ProxyHandler {
    /// Wrap a freshly built [`RemoteTransport`]. The remote connection is
    /// not opened until the local client sends `initialize`.
    pub fn new(transport: RemoteTransport) -> Self {
        Self {
            transport: Mutex::new(Some(transport)),
            remote: OnceCell::new(),
        }
    }

    /// Borrow the remote peer, returning a clear error if `initialize`
    /// hasn't completed yet.
    fn peer(&self) -> Result<&Peer<RoleClient>, ErrorData> {
        self.remote
            .get()
            .map(|r| &r.peer)
            .ok_or_else(|| internal_error("proxy session not yet initialized"))
    }

    /// Connect to the remote MCP server, performing the MCP handshake on
    /// behalf of the local stdio client. `local_init` is the parameters of
    /// the local client's own `initialize` request so the proxy can forward
    /// the client's true capability set to the remote.
    #[tracing::instrument(skip_all)]
    async fn connect(
        &self,
        upstream: Peer<RoleServer>,
        local_init: InitializeRequestParams,
    ) -> Result<&Remote, ErrorData> {
        self.remote
            .get_or_try_init(|| async {
                let transport = {
                    let mut guard = self
                        .transport
                        .lock()
                        .map_err(|_| internal_error("transport mutex poisoned"))?;
                    guard
                        .take()
                        .ok_or_else(|| internal_error("remote transport already consumed"))?
                };

                let handler = RemoteClientHandler {
                    upstream,
                    proxied_info: build_proxied_client_info(local_init),
                };

                let running: RunningService<RoleClient, RemoteClientHandler> = match transport {
                    RemoteTransport::Anonymous(t) => {
                        handler.serve(t).await.map_err(remote_error)?
                    }
                    RemoteTransport::Authorized(t) => {
                        handler.serve(t).await.map_err(remote_error)?
                    }
                };

                let peer = running.peer().clone();

                // Capability reflection: present whatever the remote sent us
                // on `initialize`, but identify the proxy as the server
                // implementation so a curious client can tell the difference.
                let mut init_result = running
                    .peer_info()
                    .cloned()
                    .unwrap_or_else(|| InitializeResult::new(ServerCapabilities::default()));
                init_result.server_info =
                    Implementation::new("hyper-mcp-remote", env!("CARGO_PKG_VERSION"));

                let handle = tokio::spawn(async move {
                    if let Err(e) = running.waiting().await {
                        tracing::warn!(error = %e, "remote MCP service ended with error");
                    } else {
                        tracing::info!("remote MCP service ended");
                    }
                });

                Ok::<_, ErrorData>(Remote {
                    peer,
                    init_result,
                    _service_handle: handle,
                })
            })
            .await
    }
}

// ---------------------------------------------------------------------------
// ServerHandler implementation — forward every typed call to the remote
// ---------------------------------------------------------------------------

impl ServerHandler for ProxyHandler {
    #[tracing::instrument(
        skip_all,
        fields(
            dir = "local→remote",
            client = %request.client_info.name,
            client_version = %request.client_info.version,
            protocol = ?request.protocol_version,
        )
    )]
    async fn initialize(
        &self,
        request: InitializeRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<InitializeResult, ErrorData> {
        let remote = self.connect(context.peer.clone(), request).await?;
        Ok(remote.init_result.clone())
    }

    // -- Tools -----------------------------------------------------------

    #[tracing::instrument(skip_all, fields(dir = "local→remote"))]
    async fn list_tools(
        &self,
        request: Option<PaginatedRequestParams>,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, ErrorData> {
        self.peer()?.list_tools(request).await.map_err(remote_error)
    }

    #[tracing::instrument(skip_all, fields(dir = "local→remote", tool = %request.name))]
    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<CallToolResult, ErrorData> {
        self.peer()?.call_tool(request).await.map_err(remote_error)
    }

    // -- Resources -------------------------------------------------------

    #[tracing::instrument(skip_all, fields(dir = "local→remote"))]
    async fn list_resources(
        &self,
        request: Option<PaginatedRequestParams>,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<ListResourcesResult, ErrorData> {
        self.peer()?
            .list_resources(request)
            .await
            .map_err(remote_error)
    }

    #[tracing::instrument(skip_all, fields(dir = "local→remote"))]
    async fn list_resource_templates(
        &self,
        request: Option<PaginatedRequestParams>,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<ListResourceTemplatesResult, ErrorData> {
        self.peer()?
            .list_resource_templates(request)
            .await
            .map_err(remote_error)
    }

    #[tracing::instrument(skip_all, fields(dir = "local→remote", uri = %request.uri))]
    async fn read_resource(
        &self,
        request: ReadResourceRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<ReadResourceResult, ErrorData> {
        self.peer()?
            .read_resource(request)
            .await
            .map_err(remote_error)
    }

    #[tracing::instrument(skip_all, fields(dir = "local→remote", uri = %request.uri))]
    async fn subscribe(
        &self,
        request: SubscribeRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<(), ErrorData> {
        self.peer()?.subscribe(request).await.map_err(remote_error)
    }

    #[tracing::instrument(skip_all, fields(dir = "local→remote", uri = %request.uri))]
    async fn unsubscribe(
        &self,
        request: UnsubscribeRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<(), ErrorData> {
        self.peer()?
            .unsubscribe(request)
            .await
            .map_err(remote_error)
    }

    // -- Prompts ---------------------------------------------------------

    #[tracing::instrument(skip_all, fields(dir = "local→remote"))]
    async fn list_prompts(
        &self,
        request: Option<PaginatedRequestParams>,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<ListPromptsResult, ErrorData> {
        self.peer()?
            .list_prompts(request)
            .await
            .map_err(remote_error)
    }

    #[tracing::instrument(skip_all, fields(dir = "local→remote", prompt = %request.name))]
    async fn get_prompt(
        &self,
        request: GetPromptRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<GetPromptResult, ErrorData> {
        self.peer()?.get_prompt(request).await.map_err(remote_error)
    }

    // -- Completions -----------------------------------------------------

    #[tracing::instrument(skip_all, fields(dir = "local→remote"))]
    async fn complete(
        &self,
        request: CompleteRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<CompleteResult, ErrorData> {
        self.peer()?.complete(request).await.map_err(remote_error)
    }

    // -- Logging ---------------------------------------------------------

    #[tracing::instrument(skip_all, fields(dir = "local→remote", level = ?request.level))]
    async fn set_level(
        &self,
        request: SetLevelRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<(), ErrorData> {
        self.peer()?.set_level(request).await.map_err(remote_error)
    }

    // -- Notifications (local stdio client → remote) ----------------------

    #[tracing::instrument(skip_all, fields(dir = "local→remote"))]
    async fn on_cancelled(
        &self,
        notification: CancelledNotificationParam,
        _ctx: NotificationContext<RoleServer>,
    ) {
        if let Ok(peer) = self.peer()
            && let Err(e) = peer.notify_cancelled(notification).await
        {
            tracing::warn!(error = %e, "failed to forward cancellation to remote");
        }
    }

    /// Demoted to `debug` because progress notifications can fire many
    /// times per second during long-running tool calls.
    #[tracing::instrument(level = "debug", skip_all, fields(dir = "local→remote"))]
    async fn on_progress(
        &self,
        notification: ProgressNotificationParam,
        _ctx: NotificationContext<RoleServer>,
    ) {
        if let Ok(peer) = self.peer()
            && let Err(e) = peer.notify_progress(notification).await
        {
            tracing::warn!(error = %e, "failed to forward progress to remote");
        }
    }

    #[tracing::instrument(skip_all, fields(dir = "local→remote"))]
    async fn on_initialized(&self, _ctx: NotificationContext<RoleServer>) {
        // The rmcp client we manage internally sends its own `initialized`
        // notification to the remote during `serve(...)`. We deliberately do
        // not forward the local client's `initialized` again.
        tracing::debug!("local client sent initialized");
    }

    #[tracing::instrument(skip_all, fields(dir = "local→remote"))]
    async fn on_roots_list_changed(&self, _ctx: NotificationContext<RoleServer>) {
        if let Ok(peer) = self.peer()
            && let Err(e) = peer.notify_roots_list_changed().await
        {
            tracing::warn!(error = %e, "failed to forward roots_list_changed to remote");
        }
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Build the [`ClientInfo`] the proxy will use when initializing the remote
/// MCP server connection.
///
/// We propagate the **local client's** capabilities and protocol version
/// verbatim, only rewriting `client_info.name` so the remote can see traffic
/// is being proxied (e.g. `"Claude Desktop (via hyper-mcp-remote 0.1.0)"`).
/// This mirrors `mcp-remote`'s name-suffixing behavior and ensures the
/// remote will continue to send sampling / elicitation / roots requests if
/// (and only if) the local client actually supports them.
fn build_proxied_client_info(mut local: InitializeRequestParams) -> ClientInfo {
    const PROXY_TAG: &str = concat!(" (via hyper-mcp-remote ", env!("CARGO_PKG_VERSION"), ")");
    local.client_info.name.push_str(PROXY_TAG);
    // `InitializeRequestParams` and `ClientInfo` are the same struct under
    // a different alias in rmcp 1.7.
    local
}

fn internal_error(msg: impl Into<String>) -> ErrorData {
    ErrorData::new(ErrorCode::INTERNAL_ERROR, msg.into(), None)
}

fn remote_error(e: impl std::fmt::Display) -> ErrorData {
    ErrorData::new(
        ErrorCode::INTERNAL_ERROR,
        format!("remote MCP server error: {e}"),
        None,
    )
}

fn upstream_error(e: impl std::fmt::Display) -> ErrorData {
    ErrorData::new(
        ErrorCode::INTERNAL_ERROR,
        format!("local stdio client error: {e}"),
        None,
    )
}

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

    #[test]
    fn proxied_client_info_preserves_capabilities_and_tags_name() {
        let mut input = InitializeRequestParams::default();
        input.client_info.name = "Test Client".to_string();
        input.client_info.version = "9.9.9".to_string();
        input.protocol_version = ProtocolVersion::default();

        let out = build_proxied_client_info(input.clone());

        assert_eq!(out.client_info.version, "9.9.9");
        assert!(
            out.client_info.name.starts_with("Test Client"),
            "name should keep the original prefix; got {:?}",
            out.client_info.name
        );
        assert!(
            out.client_info.name.contains("hyper-mcp-remote"),
            "name should be suffixed with proxy identity; got {:?}",
            out.client_info.name
        );
        assert_eq!(
            out.capabilities, input.capabilities,
            "client capabilities must be forwarded verbatim"
        );
        assert_eq!(out.protocol_version, input.protocol_version);
    }

    #[test]
    fn helper_error_codes_are_internal_error() {
        let e1 = internal_error("x");
        let e2 = remote_error("y");
        let e3 = upstream_error("z");
        assert_eq!(e1.code, ErrorCode::INTERNAL_ERROR);
        assert_eq!(e2.code, ErrorCode::INTERNAL_ERROR);
        assert_eq!(e3.code, ErrorCode::INTERNAL_ERROR);
    }

    #[test]
    fn remote_and_upstream_have_distinct_prefixes() {
        assert!(remote_error("boom").message.contains("remote MCP server"));
        assert!(
            upstream_error("boom")
                .message
                .contains("local stdio client")
        );
    }

    // Silence "unused" warning if the type is only used through trait objects.
}