pub struct TestClient { /* private fields */ }Expand description
Test client for in-process MCP testing.
Unlike the production Client, this works with MemoryTransport for
fast, in-process testing without subprocess spawning.
§Example
use fastmcp_rust::testing::prelude::*;
let (router, client_transport, server_transport) = TestServer::builder()
.with_tool(my_tool)
.build();
// Run server in a background thread (omitted here). Prefer using the
// higher-level E2E harness helpers in this crate which join threads on drop.
// Create test client
let mut client = TestClient::new(client_transport);
client.initialize().unwrap();
// Test operations
let tools = client.list_tools().unwrap();
assert!(!tools.is_empty());Implementations§
Source§impl TestClient
impl TestClient
Sourcepub fn new(transport: MemoryTransport) -> Self
pub fn new(transport: MemoryTransport) -> Self
Sourcepub fn with_cx(transport: MemoryTransport, cx: Cx) -> Self
pub fn with_cx(transport: MemoryTransport, cx: Cx) -> Self
Creates a new test client with custom Cx.
Sourcepub fn with_client_info(
self,
name: impl Into<String>,
version: impl Into<String>,
) -> Self
pub fn with_client_info( self, name: impl Into<String>, version: impl Into<String>, ) -> Self
Sets the client info for initialization.
Sourcepub fn with_capabilities(self, capabilities: ClientCapabilities) -> Self
pub fn with_capabilities(self, capabilities: ClientCapabilities) -> Self
Sets the client capabilities for initialization.
Sourcepub fn initialize(&mut self) -> McpResult<InitializeResult>
pub fn initialize(&mut self) -> McpResult<InitializeResult>
Performs the MCP initialization handshake.
Must be called before any other operations.
§Errors
Returns an error if the initialization fails.
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Returns whether the client has been initialized.
Sourcepub fn server_info(&self) -> Option<&ServerInfo>
pub fn server_info(&self) -> Option<&ServerInfo>
Returns the server info after initialization.
Sourcepub fn server_capabilities(&self) -> Option<&ServerCapabilities>
pub fn server_capabilities(&self) -> Option<&ServerCapabilities>
Returns the server capabilities after initialization.
Sourcepub fn protocol_version(&self) -> Option<&str>
pub fn protocol_version(&self) -> Option<&str>
Returns the protocol version after initialization.
Sourcepub fn list_tools(&mut self) -> McpResult<Vec<Tool>>
pub fn list_tools(&mut self) -> McpResult<Vec<Tool>>
Sourcepub fn list_resources(&mut self) -> McpResult<Vec<Resource>>
pub fn list_resources(&mut self) -> McpResult<Vec<Resource>>
Sourcepub fn list_resource_templates(&mut self) -> McpResult<Vec<ResourceTemplate>>
pub fn list_resource_templates(&mut self) -> McpResult<Vec<ResourceTemplate>>
Sourcepub fn read_resource(&mut self, uri: &str) -> McpResult<Vec<ResourceContent>>
pub fn read_resource(&mut self, uri: &str) -> McpResult<Vec<ResourceContent>>
Sourcepub fn list_prompts(&mut self) -> McpResult<Vec<Prompt>>
pub fn list_prompts(&mut self) -> McpResult<Vec<Prompt>>
Sourcepub fn get_prompt(
&mut self,
name: &str,
arguments: HashMap<String, String>,
) -> McpResult<Vec<PromptMessage>>
pub fn get_prompt( &mut self, name: &str, arguments: HashMap<String, String>, ) -> McpResult<Vec<PromptMessage>>
Sourcepub fn send_raw_request(
&mut self,
method: &str,
params: Value,
) -> McpResult<Value>
pub fn send_raw_request( &mut self, method: &str, params: Value, ) -> McpResult<Value>
Sends a raw JSON-RPC request and returns the raw response.
Useful for testing custom or non-standard methods.
§Errors
Returns an error if the request fails.
Sourcepub fn transport(&self) -> &MemoryTransport
pub fn transport(&self) -> &MemoryTransport
Returns a reference to the transport for advanced testing.
Sourcepub fn transport_mut(&mut self) -> &mut MemoryTransport
pub fn transport_mut(&mut self) -> &mut MemoryTransport
Returns a mutable reference to the transport for advanced testing.
Sourcepub fn send_request_json(
&mut self,
method: &str,
params_value: Value,
) -> McpResult<Value>
pub fn send_request_json( &mut self, method: &str, params_value: Value, ) -> McpResult<Value>
Sends a raw JSON-RPC request with already-serialized params.
This is intended for advanced E2E tests that need to inject protocol fields not covered by the typed helper methods (for example, auth metadata).
§Errors
Returns an error if the request fails or the response contains an error payload.