use std::{
collections::HashSet,
sync::{
Arc, Mutex, RwLock,
atomic::{AtomicBool, Ordering},
},
};
use super::Scope;
use crate::core::NodeId;
pub enum McpToolOutput {
Text(String),
Json(serde_json::Value),
Image { data: Vec<u8>, mime: &'static str },
}
pub type McpToolResult = Result<McpToolOutput, String>;
pub(crate) type McpReply = tokio::sync::oneshot::Sender<McpToolResult>;
pub(crate) struct McpRequest {
pub(crate) tool: String,
pub(crate) args: serde_json::Value,
pub(crate) reply: McpReply,
}
#[derive(Clone)]
pub(crate) struct RefRecord {
pub(crate) id: String,
pub(crate) window: String,
pub(crate) node_id: NodeId,
pub(crate) tag: String,
pub(crate) text: Option<String>,
pub(crate) element_id: Option<String>,
pub(crate) classes: Vec<String>,
pub(crate) attrs: Vec<(String, String)>,
pub(crate) bounds: [f32; 4],
pub(crate) interactive: bool,
}
#[derive(Default)]
pub(crate) struct RefTable {
pub(crate) next_ref: u64,
pub(crate) records: Vec<RefRecord>,
}
impl RefTable {
pub(crate) fn mint(&mut self) -> String {
self.next_ref += 1;
format!("ref_{}", self.next_ref)
}
pub(crate) fn replace_window(&mut self, window: &str, records: Vec<RefRecord>) {
self.records.retain(|record| record.window != window);
self.records.extend(records);
}
pub(crate) fn append(&mut self, records: Vec<RefRecord>) {
self.records.extend(records);
}
pub(crate) fn get(&self, id: &str) -> Option<&RefRecord> {
self.records.iter().find(|record| record.id == id)
}
}
pub(crate) struct McpShared {
enabled: AtomicBool,
scopes: RwLock<HashSet<Scope>>,
denied_tools: RwLock<HashSet<String>>,
pub(crate) refs: Mutex<RefTable>,
pub(crate) waker: Mutex<Option<Arc<dyn Fn() + Send + Sync>>>,
pub(crate) token: String,
pub(crate) app_name: String,
pub(crate) extra_instructions: Option<String>,
#[cfg(feature = "router")]
pub(crate) navigator: RwLock<Option<crate::router::Navigator>>,
}
impl McpShared {
pub(crate) fn new(
scopes: HashSet<Scope>,
denied_tools: HashSet<String>,
token: String,
app_name: String,
extra_instructions: Option<String>,
) -> Self {
Self {
enabled: AtomicBool::new(true),
scopes: RwLock::new(scopes),
denied_tools: RwLock::new(denied_tools),
refs: Mutex::new(RefTable::default()),
waker: Mutex::new(None),
token,
app_name,
extra_instructions,
#[cfg(feature = "router")]
navigator: RwLock::new(None),
}
}
pub(crate) fn is_enabled(&self) -> bool {
self.enabled.load(Ordering::Relaxed)
}
pub(crate) fn set_enabled(&self, enabled: bool) {
self.enabled.store(enabled, Ordering::Relaxed);
}
pub(crate) fn has_scope(&self, scope: &Scope) -> bool {
self.scopes.read().unwrap().contains(scope)
}
pub(crate) fn set_scopes(&self, scopes: HashSet<Scope>) {
*self.scopes.write().unwrap() = scopes;
}
pub(crate) fn add_scope(&self, scope: Scope) {
self.scopes.write().unwrap().insert(scope);
}
pub(crate) fn remove_scope(&self, scope: &Scope) {
self.scopes.write().unwrap().remove(scope);
}
pub(crate) fn is_denied(&self, tool: &str) -> bool {
self.denied_tools.read().unwrap().contains(tool)
}
pub(crate) fn deny_tool(&self, tool: impl Into<String>) {
self.denied_tools.write().unwrap().insert(tool.into());
}
pub(crate) fn allow_tool(&self, tool: &str) {
self.denied_tools.write().unwrap().remove(tool);
}
pub(crate) fn wake(&self) {
let waker = self.waker.lock().unwrap().clone();
if let Some(waker) = waker {
waker();
}
}
}