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
impl TestClient
Sourcepub fn from_router(router: McpRouter) -> Self
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.
Sourcepub async fn initialize(&mut self) -> Value
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.
Sourcepub async fn list_tools(&mut self) -> Vec<Value>
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.
Sourcepub async fn call_tool(&mut self, name: &str, args: Value) -> CallToolResult
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"));Sourcepub async fn call_tool_raw(&mut self, name: &str, args: Value) -> Value
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.
Sourcepub async fn call_tool_json(&mut self, name: &str, args: Value) -> Value
pub async fn call_tool_json(&mut self, name: &str, args: Value) -> Value
Sourcepub async fn call_tool_typed<T: DeserializeOwned>(
&mut self,
name: &str,
args: Value,
) -> T
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);Sourcepub async fn call_tool_expect_error(&mut self, name: &str, args: Value) -> Value
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).
Sourcepub async fn list_resources(&mut self) -> Vec<Value>
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.
Sourcepub async fn read_resource(&mut self, uri: &str) -> ReadResourceResult
pub async fn read_resource(&mut self, uri: &str) -> ReadResourceResult
Read a resource by URI.
Returns a typed ReadResourceResult. Panics on JSON-RPC errors.
Sourcepub async fn list_prompts(&mut self) -> Vec<Value>
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.
Sourcepub async fn get_prompt(
&mut self,
name: &str,
args: HashMap<String, String>,
) -> GetPromptResult
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.
Sourcepub async fn complete(&mut self, params: Value) -> Value
pub async fn complete(&mut self, params: Value) -> Value
Send a completion request with raw parameters.
Returns the raw JSON result. Panics on error.
Sourcepub async fn send_request(
&mut self,
method: &str,
params: Option<Value>,
) -> Value
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.
Sourcepub async fn send_request_expect_error(
&mut self,
method: &str,
params: Option<Value>,
) -> Value
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.
Sourcepub fn try_recv_notification(&mut self) -> Option<ServerNotification>
pub fn try_recv_notification(&mut self) -> Option<ServerNotification>
Try to receive a notification without blocking.
Returns None if no notification is available.
Sourcepub fn drain_notifications(&mut self) -> Vec<ServerNotification>
pub fn drain_notifications(&mut self) -> Vec<ServerNotification>
Drain all pending notifications.
Returns all notifications that have been sent since the last drain.