Skip to content

Commit 2918559

Browse files
committed
propagates appropriate messages
1 parent 3e0e7bd commit 2918559

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,
@@ -251,8 +252,11 @@ impl McpManagerHandle {
251252
}
252253
}
253254

254-
pub async fn recv(&mut self) -> Result<McpServerActorEvent, RecvError> {
255-
self.mcp_main_loop_to_handle_server_event_rx.recv().await
255+
pub async fn recv(&mut self) -> Result<McpServerEvent, RecvError> {
256+
self.mcp_main_loop_to_handle_server_event_rx
257+
.recv()
258+
.await
259+
.map(|evt| evt.into())
256260
}
257261
}
258262

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

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");
@@ -1694,11 +1694,9 @@ impl Agent {
16941694
Ok(())
16951695
}
16961696

1697-
async fn handle_mcp_server_actor_events(&self, evt: McpServerActorEvent) {
1697+
async fn handle_mcp_events(&mut self, evt: McpServerEvent) {
16981698
let converted_evt = AgentEvent::Mcp(evt);
1699-
if let Err(e) = self.agent_event_tx.send(converted_evt) {
1700-
error!(?e, "failed to emit agent event");
1701-
}
1699+
self.agent_event_buf.push(converted_evt);
17021700
}
17031701
}
17041702

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)