feat: implement MCP streamable HTTP server
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use tokio::sync::RwLock;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SessionStore {
|
||||
inner: Arc<RwLock<HashMap<String, SessionState>>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SessionState {
|
||||
pub protocol_version: String,
|
||||
pub initialized: bool,
|
||||
}
|
||||
|
||||
impl SessionStore {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: Arc::new(RwLock::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create(&self, protocol_version: &str) -> String {
|
||||
let session_id = Uuid::now_v7().to_string();
|
||||
let mut guard = self.inner.write().await;
|
||||
|
||||
guard.insert(
|
||||
session_id.clone(),
|
||||
SessionState {
|
||||
protocol_version: protocol_version.to_owned(),
|
||||
initialized: false,
|
||||
},
|
||||
);
|
||||
|
||||
session_id
|
||||
}
|
||||
|
||||
pub async fn get(&self, session_id: &str) -> Option<SessionState> {
|
||||
let guard = self.inner.read().await;
|
||||
guard.get(session_id).cloned()
|
||||
}
|
||||
|
||||
pub async fn mark_initialized(&self, session_id: &str) -> bool {
|
||||
let mut guard = self.inner.write().await;
|
||||
|
||||
if let Some(session) = guard.get_mut(session_id) {
|
||||
session.initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub async fn delete(&self, session_id: &str) -> bool {
|
||||
let mut guard = self.inner.write().await;
|
||||
guard.remove(session_id).is_some()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user