Skip to main content

TestClient

Struct TestClient 

Source
pub struct TestClient { /* private fields */ }
Expand description

An ergonomic test client for MCP servers.

Wraps an McpRouter and JsonRpcService to provide typed, concise methods for testing MCP server behavior. All methods that expect successful responses will panic on JSON-RPC errors, which is appropriate for test code.

§Construction

Use TestClient::from_router to create a client from an existing router:

use tower_mcp::{McpRouter, TestClient};

let router = McpRouter::new().server_info("test", "1.0.0");
let mut client = TestClient::from_router(router);

Implementations§

Source§

impl TestClient

Source

pub fn from_router(router: McpRouter) -> Self

Create a new test client from an McpRouter.

Sets up a notification channel and wraps the router in a JsonRpcService for JSON-RPC framing.

Source

pub async fn initialize(&mut self) -> Value

Send an initialize request and the initialized notification.

Returns the raw JSON result from the initialize response. Panics if initialization fails.

Source

pub async fn list_tools(&mut self) -> Vec<Value>

List all tools registered on the server.

Returns the tools array from the response. Panics on error.

Source

pub async fn call_tool(&mut self, name: &str, args: Value) -> CallToolResult

Call a tool by name with the given arguments.

Returns a typed CallToolResult. Panics on JSON-RPC errors.

§Example
let result = client.call_tool("echo", json!({"message": "hi"})).await;
assert_eq!(result.first_text(), Some("hi"));
Source

pub async fn call_tool_raw(&mut self, name: &str, args: Value) -> Value

Call a tool and return the raw JSON response.

Useful when you need to inspect fields not covered by CallToolResult.

Source

pub async fn call_tool_json(&mut self, name: &str, args: Value) -> Value

Call a tool and parse the result as a JSON Value.

Panics if the tool call fails, returns an error, or has no parseable content.

§Example
let value = client.call_tool_json("search", json!({"q": "rust"})).await;
assert!(value["results"].is_array());
Source

pub async fn call_tool_typed<T: DeserializeOwned>( &mut self, name: &str, args: Value, ) -> T

Call a tool and deserialize the result into a typed value.

Panics if the tool call fails, returns an error, or deserialization fails.

§Example
let result: SearchResult = client.call_tool_typed("search", json!({"q": "rust"})).await;
assert!(result.count > 0);
Source

pub async fn call_tool_expect_error(&mut self, name: &str, args: Value) -> Value

Call a tool and assert that it returns an error.

Panics if the tool call succeeds without an error. Returns the raw JSON response body (which may be a CallToolResult with isError: true or a JSON-RPC error).

Source

pub async fn list_resources(&mut self) -> Vec<Value>

List all resources registered on the server.

Returns the resources array from the response. Panics on error.

Source

pub async fn read_resource(&mut self, uri: &str) -> ReadResourceResult

Read a resource by URI.

Returns a typed ReadResourceResult. Panics on JSON-RPC errors.

Source

pub async fn list_prompts(&mut self) -> Vec<Value>

List all prompts registered on the server.

Returns the prompts array from the response. Panics on error.

Source

pub async fn get_prompt( &mut self, name: &str, args: HashMap<String, String>, ) -> GetPromptResult

Get a prompt by name with the given arguments.

Returns a typed GetPromptResult. Panics on JSON-RPC errors.

Source

pub async fn complete(&mut self, params: Value) -> Value

Send a completion request with raw parameters.

Returns the raw JSON result. Panics on error.

Source

pub async fn send_request( &mut self, method: &str, params: Option<Value>, ) -> Value

Send an arbitrary request and expect success.

This is an escape hatch for methods not covered by the typed helpers. Panics on JSON-RPC errors.

Source

pub async fn send_request_expect_error( &mut self, method: &str, params: Option<Value>, ) -> Value

Send an arbitrary request and expect a JSON-RPC error.

Panics if the response is a success. Returns the error object as JSON.

Source

pub fn try_recv_notification(&mut self) -> Option<ServerNotification>

Try to receive a notification without blocking.

Returns None if no notification is available.

Source

pub fn drain_notifications(&mut self) -> Vec<ServerNotification>

Drain all pending notifications.

Returns all notifications that have been sent since the last drain.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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