1+ //! Python bindings client handler implementation.
2+ //!
3+ //! This module provides the `PyClientHandler` struct, which implements the `ClientHandler` trait for use in Python bindings.
4+ //! It allows sending and receiving messages, managing peers, and listing root messages in a client context.
5+ //!
6+ //! # Examples
7+ //!
8+ //! ```rust
9+ //! use bindings::python::client::PyClientHandler;
10+ //! let handler = PyClientHandler::new();
11+ //! ```
12+ #![ allow( non_local_definitions) ]
13+
14+ use rmcp:: service:: { RoleClient , RequestContext } ;
15+ use rmcp:: ClientHandler ;
16+ use rmcp:: model:: { CreateMessageRequestParam , SamplingMessage , Role , Content , CreateMessageResult , ListRootsResult } ;
17+ use std:: future:: Future ;
18+ use rmcp:: service:: Peer ;
19+
20+ /// A client handler for use in Python bindings.
21+ ///
22+ /// This struct manages an optional peer and implements the `ClientHandler` trait.
23+ #[ derive( Clone ) ]
24+ pub struct PyClientHandler {
25+ /// The current peer associated with this handler, if any.
26+ peer : Option < Peer < RoleClient > > ,
27+ }
28+
29+ impl PyClientHandler {
30+ /// Creates a new `PyClientHandler` with no peer set.
31+ ///
32+ /// # Examples
33+ ///
34+ /// ```rust
35+ /// let handler = PyClientHandler::new();
36+ /// assert!(handler.get_peer().is_none());
37+ /// ```
38+ pub fn new ( ) -> Self {
39+ Self {
40+ peer : None ,
41+ }
42+ }
43+ }
44+
45+ impl ClientHandler for PyClientHandler {
46+ /// Creates a message in response to a request.
47+ ///
48+ /// # Parameters
49+ /// - `_params`: The parameters for the message creation request.
50+ /// - `_context`: The request context.
51+ ///
52+ /// # Returns
53+ /// A future resolving to a `CreateMessageResult` containing the created message.
54+ ///
55+ /// # Examples
56+ ///
57+ /// ```rust
58+ /// // Usage in async context
59+ /// // let result = handler.create_message(params, context).await;
60+ /// ```
61+ fn create_message (
62+ & self ,
63+ _params : CreateMessageRequestParam ,
64+ _context : RequestContext < RoleClient > ,
65+ ) -> impl Future < Output = Result < CreateMessageResult , rmcp:: Error > > + Send + ' _ {
66+ // Create a default message for now
67+ let message = SamplingMessage {
68+ role : Role :: Assistant ,
69+ content : Content :: text ( "" . to_string ( ) ) ,
70+ } ;
71+ let result = CreateMessageResult {
72+ model : "default-model" . to_string ( ) ,
73+ stop_reason : None ,
74+ message,
75+ } ;
76+ std:: future:: ready ( Ok ( result) )
77+ }
78+
79+ /// Lists root messages for the client.
80+ ///
81+ /// # Parameters
82+ /// - `_context`: The request context.
83+ ///
84+ /// # Returns
85+ /// A future resolving to a `ListRootsResult` containing the list of root messages.
86+ ///
87+ /// # Examples
88+ ///
89+ /// ```rust
90+ /// // Usage in async context
91+ /// // let roots = handler.list_roots(context).await;
92+ /// ```
93+ fn list_roots (
94+ & self ,
95+ _context : RequestContext < RoleClient > ,
96+ ) -> impl Future < Output = Result < ListRootsResult , rmcp:: Error > > + Send + ' _ {
97+ // Return empty list for now
98+ std:: future:: ready ( Ok ( ListRootsResult { roots : vec ! [ ] } ) )
99+ }
100+
101+ /// Returns the current peer, if any.
102+ ///
103+ /// # Returns
104+ /// An `Option<Peer<RoleClient>>` containing the current peer if set.
105+ ///
106+ /// # Examples
107+ ///
108+ /// ```rust
109+ /// let peer = handler.get_peer();
110+ /// ```
111+ fn get_peer ( & self ) -> Option < Peer < RoleClient > > {
112+ self . peer . clone ( )
113+ }
114+
115+ /// Sets the current peer.
116+ ///
117+ /// # Parameters
118+ /// - `peer`: The peer to set for this handler.
119+ ///
120+ /// # Examples
121+ ///
122+ /// ```rust
123+ /// handler.set_peer(peer);
124+ /// ```
125+ fn set_peer ( & mut self , peer : Peer < RoleClient > ) {
126+ self . peer = Some ( peer) ;
127+ }
128+ }
0 commit comments