mcp_ectors/client/
client_session.rs

1use actix::prelude::*;
2use actix_web_lab::sse::{Event, Data as SseData};
3use mcp_spec::protocol::JsonRpcMessage;
4use tokio::sync::mpsc;
5
6/// Message to send data to a connected client via SSE
7#[derive(Message)]
8#[rtype(result = "()")]
9pub struct ClientSessionMessage(pub JsonRpcMessage);
10
11/// Manages an individual client session
12pub struct ClientSessionActor {
13    sender: mpsc::Sender<Event>, // SSE channel for pushing messages
14}
15
16impl ClientSessionActor {
17    pub fn new(sender: mpsc::Sender<Event>) -> Self {
18        Self { sender }
19    }
20}
21
22impl Actor for ClientSessionActor {
23    type Context = Context<Self>;
24}
25
26impl Handler<ClientSessionMessage> for ClientSessionActor {
27    type Result = ();
28
29    fn handle(&mut self, msg: ClientSessionMessage, _ctx: &mut Self::Context) {
30        let data = serde_json::to_string(&msg.0).unwrap_or_else(|_| "{}".to_string());
31        let event = Event::Data(SseData::new(data));
32
33        // Try sending the SSE event
34        let _ = self.sender.try_send(event);
35    }
36}