Skip to main content

TestClient

Struct TestClient 

Source
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

Source

pub fn new(transport: MemoryTransport) -> Self

Creates a new test client with the given transport.

§Example
let (client_transport, server_transport) = create_memory_transport_pair();
let client = TestClient::new(client_transport);
Source

pub fn with_cx(transport: MemoryTransport, cx: Cx) -> Self

Creates a new test client with custom Cx.

Source

pub fn with_client_info( self, name: impl Into<String>, version: impl Into<String>, ) -> Self

Sets the client info for initialization.

Source

pub fn with_capabilities(self, capabilities: ClientCapabilities) -> Self

Sets the client capabilities for initialization.

Source

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.

Source

pub fn is_initialized(&self) -> bool

Returns whether the client has been initialized.

Source

pub fn server_info(&self) -> Option<&ServerInfo>

Returns the server info after initialization.

Source

pub fn server_capabilities(&self) -> Option<&ServerCapabilities>

Returns the server capabilities after initialization.

Source

pub fn protocol_version(&self) -> Option<&str>

Returns the protocol version after initialization.

Source

pub fn list_tools(&mut self) -> McpResult<Vec<Tool>>

Lists available tools.

§Errors

Returns an error if the request fails.

Source

pub fn call_tool( &mut self, name: &str, arguments: Value, ) -> McpResult<Vec<Content>>

Calls a tool with the given arguments.

§Errors

Returns an error if the tool call fails.

Source

pub fn list_resources(&mut self) -> McpResult<Vec<Resource>>

Lists available resources.

§Errors

Returns an error if the request fails.

Source

pub fn list_resource_templates(&mut self) -> McpResult<Vec<ResourceTemplate>>

Lists available resource templates.

§Errors

Returns an error if the request fails.

Source

pub fn read_resource(&mut self, uri: &str) -> McpResult<Vec<ResourceContent>>

Reads a resource by URI.

§Errors

Returns an error if the resource cannot be read.

Source

pub fn list_prompts(&mut self) -> McpResult<Vec<Prompt>>

Lists available prompts.

§Errors

Returns an error if the request fails.

Source

pub fn get_prompt( &mut self, name: &str, arguments: HashMap<String, String>, ) -> McpResult<Vec<PromptMessage>>

Gets a prompt with the given arguments.

§Errors

Returns an error if the prompt cannot be retrieved.

Source

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.

Source

pub fn close(&mut self)

Closes the client connection.

Source

pub fn transport(&self) -> &MemoryTransport

Returns a reference to the transport for advanced testing.

Source

pub fn transport_mut(&mut self) -> &mut MemoryTransport

Returns a mutable reference to the transport for advanced testing.

Source

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.

Trait Implementations§

Source§

impl Debug for TestClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more