mcp_runner/sse_proxy/types.rs
1//! Type definitions for the SSE proxy implementation.
2//!
3//! This module contains the core data structures used by the SSE proxy.
4
5use crate::server::ServerId;
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9/// Server information
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct ServerInfo {
12 /// Server name
13 pub name: String,
14 /// Server ID (as string for serialization)
15 pub id: String,
16 /// Server status (as string for serialization)
17 pub status: String,
18}
19
20/// Resource information
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct ResourceInfo {
23 /// Resource name/ID
24 pub name: String,
25 /// Resource description
26 pub description: Option<String>,
27 /// Resource metadata
28 pub metadata: Option<HashMap<String, serde_json::Value>>,
29}
30
31/// Tool information
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct ToolInfo {
34 /// Tool name
35 pub name: String,
36 /// Tool description
37 pub description: String,
38 /// Tool parameters schema
39 pub parameters: Option<serde_json::Value>,
40 /// Tool return type
41 pub return_type: Option<String>,
42}
43
44/// Server-Sent Event types
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(tag = "type")]
47pub enum SSEEvent {
48 /// Tool response event
49 #[serde(rename = "tool-response")]
50 ToolResponse {
51 /// Request ID for correlation
52 request_id: String,
53 /// Server ID that processed the request
54 server_id: String,
55 /// Tool name that was called
56 tool_name: String,
57 /// Response data
58 data: serde_json::Value,
59 },
60
61 /// Tool error event
62 #[serde(rename = "tool-error")]
63 ToolError {
64 /// Request ID for correlation
65 request_id: String,
66 /// Server ID that processed the request
67 server_id: String,
68 /// Tool name that was called
69 tool_name: String,
70 /// Error message
71 error: String,
72 },
73
74 /// Server status update event
75 #[serde(rename = "server-status")]
76 ServerStatus {
77 /// Server name
78 server_name: String,
79 /// Server ID
80 server_id: String,
81 /// Status description
82 status: String,
83 },
84
85 /// General notification event
86 #[serde(rename = "notification")]
87 Notification {
88 /// Notification title
89 title: String,
90 /// Notification message
91 message: String,
92 /// Notification level (info, warn, error)
93 level: String,
94 },
95}
96
97/// Server-Sent Event message
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct SSEMessage {
100 /// Event type
101 pub event: String,
102 /// Event data as JSON string
103 pub data: String,
104 /// Optional event ID
105 pub id: Option<String>,
106}
107
108impl SSEMessage {
109 /// Creates a new SSE message with the given event type, data payload, and optional ID
110 ///
111 /// # Arguments
112 ///
113 /// * `event` - The event type (e.g., "tool-response", "tool-error", "server-status")
114 /// * `data` - The data payload as a JSON string
115 /// * `id` - Optional event ID for correlation
116 ///
117 /// # Returns
118 ///
119 /// A new `SSEMessage` instance
120 pub fn new(event: &str, data: &str, id: Option<&str>) -> Self {
121 Self {
122 event: event.to_string(),
123 data: data.to_string(),
124 id: id.map(String::from),
125 }
126 }
127}
128
129/// Server information update message sent between
130/// McpRunner and the SSEProxy
131#[derive(Debug, Clone)]
132pub enum ServerInfoUpdate {
133 /// Update information about a specific server
134 UpdateServer {
135 /// Server name
136 name: String,
137 /// Server ID
138 id: Option<ServerId>,
139 /// Server status
140 status: String,
141 },
142
143 /// Add a new server to the proxy cache
144 AddServer {
145 /// Server name
146 name: String,
147 /// Server information
148 info: ServerInfo,
149 },
150
151 /// Shutdown the proxy
152 Shutdown,
153}