turbomcp-server 3.0.14

Production-ready MCP server with zero-boilerplate macros and transport-agnostic design
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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//! In-process channel transport for zero-overhead MCP communication.
//!
//! This transport uses `tokio::sync::mpsc` channels to pass `TransportMessage`
//! values directly between a server and client in the same process. It eliminates
//! all line framing, string allocation, flushing, and redundant JSON parsing
//! that line-based transports (STDIO, TCP) incur.
//!
//! # Usage
//!
//! ```rust,ignore
//! use turbomcp_server::transport::channel;
//!
//! // One-liner: returns a connected client transport + server join handle
//! let (client_transport, server_handle) = channel::run_in_process(&handler).await?;
//! ```

use std::collections::HashMap;
use std::sync::Arc;

use tokio::sync::{mpsc, oneshot};
use turbomcp_core::error::{ErrorKind, McpError, McpResult};
use turbomcp_core::handler::McpHandler;
use turbomcp_core::types::core::ProtocolVersion;

use crate::context::{McpSession, RequestContext};
use crate::router;
use crate::transport::{MAX_MESSAGE_SIZE, SessionState};

use turbomcp_transport::{
    Transport, TransportCapabilities, TransportError, TransportMessage, TransportMetrics,
    TransportResult, TransportState, TransportType,
};

/// Default channel buffer size.
const DEFAULT_CHANNEL_BUFFER: usize = 256;

/// Maximum number of in-flight server-to-client requests.
const MAX_PENDING_REQUESTS: usize = 64;

// ── ChannelTransport (client-side Transport impl) ───────────────────────

/// An in-process transport that communicates via `mpsc` channels.
///
/// This is the client-side half of a channel pair. It sends requests to the
/// server runner and receives responses, all without serialization overhead
/// beyond what the client's `ProtocolClient` already does.
#[derive(Debug)]
pub struct ChannelTransport {
    tx: mpsc::Sender<TransportMessage>,
    rx: tokio::sync::Mutex<mpsc::Receiver<TransportMessage>>,
    state: parking_lot::Mutex<TransportState>,
    capabilities: TransportCapabilities,
}

impl ChannelTransport {
    fn new(tx: mpsc::Sender<TransportMessage>, rx: mpsc::Receiver<TransportMessage>) -> Self {
        Self {
            tx,
            rx: tokio::sync::Mutex::new(rx),
            state: parking_lot::Mutex::new(TransportState::Connected),
            capabilities: TransportCapabilities {
                max_message_size: Some(MAX_MESSAGE_SIZE),
                supports_compression: false,
                supports_streaming: false,
                supports_bidirectional: true,
                supports_multiplexing: false,
                compression_algorithms: Vec::new(),
                custom: std::collections::HashMap::new(),
            },
        }
    }
}

impl Transport for ChannelTransport {
    fn transport_type(&self) -> TransportType {
        TransportType::Channel
    }

    fn capabilities(&self) -> &TransportCapabilities {
        &self.capabilities
    }

    fn state(
        &self,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = TransportState> + Send + '_>> {
        Box::pin(async move { self.state.lock().clone() })
    }

    fn connect(
        &self,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = TransportResult<()>> + Send + '_>> {
        Box::pin(async move {
            *self.state.lock() = TransportState::Connected;
            Ok(())
        })
    }

    fn disconnect(
        &self,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = TransportResult<()>> + Send + '_>> {
        Box::pin(async move {
            *self.state.lock() = TransportState::Disconnected;
            Ok(())
        })
    }

    fn send(
        &self,
        message: TransportMessage,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = TransportResult<()>> + Send + '_>> {
        Box::pin(async move {
            self.tx
                .send(message)
                .await
                .map_err(|_| TransportError::ConnectionLost("Channel closed".to_string()))?;
            Ok(())
        })
    }

    fn receive(
        &self,
    ) -> std::pin::Pin<
        Box<
            dyn std::future::Future<Output = TransportResult<Option<TransportMessage>>> + Send + '_,
        >,
    > {
        Box::pin(async move {
            let mut rx = self.rx.lock().await;
            Ok(rx.recv().await)
        })
    }

    fn metrics(
        &self,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = TransportMetrics> + Send + '_>> {
        Box::pin(async move { TransportMetrics::default() })
    }
}

// ── Session handle for bidirectional communication ──────────────────────

#[derive(Debug, Clone)]
struct ChannelSessionHandle {
    request_tx: mpsc::Sender<SessionCommand>,
}

