11use std:: collections:: HashMap ;
2+ use std:: path:: PathBuf ;
23use std:: sync:: Arc ;
34use std:: time:: Duration ;
45
@@ -44,20 +45,16 @@ pub enum McpMessage {
4445 Tools ( Result < Vec < RmcpTool > , ServiceError > ) ,
4546 Prompts ( Result < Vec < RmcpPrompt > , ServiceError > ) ,
4647 ExecuteTool { request_id : u32 , result : ExecuteToolResult } ,
48+ OauthRequest { oauth_url : String } ,
4749}
4850
4951#[ derive( Debug ) ]
5052pub struct McpServerActorHandle {
5153 _server_name : String ,
5254 sender : RequestSender < McpServerActorRequest , McpServerActorResponse , McpServerActorError > ,
53- event_rx : mpsc:: Receiver < McpServerActorEvent > ,
5455}
5556
5657impl McpServerActorHandle {
57- pub async fn recv ( & mut self ) -> Option < McpServerActorEvent > {
58- self . event_rx . recv ( ) . await
59- }
60-
6158 pub async fn get_tool_specs ( & self ) -> Result < Vec < ToolSpec > , McpServerActorError > {
6259 match self
6360 . sender
@@ -153,6 +150,7 @@ impl From<ServiceError> for McpServerActorError {
153150pub enum McpServerActorEvent {
154151 /// The MCP server has launched successfully
155152 Initialized {
153+ server_name : String ,
156154 /// Time taken to launch the server
157155 serve_duration : Duration ,
158156 /// Time taken to list all tools.
@@ -165,7 +163,9 @@ pub enum McpServerActorEvent {
165163 list_prompts_duration : Option < Duration > ,
166164 } ,
167165 /// The MCP server failed to initialize successfully
168- InitializeError ( String ) ,
166+ InitializeError { server_name : String , error : String } ,
167+ /// An OAuth authentication request from the MCP server
168+ OauthRequest { server_name : String , oauth_url : String } ,
169169}
170170
171171#[ derive( Debug ) ]
@@ -195,34 +195,38 @@ pub struct McpServerActor {
195195
196196impl McpServerActor {
197197 /// Spawns an actor to manage the MCP server, returning a [McpServerActorHandle].
198- pub fn spawn ( server_name : String , config : McpServerConfig ) -> McpServerActorHandle {
199- let ( event_tx, event_rx) = mpsc:: channel ( 32 ) ;
198+ pub fn spawn (
199+ server_name : String ,
200+ config : McpServerConfig ,
201+ cred_path : PathBuf ,
202+ event_tx : mpsc:: Sender < McpServerActorEvent > ,
203+ ) -> McpServerActorHandle {
200204 let ( req_tx, req_rx) = new_request_channel ( ) ;
201205
202206 let server_name_clone = server_name. clone ( ) ;
203- tokio:: spawn ( async move { Self :: launch ( server_name_clone, config, req_rx, event_tx) . await } ) ;
207+ tokio:: spawn ( async move { Self :: launch ( server_name_clone, config, cred_path , req_rx, event_tx) . await } ) ;
204208
205209 McpServerActorHandle {
206210 _server_name : server_name,
207211 sender : req_tx,
208- event_rx,
209212 }
210213 }
211214
212215 async fn launch (
213216 server_name : String ,
214217 config : McpServerConfig ,
218+ cred_path : PathBuf ,
215219 req_rx : RequestReceiver < McpServerActorRequest , McpServerActorResponse , McpServerActorError > ,
216220 event_tx : mpsc:: Sender < McpServerActorEvent > ,
217221 ) {
218222 let ( message_tx, message_rx) = mpsc:: channel ( 32 ) ;
219- match McpService :: new ( server_name. clone ( ) , config. clone ( ) , message_tx. clone ( ) )
223+ match McpService :: new ( server_name. clone ( ) , config. clone ( ) , cred_path , message_tx. clone ( ) )
220224 . launch ( )
221225 . await
222226 {
223227 Ok ( ( service_handle, launch_md) ) => {
224228 let s = Self {
225- server_name,
229+ server_name : server_name . clone ( ) ,
226230 _config : config,
227231 tools : launch_md. tools . unwrap_or_default ( ) ,
228232 prompts : launch_md. prompts . unwrap_or_default ( ) ,
@@ -237,6 +241,7 @@ impl McpServerActor {
237241 let _ = s
238242 . event_tx
239243 . send ( McpServerActorEvent :: Initialized {
244+ server_name,
240245 serve_duration : launch_md. serve_time_taken ,
241246 list_tools_duration : launch_md. list_tools_duration ,
242247 list_prompts_duration : launch_md. list_prompts_duration ,
@@ -246,7 +251,10 @@ impl McpServerActor {
246251 } ,
247252 Err ( err) => {
248253 let _ = event_tx
249- . send ( McpServerActorEvent :: InitializeError ( err. to_string ( ) ) )
254+ . send ( McpServerActorEvent :: InitializeError {
255+ server_name,
256+ error : err. to_string ( ) ,
257+ } )
250258 . await ;
251259 } ,
252260 }
@@ -331,6 +339,18 @@ impl McpServerActor {
331339 ) ;
332340 } ,
333341 } ,
342+ McpMessage :: OauthRequest { oauth_url } => {
343+ if let Err ( err) = self
344+ . event_tx
345+ . send ( McpServerActorEvent :: OauthRequest {
346+ server_name : self . server_name . clone ( ) ,
347+ oauth_url,
348+ } )
349+ . await
350+ {
351+ error ! ( ?self . server_name, ?err, "failed to send oauth request" ) ;
352+ }
353+ } ,
334354 }
335355 }
336356
@@ -340,7 +360,7 @@ impl McpServerActor {
340360 let service_handle = self . service_handle . clone ( ) ;
341361 let tx = self . message_tx . clone ( ) ;
342362 tokio:: spawn ( async move {
343- let res = service_handle. list_tools ( ) . await ;
363+ let res = service_handle. list_all_tools ( ) . await ;
344364 let _ = tx. send ( McpMessage :: Tools ( res) ) . await ;
345365 } ) ;
346366 }
@@ -351,7 +371,7 @@ impl McpServerActor {
351371 let service_handle = self . service_handle . clone ( ) ;
352372 let tx = self . message_tx . clone ( ) ;
353373 tokio:: spawn ( async move {
354- let res = service_handle. list_prompts ( ) . await ;
374+ let res = service_handle. list_all_prompts ( ) . await ;
355375 let _ = tx. send ( McpMessage :: Prompts ( res) ) . await ;
356376 } ) ;
357377 }
0 commit comments