mcpkit_testing/
lib.rs

1//! Testing utilities for the MCP SDK.
2//!
3//! This crate provides comprehensive testing infrastructure for MCP servers
4//! and clients, including:
5//!
6//! - **Mock servers and clients** for unit testing
7//! - **Test fixtures** with pre-configured tools/resources
8//! - **Custom assertions** for MCP-specific scenarios
9//! - **Scenario runner** for defining and executing test scenarios
10//! - **Async helpers** for testing async MCP code
11//! - **Session testing** with recording and validation
12//!
13//! # Overview
14//!
15//! ## Mock Server
16//!
17//! ```rust
18//! use mcpkit_testing::{MockServer, MockTool};
19//! use mcpkit_core::types::ToolOutput;
20//!
21//! let server = MockServer::builder()
22//!     .tool(MockTool::new("add")
23//!         .description("Add two numbers")
24//!         .handler(|args| Ok(ToolOutput::text("42"))))
25//!     .build();
26//!
27//! // Use in tests with MemoryTransport
28//! ```
29//!
30//! ## Mock Client
31//!
32//! ```rust
33//! use mcpkit_testing::MockClient;
34//!
35//! let client = MockClient::new()
36//!     .with_info("test-client", "1.0.0");
37//!
38//! let request = client.create_ping_request();
39//! ```
40//!
41//! ## Test Fixtures
42//!
43//! ```rust
44//! use mcpkit_testing::fixtures;
45//!
46//! let tools = fixtures::sample_tools();
47//! let resources = fixtures::sample_resources();
48//! ```
49//!
50//! ## Assertions
51//!
52//! ```rust
53//! use mcpkit_testing::assert_tool_result;
54//! use mcpkit_core::types::CallToolResult;
55//!
56//! let result = CallToolResult::text("42");
57//! assert_tool_result!(result, "42");
58//! ```
59//!
60//! ## Test Scenarios
61//!
62//! ```rust
63//! use mcpkit_testing::scenario::{TestScenario, ResponseMatcher};
64//! use mcpkit_core::protocol::Request;
65//!
66//! let scenario = TestScenario::new("ping-test")
67//!     .request(
68//!         Request::new("ping", 1),
69//!         ResponseMatcher::success(),
70//!     );
71//! ```
72//!
73//! ## Session Testing
74//!
75//! ```rust
76//! use mcpkit_testing::session::TestSession;
77//! use mcpkit_core::protocol::{Message, Request, Response, RequestId};
78//!
79//! let session = TestSession::new("my-test");
80//! session.record_outbound(Message::Request(Request::new("ping", 1)));
81//! session.record_inbound(Message::Response(Response::success(
82//!     RequestId::from(1),
83//!     serde_json::json!({}),
84//! )));
85//! let result = session.finalize();
86//! result.assert_valid();
87//! ```
88
89#![deny(missing_docs)]
90
91pub mod assertions;
92pub mod async_helpers;
93pub mod client;
94pub mod fixtures;
95pub mod mock;
96pub mod scenario;
97pub mod session;
98
99// Re-export commonly used types
100pub use assertions::{assert_tool_error, assert_tool_success};
101pub use client::MockClient;
102pub use fixtures::{sample_resources, sample_tools};
103pub use mock::{MockServer, MockServerBuilder, MockTool};
104pub use scenario::{ResponseMatcher, TestScenario};
105pub use session::{TestSession, TestSessionResult};
106
107/// Prelude module for convenient imports.
108pub mod prelude {
109    pub use crate::assertions::{assert_tool_error, assert_tool_success};
110    pub use crate::async_helpers::{
111        TestBarrier, TestLatch, retry, wait_for, with_default_timeout, with_timeout,
112    };
113    pub use crate::client::MockClient;
114    pub use crate::fixtures::{sample_resources, sample_tools};
115    pub use crate::mock::{MockPrompt, MockResource, MockServer, MockServerBuilder, MockTool};
116    pub use crate::scenario::{
117        MessageQueue, NotificationMatcher, ResponseMatcher, TestScenario, TestStep,
118    };
119    pub use crate::session::{TestSession, TestSessionBuilder, TestSessionResult};
120}