Skip to main content

prism_mcp_rs/client/
mod.rs

1//! MCP client implementation
2//!
3//! This module provides the main client implementation for the Model Context Protocol.
4//!
5//! # Architecture
6//!
7//! The client module consists of:
8//! - [`McpClient`]: Main client struct for protocol communication
9//! - [`McpClientBuilder`]: Fluent API for client configuration
10//! - [`ClientSession`]: Session management and state tracking
11//! - [`ClientRequestHandler`]: Interface for handling server requests
12//!
13//! # Usage Patterns
14//!
15//! ## Simple Client
16
17//! ```no_run
18//! use prism_mcp_rs::client::McpClient;
19//!
20//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
21//! let client = McpClient::new("my-client".to_string(), "1.0.0".to_string());
22//!
23//! // Connect and initialize
24//! // client.connect_stdio().await?;
25//! // client.initialize().await?;
26//! # Ok(())
27//! # }
28//! ```
29//! // EXAMPLE_END
30//!
31//! ## Using ClientBuilder
32
33//! ```
34//! use prism_mcp_rs::client::{McpClientBuilder, ConnectionConfig, RetryConfig};
35//! use std::time::Duration;
36//!
37//! let client = McpClientBuilder::new()
38//!     .name("my-client")
39//!     .version("2.0.0")
40//!     .timeout(Duration::from_secs(30))
41//!     .max_retries(3)
42//!     .validate_requests(true)
43//!     .validate_responses(true)
44//!     .build();
45//! ```
46//!
47//! ## Request Handlers
48//!
49//! Clients can handle requests from servers using request handlers:
50//!
51//! ```
52//! use prism_mcp_rs::client::{ClientRequestHandler, DefaultClientRequestHandler};
53//! use prism_mcp_rs::core::error::{McpError, McpResult};
54//! use prism_mcp_rs::protocol::messages::{CreateMessageParams, ListRootsParams, ListRootsResult, ElicitParams, ElicitResult, PingParams, PingResult};
55//! use prism_mcp_rs::protocol::types::{CreateMessageResult, Role, SamplingContent, StopReason, ElicitationAction};
56//! use prism_mcp_rs::protocol::{JsonRpcRequest, JsonRpcResponse};
57//! use async_trait::async_trait;
58//! use serde_json;
59//! use std::collections::HashMap;
60//!
61//! struct MyHandler;
62//!
63//! #[async_trait]
64//! impl ClientRequestHandler for MyHandler {
65//!     async fn handle_create_message(
66//!         &self,
67//!         params: CreateMessageParams,
68//!     ) -> McpResult<CreateMessageResult> {
69//!         // Handle server's request to create a message
70//!         Ok(CreateMessageResult {
71//!             model: "test-model".to_string(),
72//!             stop_reason: Some(StopReason::EndTurn),
73//!             role: Role::Assistant,
74//!             content: SamplingContent::Text {
75//!                 text: "Hello!".to_string(),
76//!                 annotations: None,
77//!                 meta: None,
78//!             },
79//!             meta: None,
80//!         })
81//!     }
82//!
83//!     async fn handle_list_roots(&self, _params: ListRootsParams) -> McpResult<ListRootsResult> {
84//!         Ok(ListRootsResult {
85//!             roots: vec![],
86//!             meta: None,
87//!         })
88//!     }
89//!
90//!     async fn handle_elicit(&self, _params: ElicitParams) -> McpResult<ElicitResult> {
91//!         Ok(ElicitResult {
92//!             action: ElicitationAction::Accept,
93//!             content: Some(HashMap::new()),
94//!             meta: None,
95//!         })
96//!     }
97//!
98//!     async fn handle_ping(&self, _params: PingParams) -> McpResult<PingResult> {
99//!         Ok(PingResult {
100//!             meta: None,
101//!         })
102//!     }
103//! }
104//! ```
105//!
106//! # Session Management
107//!
108//! The client maintains session state throughout the connection lifecycle:
109//!
110//! ```
111//! use prism_mcp_rs::client::{ClientSession, SessionState};
112//!
113//! # async fn example(client: prism_mcp_rs::client::McpClient) -> Result<(), Box<dyn std::error::Error>> {
114//! // Session state is managed internally
115//! // You can check connection status after connecting
116//! // let connected = client.is_connected();
117//! println!("Client configured and ready to connect");
118//! # Ok(())
119//! # }
120//! ```
121//!
122//! # Features
123//!
124//! - **Auto-reconnection**: Configurable retry with exponential backoff
125//! - **Request Handling**: Bidirectional communication support
126//! - **Session Tracking**: Automatic state management
127//! - **Multiple Transports**: STDIO, HTTP, WebSocket support
128//! - **Type Safety**: Strongly typed requests and responses
129
130pub mod enhanced_builder;
131pub mod enhanced_traits;
132pub mod fluent_interfaces;
133pub mod fluent_tools;
134pub mod mcp_client;
135pub mod request_handler;
136pub mod session;
137
138// Re-export enhanced APIs
139pub use enhanced_builder::{ConnectionConfig, McpClientBuilder, RetryConfig};
140pub use mcp_client::{ClientConfig, McpClient, TransportInfo, TransportUseCase};
141
142// Legacy re-exports for backward compatibility
143
144pub use request_handler::{
145    AutomatedClientRequestHandler, ClientRequestHandler, DefaultClientRequestHandler,
146    InteractiveClientRequestHandler,
147};
148pub use session::{ClientSession, SessionConfig, SessionState};
149
150// Legacy alias for test compatibility
151pub type ClientBuilder = McpClientBuilder;