#[derive(Debug)]
enum SessionCommand {
    Request {
        method: String,
        params: serde_json::Value,
        response_tx: oneshot::Sender<McpResult<serde_json::Value>>,
    },
    Notify {
        method: String,
        params: serde_json::Value,
    },
}

#[async_trait::async_trait]
impl McpSession for ChannelSessionHandle {
    async fn call(&self, method: &str, params: serde_json::Value) -> McpResult<serde_json::Value> {
        let (response_tx, response_rx) = oneshot::channel();
        self.request_tx
            .send(SessionCommand::Request {
                method: method.to_string(),
                params,
                response_tx,
            })
            .await
            .map_err(|_| McpError::internal("Session closed"))?;

        response_rx
            .await
            .map_err(|_| McpError::internal("Response channel closed"))?
    }

    async fn notify(&self, method: &str, params: serde_json::Value) -> McpResult<()> {
        self.request_tx
            .send(SessionCommand::Notify {
                method: method.to_string(),
                params,
            })
            .await
            .map_err(|_| McpError::internal("Session closed"))?;
        Ok(())
    }
}

// ── Channel transport runner (server-side) ──────────────────────────────

/// Run a handler on an in-process channel transport.
///
/// Returns a `ChannelTransport` that the client can use, and a `JoinHandle`
/// for the server task. The server runs in the background processing requests
/// received over the channel.
///
/// This eliminates all line framing, flushing, newline scanning, and redundant
/// JSON parsing. Messages are passed as `TransportMessage` values directly
/// through `mpsc` channels.
///
/// # Example
///
/// ```rust,ignore
/// let (transport, server_handle) = channel::run_in_process(&handler).await?;
/// let client = Client::new(transport);
/// client.initialize().await?;
/// let tools = client.list_tools().await?;
/// server_handle.abort(); // shutdown
/// ```
pub async fn run_in_process<H: McpHandler + 'static>(
    handler: &H,
) -> McpResult<(ChannelTransport, tokio::task::JoinHandle<McpResult<()>>)> {
    run_in_process_with_buffer(handler, DEFAULT_CHANNEL_BUFFER).await
}

/// Like `run_in_process` but with a custom channel buffer size.
pub async fn run_in_process_with_buffer<H: McpHandler + 'static>(
    handler: &H,
    buffer_size: usize,
) -> McpResult<(ChannelTransport, tokio::task::JoinHandle<McpResult<()>>)> {
    handler.on_initialize().await?;

    // Client → Server channel
    let (client_tx, server_rx) = mpsc::channel::<TransportMessage>(buffer_size);
    // Server → Client channel
    let (server_tx, client_rx) = mpsc::channel::<TransportMessage>(buffer_size);

    let client_transport = ChannelTransport::new(client_tx, client_rx);

    let handler = handler.clone();
    let server_handle =
        tokio::spawn(async move { run_server_loop(handler, server_rx, server_tx).await });

    Ok((client_transport, server_handle))
}

