turbomcp-core 3.0.14

Core MCP types and primitives - no_std compatible for WASM targets
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
//! Minimal request context for cross-platform MCP handlers.
//!
//! This module provides a `RequestContext` type that works on all platforms,
//! including `no_std` environments. Platform-specific extensions (cancellation
//! tokens, UUIDs, etc.) are provided by runtime crates (`turbomcp-server`, `turbomcp-wasm`).
//!
//! # Design Philosophy
//!
//! The context is intentionally minimal:
//! - Uses `BTreeMap` instead of `HashMap` for `no_std` compatibility
//! - No tokio-specific types (CancellationToken, etc.)
//! - Serializable for transport across boundaries
//! - Cloneable for async handler patterns
//!
//! # Example
//!
//! ```rust
//! use turbomcp_core::context::{RequestContext, TransportType};
//!
//! let ctx = RequestContext::new("request-1", TransportType::Http)
//!     .with_metadata("user-agent", "Mozilla/5.0")
//!     .with_metadata("x-request-id", "abc123");
//!
//! assert_eq!(ctx.transport, TransportType::Http);
//! assert_eq!(ctx.get_metadata("user-agent"), Some("Mozilla/5.0"));
//! ```

use crate::auth::Principal;
use alloc::collections::BTreeMap;
use alloc::string::String;
use serde::{Deserialize, Serialize};

/// Transport type identifier.
///
/// Indicates which transport received the request. This is useful for:
/// - Logging and metrics
/// - Transport-specific behavior (e.g., different timeouts)
/// - Debugging and tracing
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum TransportType {
    /// Standard I/O transport (default for CLI tools)
    #[default]
    Stdio,
    /// HTTP transport (REST or SSE)
    Http,
    /// WebSocket transport
    WebSocket,
    /// Raw TCP transport
    Tcp,
    /// Unix domain socket transport
    Unix,
    /// WebAssembly/Worker transport (Cloudflare Workers, etc.)
    Wasm,
    /// In-process channel transport (zero-copy, no serialization overhead)
    Channel,
    /// Unknown or custom transport
    Unknown,
}

impl TransportType {
    /// Returns true if this is a network-based transport.
    #[inline]
    pub fn is_network(&self) -> bool {
        matches!(self, Self::Http | Self::WebSocket | Self::Tcp)
    }

    /// Returns true if this is a local transport.
    #[inline]
    pub fn is_local(&self) -> bool {
        matches!(self, Self::Stdio | Self::Unix | Self::Channel)
    }

    /// Returns the transport name as a string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Stdio => "stdio",
            Self::Http => "http",
            Self::WebSocket => "websocket",
            Self::Tcp => "tcp",
            Self::Unix => "unix",
            Self::Wasm => "wasm",
            Self::Channel => "channel",
            Self::Unknown => "unknown",
        }
    }
}

impl core::fmt::Display for TransportType {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Minimal request context that works on all platforms.
///
/// This struct contains only the essential information needed to process
/// a request. Platform-specific extensions (cancellation tokens, UUIDs, etc.)
/// are provided by the runtime layer.
///
/// # Thread Safety
///
/// `RequestContext` is `Send + Sync` on native targets, enabling safe use
/// across async task boundaries. On WASM targets, thread safety is not required.
///
/// # Serialization
///
/// The context is designed to be serializable, enabling transport across
/// process boundaries (e.g., for distributed tracing).
#[derive(Debug, Clone, Default)]
pub struct RequestContext {
    /// Unique request identifier (JSON-RPC id as string, or generated UUID)
    pub request_id: String,
    /// Transport type that received this request
    pub transport: TransportType,
    /// Optional metadata (headers, user info, etc.)
    ///
    /// Uses `BTreeMap` for `no_std` compatibility and deterministic iteration.
    pub metadata: BTreeMap<String, String>,
    /// Authenticated principal (set after successful authentication)
    ///
    /// This field is `None` for unauthenticated requests or when
    /// authentication is not configured.
    pub principal: Option<Principal>,
}

impl RequestContext {
    /// Create a new request context with the given ID and transport.
    ///
    /// # Example
    ///
    /// ```rust
    /// use turbomcp_core::context::{RequestContext, TransportType};
    ///
    /// let ctx = RequestContext::new("req-123", TransportType::Http);
    /// assert_eq!(ctx.request_id, "req-123");
    /// ```
    pub fn new(request_id: impl Into<String>, transport: TransportType) -> Self {
        Self {
            request_id: request_id.into(),
            transport,
            metadata: BTreeMap::new(),
            principal: None,
        }
    }

