contextvm-sdk 0.1.1

Rust SDK for the ContextVM protocol — MCP over Nostr
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
//! rmcp worker adapters.
//!
//! This file defines wrapper types that bind existing ContextVM Nostr
//! transports to rmcp's worker abstraction.

use crate::core::error::Result;
use crate::core::types::{JsonRpcMessage, JsonRpcNotification, JsonRpcRequest};
use crate::transport::client::{NostrClientTransport, NostrClientTransportConfig};
use crate::transport::server::{NostrServerTransport, NostrServerTransportConfig};
use rmcp::transport::worker::{Worker, WorkerContext, WorkerQuitReason};
use std::collections::HashSet;

use super::convert::{
    internal_to_rmcp_client_rx, internal_to_rmcp_server_rx, rmcp_client_tx_to_internal,
    rmcp_server_tx_to_internal,
};

const LOG_TARGET: &str = "contextvm_sdk::rmcp_transport::worker";
const STATELESS_SYNTHETIC_EVENT_ID: &str = "contextvm-stateless-init";

fn synthetic_initialize_message() -> JsonRpcMessage {
    JsonRpcMessage::Request(JsonRpcRequest {
        jsonrpc: "2.0".to_string(),
        id: serde_json::json!(STATELESS_SYNTHETIC_EVENT_ID),
        method: "initialize".to_string(),
        params: Some(serde_json::json!({
            "protocolVersion": crate::core::constants::mcp_protocol_version(),
            "capabilities": {},
            "clientInfo": {
                "name": "contextvm-stateless-client",
                "version": "0.1.0"
            }
        })),
    })
}

fn synthetic_initialized_notification() -> JsonRpcMessage {
    JsonRpcMessage::Notification(JsonRpcNotification {
        jsonrpc: "2.0".to_string(),
        method: "notifications/initialized".to_string(),
        params: None,
    })
}

fn should_inject_stateless_bootstrap(
    initialized_clients: &HashSet<String>,
    client_pubkey: &str,
    message: &JsonRpcMessage,
) -> bool {
    if initialized_clients.contains(client_pubkey) {
        return false;
    }

    matches!(message, JsonRpcMessage::Request(req) if req.method != "initialize")
}

fn is_synthetic_initialize_message(message: &JsonRpcMessage) -> bool {
    matches!(
        message,
        JsonRpcMessage::Request(req)
            if req.method == "initialize"
                && req.id == serde_json::json!(STATELESS_SYNTHETIC_EVENT_ID)
    )
}

/// rmcp server worker wrapper for ContextVM Nostr server transport.
///
/// Multiplexes all connected clients through a single rmcp service instance.
/// Inbound requests have their JSON-RPC `id` rewritten to the Nostr `event_id`
/// before being forwarded to the rmcp handler.  Since event IDs are globally
/// unique (SHA-256 hashes), this eliminates collisions when different clients
/// use the same JSON-RPC request IDs.  The transport's event-route store
/// handles response routing back to the originating client; server-initiated
/// notifications are broadcast to all initialized clients.
pub struct NostrServerWorker {
    transport: NostrServerTransport,
}

impl NostrServerWorker {
    /// Create a new server worker from existing server transport config.
    pub async fn new<T>(signer: T, config: NostrServerTransportConfig) -> Result<Self>
    where
        T: nostr_sdk::prelude::IntoNostrSigner,
    {
        let transport = NostrServerTransport::new(signer, config).await?;
        Ok(Self { transport })
    }

    /// Create a worker from an already-constructed raw transport.
    pub fn from_transport(transport: NostrServerTransport) -> Self {
        Self { transport }
    }

    /// Access the wrapped transport.
    pub fn transport(&self) -> &NostrServerTransport {
        &self.transport
    }
}

impl Worker for NostrServerWorker {
    type Error = crate::core::error::Error;
    type Role = rmcp::RoleServer;

    fn err_closed() -> Self::Error {
        Self::Error::Transport("rmcp worker channel closed".to_string())
    }

    fn err_join(e: tokio::task::JoinError) -> Self::Error {
        Self::Error::Other(format!("rmcp worker join error: {e}"))
    }

