Skip to main content

mcp/
events.rs

1//! MCP-specific event types and observer trait.
2
3use async_trait::async_trait;
4
5/// Events emitted by MCP servers and MCP HTTP clients.
6#[derive(Debug, Clone)]
7pub enum McpEvent {
8    /// The HTTP server started listening on the supplied address.
9    ServerStarted {
10        /// Bound socket address.
11        addr: String,
12    },
13    /// A tool-list request was received from a client.
14    ToolListRequested {
15        /// Client IP address.
16        client_addr: String,
17    },
18    /// A tool-list response was returned to the client.
19    ToolListReturned {
20        /// Client IP address.
21        client_addr: String,
22        /// Number of tools returned.
23        tool_count: usize,
24    },
25    /// A tool execution request was received from a client.
26    ToolCallReceived {
27        /// Client IP address.
28        client_addr: String,
29        /// Requested tool name.
30        tool_name: String,
31        /// Raw JSON parameters.
32        parameters: serde_json::Value,
33    },
34    /// A server-side tool execution completed.
35    ToolCallCompleted {
36        /// Client IP address.
37        client_addr: String,
38        /// Tool name.
39        tool_name: String,
40        /// Whether execution succeeded.
41        success: bool,
42        /// Optional error message.
43        error: Option<String>,
44        /// Execution duration in milliseconds.
45        duration_ms: u64,
46    },
47    /// A tool call failed at the transport or protocol layer.
48    ToolError {
49        /// Client IP or remote endpoint URL.
50        source: String,
51        /// Tool name.
52        tool_name: String,
53        /// Error message.
54        error: String,
55        /// Elapsed time in milliseconds.
56        duration_ms: u64,
57    },
58    /// A request was rejected before tool execution.
59    RequestRejected {
60        /// Client IP address.
61        client_addr: String,
62        /// Human-readable rejection reason.
63        reason: String,
64    },
65    /// An MCP client initialized successfully.
66    ConnectionInitialized {
67        /// Remote endpoint URL.
68        endpoint: String,
69        /// Number of discovered tools.
70        tool_count: usize,
71    },
72    /// An MCP client connection was closed.
73    ConnectionClosed {
74        /// Remote endpoint URL.
75        endpoint: String,
76    },
77    /// The tool cache was refreshed from the remote endpoint.
78    ToolsDiscovered {
79        /// Remote endpoint URL.
80        endpoint: String,
81        /// Number of discovered tools.
82        tool_count: usize,
83        /// Discovered tool names.
84        tool_names: Vec<String>,
85    },
86    /// Cached tool metadata was used.
87    CacheHit {
88        /// Remote endpoint URL.
89        endpoint: String,
90        /// Number of cached tools.
91        tool_count: usize,
92    },
93    /// Cached tool metadata expired.
94    CacheExpired {
95        /// Remote endpoint URL.
96        endpoint: String,
97    },
98    /// A remote tool call is being dispatched.
99    RemoteToolCallStarted {
100        /// Remote endpoint URL.
101        endpoint: String,
102        /// Tool name.
103        tool_name: String,
104        /// JSON parameters.
105        parameters: serde_json::Value,
106    },
107    /// A remote tool call completed.
108    RemoteToolCallCompleted {
109        /// Remote endpoint URL.
110        endpoint: String,
111        /// Tool name.
112        tool_name: String,
113        /// Whether the tool result succeeded.
114        success: bool,
115        /// Optional error message.
116        error: Option<String>,
117        /// Round-trip duration in milliseconds.
118        duration_ms: u64,
119    },
120}
121
122/// Narrow observer interface for MCP lifecycle events.
123#[async_trait]
124pub trait McpEventHandler: Send + Sync {
125    /// Observe an MCP event.
126    async fn on_mcp_event(&self, _event: &McpEvent) {}
127}