    /// Create a context for STDIO transport.
    #[inline]
    pub fn stdio() -> Self {
        Self::new("", TransportType::Stdio)
    }

    /// Create a context for HTTP transport.
    #[inline]
    pub fn http() -> Self {
        Self::new("", TransportType::Http)
    }

    /// Create a context for WebSocket transport.
    #[inline]
    pub fn websocket() -> Self {
        Self::new("", TransportType::WebSocket)
    }

    /// Create a context for TCP transport.
    #[inline]
    pub fn tcp() -> Self {
        Self::new("", TransportType::Tcp)
    }

    /// Create a context for WASM transport.
    #[inline]
    pub fn wasm() -> Self {
        Self::new("", TransportType::Wasm)
    }

    /// Add metadata to the context.
    ///
    /// # Example
    ///
    /// ```rust
    /// use turbomcp_core::context::{RequestContext, TransportType};
    ///
    /// let ctx = RequestContext::new("1", TransportType::Http)
    ///     .with_metadata("user-agent", "MyClient/1.0")
    ///     .with_metadata("x-trace-id", "abc123");
    ///
    /// assert_eq!(ctx.get_metadata("user-agent"), Some("MyClient/1.0"));
    /// ```
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Add metadata to the context (mutable version).
    pub fn insert_metadata(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.metadata.insert(key.into(), value.into());
    }

    /// Get metadata value by key.
    ///
    /// # Example
    ///
    /// ```rust
    /// use turbomcp_core::context::{RequestContext, TransportType};
    ///
    /// let ctx = RequestContext::new("1", TransportType::Http)
    ///     .with_metadata("key", "value");
    ///
    /// assert_eq!(ctx.get_metadata("key"), Some("value"));
    /// assert_eq!(ctx.get_metadata("missing"), None);
    /// ```
    pub fn get_metadata(&self, key: &str) -> Option<&str> {
        self.metadata.get(key).map(|s| s.as_str())
    }

    /// Check if metadata contains a key.
    pub fn has_metadata(&self, key: &str) -> bool {
        self.metadata.contains_key(key)
    }

    /// Set the request ID.
    pub fn with_request_id(mut self, id: impl Into<String>) -> Self {
        self.request_id = id.into();
        self
    }

    /// Returns true if this context has a valid (non-empty) request ID.
    pub fn has_request_id(&self) -> bool {
        !self.request_id.is_empty()
    }

    /// Set the authenticated principal.
    ///
    /// # Example
    ///
    /// ```rust
    /// use turbomcp_core::context::{RequestContext, TransportType};
    /// use turbomcp_core::auth::Principal;
    ///
    /// let ctx = RequestContext::new("1", TransportType::Http)
    ///     .with_principal(Principal::new("user-123"));
    ///
    /// assert!(ctx.principal().is_some());
    /// assert_eq!(ctx.principal().unwrap().subject, "user-123");
    /// ```
    pub fn with_principal(mut self, principal: Principal) -> Self {
        self.principal = Some(principal);
        self
    }

    /// Set the authenticated principal (mutable version).
    pub fn set_principal(&mut self, principal: Principal) {
        self.principal = Some(principal);
    }

    /// Get the authenticated principal, if any.
    ///
    /// Returns `None` if the request was not authenticated or if
    /// authentication is not configured.
    pub fn principal(&self) -> Option<&Principal> {
        self.principal.as_ref()
    }

    /// Returns true if this context has an authenticated principal.
    pub fn is_authenticated(&self) -> bool {
        self.principal.is_some()
    }

    /// Get the subject of the authenticated principal.
    ///
    /// Convenience method that returns `None` if not authenticated.
    pub fn subject(&self) -> Option<&str> {
        self.principal.as_ref().map(|p| p.subject.as_str())
    }

    /// Clear the authenticated principal.
    pub fn clear_principal(&mut self) {
        self.principal = None;
    }
}

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

    #[test]
    fn test_transport_type_display() {
        assert_eq!(TransportType::Stdio.to_string(), "stdio");
        assert_eq!(TransportType::Http.to_string(), "http");
        assert_eq!(TransportType::WebSocket.to_string(), "websocket");
        assert_eq!(TransportType::Tcp.to_string(), "tcp");
        assert_eq!(TransportType::Unix.to_string(), "unix");
        assert_eq!(TransportType::Wasm.to_string(), "wasm");
        assert_eq!(TransportType::Channel.to_string(), "channel");
        assert_eq!(TransportType::Unknown.to_string(), "unknown");
    }