    async fn run(
        mut self,
        mut context: WorkerContext<Self>,
    ) -> std::result::Result<(), WorkerQuitReason<Self::Error>> {
        self.transport
            .start()
            .await
            .map_err(WorkerQuitReason::fatal_context("starting server transport"))?;

        let mut rx = self.transport.take_message_receiver().ok_or_else(|| {
            WorkerQuitReason::fatal(
                Self::Error::Other("server message receiver already taken".to_string()),
                "taking server message receiver",
            )
        })?;

        let cancellation_token = context.cancellation_token.clone();
        let mut initialized_clients = HashSet::new();

        let quit_reason = loop {
            tokio::select! {
                _ = cancellation_token.cancelled() => {
                    break WorkerQuitReason::Cancelled;
                }
                incoming = rx.recv() => {
                    let Some(incoming) = incoming else {
                        break WorkerQuitReason::TransportClosed;
                    };

                    let crate::transport::server::IncomingRequest {
                        mut message,
                        event_id,
                        client_pubkey,
                        ..
                    } = incoming;

                    let should_inject_bootstrap = should_inject_stateless_bootstrap(
                        &initialized_clients,
                        &client_pubkey,
                        &message,
                    );

                    if should_inject_bootstrap {
                        let synthetic_init = synthetic_initialize_message();
                        let Some(rmcp_init) = internal_to_rmcp_server_rx(&synthetic_init) else {
                            break WorkerQuitReason::fatal(
                                Self::Error::Validation(
                                    "failed converting synthetic initialize request to rmcp format".to_string(),
                                ),
                                "converting synthetic initialize request",
                            );
                        };

                        if let Err(reason) = context.send_to_handler(rmcp_init).await {
                            break reason;
                        }

                        let initialized = synthetic_initialized_notification();
                        let Some(rmcp_initialized) = internal_to_rmcp_server_rx(&initialized) else {
                            break WorkerQuitReason::fatal(
                                Self::Error::Validation(
                                    "failed converting synthetic initialized notification to rmcp format".to_string(),
                                ),
                                "converting synthetic initialized notification",
                            );
                        };

                        if let Err(reason) = context.send_to_handler(rmcp_initialized).await {
                            break reason;
                        }

                        initialized_clients.insert(client_pubkey.clone());
                    }

                    if matches!(&message, JsonRpcMessage::Request(req) if req.method == "initialize")
                        || matches!(&message, JsonRpcMessage::Notification(n) if n.method == "notifications/initialized")
                    {
                        initialized_clients.insert(client_pubkey.clone());
                    }

                    // Rewrite real wire requests to the Nostr event_id.
                    // Synthetic stateless bootstrap messages must retain their
                    // sentinel ID so their responses can be dropped before they
                    // ever touch transport correlation.
                    if !is_synthetic_initialize_message(&message) {
                        if let JsonRpcMessage::Request(ref mut req) = message {
                        req.id = serde_json::json!(event_id);
                        }
                    }

                    if let Some(rmcp_msg) = internal_to_rmcp_server_rx(&message) {
                        if let Err(reason) = context.send_to_handler(rmcp_msg).await {
                            break reason;
                        }
                    } else {
                        tracing::warn!(
                            target: LOG_TARGET,
                            "Failed to convert incoming server-side message to rmcp format"
                        );
                    }
                }
                outbound = context.recv_from_handler() => {
                    let outbound = match outbound {
                        Ok(outbound) => outbound,
                        Err(reason) => break reason,
                    };

                    let result = if let Some(internal_msg) = rmcp_server_tx_to_internal(outbound.message) {
                        self.forward_server_internal(internal_msg).await
                    } else {
                        Err(Self::Error::Validation(
                            "failed converting rmcp server message to internal JSON-RPC".to_string(),
                        ))
                    };

                    let _ = outbound.responder.send(result);
                }
            }
        };

        if let Err(e) = self.transport.close().await {
            tracing::warn!(
                target: LOG_TARGET,
                error = %e,
                "Failed to close server transport cleanly"
            );
        }

        Err(quit_reason)
    }
}

/// rmcp client worker wrapper for ContextVM Nostr client transport.
pub struct NostrClientWorker {
    transport: NostrClientTransport,
}

