Skip to main content

Module memory

Module memory 

Source
Expand description

In-memory transport for testing MCP servers without subprocess spawning.

This module provides a channel-based transport for direct client-server communication within the same process. Essential for unit testing MCP servers without network/IO overhead.

§Overview

The MemoryTransport uses crossbeam channels to enable bidirectional message passing between client and server. Create a pair using create_memory_transport_pair which returns connected client and server transports.

§Example

use fastmcp_transport::memory::create_memory_transport_pair;
use fastmcp_transport::Transport;
use asupersync::Cx;

// Create connected pair
let (client_transport, server_transport) = create_memory_transport_pair();

// Use in separate threads/tasks
// Client sends, server receives (and vice versa)
let cx = Cx::for_testing();
let request = JsonRpcRequest::new("test", None, 1i64);
client_transport.send_request(&cx, &request)?;

// Server receives the message
let msg = server_transport.recv(&cx)?;

§Testing Servers

The primary use case is testing servers without subprocess spawning:

use fastmcp_transport::memory::{create_memory_transport_pair, MemoryTransport};
use std::thread;

let (mut client, mut server) = create_memory_transport_pair();

// Spawn server handler in a thread
let server_handle = thread::spawn(move || {
    // Pass server transport to your server's run loop
    run_server_with_transport(server);
});

// Use client to test
let cx = Cx::for_testing();
client.send_request(&cx, &init_request)?;
let response = client.recv(&cx)?;
assert!(matches!(response, JsonRpcMessage::Response(_)));

Structs§

MemoryTransport
In-memory transport using channels for message passing.
MemoryTransportBuilder
Builder for creating memory transport pairs with custom configuration.

Functions§

create_memory_transport_pair
Creates a connected pair of memory transports.
create_memory_transport_pair_with_capacity
Creates a connected pair of memory transports with specified channel capacity.