    #[test]
    fn test_transport_type_classification() {
        assert!(TransportType::Http.is_network());
        assert!(TransportType::WebSocket.is_network());
        assert!(TransportType::Tcp.is_network());
        assert!(!TransportType::Stdio.is_network());

        assert!(TransportType::Stdio.is_local());
        assert!(TransportType::Unix.is_local());
        assert!(TransportType::Channel.is_local());
        assert!(!TransportType::Http.is_local());
    }

    #[test]
    fn test_request_context_new() {
        let ctx = RequestContext::new("test-123", TransportType::Http);
        assert_eq!(ctx.request_id, "test-123");
        assert_eq!(ctx.transport, TransportType::Http);
        assert!(ctx.metadata.is_empty());
    }

    #[test]
    fn test_request_context_factory_methods() {
        assert_eq!(RequestContext::stdio().transport, TransportType::Stdio);
        assert_eq!(RequestContext::http().transport, TransportType::Http);
        assert_eq!(
            RequestContext::websocket().transport,
            TransportType::WebSocket
        );
        assert_eq!(RequestContext::tcp().transport, TransportType::Tcp);
        assert_eq!(RequestContext::wasm().transport, TransportType::Wasm);
    }

    #[test]
    fn test_request_context_metadata() {
        let ctx = RequestContext::new("1", TransportType::Http)
            .with_metadata("key1", "value1")
            .with_metadata("key2", "value2");

        assert_eq!(ctx.get_metadata("key1"), Some("value1"));
        assert_eq!(ctx.get_metadata("key2"), Some("value2"));
        assert_eq!(ctx.get_metadata("key3"), None);

        assert!(ctx.has_metadata("key1"));
        assert!(!ctx.has_metadata("key3"));
    }

    #[test]
    fn test_request_context_mutable_metadata() {
        let mut ctx = RequestContext::new("1", TransportType::Http);
        ctx.insert_metadata("key", "value");
        assert_eq!(ctx.get_metadata("key"), Some("value"));
    }

    #[test]
    fn test_request_context_request_id() {
        let ctx = RequestContext::new("", TransportType::Http);
        assert!(!ctx.has_request_id());

        let ctx = ctx.with_request_id("request-456");
        assert!(ctx.has_request_id());
        assert_eq!(ctx.request_id, "request-456");
    }

    #[test]
    fn test_request_context_default() {
        let ctx = RequestContext::default();
        assert_eq!(ctx.request_id, "");
        assert_eq!(ctx.transport, TransportType::Stdio);
        assert!(ctx.metadata.is_empty());
    }

    #[test]
    fn test_request_context_clone() {
        let ctx1 = RequestContext::new("1", TransportType::Http).with_metadata("key", "value");
        let ctx2 = ctx1.clone();

        assert_eq!(ctx1.request_id, ctx2.request_id);
        assert_eq!(ctx1.transport, ctx2.transport);
        assert_eq!(ctx1.get_metadata("key"), ctx2.get_metadata("key"));
    }

    #[test]
    fn test_request_context_principal() {
        let ctx = RequestContext::new("1", TransportType::Http);
        assert!(!ctx.is_authenticated());
        assert!(ctx.principal().is_none());
        assert!(ctx.subject().is_none());

        let principal = Principal::new("user-123")
            .with_email("user@example.com")
            .with_role("admin");

        let ctx = ctx.with_principal(principal);
        assert!(ctx.is_authenticated());
        assert!(ctx.principal().is_some());
        assert_eq!(ctx.subject(), Some("user-123"));
        assert_eq!(
            ctx.principal().unwrap().email,
            Some("user@example.com".to_string())
        );
        assert!(ctx.principal().unwrap().has_role("admin"));
    }

    #[test]
    fn test_request_context_principal_mutable() {
        let mut ctx = RequestContext::new("1", TransportType::Http);
        assert!(!ctx.is_authenticated());

        ctx.set_principal(Principal::new("user-456"));
        assert!(ctx.is_authenticated());
        assert_eq!(ctx.subject(), Some("user-456"));

        ctx.clear_principal();
        assert!(!ctx.is_authenticated());
    }
}