Skip to content

Commit 81d125f

Browse files
committed
propagates appropriate messages
1 parent 957d1f0 commit 81d125f

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

crates/agent/src/agent/mcp/mod.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub mod types;
110110

111111
use std::collections::HashMap;
112112
use std::path::PathBuf;
113+
use std::time::Duration;
113114

114115
use actor::{
115116
McpServerActor,
@@ -254,8 +255,11 @@ impl McpManagerHandle {
254255
}
255256
}
256257

257-
pub async fn recv(&mut self) -> Result<McpServerActorEvent, RecvError> {
258-
self.mcp_main_loop_to_handle_server_event_rx.recv().await
258+
pub async fn recv(&mut self) -> Result<McpServerEvent, RecvError> {
259+
self.mcp_main_loop_to_handle_server_event_rx
260+
.recv()
261+
.await
262+
.map(|evt| evt.into())
259263
}
260264
}
261265

@@ -464,3 +468,49 @@ pub enum McpManagerError {
464468
#[error("{}", .0)]
465469
Custom(String),
466470
}
471+
472+
/// MCP events relevant to agent operations.
473+
/// Provides abstraction over [McpServerActorEvent] to avoid leaking implementation details.
474+
#[derive(Debug, Clone, Serialize, Deserialize)]
475+
pub enum McpServerEvent {
476+
/// The MCP server has launched successfully
477+
Initialized {
478+
server_name: String,
479+
/// Time taken to launch the server
480+
serve_duration: Duration,
481+
/// Time taken to list all tools.
482+
///
483+
/// None if the server does not support tools, or there was an error fetching tools.
484+
list_tools_duration: Option<Duration>,
485+
/// Time taken to list all prompts
486+
///
487+
/// None if the server does not support prompts, or there was an error fetching prompts.
488+
list_prompts_duration: Option<Duration>,
489+
},
490+
/// The MCP server failed to initialize successfully
491+
InitializeError { server_name: String, error: String },
492+
/// An OAuth authentication request from the MCP server
493+
OauthRequest { server_name: String, oauth_url: String },
494+
}
495+
496+
impl From<McpServerActorEvent> for McpServerEvent {
497+
fn from(value: McpServerActorEvent) -> Self {
498+
match value {
499+
McpServerActorEvent::Initialized {
500+
server_name,
501+
serve_duration,
502+
list_tools_duration,
503+
list_prompts_duration,
504+
} => Self::Initialized {
505+
server_name,
506+
serve_duration,
507+
list_tools_duration,
508+
list_prompts_duration,
509+
},
510+
McpServerActorEvent::InitializeError { server_name, error } => Self::InitializeError { server_name, error },
511+
McpServerActorEvent::OauthRequest { server_name, oauth_url } => {
512+
Self::OauthRequest { server_name, oauth_url }
513+
},
514+
}
515+
}
516+
}

crates/agent/src/agent/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use agent_loop::{
5858
use chrono::Utc;
5959
use consts::MAX_RESOURCE_FILE_LENGTH;
6060
use futures::stream::FuturesUnordered;
61-
use mcp::actor::McpServerActorEvent;
61+
use mcp::McpServerEvent;
6262
use permissions::evaluate_tool_permission;
6363
use protocol::{
6464
AgentError,
@@ -494,7 +494,7 @@ impl Agent {
494494
evt = self.mcp_manager_handle.recv() => {
495495
match evt {
496496
Ok(evt) => {
497-
self.handle_mcp_server_actor_events(evt).await;
497+
self.handle_mcp_events(evt).await;
498498
},
499499
Err(e) => {
500500
error!(?e, "mcp manager handle closed");
@@ -1698,11 +1698,9 @@ impl Agent {
16981698
Ok(())
16991699
}
17001700

1701-
async fn handle_mcp_server_actor_events(&self, evt: McpServerActorEvent) {
1701+
async fn handle_mcp_events(&mut self, evt: McpServerEvent) {
17021702
let converted_evt = AgentEvent::Mcp(evt);
1703-
if let Err(e) = self.agent_event_tx.send(converted_evt) {
1704-
error!(?e, "failed to emit agent event");
1705-
}
1703+
self.agent_event_buf.push(converted_evt);
17061704
}
17071705
}
17081706

crates/agent/src/agent/protocol.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ use super::agent_loop::types::{
1717
ImageBlock,
1818
ToolUseBlock,
1919
};
20-
use super::mcp::McpManagerError;
21-
use super::mcp::actor::McpServerActorEvent;
2220
use super::mcp::types::Prompt;
21+
use super::mcp::{
22+
McpManagerError,
23+
McpServerEvent,
24+
};
2325
use super::task_executor::TaskExecutorEvent;
2426
use super::tools::{
2527
Tool,
@@ -76,7 +78,7 @@ pub enum AgentEvent {
7678
Internal(InternalEvent),
7779

7880
/// Events from MCP (Model Context Protocol) servers
79-
Mcp(McpServerActorEvent),
81+
Mcp(McpServerEvent),
8082
}
8183

8284
impl From<TaskExecutorEvent> for AgentEvent {

0 commit comments

Comments
 (0)