impl NostrClientWorker {
    /// Create a new client worker from existing client transport config.
    pub async fn new<T>(signer: T, config: NostrClientTransportConfig) -> Result<Self>
    where
        T: nostr_sdk::prelude::IntoNostrSigner,
    {
        let transport = NostrClientTransport::new(signer, config).await?;
        Ok(Self { transport })
    }

    /// Create a worker from an already-constructed raw transport.
    pub fn from_transport(transport: NostrClientTransport) -> Self {
        Self { transport }
    }

    /// Access the wrapped transport.
    pub fn transport(&self) -> &NostrClientTransport {
        &self.transport
    }
}

impl Worker for NostrClientWorker {
    type Error = crate::core::error::Error;
    type Role = rmcp::RoleClient;

    fn err_closed() -> Self::Error {
        Self::Error::Transport("rmcp worker channel closed".to_string())
    }

    fn err_join(e: tokio::task::JoinError) -> Self::Error {
        Self::Error::Other(format!("rmcp worker join error: {e}"))
    }

    async fn run(
        mut self,
        mut context: WorkerContext<Self>,
    ) -> std::result::Result<(), WorkerQuitReason<Self::Error>> {
        self.transport
            .start()
            .await
            .map_err(WorkerQuitReason::fatal_context("starting client transport"))?;

        let mut rx = self.transport.take_message_receiver().ok_or_else(|| {
            WorkerQuitReason::fatal(
                Self::Error::Other("client message receiver already taken".to_string()),
                "taking client message receiver",
            )
        })?;

        let cancellation_token = context.cancellation_token.clone();

        let quit_reason = loop {
            tokio::select! {
                _ = cancellation_token.cancelled() => {
                    break WorkerQuitReason::Cancelled;
                }
                incoming = rx.recv() => {
                    let Some(incoming) = incoming else {
                        break WorkerQuitReason::TransportClosed;
                    };

                    if let Some(rmcp_msg) = internal_to_rmcp_client_rx(&incoming) {
                        if let Err(reason) = context.send_to_handler(rmcp_msg).await {
                            break reason;
                        }
                    } else {
                        tracing::warn!(
                            target: LOG_TARGET,
                            "Failed to convert incoming client-side message to rmcp format"
                        );
                    }
                }
                outbound = context.recv_from_handler() => {
                    let outbound = match outbound {
                        Ok(outbound) => outbound,
                        Err(reason) => break reason,
                    };

                    let result = if let Some(internal_msg) = rmcp_client_tx_to_internal(outbound.message) {
                        self.transport.send(&internal_msg).await
                    } else {
                        Err(Self::Error::Validation(
                            "failed converting rmcp client message to internal JSON-RPC".to_string(),
                        ))
                    };

                    let _ = outbound.responder.send(result);
                }
            }
        };

        if let Err(e) = self.transport.close().await {
            tracing::warn!(
                target: LOG_TARGET,
                error = %e,
                "Failed to close client transport cleanly"
            );
        }

        Err(quit_reason)
    }
}