/// The core server event loop for channel transport.
///
/// This is analogous to `LineTransportRunner::run()` but operates on
/// `TransportMessage` values instead of text lines. The key differences:
///
/// - No line framing or newline scanning
/// - No string allocation for reading
/// - No flush calls
/// - JSON is parsed once from `Bytes` payload (not from a `String`)
async fn run_server_loop<H: McpHandler>(
    handler: H,
    mut incoming: mpsc::Receiver<TransportMessage>,
    outgoing: mpsc::Sender<TransportMessage>,
) -> McpResult<()> {
    // Channel for session commands (server-to-client requests/notifications)
    let (cmd_tx, mut cmd_rx) = mpsc::channel::<SessionCommand>(32);
    let session_handle = Arc::new(ChannelSessionHandle { request_tx: cmd_tx });

    // Channel for completed handler responses
    let (response_tx, mut response_rx) = mpsc::channel::<router::JsonRpcOutgoing>(32);

    // Server-to-client pending request tracking
    let mut pending_requests =
        HashMap::<serde_json::Value, oneshot::Sender<McpResult<serde_json::Value>>>::new();
    let mut next_request_id = 1u64;
    let mut session_state = SessionState::Uninitialized;

    loop {
        tokio::select! {
            // Incoming from client
            msg = incoming.recv() => {
                let Some(msg) = msg else { break; };

                // Check message size
                if msg.payload.len() > MAX_MESSAGE_SIZE {
                    send_error_msg(
                        &outgoing,
                        None,
                        McpError::invalid_request(format!(
                            "Message exceeds maximum size of {MAX_MESSAGE_SIZE} bytes"
                        )),
                    ).await?;
                    continue;
                }

                // Parse JSON directly from Bytes (no string allocation)
                let value: serde_json::Value = match serde_json::from_slice(&msg.payload) {
                    Ok(v) => v,
                    Err(e) => {
                        send_error_msg(&outgoing, None, McpError::parse_error(e.to_string())).await?;
                        continue;
                    }
                };

                // Check if it's a response to a server-to-client request
                if let Some(id) = value.get("id")
                    && (value.get("result").is_some() || value.get("error").is_some())
                {
                    if let Some(tx) = pending_requests.remove(id) {
                        if let Some(error) = value.get("error") {
                            let mcp_error = serde_json::from_value::<turbomcp_core::jsonrpc::JsonRpcError>(error.clone())
                                .map(|e| McpError::new(ErrorKind::from_i32(e.code), e.message))
                                .unwrap_or_else(|_| McpError::internal("Failed to parse error response"));
                            let _ = tx.send(Err(mcp_error));
                        } else {
                            let result = value.get("result").cloned().unwrap_or(serde_json::Value::Null);
                            let _ = tx.send(Ok(result));
                        }
                    }
                } else {
                    // Parse as JSON-RPC request directly from the Value
                    // (avoids re-serializing to string then re-parsing like LineTransportRunner does)
                    match serde_json::from_value::<turbomcp_core::jsonrpc::JsonRpcIncoming>(value) {
                        Ok(request) => {
                            if request.method == "initialize" {
                                if matches!(session_state, SessionState::Initialized(_)) {
                                    send_error_msg(
                                        &outgoing,
                                        request.id.clone(),
                                        McpError::invalid_request("Session already initialized"),
                                    )
                                    .await?;
                                    continue;
                                }

                                let initialize_request_id = request.id.clone();
                                let ctx =
                                    RequestContext::channel().with_session(session_handle.clone());
                                let core_ctx = ctx.to_core_context();
                                let response = router::route_request_with_config(
                                    &handler,
                                    request,
                                    &core_ctx,
                                    None,
                                )
                                .await;

                                if let Some(ref result) = response.result
                                    && let Some(v) =
                                        result.get("protocolVersion").and_then(|v| v.as_str())
                                {
                                    let version = ProtocolVersion::from(v);
                                    session_state = SessionState::Initialized(
                                        super::InitializedSessionState::new(
                                            version,
                                            initialize_request_id.as_ref(),
                                        ),
                                    );
                                }

                                if response.should_send() {
                                    send_response_msg(&outgoing, &response).await?;
                                }
                            } else if request.method == "notifications/initialized"
                                || request.method == "notifications/cancelled"
                            {
                                let h = handler.clone();
                                let session = session_handle.clone();
                                let resp_tx = response_tx.clone();
                                let ctx = RequestContext::channel().with_session(session);
                                let core_ctx = ctx.to_core_context();

                                tokio::spawn(async move {
                                    let response = router::route_request(&h, request, &core_ctx).await;
                                    let _ = resp_tx.send(response).await;
                                });
                            } else {
                                // Notifications (id=None) MUST NOT receive responses per
                                // JSON-RPC 2.0, so rejection paths stay silent for them.
                                let is_notification = request.id.is_none();
                                let version = match &mut session_state {
                                    SessionState::Initialized(session) => {
                                        if !session.register_request_id(request.id.as_ref()) {
                                            if !is_notification {
                                                send_error_msg(
                                                    &outgoing,
                                                    request.id.clone(),
                                                    McpError::invalid_request(
                                                        "Request ID already used in this session",
                                                    ),
                                                )
                                                .await?;
                                            }
                                            continue;
                                        }

                                        session.protocol_version().clone()
                                    }
                                    SessionState::Uninitialized => {
                                        if !is_notification {
                                            send_error_msg(
                                                &outgoing,
                                                request.id.clone(),
                                                McpError::invalid_request(
                                                    "Server not initialized. Send 'initialize' first.",
                                                ),
                                            )
                                            .await?;
                                        }
                                        continue;
                                    }
                                };

                                let h = handler.clone();
                                let session = session_handle.clone();
                                let resp_tx = response_tx.clone();
                                let ctx = RequestContext::channel().with_session(session);
                                let core_ctx = ctx.to_core_context();

                                tokio::spawn(async move {
                                    let response = router::route_request_versioned(
                                        &h, request, &core_ctx, &version,
                                    )
                                    .await;
                                    let _ = resp_tx.send(response).await;
                                });
                            }
                        }
                        Err(e) => {
                            send_error_msg(&outgoing, None, McpError::parse_error(e.to_string())).await?;
                        }
                    }
                }
            }

            // Completed handler responses
            Some(response) = response_rx.recv() => {
                if response.should_send() {
                    send_response_msg(&outgoing, &response).await?;
                }
            }

            // Outgoing server-to-client requests/notifications
            Some(cmd) = cmd_rx.recv() => {
                match cmd {
                    SessionCommand::Request { method, params, response_tx } => {
                        if pending_requests.len() >= MAX_PENDING_REQUESTS {
                            let _ = response_tx.send(Err(McpError::internal(
                                "Too many pending server-to-client requests"
                            )));
                            continue;
                        }

                        let id = serde_json::json!(format!("s-{next_request_id}"));
                        next_request_id += 1;
                        pending_requests.insert(id.clone(), response_tx);

                        let request = serde_json::json!({
                            "jsonrpc": "2.0",
                            "id": id,
                            "method": method,
                            "params": params
                        });

                        let payload = serde_json::to_vec(&request)
                            .map_err(|e| McpError::internal(e.to_string()))?;

                        outgoing.send(TransportMessage::new(
                            turbomcp_protocol::MessageId::from(format!("s-req-{}", next_request_id - 1)),
                            payload.into(),
                        ))
                        .await
                        .map_err(|_| McpError::internal("Channel closed"))?;
                    }
                    SessionCommand::Notify { method, params } => {
                        let notification = serde_json::json!({
                            "jsonrpc": "2.0",
                            "method": method,
                            "params": params
                        });

                        let payload = serde_json::to_vec(&notification)
                            .map_err(|e| McpError::internal(e.to_string()))?;

                        outgoing.send(TransportMessage::new(
                            turbomcp_protocol::MessageId::from("notification"),
                            payload.into(),
                        ))
                        .await
                        .map_err(|_| McpError::internal("Channel closed"))?;
                    }
                }
            }
        }
    }

    // Drain remaining handler responses
    drop(response_tx);
    while let Some(response) = response_rx.recv().await {
        if response.should_send() {
            send_response_msg(&outgoing, &response).await?;
        }
    }

    handler.on_shutdown().await?;

    Ok(())
}

