Expand description
Testing utilities for the MCP SDK.
This crate provides comprehensive testing infrastructure for MCP servers and clients, including:
- Mock servers and clients for unit testing
- Test fixtures with pre-configured tools/resources
- Custom assertions for MCP-specific scenarios
- Scenario runner for defining and executing test scenarios
- Async helpers for testing async MCP code
- Session testing with recording and validation
§Overview
§Mock Server
use mcpkit_testing::{MockServer, MockTool};
use mcpkit_core::types::ToolOutput;
let server = MockServer::builder()
.tool(MockTool::new("add")
.description("Add two numbers")
.handler(|args| Ok(ToolOutput::text("42"))))
.build();
// Use in tests with MemoryTransport§Mock Client
use mcpkit_testing::MockClient;
let client = MockClient::new()
.with_info("test-client", "1.0.0");
let request = client.create_ping_request();§Test Fixtures
use mcpkit_testing::fixtures;
let tools = fixtures::sample_tools();
let resources = fixtures::sample_resources();§Assertions
use mcpkit_testing::assert_tool_result;
use mcpkit_core::types::CallToolResult;
let result = CallToolResult::text("42");
assert_tool_result!(result, "42");§Test Scenarios
use mcpkit_testing::scenario::{TestScenario, ResponseMatcher};
use mcpkit_core::protocol::Request;
let scenario = TestScenario::new("ping-test")
.request(
Request::new("ping", 1),
ResponseMatcher::success(),
);§Session Testing
use mcpkit_testing::session::TestSession;
use mcpkit_core::protocol::{Message, Request, Response, RequestId};
let session = TestSession::new("my-test");
session.record_outbound(Message::Request(Request::new("ping", 1)));
session.record_inbound(Message::Response(Response::success(
RequestId::from(1),
serde_json::json!({}),
)));
let result = session.finalize();
result.assert_valid();Re-exports§
pub use assertions::assert_tool_error;pub use assertions::assert_tool_success;pub use client::MockClient;pub use fixtures::sample_resources;pub use fixtures::sample_tools;pub use mock::MockServer;pub use mock::MockServerBuilder;pub use mock::MockTool;pub use scenario::ResponseMatcher;pub use scenario::TestScenario;pub use session::TestSession;pub use session::TestSessionResult;
Modules§
- assertions
- Custom assertions for MCP testing.
- async_
helpers - Async testing utilities.
- client
- Mock client for testing MCP servers.
- fixtures
- Test fixtures for MCP testing.
- mock
- Mock implementations for testing.
- prelude
- Prelude module for convenient imports.
- scenario
- Test scenario runner for MCP protocol testing.
- session
- Session testing utilities.
Macros§
- assert_
tool_ error_ msg - Macro for asserting tool result error.
- assert_
tool_ result - Macro for asserting tool result success.