impl NostrServerWorker {
    /// Forward an outbound message from the rmcp handler to the Nostr transport.
    ///
    /// Response IDs carry the Nostr event_id set during ingest.  The transport's
    /// `send_response` uses this to look up the route (client_pubkey +
    /// original_request_id) and deliver the response to the correct client.
    /// Notifications and server-initiated requests are broadcast to all
    /// initialized clients.
    async fn forward_server_internal(&mut self, message: JsonRpcMessage) -> Result<()> {
        match message {
            JsonRpcMessage::Response(resp) => {
                let event_id = resp.id.as_str().map(str::to_owned).ok_or_else(|| {
                    crate::core::error::Error::Validation(
                        "rmcp server response id is not a string event_id".to_string(),
                    )
                })?;

                if event_id == STATELESS_SYNTHETIC_EVENT_ID {
                    tracing::debug!(
                        target: LOG_TARGET,
                        event_id = %event_id,
                        "Dropping synthetic initialize response before wire transport"
                    );
                    return Ok(());
                }

                self.transport
                    .send_response(&event_id, JsonRpcMessage::Response(resp))
                    .await
            }
            JsonRpcMessage::ErrorResponse(resp) => {
                let event_id = resp.id.as_str().map(str::to_owned).ok_or_else(|| {
                    crate::core::error::Error::Validation(
                        "rmcp server error response id is not a string event_id".to_string(),
                    )
                })?;

                if event_id == STATELESS_SYNTHETIC_EVENT_ID {
                    tracing::debug!(
                        target: LOG_TARGET,
                        event_id = %event_id,
                        "Dropping synthetic initialize error before wire transport"
                    );
                    return Ok(());
                }

                self.transport
                    .send_response(&event_id, JsonRpcMessage::ErrorResponse(resp))
                    .await
            }
            JsonRpcMessage::Notification(notification) => {
                let message = JsonRpcMessage::Notification(notification);
                self.transport.broadcast_notification(&message).await
            }
            JsonRpcMessage::Request(request) => {
                let message = JsonRpcMessage::Request(request);
                self.transport.broadcast_notification(&message).await
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::types::JsonRpcResponse;

    #[test]
    fn test_should_inject_stateless_bootstrap_for_first_non_initialize_request() {
        let initialized_clients = HashSet::new();
        let message = JsonRpcMessage::Request(JsonRpcRequest {
            jsonrpc: "2.0".to_string(),
            id: serde_json::json!(1),
            method: "tools/list".to_string(),
            params: Some(serde_json::json!({})),
        });

        assert!(should_inject_stateless_bootstrap(
            &initialized_clients,
            "client-a",
            &message,
        ));
    }

    #[test]
    fn test_should_not_inject_stateless_bootstrap_for_real_initialize() {
        let initialized_clients = HashSet::new();
        let message = JsonRpcMessage::Request(JsonRpcRequest {
            jsonrpc: "2.0".to_string(),
            id: serde_json::json!(1),
            method: "initialize".to_string(),
            params: Some(serde_json::json!({})),
        });

        assert!(!should_inject_stateless_bootstrap(
            &initialized_clients,
            "client-a",
            &message,
        ));
    }

    #[test]
    fn test_synthetic_initialize_keeps_sentinel_id() {
        let message = synthetic_initialize_message();

        match message {
            JsonRpcMessage::Request(req) => {
                assert_eq!(req.id, serde_json::json!(STATELESS_SYNTHETIC_EVENT_ID));
                assert_eq!(req.method, "initialize");
            }
            other => panic!("expected request, got {other:?}"),
        }
    }

    #[test]
    fn test_real_request_is_rewritten_to_event_id() {
        let mut message = JsonRpcMessage::Request(JsonRpcRequest {
            jsonrpc: "2.0".to_string(),
            id: serde_json::json!(1),
            method: "tools/list".to_string(),
            params: Some(serde_json::json!({})),
        });

        if let JsonRpcMessage::Request(ref mut req) = message {
            req.id = serde_json::json!("real-event-id");
        }

        match message {
            JsonRpcMessage::Request(req) => {
                assert_eq!(req.id, serde_json::json!("real-event-id"));
            }
            other => panic!("expected request, got {other:?}"),
        }
    }

    #[test]
    fn test_synthetic_initialize_response_uses_sentinel_for_drop() {
        let message = JsonRpcMessage::Response(JsonRpcResponse {
            jsonrpc: "2.0".to_string(),
            id: serde_json::json!(STATELESS_SYNTHETIC_EVENT_ID),
            result: serde_json::json!({}),
        });

        match message {
            JsonRpcMessage::Response(resp) => {
                assert_eq!(resp.id.as_str(), Some(STATELESS_SYNTHETIC_EVENT_ID));
            }
            other => panic!("expected response, got {other:?}"),
        }
    }

    #[test]
    fn test_synthetic_initialized_notification_shape() {
        let message = synthetic_initialized_notification();
        match message {
            JsonRpcMessage::Notification(notification) => {
                assert_eq!(notification.method, "notifications/initialized");
            }
            other => panic!("expected notification, got {other:?}"),
        }
    }

    #[test]
    fn test_is_synthetic_initialize_message_detects_sentinel() {
        assert!(is_synthetic_initialize_message(
            &synthetic_initialize_message()
        ));
    }
}