/// Serialize and send a JSON-RPC response over the channel.
async fn send_response_msg(
    tx: &mpsc::Sender<TransportMessage>,
    response: &router::JsonRpcOutgoing,
) -> McpResult<()> {
    let payload = router::serialize_response(response)?;
    tx.send(TransportMessage::new(
        response
            .id
            .as_ref()
            .map(|id| turbomcp_protocol::MessageId::from(id.to_string()))
            .unwrap_or_else(|| turbomcp_protocol::MessageId::from("response")),
        bytes::Bytes::from(payload),
    ))
    .await
    .map_err(|_| McpError::internal("Channel closed"))?;
    Ok(())
}

/// Serialize and send a JSON-RPC error over the channel.
async fn send_error_msg(
    tx: &mpsc::Sender<TransportMessage>,
    id: Option<serde_json::Value>,
    error: McpError,
) -> McpResult<()> {
    let response = router::JsonRpcOutgoing::error(id, error);
    send_response_msg(tx, &response).await
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::Value;
    use turbomcp_core::context::RequestContext as CoreRequestContext;
    use turbomcp_core::error::McpResult;
    use turbomcp_types::{
        Prompt, PromptResult, Resource, ResourceResult, ServerInfo, Tool, ToolResult,
    };

    #[derive(Clone)]
    struct TestHandler;

    impl McpHandler for TestHandler {
        fn server_info(&self) -> ServerInfo {
            ServerInfo::new("channel-test", "1.0.0")
        }

        fn list_tools(&self) -> Vec<Tool> {
            vec![Tool::new("ping", "Ping tool")]
        }

        fn list_resources(&self) -> Vec<Resource> {
            vec![]
        }

        fn list_prompts(&self) -> Vec<Prompt> {
            vec![]
        }

        async fn call_tool(
            &self,
            _name: &str,
            _args: Value,
            _ctx: &CoreRequestContext,
        ) -> McpResult<ToolResult> {
            Ok(ToolResult::text("pong"))
        }

        async fn read_resource(
            &self,
            uri: &str,
            _ctx: &CoreRequestContext,
        ) -> McpResult<ResourceResult> {
            Err(McpError::resource_not_found(uri))
        }

        async fn get_prompt(
            &self,
            name: &str,
            _args: Option<Value>,
            _ctx: &CoreRequestContext,
        ) -> McpResult<PromptResult> {
            Err(McpError::prompt_not_found(name))
        }
    }

    #[tokio::test]
    async fn test_channel_transport_roundtrip() {
        let handler = TestHandler;
        let (transport, server_handle) = run_in_process(&handler).await.unwrap();

        // Send initialize request
        let init_request = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-11-25",
                "clientInfo": { "name": "test", "version": "1.0.0" },
                "capabilities": {}
            }
        });
        let payload = serde_json::to_vec(&init_request).unwrap();
        transport
            .send(TransportMessage::new(
                turbomcp_protocol::MessageId::from("1"),
                payload.into(),
            ))
            .await
            .unwrap();

        // Receive response
        let response = transport.receive().await.unwrap().unwrap();
        let value: serde_json::Value = serde_json::from_slice(&response.payload).unwrap();
        assert!(value.get("result").is_some());
        assert_eq!(value["result"]["serverInfo"]["name"], "channel-test");

        // Send ping tool call
        let ping_request = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/call",
            "params": { "name": "ping", "arguments": {} }
        });
        let payload = serde_json::to_vec(&ping_request).unwrap();
        transport
            .send(TransportMessage::new(
                turbomcp_protocol::MessageId::from("2"),
                payload.into(),
            ))
            .await
            .unwrap();

        let response = transport.receive().await.unwrap().unwrap();
        let value: serde_json::Value = serde_json::from_slice(&response.payload).unwrap();
        assert!(value.get("result").is_some());

        // Cleanup
        drop(transport);
        let _ = server_handle.await;
    }

    // JSON-RPC 2.0: notifications (no id) must not receive responses.
    // Rejecting a pre-init notification with an error over the channel
    // is a spec violation.
    #[tokio::test]
    async fn test_channel_transport_silent_on_notification_before_init() {
        let handler = TestHandler;
        let (transport, server_handle) = run_in_process(&handler).await.unwrap();

        let notification = serde_json::json!({
            "jsonrpc": "2.0",
            "method": "tools/list"
        });
        let payload = serde_json::to_vec(&notification).unwrap();
        transport
            .send(TransportMessage::new(
                turbomcp_protocol::MessageId::from("n1"),
                payload.into(),
            ))
            .await
            .unwrap();

        let received =
            tokio::time::timeout(std::time::Duration::from_millis(200), transport.receive()).await;
        assert!(
            received.is_err(),
            "notifications must not receive a response, got: {received:?}"
        );

        drop(transport);
        let _ = server_handle.await;
    }

    #[tokio::test]
    async fn test_channel_transport_rejects_duplicate_request_ids() {
        let handler = TestHandler;
        let (transport, server_handle) = run_in_process(&handler).await.unwrap();

        let init_request = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-11-25",
                "clientInfo": { "name": "test", "version": "1.0.0" },
                "capabilities": {}
            }
        });
        let init_payload = serde_json::to_vec(&init_request).unwrap();
        transport
            .send(TransportMessage::new(
                turbomcp_protocol::MessageId::from("1"),
                init_payload.into(),
            ))
            .await
            .unwrap();
        let _ = transport.receive().await.unwrap().unwrap();

        let request = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/list"
        });
        let payload = serde_json::to_vec(&request).unwrap();

        transport
            .send(TransportMessage::new(
                turbomcp_protocol::MessageId::from("2-first"),
                payload.clone().into(),
            ))
            .await
            .unwrap();
        let first = transport.receive().await.unwrap().unwrap();
        let first_value: serde_json::Value = serde_json::from_slice(&first.payload).unwrap();
        assert!(first_value.get("result").is_some());

        transport
            .send(TransportMessage::new(
                turbomcp_protocol::MessageId::from("2-duplicate"),
                payload.into(),
            ))
            .await
            .unwrap();
        let duplicate = transport.receive().await.unwrap().unwrap();
        let duplicate_value: serde_json::Value =
            serde_json::from_slice(&duplicate.payload).unwrap();
        assert_eq!(duplicate_value["error"]["code"], -32600);
        assert!(
            duplicate_value["error"]["message"]
                .as_str()
                .is_some_and(|message| message.contains("already used"))
        );

        drop(transport);
        let _ = server_handle.await;
    }
}